diff options
| author | citrons <citrons@mondecitronne.com> | 2023-10-13 22:30:00 -0500 |
|---|---|---|
| committer | citrons <citrons@mondecitronne.com> | 2023-10-13 22:30:00 -0500 |
| commit | 5b8082c9cf3d70d1b158d2d7744fd5c008f788d3 (patch) | |
| tree | b582e1b37908a7761763dd5eb3f8268235a248d3 | |
| parent | 648bc6f1c2dc970a505adf849a8fad442c2d8e49 (diff) | |
use integer scaling for graphics
| -rw-r--r-- | counter.c | 12 | ||||
| -rw-r--r-- | counter.h | 2 | ||||
| -rw-r--r-- | world.c | 42 |
3 files changed, 27 insertions, 29 deletions
@@ -27,9 +27,9 @@ static SDL_Texture *get_font(SDL_Renderer *rend) { } int draw_counter(SDL_Renderer *rend, - SDL_Point screen_pos, double size, SDL_Color color, + SDL_Point screen_pos, int size, SDL_Color color, int number, int digits) { - double scale = size / FONT_HEIGHT; + int scale = size / FONT_HEIGHT; SDL_Texture *font = get_font(rend); SDL_SetTextureColorMod(font, color.r, color.g, color.b); @@ -44,9 +44,9 @@ int draw_counter(SDL_Renderer *rend, for (int i = digits - 1; i >= 0; i--) { int digit = n % DIGIT_BASE; - int width = (double) FONT_WIDTH * scale + 0.5; - int height = (double) FONT_HEIGHT * scale + 0.5; - int offset = (double) FONT_WIDTH * scale * (double) i + 0.5; + int width = FONT_WIDTH * scale; + int height = FONT_HEIGHT * scale; + int offset = FONT_WIDTH * scale * i; SDL_Rect draw = {screen_pos.x + offset, screen_pos.y, width, height}; SDL_Rect digit_rect = { 0, FONT_HEIGHT * digit, FONT_WIDTH, FONT_HEIGHT, @@ -56,5 +56,5 @@ int draw_counter(SDL_Renderer *rend, n /= DIGIT_BASE; } - return (double) FONT_WIDTH * scale * (double) digits + 0.5; + return FONT_WIDTH * scale * digits; } @@ -4,7 +4,7 @@ #include <SDL.h> int draw_counter(SDL_Renderer *rend, - SDL_Point screen_pos, double size, SDL_Color color, + SDL_Point screen_pos, int size, SDL_Color color, int number, int digits); #endif @@ -198,15 +198,15 @@ void player_destroy(world *w, int x, int y) { } } -static double get_view_scale(SDL_Window *win) { +static int get_view_scale(SDL_Window *win) { int sw, sh; SDL_GetWindowSize(win, &sw, &sh); - double view_scale; + int view_scale; if (sh < sw) - view_scale = (double) sh / (double) VIEW_DIM; + view_scale = sh / VIEW_DIM; else - view_scale = (double) sw / (double) VIEW_DIM; + view_scale = sw / VIEW_DIM; return view_scale; } @@ -214,25 +214,25 @@ static double get_view_scale(SDL_Window *win) { static void draw_ui(world *w, SDL_Window *win, SDL_Renderer *rend) { int sw, sh; SDL_GetWindowSize(win, &sw, &sh); - double view_scale = get_view_scale(win); + int view_scale = get_view_scale(win); - double bar_size = 1.5; - double padding = 0.16; + int bar_size = 1.5 * view_scale; + int padding = 0.24 * view_scale; SDL_Rect bar = { - 0, sh - view_scale * (bar_size + padding), sw, view_scale * 3, + 0, sh - (bar_size + padding), sw, bar_size + padding, }; SDL_SetRenderDrawColor(rend, 0x00, 0x00, 0x00, 0xFF); SDL_RenderFillRect(rend, &bar); - int x = view_scale * padding; + int x = padding; for (int i = 0; i < MAX_COLLECTIBLE; i++) { int score = w->player.scores[i]; SDL_Color grey = {0x20, 0x20, 0x10, 0xFF}; SDL_Color collectible_color = tile_colors[i]; SDL_Color color = score > 0 ? collectible_color : grey; - SDL_Point pos = {x, bar.y + view_scale * padding}; - x += draw_counter(rend, pos, view_scale * bar_size, color, score, 3); + SDL_Point pos = {x, bar.y + padding}; + x += draw_counter(rend, pos, bar_size, color, score, 3); x += view_scale * 1; } } @@ -240,12 +240,12 @@ static void draw_ui(world *w, SDL_Window *win, SDL_Renderer *rend) { void draw_world(world *w, SDL_Window *win, SDL_Renderer *rend) { int sw, sh; SDL_GetWindowSize(win, &sw, &sh); - double view_scale = get_view_scale(win); + int view_scale = get_view_scale(win); - double view_width = (double) sw / view_scale; - double view_height = (double) sh / view_scale; - double view_x = w->view_pos.x - view_width / 2; - double view_y = w->view_pos.y - view_height / 2; + int view_width = sw / view_scale; + int view_height = sh / view_scale; + int view_x = w->view_pos.x - view_width / 2; + int view_y = w->view_pos.y - view_height / 2; SDL_Rect view_rect = {view_x, view_y, view_width + 1, view_height + 1}; for (int i = 0; i < MAX_CHUNKS; i++) { @@ -271,12 +271,10 @@ void draw_world(world *w, SDL_Window *win, SDL_Renderer *rend) { int world_x = x * CHUNK_DIM; int world_y = y * CHUNK_DIM; - int draw_x = ((double) world_x - view_x) * view_scale; - int draw_y = ((double) world_y - view_y) * view_scale; + int draw_x = (world_x - view_x) * view_scale; + int draw_y = (world_y - view_y) * view_scale; SDL_Rect draw_rect = { - draw_x, draw_y, - (double) CHUNK_DIM * view_scale + 0.5, - (double) CHUNK_DIM * view_scale + 0.5, + draw_x, draw_y, CHUNK_DIM * view_scale, CHUNK_DIM * view_scale, }; SDL_RenderCopy(rend, tex, NULL, &draw_rect); } @@ -285,7 +283,7 @@ void draw_world(world *w, SDL_Window *win, SDL_Renderer *rend) { int player_x = (w->player.pos.x - view_x) * view_scale; int player_y = (w->player.pos.y - view_y) * view_scale; SDL_Rect player_rect = { - player_x, player_y, view_scale + 0.5, view_scale + 0.5, + player_x, player_y, view_scale, view_scale, }; SDL_SetRenderDrawColor(rend, 0xFF, 0x00, 0xFF, 0xFF); SDL_RenderFillRect(rend, &player_rect); |
