code organization changes

This commit is contained in:
2023-12-11 23:00:31 +02:00
committed by Illusion
parent b1e734e9ab
commit f491dafa56
10 changed files with 188 additions and 172 deletions
+5 -5
View File
@@ -8,7 +8,7 @@ FMOD_CHANNELGROUP *sfx_channel_group;
FMOD_SOUND *scribble_sound; FMOD_SOUND *scribble_sound;
FMOD_SOUND *win_sound; FMOD_SOUND *win_sound;
int audio_init() { int audio_init(void) {
// create fmod_system // create fmod_system
FMOD_RESULT fmod_res = FMOD_System_Create(&fmod_system, FMOD_VERSION); FMOD_RESULT fmod_res = FMOD_System_Create(&fmod_system, FMOD_VERSION);
if (fmod_res != FMOD_OK) { if (fmod_res != FMOD_OK) {
@@ -41,25 +41,25 @@ int audio_init() {
return 0; return 0;
} }
void audio_update() { void audio_update(void) {
FMOD_System_Update(fmod_system); 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); FMOD_RESULT fmod_res = FMOD_System_PlaySound(fmod_system, scribble_sound, sfx_channel_group, false, NULL);
if (fmod_res != FMOD_OK) { if (fmod_res != FMOD_OK) {
printf("Failed to play \"scribble\" sound\n"); 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); FMOD_RESULT fmod_res = FMOD_System_PlaySound(fmod_system, win_sound, sfx_channel_group, false, NULL);
if (fmod_res != FMOD_OK) { if (fmod_res != FMOD_OK) {
printf("Failed to play \"win\" sound\n"); printf("Failed to play \"win\" sound\n");
}; };
} }
void audio_close() { void audio_close(void) {
FMOD_Sound_Release(win_sound); FMOD_Sound_Release(win_sound);
FMOD_Sound_Release(scribble_sound); FMOD_Sound_Release(scribble_sound);
FMOD_ChannelGroup_Release(sfx_channel_group); FMOD_ChannelGroup_Release(sfx_channel_group);
+5 -5
View File
@@ -1,7 +1,7 @@
#pragma once #pragma once
int audio_init(); int audio_init(void);
void audio_close(); void audio_close(void);
void audio_update(); void audio_update(void);
void audio_play_scribble(); void audio_play_scribble(void);
void audio_play_win(); void audio_play_win(void);
+153 -136
View File
@@ -34,6 +34,14 @@ static const char *help_texts[HELP_TEXT_SIZE] = {
"Ctrl+Q/Esc - Quit" "Ctrl+Q/Esc - Quit"
}; };
//////////////////////////////////////
//
//
// Drawing functions
//
//
//////////////////////////////////////
void draw_win(Cudoku *game) { void draw_win(Cudoku *game) {
Size window = zephr_get_window_size(); Size window = zephr_get_window_size();
Color bg_color = {0, 0, 0, 200.f}; 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(); Size window = zephr_get_window_size();
Color bg_color = {0, 0, 0, 200.f}; Color bg_color = {0, 0, 0, 200.f};
Color text_color = {237.f, 225.f, 215.f, 255.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); draw_text("Paused", 100.f, constraints, &text_color, ALIGN_CENTER);
} }
void pause_game(Cudoku *game) { void draw_mistakes_highlight(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) {
for (int i = 0; i < 9; i++) { for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) { for (int j = 0; j < 9; j++) {
Cell cell = game->board[i][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); 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) { void toggle_check(Cudoku *game) {
if (game->has_won || game->timer.state == TIMER_PAUSED) return; if (game->has_won || game->timer.state == TIMER_PAUSED) return;
@@ -523,113 +650,3 @@ bool toggle_help(Cudoku *game) {
return game->should_draw_help; 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);
}
+7 -6
View File
@@ -24,7 +24,13 @@ typedef struct Cudoku {
} Cudoku; } Cudoku;
void draw_selection_box(int x, int y, const Color color); 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 toggle_check(Cudoku *game);
void do_selection(Cudoku *game, int x, int y); void do_selection(Cudoku *game, int x, int y);
void set_selected_number(Cudoku *game, int number); void set_selected_number(Cudoku *game, int number);
@@ -33,9 +39,4 @@ void toggle_selection(Cudoku *game);
void generate_random_board(Cudoku *game); void generate_random_board(Cudoku *game);
void reset_board(Cudoku *game); void reset_board(Cudoku *game);
bool toggle_help(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 pause_game(Cudoku *game);
void draw_pause_overlay();
+1 -1
View File
@@ -163,7 +163,7 @@ int main(int argc, char *argv[]) {
} }
if (game.should_highlight_mistakes) { if (game.should_highlight_mistakes) {
highlight_mistakes(&game); draw_mistakes_highlight(&game);
} }
if (timer_ended(&game.help_timer)) { if (timer_ended(&game.help_timer)) {
+2 -2
View File
@@ -5,14 +5,14 @@
struct timeval start_time; struct timeval start_time;
double get_time() { double get_time(void) {
struct timeval current_time; struct timeval current_time;
gettimeofday(&current_time, NULL); gettimeofday(&current_time, NULL);
return (double)(current_time.tv_sec - start_time.tv_sec) + (double)(current_time.tv_usec - start_time.tv_usec) / 1000000.0; 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); gettimeofday(&start_time, NULL);
} }
+2 -2
View File
@@ -15,8 +15,8 @@ typedef struct Timer {
TimerState state; TimerState state;
} Timer; } Timer;
double get_time(); double get_time(void);
void start_internal_timer(); void start_internal_timer(void);
bool timer_ended(Timer *timer); bool timer_ended(Timer *timer);
void timer_start(Timer *timer, float duration); void timer_start(Timer *timer, float duration);
void timer_stop(Timer *timer); void timer_stop(Timer *timer);
+11 -13
View File
@@ -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; XWindowAttributes win_attrs;
XGetWindowAttributes(x11_display, x11_window, &win_attrs); XGetWindowAttributes(x11_display, x11_window, &win_attrs);
glViewport(0, 0, win_attrs.width, win_attrs.height); 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; return res;
} }
void x11_close() { void x11_close(void) {
glXMakeCurrent(x11_display, 0, 0); glXMakeCurrent(x11_display, 0, 0);
glXDestroyContext(x11_display, glx_context); glXDestroyContext(x11_display, glx_context);
@@ -640,8 +640,7 @@ void x11_close() {
gladLoaderUnloadGLX(); gladLoaderUnloadGLX();
} }
void x11_go_fullscreen() void x11_go_fullscreen(void) {
{
XEvent xev; XEvent xev;
Atom wm_state = XInternAtom(x11_display, "_NET_WM_STATE", False); Atom wm_state = XInternAtom(x11_display, "_NET_WM_STATE", False);
Atom fullscreen = XInternAtom(x11_display, "_NET_WM_STATE_FULLSCREEN", False); Atom fullscreen = XInternAtom(x11_display, "_NET_WM_STATE_FULLSCREEN", False);
@@ -657,8 +656,7 @@ void x11_go_fullscreen()
SubstructureNotifyMask | SubstructureRedirectMask, &xev); SubstructureNotifyMask | SubstructureRedirectMask, &xev);
} }
void x11_return_fullscreen() void x11_return_fullscreen(void) {
{
XEvent xev; XEvent xev;
Atom wm_state = XInternAtom(x11_display, "_NET_WM_STATE", False); Atom wm_state = XInternAtom(x11_display, "_NET_WM_STATE", False);
Atom fullscreen = XInternAtom(x11_display, "_NET_WM_STATE_FULLSCREEN", 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; return 0;
} }
void deinit_zephr() { void deinit_zephr(void) {
x11_close(); x11_close();
audio_close(); audio_close();
} }
bool zephr_should_quit() { bool zephr_should_quit(void) {
glClearColor(0, 0, 0, 1); glClearColor(0, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
@@ -744,26 +742,26 @@ bool zephr_should_quit() {
} }
// This MUST be called at the end of the frame // This MUST be called at the end of the frame
void zephr_swap_buffers() { void zephr_swap_buffers(void) {
glXSwapBuffers(x11_display, x11_window); glXSwapBuffers(x11_display, x11_window);
} }
Size zephr_get_window_size() { Size zephr_get_window_size(void) {
return zephr_ctx.window.size; return zephr_ctx.window.size;
} }
// This MUST be called after calling init_zephr() // 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); 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); x11_toggle_fullscreen(zephr_ctx.window.is_fullscreen);
zephr_ctx.window.is_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; zephr_ctx.should_quit = true;
} }
+1 -1
View File
@@ -19,7 +19,7 @@ Matrix4x4 orthographic_projection_2d(float left, float right, float bottom, floa
return result; return result;
} }
Matrix4x4 identity() { Matrix4x4 identity(void) {
Matrix4x4 result = {0}; Matrix4x4 result = {0};
result.m[0][0] = 1.0f; result.m[0][0] = 1.0f;
+1 -1
View File
@@ -39,7 +39,7 @@ typedef struct Color {
} Color; } Color;
float to_radians(float degrees); float to_radians(float degrees);
Matrix4x4 identity(); Matrix4x4 identity(void);
void mult_4x4(Matrix4x4 *multiplicant, Matrix4x4 *multiplier); void mult_4x4(Matrix4x4 *multiplicant, Matrix4x4 *multiplier);
void apply_scale(Matrix4x4 *matrix, Sizef scale); void apply_scale(Matrix4x4 *matrix, Sizef scale);
void apply_rotation(Matrix4x4 *model, float angle); void apply_rotation(Matrix4x4 *model, float angle);