Files
cudoku/main.c
T

194 lines
5.0 KiB
C
Raw Normal View History

2023-12-11 22:42:19 +02:00
#include <stdbool.h>
#include <stdio.h>
2023-12-11 22:42:19 +02:00
#include <stdlib.h>
#include <string.h>
2023-09-12 03:36:30 +02:00
#include <time.h>
2023-12-11 22:42:19 +02:00
#include <X11/Xlib.h>
2023-10-05 23:02:23 +02:00
#include "audio.h"
2023-12-11 22:42:19 +02:00
#include "cudoku.h"
#include "timer.h"
2023-11-29 01:42:44 +02:00
#include "zephr.h"
2023-12-11 22:42:19 +02:00
#include "zephr_math.h"
#define DEBUG 1
const char *font_path = "assets/fonts/Rubik/Rubik-VariableFont_wght.ttf";
const char *title = "Cudoku";
2023-09-12 23:32:58 +02:00
void usage() {
printf("Usage: cudoku [OPTION]\n\n");
2023-12-11 22:42:19 +02:00
printf(" %-30s%-20s", "-h, --help", "prints this help message\n");
printf(" %-30s%-20s", "-f, --font <path_to_font>", "use a custom font file to render text\n");
2023-09-04 01:57:39 +03:00
}
2023-12-11 22:42:19 +02:00
void handle_keypress(ZephrEvent e, Cudoku *game) {
if (e.key.code == ZEPHR_KEYCODE_R) {
reset_board(game);
} else if (
2023-12-11 22:42:19 +02:00
e.key.code == ZEPHR_KEYCODE_ESCAPE ||
((e.key.mods & ZEPHR_KEY_MOD_CTRL) && e.key.code == ZEPHR_KEYCODE_Q)
) {
zephr_quit();
} else if (
e.key.mods & (ZEPHR_KEY_MOD_CTRL | ZEPHR_KEY_MOD_SHIFT) &&
e.key.code == ZEPHR_KEYCODE_W) {
#if DEBUG
printf("DEBUG: Ctrl + Shift + W pressed. Winning game\n");
game->has_won = true;
game->should_draw_selection = false;
game->should_highlight_mistakes = false;
game->should_draw_help = false;
game->win_time = get_time();
timer_stop(&game->timer);
audio_play_win();
#endif
2023-12-11 22:42:19 +02:00
} else if (e.key.code >= ZEPHR_KEYCODE_1 && e.key.code <= ZEPHR_KEYCODE_9) {
set_selected_number(game, e.key.code - ZEPHR_KEYCODE_1 + 1);
} else if (e.key.code == ZEPHR_KEYCODE_0) {
set_selected_number(game, 0);
} else if (e.key.code == ZEPHR_KEYCODE_BACKSPACE ||
e.key.code == ZEPHR_KEYCODE_DELETE) {
2023-09-12 23:32:58 +02:00
set_selected_number(game, 0);
2023-09-13 01:06:19 +02:00
} else if (
2023-12-11 22:42:19 +02:00
e.key.code == ZEPHR_KEYCODE_LEFT ||
e.key.code == ZEPHR_KEYCODE_A ||
e.key.code == ZEPHR_KEYCODE_H) {
2023-09-12 23:32:58 +02:00
move_selection(game, -1, 0);
2023-09-13 01:06:19 +02:00
} else if (
2023-12-11 22:42:19 +02:00
e.key.code == ZEPHR_KEYCODE_RIGHT ||
e.key.code == ZEPHR_KEYCODE_D ||
e.key.code == ZEPHR_KEYCODE_L) {
2023-09-12 23:32:58 +02:00
move_selection(game, 1, 0);
2023-09-13 01:06:19 +02:00
} else if (
2023-12-11 22:42:19 +02:00
e.key.code == ZEPHR_KEYCODE_UP ||
e.key.code == ZEPHR_KEYCODE_W ||
e.key.code == ZEPHR_KEYCODE_K) {
2023-09-12 23:32:58 +02:00
move_selection(game, 0, -1);
2023-09-13 01:06:19 +02:00
} else if (
2023-12-11 22:42:19 +02:00
e.key.code == ZEPHR_KEYCODE_DOWN ||
e.key.code == ZEPHR_KEYCODE_S ||
e.key.code == ZEPHR_KEYCODE_J) {
2023-09-12 23:32:58 +02:00
move_selection(game, 0, 1);
2023-09-13 01:06:19 +02:00
} else if (
2023-12-11 22:42:19 +02:00
e.key.code == ZEPHR_KEYCODE_ENTER ||
e.key.code == ZEPHR_KEYCODE_SPACE) {
2023-09-12 23:32:58 +02:00
toggle_selection(game);
2023-12-11 22:42:19 +02:00
} else if (e.key.code == ZEPHR_KEYCODE_N) {
generate_random_board(game);
2023-12-11 22:42:19 +02:00
} else if (e.key.code == ZEPHR_KEYCODE_C) {
toggle_check(game);
2023-12-11 22:42:19 +02:00
} else if (e.key.code == ZEPHR_KEYCODE_P) {
2023-11-30 00:44:00 +02:00
pause_game(game);
2023-12-11 22:42:19 +02:00
} else if (e.key.code == ZEPHR_KEYCODE_F1) {
if (!toggle_help(game)) {
timer_stop(&game->help_timer);
}
2023-09-12 23:32:58 +02:00
}
}
int main(int argc, char *argv[]) {
if (argc > 1) {
char *flag = argv[1];
if (strcmp(flag, "-h") == 0 || strcmp(flag, "--help") == 0) {
usage();
return 0;
}
2023-09-12 03:21:18 +02:00
for (int i = 1; i < argc; i++) {
2023-12-11 22:42:19 +02:00
char *option = argv[i];
2023-09-12 03:21:18 +02:00
2023-12-11 22:42:19 +02:00
if (strcmp(option, "-f") == 0 || strcmp(option, "--font") == 0) {
2023-09-12 03:21:18 +02:00
if (i + 1 < argc) {
font_path = argv[i + 1];
i++;
} else {
2023-11-29 05:28:54 +02:00
printf("[WARN]: Used font flag with no provided font, defaulting to Rubik\n");
2023-09-12 23:32:58 +02:00
}
2023-09-12 03:21:18 +02:00
}
}
}
2023-11-26 05:34:42 +02:00
Size window_size = {900, 900};
2023-12-11 22:42:19 +02:00
int res = init_zephr(font_path, title, window_size);
if (res != 0) {
printf("[ERROR]: could not initialize zephr\n");
2023-09-12 03:21:18 +02:00
return 1;
}
2023-11-29 01:42:44 +02:00
zephr_make_window_non_resizable();
2023-09-12 03:21:18 +02:00
2023-09-14 01:41:39 +02:00
srand(time(NULL));
Cudoku game = {0};
2023-10-02 23:59:48 +02:00
game.should_draw_help = true;
2023-09-14 01:41:39 +02:00
generate_random_board(&game);
timer_start(&game.help_timer, 5.0f);
timer_start(&game.timer, 0.0f);
while (!zephr_should_quit()) {
2023-12-11 22:42:19 +02:00
ZephrEvent event;
2023-11-29 05:28:54 +02:00
2023-12-11 22:42:19 +02:00
while (zephr_iter_events(&event)) {
switch (event.type) {
case ZEPHR_EVENT_UNKNOWN:
printf("[WARN]: Unknown event\n");
break;
case ZEPHR_EVENT_KEY_PRESSED:
handle_keypress(event, &game);
break;
case ZEPHR_EVENT_MOUSE_BUTTON_PRESSED:
if (event.mouse.button == ZEPHR_MOUSE_BUTTON_LEFT) {
do_selection(&game, event.mouse.position.x, event.mouse.position.y);
}
break;
case ZEPHR_EVENT_WINDOW_CLOSED:
2023-11-29 05:28:54 +02:00
zephr_quit();
break;
2023-12-11 22:42:19 +02:00
default:
case ZEPHR_EVENT_WINDOW_RESIZED:
case ZEPHR_EVENT_KEY_RELEASED:
break;
2023-11-29 05:28:54 +02:00
}
}
2023-11-29 05:28:54 +02:00
draw_board(&game, window_size);
2023-11-26 05:34:42 +02:00
draw_timer(&game.timer);
2023-11-29 05:28:54 +02:00
if (game.should_draw_selection) {
draw_selection_box(game.selection.y, game.selection.x, (Color){102, 102, 255, 127});
}
if (game.should_highlight_mistakes) {
highlight_mistakes(&game);
}
2023-11-26 21:32:19 +02:00
if (timer_ended(&game.help_timer)) {
timer_stop(&game.help_timer);
game.should_draw_help = false;
}
if (game.should_draw_help) {
draw_help(&game.help_timer);
}
2023-11-26 05:34:42 +02:00
if (game.has_won) {
draw_win(&game);
}
2023-11-30 00:44:00 +02:00
if (game.timer.state == TIMER_PAUSED) {
draw_pause_overlay();
}
zephr_swap_buffers();
}
deinit_zephr();
2023-09-02 00:17:15 +03:00
return 0;
}