Moved rendering code. Added keyboard input handling
This commit is contained in:
@@ -5,10 +5,59 @@
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
|
||||
const Color tile_colors[] = {
|
||||
LIGHTGRAY, // 0 - Empty
|
||||
BEIGE, // 2
|
||||
GOLD, // 4
|
||||
ORANGE, // 8
|
||||
ORANGE, // 16
|
||||
RED, // 32
|
||||
MAROON, // 64
|
||||
YELLOW, // 128
|
||||
GOLD, // 256
|
||||
ORANGE, // 512
|
||||
RED, // 1024
|
||||
MAROON, // 2048
|
||||
PINK, // 4096
|
||||
MAGENTA, // 8192
|
||||
};
|
||||
|
||||
static bool is_dragging;
|
||||
static Vector2 drag_start_pos;
|
||||
static bool is_animating;
|
||||
|
||||
// Common logic shared between both input handlers
|
||||
static void after_input(GameState_t *game_state) {
|
||||
if (!spawn_tile(game_state) && !can_move(game_state)) {
|
||||
game_state->status = LOST;
|
||||
return;
|
||||
}
|
||||
|
||||
// We moved, merged or spawned a tile, so we will be animating
|
||||
is_animating = true;
|
||||
|
||||
// TODO: Allow for infinite play
|
||||
for (int y = 0; y < BOARD_HEIGHT; y++) {
|
||||
for (int x = 0; x < BOARD_WIDTH; x++) {
|
||||
if (game_state->board[y][x] == 2048) {
|
||||
game_state->status = WON;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
FILE *f = fopen("save.dat", "wb");
|
||||
if (f) {
|
||||
fwrite(game_state, sizeof(GameState_t), 1, f);
|
||||
fclose(f);
|
||||
}
|
||||
}
|
||||
|
||||
void handle_mouse_input(GameState_t *game_state) {
|
||||
// Don't allow any input while animating so we stay in sync
|
||||
if (is_animating)
|
||||
return;
|
||||
|
||||
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) {
|
||||
drag_start_pos = GetMousePosition();
|
||||
|
||||
@@ -51,27 +100,80 @@ void handle_mouse_input(GameState_t *game_state) {
|
||||
move(game_state, UP);
|
||||
}
|
||||
}
|
||||
if (!spawn_tile(game_state) && !can_move(game_state)) {
|
||||
game_state->status = LOST;
|
||||
return;
|
||||
}
|
||||
// TODO: Allow for infinite play
|
||||
for (int y = 0; y < BOARD_HEIGHT; y++) {
|
||||
for (int x = 0; x < BOARD_WIDTH; x++) {
|
||||
if (game_state->board[y][x] == 2048) {
|
||||
game_state->status = WON;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
FILE *f = fopen("save.dat", "wb");
|
||||
if (f) {
|
||||
fwrite(game_state, sizeof(GameState_t), 1, f);
|
||||
fclose(f);
|
||||
}
|
||||
after_input(game_state);
|
||||
}
|
||||
|
||||
is_dragging = false;
|
||||
}
|
||||
}
|
||||
|
||||
void handle_keyboard_input(GameState_t *game_state) {
|
||||
if (IsKeyReleased(KEY_R) || (game_state->status != PLAYING && IsKeyReleased(KEY_ENTER))) {
|
||||
reset_board(game_state);
|
||||
}
|
||||
|
||||
if (IsKeyReleased(KEY_UP)) {
|
||||
move(game_state, UP);
|
||||
} else if (IsKeyReleased(KEY_DOWN)) {
|
||||
move(game_state, DOWN);
|
||||
} else if (IsKeyReleased(KEY_LEFT)) {
|
||||
move(game_state, LEFT);
|
||||
} else if (IsKeyReleased(KEY_RIGHT)) {
|
||||
move(game_state, RIGHT);
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
|
||||
after_input(game_state);
|
||||
}
|
||||
|
||||
// Main render loop, responsible ONLY for redering
|
||||
void render(GameState_t *game_state) {
|
||||
for (int y = 0; y < BOARD_HEIGHT; y++) {
|
||||
for (int x = 0; x < BOARD_WIDTH; x++) {
|
||||
int tile_value = game_state->board[y][x];
|
||||
int pos_x = BOARD_MARGIN + x * (TILE_SIZE + TILE_SPACING);
|
||||
int pos_y = BOARD_MARGIN + y * (TILE_SIZE + TILE_SPACING);
|
||||
int color_index = (tile_value == 0) ? 0 : (int)log2(tile_value);
|
||||
|
||||
if ((size_t)color_index >= sizeof(tile_colors) / sizeof(tile_colors[0])) {
|
||||
color_index = sizeof(tile_colors) / sizeof(tile_colors[0]) - 1; // Cap to the last color
|
||||
}
|
||||
|
||||
DrawRectangle(pos_x, pos_y, TILE_SIZE, TILE_SIZE, tile_colors[color_index]);
|
||||
if (tile_value != 0) {
|
||||
const char *text = TextFormat("%d", tile_value);
|
||||
DrawText(text, pos_x + TILE_SIZE / 2 - MeasureText(text, 20) / 2, pos_y + TILE_SIZE / 2 - 10, 20, BLACK);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
DrawText(TextFormat("Score: %d", game_state->score), WINDOW_HEIGHT, BOARD_MARGIN, 20, BLACK);
|
||||
DrawText(TextFormat("Moves used: %d", game_state->moves_made), WINDOW_HEIGHT, BOARD_MARGIN + 20, 20, BLACK);
|
||||
|
||||
if (game_state->status == PLAYING) {
|
||||
DrawText("Drag with the mouse to move tiles", 400 - MeasureText("Drag with the mouse to move tiles", 20) / 2,
|
||||
WINDOW_HEIGHT - 40, 20, GRAY);
|
||||
|
||||
// Reset button
|
||||
DrawRectangle(500, 455, 275, 24, RED);
|
||||
DrawText("Reset", 500 + 275 / 2 - MeasureText("Reset", 20) / 2, 455 + 4, 20, WHITE);
|
||||
} else {
|
||||
Color overlay = Fade(BLACK, 0.7f);
|
||||
DrawRectangle(0, 0, WINDOW_WIDTH, 700, overlay);
|
||||
|
||||
if (game_state->status == WON) {
|
||||
DrawText("You win!", 400 - MeasureText("You win!", 40) / 2, 250, 40, GREEN);
|
||||
} else if (game_state->status == LOST) {
|
||||
DrawText("Game over!", 400 - MeasureText("Game over!", 40) / 2, 250, 40, RED);
|
||||
}
|
||||
|
||||
// Draw the scores under the win/loss message
|
||||
DrawText(TextFormat("Score: %d", game_state->score),
|
||||
400 - MeasureText(TextFormat("Score: %d", game_state->score), 20) / 2, 290, 20, WHITE);
|
||||
DrawText(TextFormat("Moves used: %d", game_state->moves_made),
|
||||
400 - MeasureText(TextFormat("Moves used: %d", game_state->moves_made), 20) / 2, 310, 20, WHITE);
|
||||
|
||||
DrawRectangle(400 - 100, 450, 200, 40, RED);
|
||||
DrawText("Try again", 400 - MeasureText("Try again", 20) / 2, 455 + 4, 20, WHITE);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user