mirror of
https://github.com/IllusionMan1212/cudoku.git
synced 2026-10-24 02:12:25 +00:00
super basic UI constraint and alignment system with a super basic "game framework"
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
@@ -5,15 +6,14 @@
|
||||
#include <glad/glx.h>
|
||||
#include "3rdparty/stb/stb_image.h"
|
||||
|
||||
#include "x11.h"
|
||||
#include "cudoku.h"
|
||||
#include "font.h"
|
||||
#include "ui.h"
|
||||
#include "audio.h"
|
||||
|
||||
#define DEBUG 1
|
||||
|
||||
Display *display;
|
||||
Window window;
|
||||
const char *font_path = "assets/fonts/Rubik/Rubik-VariableFont_wght.ttf";
|
||||
const char *title = "Cudoku";
|
||||
|
||||
void usage() {
|
||||
printf("Usage: cudoku [OPTION]\n\n");
|
||||
@@ -73,7 +73,7 @@ void handle_keypress(XEvent xev, Cudoku *game) {
|
||||
} else if (XLookupKeysym(&xev.xkey, 0) == XK_n) {
|
||||
generate_random_board(game);
|
||||
} else if (XLookupKeysym(&xev.xkey, 0) == XK_F11) {
|
||||
toggle_fullscreen(display, window);
|
||||
/* toggle_fullscreen(display, window); */
|
||||
} else if (XLookupKeysym(&xev.xkey, 0) == XK_c) {
|
||||
toggle_check(game);
|
||||
/* } else if (XLookupKeysym(&xev.xkey, 0) == XK_m) { */
|
||||
@@ -86,17 +86,8 @@ void handle_keypress(XEvent xev, Cudoku *game) {
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
int res = audio_init();
|
||||
if (res != 0) {
|
||||
printf("[ERROR]: failed to initialize audio\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
const char *font_path = "assets/fonts/Rubik/Rubik-VariableFont_wght.ttf";
|
||||
bool use_texture = false;
|
||||
|
||||
// 900 is the initial width and height
|
||||
int width = 900, height = 900;
|
||||
// scale factors for the entire board to nicely fit as a square inside
|
||||
// the window
|
||||
float x_scale = 1.f, y_scale = 1.f;
|
||||
@@ -130,15 +121,10 @@ int main(int argc, char *argv[]) {
|
||||
}
|
||||
}
|
||||
|
||||
res = init_x11(&display, &window);
|
||||
if (res < 0) return 1;
|
||||
|
||||
res = init_fonts(font_path);
|
||||
if (res == -1) {
|
||||
printf("[ERROR]: could not initialize freetype library\n");
|
||||
return 1;
|
||||
} else if (res == -2) {
|
||||
printf("[ERROR]: could not load font file: \"%s\"\n", font_path);
|
||||
Color clear_color = {0.2f, 0.15f, 0.5f, 1.f};
|
||||
int res = init_zephr(font_path, title, (Size){800, 600}, &clear_color);
|
||||
if (res != 0) {
|
||||
printf("[ERROR]: could not initialize zephr\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -148,10 +134,6 @@ int main(int argc, char *argv[]) {
|
||||
game.should_draw_help = true;
|
||||
generate_random_board(&game);
|
||||
|
||||
unsigned int font_vao, font_vbo;
|
||||
prepare_font(&font_vao, &font_vbo);
|
||||
Shader font_shader = create_shader("shaders/font_v.vert", "shaders/font_f.frag");
|
||||
|
||||
Shader grid_shader;
|
||||
unsigned int grid_texture;
|
||||
if (!use_texture)
|
||||
@@ -170,107 +152,147 @@ int main(int argc, char *argv[]) {
|
||||
unsigned int win_vao = prepare_win_overlay();
|
||||
|
||||
float delta_t, last_t = 0.0;
|
||||
start_internal_timer();
|
||||
timer_start(&game.help_timer, 5.0f);
|
||||
timer_start(&game.timer, 0.0f);
|
||||
|
||||
bool quit = false;
|
||||
while (!quit) {
|
||||
while (!zephr_should_quit()) {
|
||||
float now = get_time();
|
||||
delta_t = now - last_t;
|
||||
last_t = now;
|
||||
|
||||
while (XPending(display)) {
|
||||
XEvent xev;
|
||||
XNextEvent(display, &xev);
|
||||
Size window_size = zephr_get_window_size();
|
||||
|
||||
if (xev.type == ConfigureNotify) {
|
||||
XConfigureEvent xce = xev.xconfigure;
|
||||
/* int width = 15.f; */
|
||||
/* int height = 35.f; */
|
||||
/* int padding = 10.f; */
|
||||
|
||||
if (xce.width != width || xce.height != height) {
|
||||
width = xce.width;
|
||||
height = xce.height;
|
||||
resize_x11_window(display, window);
|
||||
set_scale_factor(xce.width, xce.height, &x_scale, &y_scale);
|
||||
}
|
||||
float ratio_w = (float)window_size.width / 1920.f;
|
||||
float ratio_h = (float)window_size.height / 1080.f;
|
||||
|
||||
} else if (xev.type == KeyPress) {
|
||||
if (XLookupKeysym(&xev.xkey, 0) == XK_Escape ||
|
||||
(XLookupKeysym(&xev.xkey, 0) == XK_q && xev.xkey.state & ControlMask)) {
|
||||
quit = true;
|
||||
break;
|
||||
} else {
|
||||
handle_keypress(xev, &game);
|
||||
}
|
||||
} else if (xev.type == ButtonPress) {
|
||||
if (xev.xbutton.button == Button1) {
|
||||
int x = xev.xbutton.x;
|
||||
int y = xev.xbutton.y;
|
||||
do_selection(&game, x, y, width, height, x_scale, y_scale);
|
||||
}
|
||||
}
|
||||
int width = window_size.height;
|
||||
int height = window_size.width;
|
||||
|
||||
if (window_size.height > window_size.width) {
|
||||
width = window_size.width;
|
||||
height = window_size.width;
|
||||
} else {
|
||||
width = window_size.height;
|
||||
height = window_size.height;
|
||||
}
|
||||
|
||||
glClearColor(0.2, 0.2, 0.2, 1.0);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
draw_win();
|
||||
|
||||
float transform[4][4] = {
|
||||
{x_scale, 0, 0, 0},
|
||||
{0, y_scale, 0, 0},
|
||||
{0, 0, 1, 0},
|
||||
{0, 0, 0, 1},
|
||||
};
|
||||
/* draw_quad( */
|
||||
/* (Vec2){0.0, 0.0}, */
|
||||
/* (Size){width, height}, */
|
||||
/* &(Color){240.f, 235.f, 227.f, 255.f}, */
|
||||
/* ALIGN_CENTER); */
|
||||
/* UIConstraints constraints = {0}; */
|
||||
/* set_x_constraint(&constraints, 0.0, UI_CONSTRAINT_FIXED); */
|
||||
/* set_y_constraint(&constraints, 0.0, UI_CONSTRAINT_FIXED); */
|
||||
/* set_width_constraint(&constraints, 50, UI_CONSTRAINT_FIXED); */
|
||||
/* set_height_constraint(&constraints, 50, UI_CONSTRAINT_FIXED); */
|
||||
|
||||
Matrix4x4 projection = orthographic_projection_2d(0.0f, width, 0.0f, height);
|
||||
/* draw_quad( */
|
||||
/* constraints, */
|
||||
/* &(Color){240.f, 235.f, 227.f, 255.f}, */
|
||||
/* ALIGN_TOP_LEFT */
|
||||
/* ); */
|
||||
|
||||
if (!use_texture)
|
||||
draw_bg_grid_shader(grid_shader, board_vao, (float *)transform, width < height ? width : height);
|
||||
else
|
||||
draw_bg_grid_texture(grid_shader, board_vao, grid_texture, (float *)transform);
|
||||
/* Size size = calculate_text_size("Bruh", 1.0f); */
|
||||
/* float x = clampf(500.0f * timer_elapsed(&my_time), 0.0, window_size.width - size.width); */
|
||||
/* draw_text("Bruh", 1.0f, (Vec2){0.0, size.height}, &(Color){255.f, 0.f, 0.f, 255.f}); */
|
||||
|
||||
if (game.should_draw_selection)
|
||||
draw_selection_box(
|
||||
selection_shader,
|
||||
selection_vao,
|
||||
selection_vbo,
|
||||
game.selection.x,
|
||||
game.selection.y,
|
||||
(float *)transform,
|
||||
selection_color);
|
||||
|
||||
draw_numbers(font_shader, font_vao, font_vbo, (float *)transform, game.board);
|
||||
|
||||
if (game.should_highlight_mistakes) {
|
||||
highlight_mistakes(selection_shader, selection_vao, selection_vbo, (float *)transform, &game);
|
||||
|
||||
char* text = "Mistake highlighter is ON";
|
||||
float text_scale = 0.2;
|
||||
Size text_size = calculate_text_size(text, text_scale);
|
||||
Vec2 text_pos = { 0.0f, text_size.height - 10.f };
|
||||
Color text_color = { 200.0f, 50.0f, 50.0f, 1.0f };
|
||||
draw_text_at(font_shader, text, text_pos, text_scale, font_vao, font_vbo, (float *)projection.m, &text_color);
|
||||
}
|
||||
|
||||
draw_timer(font_shader, font_vao, font_vbo, (float *)projection.m, &game.timer, width);
|
||||
|
||||
if (timer_ended(&game.help_timer)) {
|
||||
timer_stop(&game.help_timer);
|
||||
game.should_draw_help = false;
|
||||
}
|
||||
|
||||
if (game.should_draw_help)
|
||||
draw_help_overlay(selection_shader, font_shader, selection_vao, selection_vbo, font_vao, font_vbo, &projection, height, &game.help_timer);
|
||||
|
||||
if (game.has_won) {
|
||||
draw_win_overlay(win_shader, font_shader, win_vao, font_vao, font_vbo, (float *)transform, &game);
|
||||
}
|
||||
|
||||
glXSwapBuffers(display, window);
|
||||
audio_update();
|
||||
zephr_swap_buffers();
|
||||
}
|
||||
/* while (!quit) { */
|
||||
|
||||
audio_cleanup();
|
||||
close_x11(&display, &window);
|
||||
/* /1* while (XPending(display)) { *1/ */
|
||||
/* /1* XEvent xev; *1/ */
|
||||
/* /1* XNextEvent(display, &xev); *1/ */
|
||||
|
||||
/* /1* if (xev.type == ConfigureNotify) { *1/ */
|
||||
/* /1* XConfigureEvent xce = xev.xconfigure; *1/ */
|
||||
|
||||
/* /1* if (xce.width != width || xce.height != height) { *1/ */
|
||||
/* /1* width = xce.width; *1/ */
|
||||
/* /1* height = xce.height; *1/ */
|
||||
/* /1* resize_x11_window(display, window); *1/ */
|
||||
/* /1* set_scale_factor(xce.width, xce.height, &x_scale, &y_scale); *1/ */
|
||||
/* /1* } *1/ */
|
||||
|
||||
/* /1* } else if (xev.type == KeyPress) { *1/ */
|
||||
/* /1* if (XLookupKeysym(&xev.xkey, 0) == XK_Escape || *1/ */
|
||||
/* /1* (XLookupKeysym(&xev.xkey, 0) == XK_q && xev.xkey.state & ControlMask)) { *1/ */
|
||||
/* /1* quit = true; *1/ */
|
||||
/* /1* break; *1/ */
|
||||
/* /1* } else { *1/ */
|
||||
/* /1* handle_keypress(xev, &game); *1/ */
|
||||
/* /1* } *1/ */
|
||||
/* /1* } else if (xev.type == ButtonPress) { *1/ */
|
||||
/* /1* if (xev.xbutton.button == Button1) { *1/ */
|
||||
/* /1* int x = xev.xbutton.x; *1/ */
|
||||
/* /1* int y = xev.xbutton.y; *1/ */
|
||||
/* /1* do_selection(&game, x, y, width, height, x_scale, y_scale); *1/ */
|
||||
/* /1* } *1/ */
|
||||
/* /1* } *1/ */
|
||||
/* /1* } *1/ */
|
||||
|
||||
/* float transform[4][4] = { */
|
||||
/* {x_scale, 0, 0, 0}, */
|
||||
/* {0, y_scale, 0, 0}, */
|
||||
/* {0, 0, 1, 0}, */
|
||||
/* {0, 0, 0, 1}, */
|
||||
/* }; */
|
||||
|
||||
/* /1* Matrix4x4 projection = orthographic_projection_2d(0.0f, width, 0.0f, height); *1/ */
|
||||
|
||||
/* /1* if (!use_texture) *1/ */
|
||||
/* /1* draw_bg_grid_shader(grid_shader, board_vao, (float *)transform, width < height ? width : height); *1/ */
|
||||
/* /1* else *1/ */
|
||||
/* /1* draw_bg_grid_texture(grid_shader, board_vao, grid_texture, (float *)transform); *1/ */
|
||||
|
||||
/* if (game.should_draw_selection) */
|
||||
/* draw_selection_box( */
|
||||
/* selection_shader, */
|
||||
/* selection_vao, */
|
||||
/* selection_vbo, */
|
||||
/* game.selection.x, */
|
||||
/* game.selection.y, */
|
||||
/* (float *)transform, */
|
||||
/* selection_color); */
|
||||
|
||||
/* /1* draw_numbers(font_shader, font_vao, font_vbo, (float *)transform, game.board); *1/ */
|
||||
|
||||
/* /1* if (game.should_highlight_mistakes) { *1/ */
|
||||
/* /1* highlight_mistakes(selection_shader, selection_vao, selection_vbo, (float *)transform, &game); *1/ */
|
||||
|
||||
/* /1* char* text = "Mistake highlighter is ON"; *1/ */
|
||||
/* /1* float text_scale = 0.2; *1/ */
|
||||
/* /1* Size text_size = calculate_text_size(text, text_scale); *1/ */
|
||||
/* /1* Vec2 text_pos = { 0.0f, text_size.height - 10.f }; *1/ */
|
||||
/* /1* Color text_color = { 200.0f, 50.0f, 50.0f, 1.0f }; *1/ */
|
||||
/* /1* draw_text_at(font_shader, text, text_pos, text_scale, font_vao, font_vbo, (float *)projection.m, &text_color); *1/ */
|
||||
/* /1* } *1/ */
|
||||
|
||||
/* /1* draw_timer(font_shader, font_vao, font_vbo, (float *)projection.m, &game.timer, width); *1/ */
|
||||
|
||||
/* if (timer_ended(&game.help_timer)) { */
|
||||
/* timer_stop(&game.help_timer); */
|
||||
/* game.should_draw_help = false; */
|
||||
/* } */
|
||||
|
||||
/* /1* if (game.should_draw_help) *1/ */
|
||||
/* /1* draw_help_overlay(selection_shader, font_shader, selection_vao, selection_vbo, font_vao, font_vbo, &projection, height, &game.help_timer); *1/ */
|
||||
|
||||
/* /1* if (game.has_won) { *1/ */
|
||||
/* /1* draw_win_overlay(win_shader, font_shader, win_vao, font_vao, font_vbo, (float *)transform, &game); *1/ */
|
||||
/* /1* } *1/ */
|
||||
|
||||
/* /1* glXSwapBuffers(display, window); *1/ */
|
||||
/* } */
|
||||
|
||||
deinit_zephr();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user