diff --git a/cudoku.c b/cudoku.c index ba15835..b39da88 100644 --- a/cudoku.c +++ b/cudoku.c @@ -63,14 +63,26 @@ void draw_win() { Sizef text_size = calculate_text_size(win_text, font_size); - set_x_constraint(&constraints, 0, UI_CONSTRAINT_FIXED); + set_x_constraint(&constraints, 50, UI_CONSTRAINT_FIXED); set_y_constraint(&constraints, 0, UI_CONSTRAINT_FIXED); - set_width_constraint(&constraints, 200, UI_CONSTRAINT_FIXED); - set_height_constraint(&constraints, 200, UI_CONSTRAINT_FIXED); + set_width_constraint(&constraints, 50, UI_CONSTRAINT_FIXED); + set_height_constraint(&constraints, 50, UI_CONSTRAINT_FIXED); - draw_quad(constraints, &bg_color, 100.00, ALIGN_TOP_LEFT); - draw_quad(constraints, &bg_color, 0.50, ALIGN_BOTTOM_RIGHT); - draw_quad(constraints, &bg_color, 0.0, align); + UIConstraints circle_const; + set_x_constraint(&circle_const, 50, UI_CONSTRAINT_FIXED); + set_y_constraint(&circle_const, 0, UI_CONSTRAINT_FIXED); + set_width_constraint(&circle_const, 70, UI_CONSTRAINT_FIXED); + set_height_constraint(&circle_const, 90, UI_CONSTRAINT_FIXED); + +/* draw_circle(circle_const, NULL, ALIGN_LEFT_CENTER); */ + /* draw_quad(constraints, &bg_color, 0.0, ALIGN_TOP_LEFT); */ +/* set_x_constraint(&constraints, -50, UI_CONSTRAINT_FIXED); */ +/* draw_quad(constraints, &bg_color, 25.0, ALIGN_TOP_RIGHT); */ +/* set_x_constraint(&constraints, 0, UI_CONSTRAINT_FIXED); */ +/* draw_quad(constraints, &bg_color, 0.5, ALIGN_BOTTOM_RIGHT); */ +/* draw_quad(constraints, &bg_color, 0.0, align); */ + + /* draw_triangle(constraints, &bg_color, ALIGN_CENTER); */ draw_text(win_text, font_size, (Vec2f){0.0, 0.0}, &text_color, align); } diff --git a/helper.c b/helper.c index 1fc8274..f208a76 100644 --- a/helper.c +++ b/helper.c @@ -1,3 +1,5 @@ +#include + #include "helper.h" Matrix4x4 identity() { @@ -11,6 +13,34 @@ Matrix4x4 identity() { return result; } +void mult_4x4(Matrix4x4 *multiplicant, Matrix4x4 *multiplier) { + Matrix4x4 result = identity(); + + for (int i = 0; i < 4; i++) { + for (int j = 0; j < 4; j++) { + result.m[i][j] = 0; + for (int k = 0; k < 4; k++) { + result.m[i][j] += multiplicant->m[i][k] * multiplier->m[k][j]; + } + } + } + + *multiplicant = result; +} + +// TODO: rotate around the center of the element +// currently we rotate around the 0, 0 point of the element +void apply_rotation(Matrix4x4 *model, float angle) { + Matrix4x4 ident = identity(); + + ident.m[0][0] = cos(angle); + ident.m[0][1] = sin(angle); + ident.m[1][0] = -sin(angle); + ident.m[1][1] = cos(angle); + + mult_4x4(model, &ident); +} + Matrix4x4 orthographic_projection_2d(float left, float right, float bottom, float top) { Matrix4x4 result = identity(); @@ -41,3 +71,7 @@ float clampf(float value, float min, float max) { return value; } + +float to_radians(float degrees) { + return degrees * (M_PI / 180.f); +} diff --git a/helper.h b/helper.h index 0545522..60d72c6 100644 --- a/helper.h +++ b/helper.h @@ -5,7 +5,10 @@ typedef struct { } Matrix4x4; Matrix4x4 identity(); +void mult_4x4(Matrix4x4 *multiplicant, Matrix4x4 *multiplier); +void apply_rotation(Matrix4x4 *model, float angle); Matrix4x4 orthographic_projection_2d(float left, float right, float bottom, float top); int max(int a, int b); float maxf(float a, float b); float clampf(float value, float min, float max); +float to_radians(float degrees); diff --git a/todo.txt b/todo.txt index c2b05ea..2b9c233 100644 --- a/todo.txt +++ b/todo.txt @@ -1,4 +1,6 @@ -- draw functions for other primitive shapes (circle, triangle?) +- rotations/scaling for text (fix the code for it) + +- maybe force the window to be a specific resolution - border decorations for shapes - look into SDF/vector text - unicode support? diff --git a/ui.c b/ui.c index 8c84cca..3655384 100644 --- a/ui.c +++ b/ui.c @@ -369,16 +369,10 @@ void draw_quad(UIConstraints constraints, Color *color, float border_radius, Ali set_vec4f(ui_shader, "aColor", 0.f, 0.f, 0.f, 1.f); } - printf("width: %f, height: %f\n", constraints.width, constraints.height); set_float(ui_shader, "uiWidth", constraints.width); set_float(ui_shader, "uiHeight", constraints.height); set_float(ui_shader, "borderRadius", border_radius); set_mat4f(ui_shader, "projection", (float *)zephr_context.projection.m); - Matrix4x4 model = identity(); - - set_mat4f(font_shader, "model", (float *)model.m); - - glBindVertexArray(ui_vao); Vec2f pos = { 0.f, 0.f }; Sizef size = { 0.f, 0.f }; @@ -386,16 +380,25 @@ void draw_quad(UIConstraints constraints, Color *color, float border_radius, Ali apply_constraints(constraints, &pos, &size); apply_alignment(align, &pos, size); + Matrix4x4 model = identity(); + apply_rotation(&model, to_radians(0)); + model.m[3][0] = pos.x; + model.m[3][1] = pos.y; + + set_mat4f(font_shader, "model", (float *)model.m); + + glBindVertexArray(ui_vao); + float vertices[6][4] = { // bottom left tri - {pos.x, pos.y + size.height, 0.0, 1.0}, - {pos.x, pos.y, 0.0, 0.0}, - {pos.x + size.width, pos.y, 1.0, 0.0}, + {0, 0 + size.height, 0.0, 1.0}, + {0, 0, 0.0, 0.0}, + {0 + size.width, 0, 1.0, 0.0}, // top right tri - {pos.x, pos.y + size.height, 0.0, 1.0}, - {pos.x + size.width, pos.y, 1.0, 0.0}, - {pos.x + size.width, pos.y + size.height, 1.0, 1.0}, + {0, 0 + size.height, 0.0, 1.0}, + {0 + size.width, 0, 1.0, 0.0}, + {0 + size.width, 0 + size.height, 1.0, 1.0}, }; glBindBuffer(GL_ARRAY_BUFFER, ui_vbo); @@ -407,6 +410,57 @@ void draw_quad(UIConstraints constraints, Color *color, float border_radius, Ali glBindVertexArray(0); } +void draw_circle(UIConstraints constraints, Color *color, Alignment align) { + float radius = 0.f; + + if (constraints.width > constraints.height) { + radius = constraints.height / 2.f; + } else { + radius = constraints.width / 2.f; + } + draw_quad(constraints, color, radius, align); +} + +void draw_triangle(UIConstraints constraints, Color *color, Alignment align) { + use_shader(ui_shader); + + if (color) { + set_vec4f(ui_shader, "aColor", color->r / 255.f, color->g / 255.f, color->b / 255.f, color->a / 255.f); + } else { + set_vec4f(ui_shader, "aColor", 0.f, 0.f, 0.f, 1.f); + } + set_mat4f(ui_shader, "projection", (float *)zephr_context.projection.m); + + Vec2f pos = { 0.f, 0.f }; + Sizef size = { 0.f, 0.f }; + + apply_constraints(constraints, &pos, &size); + apply_alignment(align, &pos, size); + + Matrix4x4 model = identity(); + apply_rotation(&model, to_radians(0)); + model.m[3][0] = pos.x; + model.m[3][1] = pos.y; + + set_mat4f(font_shader, "model", (float *)model.m); + + glBindVertexArray(ui_vao); + + float vertices[3][4] = { + {0 + size.width, 0, 0.0, 0.0}, + {0, 0, 0.0, 0.0}, + {0 + size.width / 2, 0 + size.height, 0.0, 0.0}, + }; + + glBindBuffer(GL_ARRAY_BUFFER, ui_vbo); + glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices); + glBindBuffer(GL_ARRAY_BUFFER, 0); + + glDrawArrays(GL_TRIANGLES, 0, 3); + + glBindVertexArray(0); +} + void draw_text(const char* text, int font_size, Vec2f pos, Color *color, Alignment alignment) { use_shader(font_shader); if (color) { @@ -421,9 +475,6 @@ void draw_text(const char* text, int font_size, Vec2f pos, Color *color, Alignme set_mat4f(font_shader, "model", (float *)model.m); - glActiveTexture(GL_TEXTURE0); - glBindVertexArray(font_vao); - Sizef text_size = calculate_text_size(text, FONT_PIXEL_SIZE); float font_scale = (float)font_size / FONT_PIXEL_SIZE; @@ -435,6 +486,9 @@ void draw_text(const char* text, int font_size, Vec2f pos, Color *color, Alignme float first_char_bearing_w = characters[(int)text[0]].bearing.width; + glActiveTexture(GL_TEXTURE0); + glBindVertexArray(font_vao); + int c = 0; int x = 0; while (text[c] != '\0') { diff --git a/ui.h b/ui.h index a3e6eea..6b505fd 100644 --- a/ui.h +++ b/ui.h @@ -82,5 +82,7 @@ void set_y_constraint(UIConstraints *constraints, float value, UIConstraint type void set_width_constraint(UIConstraints *constraints, float value, UIConstraint type); void set_height_constraint(UIConstraints *constraints, float value, UIConstraint type); void draw_quad(UIConstraints constraints, Color *color, float border_radius, Alignment align); +void draw_circle(UIConstraints constraints, Color *color, Alignment align); +void draw_triangle(UIConstraints constraints, Color *color, Alignment align); void draw_text(const char* text, int font_size, Vec2f pos, Color *color, Alignment align); Sizef calculate_text_size(const char *text, int font_size);