From 18390b886ce1b109e654f2f647aa713c3a7f0ec4 Mon Sep 17 00:00:00 2001 From: IllusionMan1212 Date: Mon, 11 Dec 2023 22:56:27 +0200 Subject: [PATCH] code organization changes --- audio.c | 10 +- audio.h | 10 +- cudoku.c | 289 +++++++++++++++++++++++++++------------------------ cudoku.h | 13 +-- main.c | 2 +- timer.c | 4 +- timer.h | 4 +- zephr.c | 24 ++--- zephr_math.c | 2 +- zephr_math.h | 2 +- 10 files changed, 188 insertions(+), 172 deletions(-) diff --git a/audio.c b/audio.c index 3e4c9ba..721ced6 100644 --- a/audio.c +++ b/audio.c @@ -8,7 +8,7 @@ FMOD_CHANNELGROUP *sfx_channel_group; FMOD_SOUND *scribble_sound; FMOD_SOUND *win_sound; -int audio_init() { +int audio_init(void) { // create fmod_system FMOD_RESULT fmod_res = FMOD_System_Create(&fmod_system, FMOD_VERSION); if (fmod_res != FMOD_OK) { @@ -41,25 +41,25 @@ int audio_init() { return 0; } -void audio_update() { +void audio_update(void) { FMOD_System_Update(fmod_system); } -void audio_play_scribble() { +void audio_play_scribble(void) { FMOD_RESULT fmod_res = FMOD_System_PlaySound(fmod_system, scribble_sound, sfx_channel_group, false, NULL); if (fmod_res != FMOD_OK) { printf("Failed to play \"scribble\" sound\n"); }; } -void audio_play_win() { +void audio_play_win(void) { FMOD_RESULT fmod_res = FMOD_System_PlaySound(fmod_system, win_sound, sfx_channel_group, false, NULL); if (fmod_res != FMOD_OK) { printf("Failed to play \"win\" sound\n"); }; } -void audio_close() { +void audio_close(void) { FMOD_Sound_Release(win_sound); FMOD_Sound_Release(scribble_sound); FMOD_ChannelGroup_Release(sfx_channel_group); diff --git a/audio.h b/audio.h index c0ac5de..59b92f1 100644 --- a/audio.h +++ b/audio.h @@ -1,7 +1,7 @@ #pragma once -int audio_init(); -void audio_close(); -void audio_update(); -void audio_play_scribble(); -void audio_play_win(); +int audio_init(void); +void audio_close(void); +void audio_update(void); +void audio_play_scribble(void); +void audio_play_win(void); diff --git a/cudoku.c b/cudoku.c index 22e9c6b..242a5c0 100644 --- a/cudoku.c +++ b/cudoku.c @@ -34,6 +34,14 @@ static const char *help_texts[HELP_TEXT_SIZE] = { "Ctrl+Q/Esc - Quit" }; +////////////////////////////////////// +// +// +// Drawing functions +// +// +////////////////////////////////////// + void draw_win(Cudoku *game) { Size window = zephr_get_window_size(); Color bg_color = {0, 0, 0, 200.f}; @@ -125,7 +133,17 @@ void draw_board(Cudoku *game, Size window_size) { } } -void draw_pause_overlay() { +void draw_selection_box(int x, int y, const Color color) { + UIConstraints constraints = {0}; + set_x_constraint(&constraints, x * 100, UI_CONSTRAINT_FIXED); + set_y_constraint(&constraints, y * 100, UI_CONSTRAINT_FIXED); + set_width_constraint(&constraints, 100, UI_CONSTRAINT_FIXED); + set_height_constraint(&constraints, 100, UI_CONSTRAINT_FIXED); + + draw_quad(constraints, &color, 0.0, ALIGN_TOP_LEFT); +} + +void draw_pause_overlay(void) { Size window = zephr_get_window_size(); Color bg_color = {0, 0, 0, 200.f}; Color text_color = {237.f, 225.f, 215.f, 255.f}; @@ -144,31 +162,7 @@ void draw_pause_overlay() { draw_text("Paused", 100.f, constraints, &text_color, ALIGN_CENTER); } -void pause_game(Cudoku *game) { - if (game->has_won) return; - - if (game->timer.state == TIMER_PAUSED) { - timer_resume(&game->timer); - } else if (game->timer.state == TIMER_RUNNING) { - game->should_draw_help = false; - game->should_draw_selection = false; - game->should_highlight_mistakes = false; - timer_pause(&game->timer); - timer_stop(&game->help_timer); - } -} - -void draw_selection_box(int x, int y, const Color color) { - UIConstraints constraints = {0}; - set_x_constraint(&constraints, x * 100, UI_CONSTRAINT_FIXED); - set_y_constraint(&constraints, y * 100, UI_CONSTRAINT_FIXED); - set_width_constraint(&constraints, 100, UI_CONSTRAINT_FIXED); - set_height_constraint(&constraints, 100, UI_CONSTRAINT_FIXED); - - draw_quad(constraints, &color, 0.0, ALIGN_TOP_LEFT); -} - -void highlight_mistakes(Cudoku *game) { +void draw_mistakes_highlight(Cudoku *game) { for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { Cell cell = game->board[i][j]; @@ -192,6 +186,139 @@ void highlight_mistakes(Cudoku *game) { draw_text(text, font_size, constraints, &(Color){255, 0, 0, 255}, ALIGN_BOTTOM_LEFT); } +void draw_help(Timer *timer) { + int const text_padding = 10; + int const help_font_size = 24; + int const closing_in_font_size = 18; + Color const bg_color = { + .r = 0.f, + .g = 0.f, + .b = 0.f, + .a = 185.f + }; + Color const text_color = { + 237.f, + 255.f, + 255.f, + 255.f, + }; + Color const closing_in_text_color = { + .r = 255.0f, + .g = 150.0f, + .b = 0.0f, + .a = 255.0f, + }; + + float overlay_height = 0; + float overlay_width = 0; + float total_help_texts_height = 10; + + for (int i = 0; i < HELP_TEXT_SIZE; i++) { + Sizef text_size = calculate_text_size(help_texts[i], help_font_size); + overlay_height += text_size.height + text_padding; + overlay_width = CORE_MAX(overlay_width, text_size.width + text_padding * 2); + } + Sizef size = {.width = overlay_width, .height = overlay_height + text_padding * 2}; + UIConstraints constraints = {0}; + set_width_constraint(&constraints, size.width, UI_CONSTRAINT_FIXED); + set_height_constraint(&constraints, size.height, UI_CONSTRAINT_FIXED); + draw_quad(constraints, &bg_color, 0.0, ALIGN_TOP_LEFT); + + GlyphInstanceList batch; + new_glyph_instance_list(&batch, 100); + + set_x_constraint(&constraints, text_padding, UI_CONSTRAINT_FIXED); + for (int i = 0; i < HELP_TEXT_SIZE; i++) { + Sizef text_size = calculate_text_size(help_texts[i], help_font_size); + set_y_constraint(&constraints, total_help_texts_height, UI_CONSTRAINT_FIXED); + set_width_constraint(&constraints, 1, UI_CONSTRAINT_FIXED); + set_height_constraint(&constraints, 1, UI_CONSTRAINT_FIXED); + add_text_instance(&batch, help_texts[i], help_font_size, constraints, &text_color, ALIGN_TOP_LEFT); + + total_help_texts_height += text_size.height + text_padding; + } + + if (timer->state == TIMER_RUNNING) { + char closing_in_text[128]; + snprintf(closing_in_text, sizeof(closing_in_text), "Hiding in %ds", (int)timer_remaining(timer) + 1); + Sizef closing_in_text_size = calculate_text_size(closing_in_text, closing_in_font_size); + set_x_constraint(&constraints, overlay_width - closing_in_text_size.width - text_padding, UI_CONSTRAINT_FIXED); + set_y_constraint(&constraints, text_padding, UI_CONSTRAINT_FIXED); + add_text_instance(&batch, closing_in_text, closing_in_font_size, constraints, &closing_in_text_color, ALIGN_TOP_LEFT); + } + + draw_text_batch(&batch); +} + +void draw_timer(Timer *timer) { + if (timer->state == TIMER_STOPPED) return; + + const int font_size = 32; + const Color text_color = { + .r = 66.0f, + .g = 92.0f, + .b = 124.0f, + .a = 255.0f, + }; + const Color bg_color = { + .r = 0.0f, + .g = 0.0f, + .b = 0.0f, + .a = 25.0f, + }; + const Alignment alignment = ALIGN_BOTTOM_RIGHT; + + char timer_text[64]; + + double elapsed = timer_elapsed(timer); + + if (timer->state == TIMER_PAUSED) { + // if the timer is paused, we want to show the time at which it was paused + elapsed = timer->elapsed; + } + + int minutes = (int)elapsed / 60; + int seconds = (int)elapsed % 60; + + snprintf(timer_text, sizeof(timer_text), "Time: %02d:%02d", minutes, seconds); + + Sizef text_size = calculate_text_size(timer_text, font_size); + + UIConstraints constraints = {0}; + set_x_constraint(&constraints, 0, UI_CONSTRAINT_FIXED); + set_y_constraint(&constraints, 0, UI_CONSTRAINT_FIXED); + set_width_constraint(&constraints, text_size.width + 4, UI_CONSTRAINT_FIXED); + set_height_constraint(&constraints, text_size.height + 4, UI_CONSTRAINT_FIXED); + + draw_quad(constraints, &bg_color, 0.0, alignment); + + set_width_constraint(&constraints, 1, UI_CONSTRAINT_FIXED); + set_height_constraint(&constraints, 1, UI_CONSTRAINT_FIXED); + draw_text(timer_text, font_size, constraints, &text_color, alignment); +} + +////////////////////////////////////// +// +// +// Game logic functions +// +// +////////////////////////////////////// + +void pause_game(Cudoku *game) { + if (game->has_won) return; + + if (game->timer.state == TIMER_PAUSED) { + timer_resume(&game->timer); + } else if (game->timer.state == TIMER_RUNNING) { + game->should_draw_help = false; + game->should_draw_selection = false; + game->should_highlight_mistakes = false; + timer_pause(&game->timer); + timer_stop(&game->help_timer); + } +} + void toggle_check(Cudoku *game) { if (game->has_won || game->timer.state == TIMER_PAUSED) return; @@ -523,113 +650,3 @@ bool toggle_help(Cudoku *game) { return game->should_draw_help; } -void draw_help(Timer *timer) { - int const text_padding = 10; - int const help_font_size = 24; - int const closing_in_font_size = 18; - Color const bg_color = { - .r = 0.f, - .g = 0.f, - .b = 0.f, - .a = 185.f - }; - Color const text_color = { - 237.f, - 255.f, - 255.f, - 255.f, - }; - Color const closing_in_text_color = { - .r = 255.0f, - .g = 150.0f, - .b = 0.0f, - .a = 255.0f, - }; - - float overlay_height = 0; - float overlay_width = 0; - float total_help_texts_height = 10; - - for (int i = 0; i < HELP_TEXT_SIZE; i++) { - Sizef text_size = calculate_text_size(help_texts[i], help_font_size); - overlay_height += text_size.height + text_padding; - overlay_width = CORE_MAX(overlay_width, text_size.width + text_padding * 2); - } - Sizef size = {.width = overlay_width, .height = overlay_height + text_padding * 2}; - UIConstraints constraints = {0}; - set_width_constraint(&constraints, size.width, UI_CONSTRAINT_FIXED); - set_height_constraint(&constraints, size.height, UI_CONSTRAINT_FIXED); - draw_quad(constraints, &bg_color, 0.0, ALIGN_TOP_LEFT); - - GlyphInstanceList batch; - new_glyph_instance_list(&batch, 100); - - set_x_constraint(&constraints, text_padding, UI_CONSTRAINT_FIXED); - for (int i = 0; i < HELP_TEXT_SIZE; i++) { - Sizef text_size = calculate_text_size(help_texts[i], help_font_size); - set_y_constraint(&constraints, total_help_texts_height, UI_CONSTRAINT_FIXED); - set_width_constraint(&constraints, 1, UI_CONSTRAINT_FIXED); - set_height_constraint(&constraints, 1, UI_CONSTRAINT_FIXED); - add_text_instance(&batch, help_texts[i], help_font_size, constraints, &text_color, ALIGN_TOP_LEFT); - - total_help_texts_height += text_size.height + text_padding; - } - - if (timer->state == TIMER_RUNNING) { - char closing_in_text[128]; - snprintf(closing_in_text, sizeof(closing_in_text), "Hiding in %ds", (int)timer_remaining(timer) + 1); - Sizef closing_in_text_size = calculate_text_size(closing_in_text, closing_in_font_size); - set_x_constraint(&constraints, overlay_width - closing_in_text_size.width - text_padding, UI_CONSTRAINT_FIXED); - set_y_constraint(&constraints, text_padding, UI_CONSTRAINT_FIXED); - add_text_instance(&batch, closing_in_text, closing_in_font_size, constraints, &closing_in_text_color, ALIGN_TOP_LEFT); - } - - draw_text_batch(&batch); -} - -void draw_timer(Timer *timer) { - if (timer->state == TIMER_STOPPED) return; - - const int font_size = 32; - const Color text_color = { - .r = 66.0f, - .g = 92.0f, - .b = 124.0f, - .a = 255.0f, - }; - const Color bg_color = { - .r = 0.0f, - .g = 0.0f, - .b = 0.0f, - .a = 25.0f, - }; - const Alignment alignment = ALIGN_BOTTOM_RIGHT; - - char timer_text[64]; - - double elapsed = timer_elapsed(timer); - - if (timer->state == TIMER_PAUSED) { - // if the timer is paused, we want to show the time at which it was paused - elapsed = timer->elapsed; - } - - int minutes = (int)elapsed / 60; - int seconds = (int)elapsed % 60; - - snprintf(timer_text, sizeof(timer_text), "Time: %02d:%02d", minutes, seconds); - - Sizef text_size = calculate_text_size(timer_text, font_size); - - UIConstraints constraints = {0}; - set_x_constraint(&constraints, 0, UI_CONSTRAINT_FIXED); - set_y_constraint(&constraints, 0, UI_CONSTRAINT_FIXED); - set_width_constraint(&constraints, text_size.width + 4, UI_CONSTRAINT_FIXED); - set_height_constraint(&constraints, text_size.height + 4, UI_CONSTRAINT_FIXED); - - draw_quad(constraints, &bg_color, 0.0, alignment); - - set_width_constraint(&constraints, 1, UI_CONSTRAINT_FIXED); - set_height_constraint(&constraints, 1, UI_CONSTRAINT_FIXED); - draw_text(timer_text, font_size, constraints, &text_color, alignment); -} diff --git a/cudoku.h b/cudoku.h index cb5f9ed..551d541 100644 --- a/cudoku.h +++ b/cudoku.h @@ -24,7 +24,13 @@ typedef struct Cudoku { } Cudoku; void draw_selection_box(int x, int y, const Color color); -void highlight_mistakes(Cudoku *game); +void draw_mistakes_highlight(Cudoku *game); +void draw_pause_overlay(void); +void draw_help(Timer *timer); +void draw_timer(Timer *timer); +void draw_win(Cudoku *game); +void draw_board(Cudoku *game, Size window_size); + void toggle_check(Cudoku *game); void do_selection(Cudoku *game, int x, int y); void set_selected_number(Cudoku *game, int number); @@ -33,9 +39,4 @@ void toggle_selection(Cudoku *game); void generate_random_board(Cudoku *game); void reset_board(Cudoku *game); bool toggle_help(Cudoku *game); -void draw_help(Timer *timer); -void draw_timer(Timer *timer); -void draw_win(Cudoku *game); -void draw_board(Cudoku *game, Size window_size); void pause_game(Cudoku *game); -void draw_pause_overlay(); diff --git a/main.c b/main.c index a05984c..0729e8d 100644 --- a/main.c +++ b/main.c @@ -163,7 +163,7 @@ int main(int argc, char *argv[]) { } if (game.should_highlight_mistakes) { - highlight_mistakes(&game); + draw_mistakes_highlight(&game); } if (timer_ended(&game.help_timer)) { diff --git a/timer.c b/timer.c index 37d1eb4..3675e8b 100644 --- a/timer.c +++ b/timer.c @@ -5,14 +5,14 @@ struct timeval start_time; -double get_time() { +double get_time(void) { struct timeval current_time; gettimeofday(¤t_time, NULL); return (double)(current_time.tv_sec - start_time.tv_sec) + (double)(current_time.tv_usec - start_time.tv_usec) / 1000000.0; } -void start_internal_timer() { +void start_internal_timer(void) { gettimeofday(&start_time, NULL); } diff --git a/timer.h b/timer.h index 75b3b35..7f64a46 100644 --- a/timer.h +++ b/timer.h @@ -15,8 +15,8 @@ typedef struct Timer { TimerState state; } Timer; -double get_time(); -void start_internal_timer(); +double get_time(void); +void start_internal_timer(void); bool timer_ended(Timer *timer); void timer_start(Timer *timer, float duration); void timer_stop(Timer *timer); diff --git a/zephr.c b/zephr.c index b5b0f9f..9923203 100644 --- a/zephr.c +++ b/zephr.c @@ -477,7 +477,7 @@ ZephrKeycode x11_keysym_to_zephr_keycode_map(KeySym sym) { /* } */ /* } */ -void x11_resize_window() { +void x11_resize_window(void) { XWindowAttributes win_attrs; XGetWindowAttributes(x11_display, x11_window, &win_attrs); glViewport(0, 0, win_attrs.width, win_attrs.height); @@ -629,7 +629,7 @@ int x11_init(const char* title, int window_width, int window_height) { return res; } -void x11_close() { +void x11_close(void) { glXMakeCurrent(x11_display, 0, 0); glXDestroyContext(x11_display, glx_context); @@ -640,8 +640,7 @@ void x11_close() { gladLoaderUnloadGLX(); } -void x11_go_fullscreen() -{ +void x11_go_fullscreen(void) { XEvent xev; Atom wm_state = XInternAtom(x11_display, "_NET_WM_STATE", False); Atom fullscreen = XInternAtom(x11_display, "_NET_WM_STATE_FULLSCREEN", False); @@ -657,8 +656,7 @@ void x11_go_fullscreen() SubstructureNotifyMask | SubstructureRedirectMask, &xev); } -void x11_return_fullscreen() -{ +void x11_return_fullscreen(void) { XEvent xev; Atom wm_state = XInternAtom(x11_display, "_NET_WM_STATE", False); Atom fullscreen = XInternAtom(x11_display, "_NET_WM_STATE_FULLSCREEN", False); @@ -729,12 +727,12 @@ u32 init_zephr(const char* font_path, const char* window_title, Size window_size return 0; } -void deinit_zephr() { +void deinit_zephr(void) { x11_close(); audio_close(); } -bool zephr_should_quit() { +bool zephr_should_quit(void) { glClearColor(0, 0, 0, 1); glClear(GL_COLOR_BUFFER_BIT); @@ -744,26 +742,26 @@ bool zephr_should_quit() { } // This MUST be called at the end of the frame -void zephr_swap_buffers() { +void zephr_swap_buffers(void) { glXSwapBuffers(x11_display, x11_window); } -Size zephr_get_window_size() { +Size zephr_get_window_size(void) { return zephr_ctx.window.size; } // This MUST be called after calling init_zephr() -void zephr_make_window_non_resizable() { +void zephr_make_window_non_resizable(void) { x11_make_window_non_resizable(zephr_ctx.window.size.width, zephr_ctx.window.size.height); } -void zephr_toggle_fullscreen() { +void zephr_toggle_fullscreen(void) { x11_toggle_fullscreen(zephr_ctx.window.is_fullscreen); zephr_ctx.window.is_fullscreen = !zephr_ctx.window.is_fullscreen; } -void zephr_quit() { +void zephr_quit(void) { zephr_ctx.should_quit = true; } diff --git a/zephr_math.c b/zephr_math.c index 740f8bc..cde37fe 100644 --- a/zephr_math.c +++ b/zephr_math.c @@ -19,7 +19,7 @@ Matrix4x4 orthographic_projection_2d(float left, float right, float bottom, floa return result; } -Matrix4x4 identity() { +Matrix4x4 identity(void) { Matrix4x4 result = {0}; result.m[0][0] = 1.0f; diff --git a/zephr_math.h b/zephr_math.h index 5f69654..b2d3183 100644 --- a/zephr_math.h +++ b/zephr_math.h @@ -39,7 +39,7 @@ typedef struct Color { } Color; float to_radians(float degrees); -Matrix4x4 identity(); +Matrix4x4 identity(void); void mult_4x4(Matrix4x4 *multiplicant, Matrix4x4 *multiplier); void apply_scale(Matrix4x4 *matrix, Sizef scale); void apply_rotation(Matrix4x4 *model, float angle);