lav-sdl

SDL верзија програма Лавиринт
Дневник | Датотеке | Референце | ПРОЧИТАЈМЕ | ЛИЦЕНЦА

чување 6fda5d1a4a1a4e05dbe70fc4b3e612bd4081ffbc
родитељ 3f1152ffebfe11e9eaa30d30bd937e9b5df2d5b4
Аутор: Страхиња Радић <contact@strahinja.org>
Датум:   Sat, 19 Aug 2023 17:36:22 +0200

Delegate scaling to SDL

Signed-off-by: Страхиња Радић <contact@strahinja.org>

Diffstat:
Mlav-sdl.c | 78+++++++++++++++++++++++++++---------------------------------------------------
измењених датотека: 1, додавања: 27(+), брисања: 51(-)

diff --git a/lav-sdl.c b/lav-sdl.c @@ -19,7 +19,7 @@ #define ANIM_STEPS 10 #define CELL_SIZE 32 #define DEFAULT_WIDTH 1280 -#define DEFAULT_HEIGHT 960 +#define DEFAULT_HEIGHT 720 #define HELP_DIALOG_WIDTH 640 #define HELP_DIALOG_HEIGHT 300 #define LABYRINTH_MAXHEIGHT 20 @@ -102,7 +102,6 @@ static int help_shown = 0; static int redraw = 1; static SDL_Renderer* renderer = NULL; static int running = 1; -static float scale_factor = 1.0f; static int screen_width = DEFAULT_WIDTH; static int screen_height = DEFAULT_HEIGHT; static SDL_Texture* sprites = NULL; @@ -138,7 +137,6 @@ void print_debug(const char* fmt, ...); void print_error(const char* fmt, ...); void reset(void); void step_animation(void); -void update_size(void); void assign_pointIF(const SDL_Point* from, SDL_FPoint* to) @@ -206,13 +204,13 @@ draw(void) cx = screen_width / 2; cy = screen_height / 2; - startx = cx - labyrinth.width / 2 * CELL_SIZE * scale_factor; - starty = cy - labyrinth.height / 2 * CELL_SIZE * scale_factor; + startx = cx - labyrinth.width / 2 * CELL_SIZE; + starty = cy - labyrinth.height / 2 * CELL_SIZE; bg_rect.x = 0; bg_rect.y = 0; - bg_rect.w = CELL_SIZE * scale_factor * labyrinth.width; - bg_rect.h = CELL_SIZE * scale_factor * labyrinth.height; + bg_rect.w = CELL_SIZE * labyrinth.width; + bg_rect.h = CELL_SIZE * labyrinth.height; SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE); SDL_RenderClear(renderer); @@ -232,8 +230,8 @@ draw(void) SDL_RenderCopyF(renderer, background, &bg_rect, &dest_rect); /* Draw nuggets/cobwebs */ - dest_rect.w = CELL_SIZE * scale_factor; - dest_rect.h = CELL_SIZE * scale_factor; + dest_rect.w = CELL_SIZE; + dest_rect.h = CELL_SIZE; for (y = 0; y < labyrinth.height; y++) for (x = 0; x < labyrinth.width; x++) { @@ -252,8 +250,8 @@ draw(void) } /* Draw hero */ - dest_rect.w = CELL_SIZE * scale_factor; - dest_rect.h = CELL_SIZE * scale_factor; + dest_rect.w = CELL_SIZE; + dest_rect.h = CELL_SIZE; get_hero_sprite_xy(&cell_rect.x, &cell_rect.y); if (animation.active) labyrinth_coord_to_screen_coordF(animation.position.x, @@ -305,8 +303,8 @@ draw_background(SDL_Rect* bg_rect, SDL_Rect* cell_rect, SDL_FRect* dest_rect) SDL_SetRenderDrawColor(renderer, 0x22, 0x22, 0x22, SDL_ALPHA_OPAQUE); SDL_RenderFillRect(renderer, bg_rect); - dest_rect->w = CELL_SIZE * scale_factor; - dest_rect->h = CELL_SIZE * scale_factor; + dest_rect->w = CELL_SIZE; + dest_rect->h = CELL_SIZE; for (int y = 0; y < labyrinth.height; y++) for (int x = 0; x < labyrinth.width; x++) { @@ -330,14 +328,14 @@ void draw_help_dialog(void) { const char** phelp_text = help_text; - int sx = screen_width / 2 - HELP_DIALOG_WIDTH / 2 * scale_factor; - int sy = screen_height / 2 - HELP_DIALOG_HEIGHT / 2 * scale_factor; + int sx = screen_width / 2 - HELP_DIALOG_WIDTH / 2; + int sy = screen_height / 2 - HELP_DIALOG_HEIGHT / 2; SDL_Rect dialog_rect; dialog_rect.x = sx; dialog_rect.y = sy; - dialog_rect.w = HELP_DIALOG_WIDTH * scale_factor; - dialog_rect.h = HELP_DIALOG_HEIGHT * scale_factor; + dialog_rect.w = HELP_DIALOG_WIDTH; + dialog_rect.h = HELP_DIALOG_HEIGHT; SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND); SDL_SetRenderDrawColor(renderer, 0, 0, 0, .75 * 255); @@ -360,10 +358,9 @@ draw_help_dialog(void) while (*phelp_text) { - draw_text(sx + (2 * TER16_WIDTH) * scale_factor, - sy + (TER16_HEIGHT + 2) * scale_factor - + (phelp_text - help_text) * (TER16_HEIGHT + 2) - * scale_factor, + draw_text(sx + (2 * TER16_WIDTH), + sy + (TER16_HEIGHT + 2) + + (phelp_text - help_text) * (TER16_HEIGHT + 2), TER16_WIDTH, TER16_HEIGHT, ter16_texture, *phelp_text); phelp_text++; } @@ -398,8 +395,8 @@ draw_text(const int x, const int y, const int font_width, const int font_height, dest_rect.x = x; dest_rect.y = y; - dest_rect.w = font_width * scale_factor; - dest_rect.h = font_height * scale_factor; + dest_rect.w = font_width; + dest_rect.h = font_height; if (SDL_QueryTexture(font_tex, NULL, NULL, &tex_w, &tex_h) < 0) { @@ -427,7 +424,7 @@ draw_text(const int x, const int y, const int font_width, const int font_height, SDL_RenderCopyF(renderer, font_tex, &source_rect, &dest_rect); - dest_rect.x += font_width * scale_factor; + dest_rect.x += font_width; pbuf++; } @@ -500,9 +497,6 @@ handle_event(SDL_Event* event) switch (event->window.event) { case SDL_WINDOWEVENT_RESIZED: - update_size(); - print_debug("Got Resized, size: %d x %d, scale = %2.2f", - screen_width, screen_height, scale_factor); redraw = 1; break; } @@ -565,16 +559,16 @@ void labyrinth_coord_to_screen_coord(const int x, const int y, const float startx, const float starty, float* to_x, float* to_y) { - *to_x = startx + x * CELL_SIZE * scale_factor; - *to_y = starty + y * CELL_SIZE * scale_factor; + *to_x = startx + x * CELL_SIZE; + *to_y = starty + y * CELL_SIZE; } void labyrinth_coord_to_screen_coordF(const float x, const float y, const float startx, const float starty, float* to_x, float* to_y) { - *to_x = startx + x * CELL_SIZE * scale_factor; - *to_y = starty + y * CELL_SIZE * scale_factor; + *to_x = startx + x * CELL_SIZE; + *to_y = starty + y * CELL_SIZE; } void @@ -874,24 +868,6 @@ step_animation(void) } } -void -update_size(void) -{ - float factor1; - float factor2; - - SDL_GetWindowSize(window, &screen_width, &screen_height); - - factor1 = (float)screen_width / DEFAULT_WIDTH; - factor2 = (float)screen_height / DEFAULT_HEIGHT; - - scale_factor = MAX(factor1, factor2); - if (LABYRINTH_MAXWIDTH * CELL_SIZE * scale_factor > screen_width - || LABYRINTH_MAXHEIGHT * CELL_SIZE * scale_factor - > screen_height) - scale_factor = MIN(factor1, factor2); -} - int main(int argc, char** argv) { @@ -918,8 +894,9 @@ main(int argc, char** argv) } SDL_SetWindowTitle(window, WIN_TITLE); - SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1"); + SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "best"); SDL_SetHintWithPriority(SDL_HINT_RENDER_VSYNC, "1", SDL_HINT_OVERRIDE); + SDL_RenderSetLogicalSize(renderer, DEFAULT_WIDTH, DEFAULT_HEIGHT); load_textures(); if (argc > 1) @@ -928,7 +905,6 @@ main(int argc, char** argv) load_labyrinth(DEFAULT_DATAFILE); location_stack = (SDL_Point*)calloc(LOCATION_STACK_MAX, sizeof(SDL_Point)); - update_size(); animation.active = 0; SDL_ShowCursor(0);