2023-09-03 00:51:45 +03:00
|
|
|
#include <stdbool.h>
|
2023-09-08 16:38:14 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <string.h>
|
2023-09-03 00:51:45 +03:00
|
|
|
|
|
|
|
|
#include <glad/glx.h>
|
2023-09-08 16:38:14 +02:00
|
|
|
#include "3rdparty/stb/stb_image.h"
|
2023-09-03 00:51:45 +03:00
|
|
|
|
|
|
|
|
#include "x11.h"
|
|
|
|
|
#include "cudoku.h"
|
|
|
|
|
|
|
|
|
|
// TODO: everything from scratch
|
|
|
|
|
// X11 for windowing and input
|
|
|
|
|
// OpenGL for graphics
|
|
|
|
|
// OpenAL for audio ?? (copilot suggested this. I do want to do some audio desu)
|
|
|
|
|
// Simply port the js algo for sudoku generation
|
|
|
|
|
// Maybe after the game works fine we can use some winapi to add windows support
|
|
|
|
|
|
2023-09-08 16:38:14 +02:00
|
|
|
void usage() {
|
|
|
|
|
printf("Usage: cudoku [OPTION]\n\n");
|
|
|
|
|
printf(" %-20s%-20s", "-h, --help", "prints this help message\n");
|
|
|
|
|
printf(" %-20s%-20s", "--use-texture", "use a texture instead of generating the grid using shaders\n");
|
2023-09-04 01:57:39 +03:00
|
|
|
}
|
|
|
|
|
|
2023-09-03 00:51:45 +03:00
|
|
|
Display *display;
|
|
|
|
|
Window window;
|
|
|
|
|
|
2023-09-08 16:38:14 +02:00
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
|
bool use_texture = false;
|
|
|
|
|
|
|
|
|
|
if (argc > 1) {
|
|
|
|
|
char *flag = argv[1];
|
|
|
|
|
if (strcmp(flag, "--use-texture") == 0) {
|
|
|
|
|
use_texture = true;
|
|
|
|
|
}
|
|
|
|
|
if (strcmp(flag, "-h") == 0 || strcmp(flag, "--help") == 0) {
|
|
|
|
|
usage();
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-03 00:51:45 +03:00
|
|
|
int res = init_x11(&display, &window);
|
|
|
|
|
if (res < 0) return 1;
|
|
|
|
|
|
2023-09-04 01:57:39 +03:00
|
|
|
// 900 is the initial width and height
|
2023-09-03 00:51:45 +03:00
|
|
|
int width = 900, height = 900;
|
2023-09-04 01:57:39 +03:00
|
|
|
// scale factors for the entire board to nicely fit as a square inside
|
|
|
|
|
// the window
|
|
|
|
|
float x_scale = 1.f, y_scale = 1.f;
|
2023-09-03 00:51:45 +03:00
|
|
|
|
2023-09-08 16:38:14 +02:00
|
|
|
Shader grid_shader;
|
|
|
|
|
if (!use_texture)
|
|
|
|
|
grid_shader = create_shader("shaders/board_v.glsl", "shaders/grid_f.glsl");
|
|
|
|
|
else
|
|
|
|
|
grid_shader = create_shader("shaders/board_v.glsl", "shaders/board_f.glsl");
|
|
|
|
|
|
|
|
|
|
int vao = prepare_bg(use_texture);
|
2023-09-03 00:51:45 +03:00
|
|
|
|
|
|
|
|
bool quit = false;
|
|
|
|
|
while (!quit) {
|
|
|
|
|
while (XPending(display)) {
|
|
|
|
|
XEvent xev;
|
|
|
|
|
XNextEvent(display, &xev);
|
|
|
|
|
|
|
|
|
|
if (xev.type == ConfigureNotify) {
|
|
|
|
|
XConfigureEvent xce = xev.xconfigure;
|
|
|
|
|
|
|
|
|
|
if (xce.width != width || xce.height != height) {
|
|
|
|
|
width = xce.width;
|
|
|
|
|
height = xce.height;
|
|
|
|
|
resize_x11_window(display, window);
|
2023-09-08 16:38:14 +02:00
|
|
|
set_scale_factor(xce.width, xce.height, &x_scale, &y_scale);
|
2023-09-03 00:51:45 +03:00
|
|
|
}
|
|
|
|
|
|
2023-09-04 01:57:39 +03:00
|
|
|
} else if (xev.type == KeyPress) {
|
|
|
|
|
if (XLookupKeysym(&xev.xkey, 0) == XK_Escape || XLookupKeysym(&xev.xkey, 0) == XK_q) {
|
|
|
|
|
quit = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2023-09-03 00:51:45 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
glClearColor(0.0, 0.0, 0.0, 1.0);
|
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
|
|
2023-09-08 01:51:09 +02:00
|
|
|
float transform[4][4] = {
|
|
|
|
|
{x_scale, 0, 0, 0},
|
|
|
|
|
{0, y_scale, 0, 0},
|
|
|
|
|
{0, 0, 1, 0},
|
|
|
|
|
{0, 0, 0, 1},
|
|
|
|
|
};
|
|
|
|
|
|
2023-09-08 16:38:14 +02:00
|
|
|
if (!use_texture)
|
|
|
|
|
draw_bg_grid_shader(grid_shader, vao, (float *)transform, width < height ? width : height);
|
|
|
|
|
else
|
|
|
|
|
draw_bg_grid_texture(grid_shader, vao, (float *)transform);
|
2023-09-03 00:51:45 +03:00
|
|
|
|
|
|
|
|
glXSwapBuffers(display, window);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
close_x11(&display, &window);
|
|
|
|
|
|
2023-09-02 00:17:15 +03:00
|
|
|
return 0;
|
|
|
|
|
}
|
2023-09-03 00:51:45 +03:00
|
|
|
|