fix: remove number character array because numbers are already included in the ascii character array

This commit is contained in:
2023-11-18 19:59:56 +02:00
parent d6cc129bed
commit 129e038c19
2 changed files with 8 additions and 35 deletions
+4 -31
View File
@@ -6,7 +6,6 @@
#include "font.h" #include "font.h"
#include "shader.h" #include "shader.h"
Character numbers[9];
Character characters[128]; Character characters[128];
int init_fonts(const char *font_path) { int init_fonts(const char *font_path) {
@@ -34,31 +33,6 @@ int init_fonts(const char *font_path) {
glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
for (int i = 0; i < 9; i++) {
if (FT_Load_Char(face, '0' + (i + 1), FT_LOAD_RENDER)) {
printf("[ERROR]: failed to load glyph for number '%c'\n", '0' + i);
}
unsigned int texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, face->glyph->bitmap.width,
face->glyph->bitmap.rows, 0, GL_RED, GL_UNSIGNED_BYTE,
face->glyph->bitmap.buffer);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
numbers[i].texture_id = texture;
numbers[i].advance = face->glyph->advance.x;
Size s = { .width = face->glyph->bitmap.width, .height = face->glyph->bitmap.rows };
numbers[i].size = s;
Size bearing = { .width = face->glyph->bitmap_left, .height = face->glyph->bitmap_top };
numbers[i].bearing = bearing;
}
for (int c = 0; c < 128; c++) { for (int c = 0; c < 128; c++) {
if (FT_Load_Char(face, c, FT_LOAD_RENDER)) { if (FT_Load_Char(face, c, FT_LOAD_RENDER)) {
printf("[ERROR]: failed to load glyph for char '%c'\n", c); printf("[ERROR]: failed to load glyph for char '%c'\n", c);
@@ -78,10 +52,8 @@ int init_fonts(const char *font_path) {
characters[c].texture_id = texture; characters[c].texture_id = texture;
characters[c].advance = face->glyph->advance.x; characters[c].advance = face->glyph->advance.x;
Size s = { .width = face->glyph->bitmap.width, .height = face->glyph->bitmap.rows }; characters[c].size = (Size){ .width = face->glyph->bitmap.width, .height = face->glyph->bitmap.rows };
characters[c].size = s; characters[c].bearing = (Size){ .width = face->glyph->bitmap_left, .height = face->glyph->bitmap_top };
Size bearing = { .width = face->glyph->bitmap_left, .height = face->glyph->bitmap_top };
characters[c].bearing = bearing;
} }
glBindTexture(GL_TEXTURE_2D, 0); glBindTexture(GL_TEXTURE_2D, 0);
@@ -135,7 +107,8 @@ void draw_number(Shader shader, Cell cell, int row, int column, float scale, uns
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);
glBindVertexArray(vao); glBindVertexArray(vao);
Character ch = numbers[cell.value - 1]; // get ascii value of cell value and use it to index into characters
Character ch = characters['0' + cell.value];
float w = ch.size.width; float w = ch.size.width;
float h = ch.size.height; float h = ch.size.height;
+4 -4
View File
@@ -10,6 +10,8 @@
#include "font.h" #include "font.h"
#include "audio.h" #include "audio.h"
#define DEBUG 1
Display *display; Display *display;
Window window; Window window;
@@ -27,6 +29,7 @@ void handle_keypress(XEvent xev, Cudoku *game) {
xev.xkey.state & ControlMask && xev.xkey.state & ControlMask &&
xev.xkey.state & Mod1Mask && xev.xkey.state & Mod1Mask &&
XLookupKeysym(&xev.xkey, 0) == XK_w) { XLookupKeysym(&xev.xkey, 0) == XK_w) {
#if DEBUG
printf("DEBUG: Ctrl + Shift + W pressed. Winning game\n"); printf("DEBUG: Ctrl + Shift + W pressed. Winning game\n");
game->has_won = true; game->has_won = true;
@@ -37,6 +40,7 @@ void handle_keypress(XEvent xev, Cudoku *game) {
timer_stop(&game->timer); timer_stop(&game->timer);
audio_play_win(); audio_play_win();
#endif
} else if (XLookupKeysym(&xev.xkey, 0) >= XK_0 && XLookupKeysym(&xev.xkey, 0) <= XK_9) { } else if (XLookupKeysym(&xev.xkey, 0) >= XK_0 && XLookupKeysym(&xev.xkey, 0) <= XK_9) {
set_selected_number(game, XLookupKeysym(&xev.xkey, 0) - XK_0); set_selected_number(game, XLookupKeysym(&xev.xkey, 0) - XK_0);
} else if (XLookupKeysym(&xev.xkey, 0) == XK_BackSpace || } else if (XLookupKeysym(&xev.xkey, 0) == XK_BackSpace ||
@@ -207,10 +211,6 @@ int main(int argc, char *argv[]) {
} }
} }
/* printf("clock: %f\n", get_time()); */
/* printf("delta: %f\n", delta_t); */
/* printf("FPS: %f\n", 1.0 / delta_t); */
glClearColor(0.2, 0.2, 0.2, 1.0); glClearColor(0.2, 0.2, 0.2, 1.0);
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);