diff options
| author | Menase Teshale <osome3717@gmail.com> | 2026-09-19 12:12:31 +0300 |
|---|---|---|
| committer | Menase Teshale <osome3717@gmail.com> | 2026-09-19 12:12:31 +0300 |
| commit | af1e4f1d6c47498a590bd64428163a6b9dbca385 (patch) | |
| tree | 374e9028e45bcf960c4daf2790024482a8e72410 | |
| parent | 9dfdb7df2929fcfdf5f932b2e27499d3f0e5d412 (diff) | |
Started relativising sizing and positioning
| -rw-r--r-- | src/app.h | 3 | ||||
| -rw-r--r-- | src/game.c | 60 | ||||
| -rw-r--r-- | src/main.c | 14 | ||||
| -rw-r--r-- | src/menu.c | 14 |
4 files changed, 50 insertions, 41 deletions
@@ -23,4 +23,7 @@ void free_menu(); void draw_menu(SDL_Renderer* renderer); SDL_AppResult menu_event(SDL_Event* e); +float get_window_relative_xpos(float vp); +float get_window_relative_ypos(float vp); + #endif // APP_H_ @@ -15,8 +15,8 @@ #define PIECE_SOURCE_YOFFSET 4 #define PIECE_SOURCE_WIDTH 32 #define PIECE_SOURCE_HEIGHT 32 -#define PIECE_DEST_WIDTH 50 -#define PIECE_DEST_HEIGHT 50 +#define PIECE_DEST_HEIGHT_REL 0.09 +#define PIECE_DEST_ASPECT_RATIO (1.0) //Piece should be as long as it is wide #define PIECE_LINEAR_SPEED 1.5 #define PIECE_SWAP_SPEED 3 #define PIECE_MAX_ROTATION 40 @@ -30,9 +30,9 @@ #define BOARD_WIDTH 8 #define BOARD_HEIGHT 6 -#define BOARD_XOFFSET 200 -#define BOARD_YOFFSET 150 -#define BOARD_PADDING 4 +/* #define BOARD_XOFFSET 200 */ +#define BOARD_YOFFSET_REL 0.2 +#define BOARD_PADDING_REL 0.003 #define BACKGROUND_SCROLL_SPEED 40 #define MAX_CLEARED_TILES_STORED 50 @@ -147,18 +147,8 @@ s_game game = {0}; extern SDL_Window* window; -static inline float get_window_relative_xpos(float vp) { - int width; - SDL_GetWindowSizeInPixels(window, &width, NULL); - - return width * vp; -} - -static inline float get_window_relative_ypos(float vp) { - int height; - SDL_GetWindowSizeInPixels(window, NULL, &height); - - return height * vp; +static inline float get_aspect_ratio() { + return get_window_relative_ypos(1) / get_window_relative_xpos(1); } static inline float random_float_0to1() { @@ -172,10 +162,21 @@ static inline bool filled_tile_at(int y, int x) { return game.board[y][x].state == tile_filled; } +static inline float get_board_width(float piece_width, float board_padding) { + return game.board_width * piece_width + (game.board_width - 1) * board_padding; +} + SDL_FPoint board_position(int y, int x) { + float piece_height = get_window_relative_ypos(PIECE_DEST_HEIGHT_REL); + float piece_width = piece_height / PIECE_DEST_ASPECT_RATIO; + float board_padding = get_window_relative_xpos(BOARD_PADDING_REL); + float board_yoffset = get_window_relative_ypos(BOARD_YOFFSET_REL); + float board_xoffset = + (get_window_relative_xpos(1) - get_board_width(piece_width, board_padding)) / 2; // Center the board on the x-axis + return (SDL_FPoint) { - .x = (PIECE_DEST_WIDTH + BOARD_PADDING) * x + BOARD_XOFFSET, - .y = (PIECE_DEST_HEIGHT + BOARD_PADDING) * y + BOARD_YOFFSET + .x = (piece_width + board_padding) * x + board_xoffset, + .y = (piece_height + board_padding / PIECE_DEST_ASPECT_RATIO) * y + board_yoffset }; } @@ -226,10 +227,13 @@ float rand_float() { } void add_piece_at(int y, int x) { + float piece_height = get_window_relative_ypos(PIECE_DEST_HEIGHT_REL); + float piece_width = piece_height / PIECE_DEST_ASPECT_RATIO; + int new_piece_id = add_piece((piece) { .position = { - .x = (800 - PIECE_DEST_WIDTH) * rand_float(), - .y = (600 - PIECE_DEST_HEIGHT) * rand_float() + .x = (get_window_relative_xpos(1) - piece_width) * rand_float(), + .y = (get_window_relative_ypos(1) - piece_height) * rand_float() }, .orientation = 0, .piece_color = rand() % 9, @@ -379,14 +383,17 @@ int mouse_on_piece() { SDL_FPoint mouse_position; SDL_GetMouseState(&mouse_position.x, &mouse_position.y); + float piece_height = get_window_relative_ypos(PIECE_DEST_HEIGHT_REL); + float piece_width = piece_height / PIECE_DEST_ASPECT_RATIO; + for (int i = 0; i < game.num_pieces; i++) { const piece* piece = &game.pieces[i]; SDL_FRect piece_box = (SDL_FRect){ .x = piece->position.x, .y = piece->position.y, - .w = PIECE_DEST_WIDTH, - .h = PIECE_DEST_HEIGHT + .w = piece_width, + .h = piece_height }; if (SDL_PointInRectFloat(&mouse_position, &piece_box)) { @@ -659,6 +666,9 @@ void update_game(float dt) { void draw_piece(SDL_Renderer* renderer, const piece* piece) { const int piece_source_xoffs[] = {4, 209}; + float piece_height = get_window_relative_ypos(PIECE_DEST_HEIGHT_REL); + float piece_width = piece_height / PIECE_DEST_ASPECT_RATIO; + SDL_RenderTextureRotated(renderer, game.spritesheet, &(SDL_FRect){ @@ -670,8 +680,8 @@ void draw_piece(SDL_Renderer* renderer, const piece* piece) { &(SDL_FRect){ .x = piece->position.x, .y = piece->position.y, - .w = PIECE_DEST_WIDTH, - .h = PIECE_DEST_HEIGHT + .w = piece_width, + .h = piece_height }, piece->orientation, NULL, @@ -9,8 +9,8 @@ #include "app.h" #include "font.h" -#define WINDOW_WIDTH 800 -#define WINDOW_HEIGHT 600 +#define WINDOW_WIDTH 1280 +#define WINDOW_HEIGHT 720 #define WINDOW_TITLE "Match 3" #define MAX_DELTATIME (1000.0 / 30) @@ -21,6 +21,8 @@ MIX_Mixer* mixer; application_state app_state; +int window_width, window_height; + SDL_AppResult SDL_AppInit(void** userdata, int nargs, char** argc) { SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO); srand(time(NULL)); @@ -63,6 +65,7 @@ SDL_AppResult SDL_AppEvent(void* userdata, SDL_Event* e) { SDL_AppResult SDL_AppIterate(void* userdata) { static uint64_t then = 0, now = 0; + SDL_GetWindowSizeInPixels(window, &window_width, &window_height); then = now; now = SDL_GetTicks(); @@ -106,3 +109,10 @@ void init_app(SDL_Renderer* renderer, MIX_Mixer* mixer) { void free_app() { free_game(); } + +float get_window_relative_xpos(float vp) { + return vp * window_width; +} +float get_window_relative_ypos(float vp) { + return vp * window_height; +} @@ -17,20 +17,6 @@ sprite_font text_font; extern SDL_Window* window; extern application_state app_state; -static inline float get_window_relative_xpos(float vp) { - int width; - SDL_GetWindowSizeInPixels(window, &width, NULL); - - return width * vp; -} - -static inline float get_window_relative_ypos(float vp) { - int height; - SDL_GetWindowSizeInPixels(window, NULL, &height); - - return height * vp; -} - void init_menu(SDL_Renderer* renderer) { if (text_font.spritesheet == NULL) { load_sprite_font(renderer, |
