text drawing using freetype

This commit is contained in:
2023-09-12 03:21:54 +02:00
parent 6f70018892
commit 41d28a222c
18 changed files with 697 additions and 50 deletions
+87 -26
View File
@@ -3,57 +3,109 @@
#include <string.h>
#include <glad/glx.h>
#include <time.h>
#include "3rdparty/stb/stb_image.h"
#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
#include "font.h"
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");
printf(" %-20s%-20s", "-f, --font FONT", "use a custom font file to render text\n");
}
Display *display;
Window window;
int main(int argc, char *argv[]) {
const char *font_path = "assets/fonts/Rubik/Rubik-VariableFont_wght.ttf";
/* const char *font_path = "assets/fonts/Roboto/Roboto-Bold.ttf"; */
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;
}
}
int res = init_x11(&display, &window);
if (res < 0) return 1;
// 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;
if (argc > 1) {
char *flag = argv[1];
if (strcmp(flag, "-h") == 0 || strcmp(flag, "--help") == 0) {
usage();
return 0;
}
for (int i = 1; i < argc; i++) {
char *flag = argv[i];
if (strcmp(flag, "-f") == 0 || strcmp(flag, "--font") == 0) {
if (i + 1 < argc) {
font_path = argv[i + 1];
i++;
} else {
printf("Error: used font flag with no provided font, defaulting to Rubik\n");
}
} else if (strcmp(flag, "--use-texture") == 0) {
use_texture = true;
}
}
}
int 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);
return 1;
}
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)
grid_shader = create_shader("shaders/board_v.glsl", "shaders/grid_f.glsl");
grid_shader = create_shader("shaders/board_v.vert", "shaders/grid_f.frag");
else
grid_shader = create_shader("shaders/board_v.glsl", "shaders/board_f.glsl");
grid_shader = create_shader("shaders/board_v.vert", "shaders/board_f.frag");
int vao = prepare_bg(use_texture);
int vao = prepare_bg(use_texture, &grid_texture);
int board[9][9] = {
{0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0},
};
srand(time(NULL));
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
int r = (rand() % 9) + 1;
board[i][j] = r;
}
}
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
printf("%d | ", board[i][j]);
}
printf("\n");
}
printf("\n");
bool quit = false;
while (!quit) {
@@ -76,10 +128,17 @@ int main(int argc, char *argv[]) {
quit = true;
break;
}
} else if (xev.type == ButtonPress) {
if (xev.xbutton.button == Button1) {
int x = xev.xbutton.x;
int y = xev.xbutton.y;
// TOOD:
printf("x: %d, y: %d\n", x, y);
}
}
}
glClearColor(0.0, 0.0, 0.0, 1.0);
glClearColor(0.2, 0.2, 0.2, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
float transform[4][4] = {
@@ -92,7 +151,9 @@ int main(int argc, char *argv[]) {
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);
draw_bg_grid_texture(grid_shader, vao, grid_texture, (float *)transform);
/* draw_numbers(font_shader, font_vao, font_vbo, (float *)transform, board); */
draw_win_overlay(font_shader, font_vao, font_vbo, (float *)transform);
glXSwapBuffers(display, window);
}