summaryrefslogtreecommitdiff
path: root/src/game.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/game.c')
-rw-r--r--src/game.c60
1 files changed, 35 insertions, 25 deletions
diff --git a/src/game.c b/src/game.c
index 183f716..9a52ca5 100644
--- a/src/game.c
+++ b/src/game.c
@@ -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,