1
0
Aaro Saila 2 týždňov pred
rodič
commit
93b9df8a1a

+ 93 - 6
src/game/MainMenu.c

@@ -2,29 +2,113 @@
 #include "scufeng/Mesh.h"
 #include "scufeng/log.h"
 #include "cglm/vec3.h"
+#include "cglm/call/vec3.h"
+#include "cglm/io.h"
 
 #include "MainMenu.h"
 #include "game_common.h"
+#include "scufeng/text.h"
+
+static LoginPrompt LoginPrompt_alloc(const unsigned int screen_w, const unsigned int screen_h) {
+    LoginPrompt lp = {
+        .bg = SEMesh_rect_color_alloc((vec4) { 0.05f, 0.05f, 0.05f, 1.0f }),
+        .username_text = SEMesh_string_alloc("Username:", GLM_VEC3_ONE),
+        .username_input = SETextInput_alloc(
+            "",
+            0,
+            0,
+            0,
+            GLM_VEC3_ONE,
+            (vec4) { 0.0025f, 0.0025f, 0.0025f, 1.0f },
+            TI_FIXED_WIDTH
+        ),
+        .password_text = SEMesh_string_alloc("Password:", GLM_VEC3_ONE),
+        .password_input = SETextInput_alloc(
+            "",
+            0,
+            0,
+            0,
+            GLM_VEC3_ONE,
+            (vec4) { 0.0025f, 0.0025f, 0.0025f, 1.0f },
+            TI_FIXED_WIDTH
+        ),
+        .exit_text = SEMesh_string_alloc("X", GLM_VEC3_ONE),
+        .login_text = SEMesh_string_alloc("Login", GLM_VEC3_ONE)
+    };
+
+    vec2 bg_dims = { screen_w * 0.75f, screen_h * 0.75f };
+    vec2 bg_pos = { screen_w * 0.5f, screen_h * 0.5f };
+    lp.bg.transform.scale[0] = bg_dims[0];
+    lp.bg.transform.scale[1] = bg_dims[1];
+    lp.bg.transform.pos[0] = bg_pos[0];
+    lp.bg.transform.pos[1] = bg_pos[1];
+    vec4 bg_bbox;
+    SEMesh_bbox_get_2d_v(&lp.bg, bg_bbox);
+
+    puts("bg_dims");
+    glm_vec2_print(bg_dims, stdout);
+    puts("bg_pos");
+    glm_vec2_print(bg_pos, stdout);
+    puts("bg_bbox");
+    glm_vec4_print(bg_bbox, stdout);
+
+    lp.exit_text.transform.pos[0] = bg_bbox[0] + bg_dims[0] * 0.025f; 
+    lp.exit_text.transform.pos[1] = bg_bbox[1] + bg_dims[1] * 0.05f;
+    glm_vec3_print(lp.exit_text.transform.pos, stdout);
+
+    SETextInput_fixed_width_set(&lp.username_input, bg_dims[0] * 0.25f);
+    SETextInput_fixed_width_set(&lp.password_input, bg_dims[0] * 0.25f);
+
+    return lp;
+}
+
+static void LoginPrompt_render(LoginPrompt *const lp) {
+    SEMesh *const meshes[] = {
+        &lp->bg,
+        &lp->username_text,
+        &lp->password_text,
+        &lp->exit_text,
+        &lp->login_text
+    };
+    SETextInput *const tis[] = {
+        &lp->username_input,
+        &lp->password_input
+    };
+
+    SEMesh_render_multiple_2d(meshes, sizeof(meshes) / sizeof(meshes[0]));
+    SETextInput_render_multiple_2d(tis, sizeof(tis) / sizeof(tis[0]));
+}
 
 MainMenu MainMenu_alloc(const unsigned int screen_w, const unsigned int screen_h) {
     MainMenu mm;
 
-    mm.title_text = SEMesh_string_alloc("Pong", 1.0f, GLM_VEC3_ONE);
+    mm.title_text = SEMesh_string_alloc("Pong", GLM_VEC3_ONE);
+    SEMesh_rect_textured_scale_set(&mm.title_text, screen_w * 0.16f, 0.0f);
     mm.title_text.transform.pos[0] = screen_w / 2.0f;
-    mm.title_text.transform.pos[1] = screen_h * 0.2f;
+    mm.title_text.transform.pos[1] = screen_h * 0.1f;
+
+    mm.login_text = SEMesh_string_alloc("Login", GLM_VEC3_ONE);
+    SEMesh_rect_textured_scale_set(&mm.login_text, screen_w * 0.06f, 0.0f);
+    mm.login_text.transform.pos[0] = screen_w / 6.0f;
+    mm.login_text.transform.pos[1] = screen_h * 0.2f;
 
-    mm.sp_text = SEMesh_string_alloc("Singleplayer", 1.0f, GLM_VEC3_ONE);
+    mm.sp_text = SEMesh_string_alloc("Singleplayer", GLM_VEC3_ONE);
+    SEMesh_rect_textured_scale_set(&mm.sp_text, screen_w * 0.25f, 0.0f);
     mm.sp_text.transform.pos[0] = screen_w * 0.25f;
     mm.sp_text.transform.pos[1] = screen_h * 0.5f;
 
-    mm.mp_text = SEMesh_string_alloc("Multiplayer", 1.0f, GLM_VEC3_ONE);
+    mm.mp_text = SEMesh_string_alloc("Multiplayer", GLM_VEC3_ONE);
+    SEMesh_rect_textured_scale_set(&mm.mp_text, mm.sp_text.transform.scale[0], 0.0f);
     mm.mp_text.transform.pos[0] = screen_w - mm.sp_text.transform.pos[0];
     mm.mp_text.transform.pos[1] = mm.sp_text.transform.pos[1];
 
-    mm.quit_text = SEMesh_string_alloc("Quit", 1.0f, GLM_VEC3_ONE);
+    mm.quit_text = SEMesh_string_alloc("Quit", GLM_VEC3_ONE);
+    SEMesh_rect_textured_scale_set(&mm.quit_text, 0.0f, mm.title_text.transform.scale[1] / 2.0f);
     mm.quit_text.transform.pos[0] = screen_w / 2.0f;
     mm.quit_text.transform.pos[1] = screen_h * 0.8f;
 
+    mm.login_prompt = LoginPrompt_alloc(screen_w, screen_h);
+
     return mm;
 }
 
@@ -61,9 +145,12 @@ Screen MainMenu_tick(MainMenu *const mm) {
         &mm->title_text,
         &mm->sp_text,
         &mm->mp_text,
-        &mm->quit_text
+        &mm->quit_text,
+        &mm->login_text
     };
     SEMesh_render_multiple_2d(meshes, sizeof(meshes) / sizeof(meshes[0]));
 
+    // LoginPrompt_render(&mm->login_prompt);
+
     return PONG_MAIN_MENU;
 }

+ 13 - 0
src/game/MainMenu.h

@@ -1,14 +1,27 @@
 #pragma once
 
 #include "scufeng/Mesh.h"
+#include "scufeng/text.h"
 
 #include "game_common.h"
 
+typedef struct {
+    SEMesh bg;
+    SEMesh username_text;
+    SETextInput username_input;
+    SEMesh password_text;
+    SETextInput password_input;
+    SEMesh exit_text;
+    SEMesh login_text;
+} LoginPrompt;
+
 typedef struct {
     SEMesh title_text;
     SEMesh sp_text;
     SEMesh mp_text;
     SEMesh quit_text;
+    SEMesh login_text;
+    LoginPrompt login_prompt;
 } MainMenu;
 
 MainMenu MainMenu_alloc(const unsigned int screen_w, const unsigned int screen_h);

+ 0 - 1
src/game/game_common.c

@@ -112,7 +112,6 @@ void win_lose_render(
     const char *text_str = win ? "You Win!" : "You Lose!";
     SEMesh text = SEMesh_string_alloc(
         text_str,
-        1.0f,
         (vec3) { 1.0f, 1.0f, 1.0f }
     );
     text.transform.pos[0] = screen_w / 2.0f;

+ 2 - 2
src/game/pong.c

@@ -8,7 +8,7 @@
 #include "MultiPlayer.h"
 #include "src/game/game_common.h"
 
-static const unsigned int screen_w = 1200 / 2;
+static const unsigned int screen_w = 1200;
 static const unsigned int screen_h = 600;
 
 int main() {
@@ -18,7 +18,7 @@ int main() {
     se_window_create(screen_w, screen_h, "Pong", -1);
     se_swap_interval(0);
     se_fps_lock_set_fps(60);
-    se_font_load(FONT_PATH, 0, 48);
+    se_font_load(FONT_PATH, 0, 48 * 2, nullptr);
 
     se_clear_color(0.1f, 0.1f, 0.1f, 1.0f);
 

+ 1 - 1
subprojects/scufeng-x86_64/GIT_COMMIT

@@ -1 +1 @@
-16d65894711b730f829790748d3802c4603d4a8c
+fd88568b43605f8593514e2e200f1222cc5197d8

+ 22 - 4
subprojects/scufeng-x86_64/include/scufcont/DArray.h

@@ -31,10 +31,6 @@
     SCDARRAY_EXPANDER(SCDARRAY_CLEAR_NO_RESIZE, type, type_name, clear_no_resize)       \
     SCDARRAY_EXPANDER(SCDARRAY_CLEAR, type, type_name, clear)
 
-#define SCDARRAY_HEADER(type, type_name) \
-    SCDARRAY_STRUCT(type, type_name)     \
-    SCDARRAY_SIGS(type, type_name)
-
 #define SCDARRAY_SIGS(type, type_name)                                                       \
     SCDARRAY_EXPANDER(SCDARRAY_ALLOC_SIG, type, type_name, alloc);                           \
     SCDARRAY_EXPANDER(SCDARRAY_FREE_SIG, type, type_name, free);                             \
@@ -47,6 +43,10 @@
     SCDARRAY_EXPANDER(SCDARRAY_CLEAR_NO_RESIZE_SIG, type, type_name, clear_no_resize);       \
     SCDARRAY_EXPANDER(SCDARRAY_CLEAR_SIG, type, type_name, clear);
 
+#define SCDARRAY_HEADER(type, type_name) \
+    SCDARRAY_STRUCT(type, type_name)     \
+    SCDARRAY_SIGS(type, type_name)
+
 // SCDARRAY_ALLOC_SIG(type, SCDArray_##type_name, SCDArray_##type_name##_alloc);
 // SCDARRAY_PUSH_BACK_SIG(type, SCDArray_##type_name, SCDArray_##type_name##_push_back);
 
@@ -61,6 +61,12 @@
 
 #define SCDARRAY_ALLOC_SIG(type, type_name, func_name) ScufContResult func_name(type_name *const darr)
 
+/**
+  @fn SCDArray_alloc
+
+  @return SCUFCONT_OUT_OF_MEMORY
+  @return SCUFCONT_SUCCESS
+*/
 #define SCDARRAY_ALLOC(type, type_name, func_name)     \
     SCDARRAY_ALLOC_SIG(type, type_name, func_name) {   \
         darr->data = (type *) calloc(1, sizeof(type)); \
@@ -90,6 +96,12 @@
 
 #define SCDARRAY_PUSH_BACK_SIG(type, type_name, func_name) ScufContResult func_name(type_name *const darr, const type *const new_elem)
 
+/**
+  @fn SCDArray_push_back
+
+  @return SCUFCONT_OUT_OF_MEMORY
+  @return SCUFCONT_SUCCESS
+*/
 #define SCDARRAY_PUSH_BACK(type, type_name, func_name)                                     \
     SCDARRAY_PUSH_BACK_SIG(type, type_name, func_name) {                                   \
         if (darr->len + 1 > darr->capacity) {                                              \
@@ -106,6 +118,12 @@
 
 #define SCDARRAY_PUSH_BACK_MULTIPLE_SIG(type, type_name, func_name) ScufContResult func_name(type_name *const darr, const type *const new_elems, const size_t new_elems_n)
 
+/**
+  @fn SCDArray_push_back_multiple
+
+  @return SCUFCONT_OUT_OF_MEMORY
+  @return SCUFCONT_SUCCESS
+*/
 #define SCDARRAY_PUSH_BACK_MULTIPLE(type, type_name, func_name)                                       \
     SCDARRAY_PUSH_BACK_MULTIPLE_SIG(type, type_name, func_name) {                                     \
         if (darr->len + new_elems_n > darr->capacity) {                                               \

+ 67 - 0
subprojects/scufeng-x86_64/include/scufcont/DString.h

@@ -17,11 +17,36 @@ typedef struct {
     size_t capacity;
 } SCDString;
 
+/**
+  @return SCUFCONT_OUT_OF_MEMORY
+  @return SCUFCONT_SUCCESS
+*/
 ScufContResult SCDString_alloc(SCDString *const dstr, const char *str, const size_t len);
+
+/**
+  @param dstr Must not be allocated with any of the alloc functions. This functions allocates it.
+
+  @return SCUFCONT_OUT_OF_MEMORY
+  @return SCUFCONT_SUCCESS
+*/
 ScufContResult SCDString_alloc_copy(SCDString *const dstr, const SCDString *const o);
+
+/**
+  @return SCUFCONT_OUT_OF_MEMORY
+  @return SCUFCONT_SUCCESS
+*/
 ScufContResult SCDString_reserve(SCDString *const dstr, const size_t new_capacity);
+
 void SCDString_free(SCDString *const dstr);
+
+/**
+  @param dst Must have been allocated with one of the allocation functions.
+
+  @return SCUFCONT_OUT_OF_MEMORY
+  @return SCUFCONT_SUCCESS
+*/
 ScufContResult SCDString_copy(const SCDString *const src, SCDString *const dst);
+
 bool SCDString_equal(const SCDString *const s1, const SCDString *s2);
 bool SCDString_equal_c_str(const SCDString *const s1, const char *s2);
 bool SCDString_equal_next_chars_c_str(
@@ -30,12 +55,27 @@ bool SCDString_equal_next_chars_c_str(
     const size_t str_len,
     const size_t s
 );
+
+/**
+  @return SCUFCONT_OUT_OF_MEMORY
+  @return SCUFCONT_SUCCESS
+*/
 ScufContResult SCDString_concat_char(SCDString *const dstr, const char ch);
+
+/**
+  @return SCUFCONT_OUT_OF_MEMORY
+  @return SCUFCONT_SUCCESS
+*/
 ScufContResult SCDString_concat_c_str(
     SCDString *const dstr,
     const char *str,
     const size_t str_len
 );
+
+/**
+  @return SCUFCONT_OUT_OF_MEMORY
+  @return SCUFCONT_SUCCESS
+*/
 ScufContResult SCDString_alloc_concat_c_strs(
     SCDString *const dstr,
     const char *s1,
@@ -43,16 +83,43 @@ ScufContResult SCDString_alloc_concat_c_strs(
     const char *s2,
     const size_t s2_len
 );
+
+/**
+  @return SCUFCONT_OUT_OF_MEMORY
+  @return SCUFCONT_SUCCESS
+*/
 ScufContResult SCDString_split(
     const SCDString *const dstr,
     const char *delim,
     SCDString **const split_arr,
     size_t *const arr_len
 );
+
 void SCDString_split_arr_free(SCDString **const arr, const size_t arr_len);
+
+/**
+  @return SCUFCONT_OUT_OF_BOUNDS
+  @return SCUFCONT_SUCCESS
+*/
 ScufContResult SCDString_char_mod(SCDString *const dstr, const size_t at, const char ch);
+
+/**
+  @return SCUFCONT_OUT_OF_BOUNDS
+  @return SCUFCONT_SUCCESS
+*/
 ScufContResult SCDString_char_remove_no_resize(SCDString *const dstr, const size_t at);
+
+/**
+  @return SCUFCONT_OUT_OF_BOUNDS
+  @return SCUFCONT_SUCCESS
+*/
 ScufContResult SCDString_char_remove(SCDString *const dstr, const size_t at);
+
+/**
+  @return SCUFCONT_OUT_OF_BOUNDS
+  @return SCUFCONT_OUT_OF_MEMORY
+  @return SCUFCONT_SUCCESS
+*/
 ScufContResult SCDString_char_insert(SCDString *const dstr, const size_t at, const char ch);
 
 #endif // SCUFCONT_DSTRING_H_

+ 19 - 6
subprojects/scufeng-x86_64/include/scufeng/Mesh.h

@@ -56,24 +56,37 @@ void SEMesh_set_draw_mode(SEMesh *mesh, const SEDrawMode mode);
 void SEMesh_render(const SEMesh *const mesh);
 void SEMesh_color_set(SEMesh *const mesh, const vec4 color);
 void SEMesh_rect_dims_get(const SEMesh *const mesh, vec2 dims);
+void SEMesh_scale_set_to_texture_size(SEMesh *const mesh);
+
+/**
+  Set x or y to 0 to automatically calculate that value based on the
+  w/h ratio of the rect's texture. Setting both to 0 will result in
+  scale being { 0, 0 }.
+*/
+void SEMesh_rect_textured_scale_set(SEMesh *const mesh, float x, float y);
+
 void SEMesh_line_segment_coords_set(SEMesh *const mesh, const vec2 a, const vec2 b);
 SEMesh SEMesh_line_segment_color_alloc(const vec2 a, const vec2 b, const vec4 color);
 void SEMesh_bbox_render_2d(const SEMesh *const mesh);
 void SEMesh_bbox_get_2d(
     const SEMesh *const mesh,
-    float *left,
-    float *right,
-    float *top,
-    float *bottom
+    float *const left,
+    float *const top,
+    float *const right,
+    float *const bottom
 );
+
+/**
+  @param bbox Returns the bbox in order: { left, top, right, bottom }.
+*/
+void SEMesh_bbox_get_2d_v(const SEMesh *const mesh, vec4 bbox);
+
 bool SEMesh_rect_is_pos_inside(const SEMesh *const mesh, const float x, const float y);
 SEMesh SEMesh_rect_color_alloc(vec4 color);
 SEMesh SEMesh_string_alloc(
     const char *text,
-    const float scale,
     const vec3 color
 );
 void SEMesh_render_multiple_2d(SEMesh *const *const meshes, const size_t meshes_n);
-// void SEShape_transform_apply(SEShape *const shape);
 
 #endif // SCUFENG_SHAPE_H_

+ 2 - 0
subprojects/scufeng-x86_64/include/scufeng/scufcont_impl.h

@@ -3,7 +3,9 @@
 
 #include "scufcont/DArray.h"
 #include "scufeng/Mesh.h"
+#include "scufeng/text.h"
 
 SCDARRAY_HEADER(SETransform*, SEShapep)
+SCDARRAY_HEADER(SEFont, SEFont)
 
 #endif // SE_SCUFCONT_IMPL_H_

+ 18 - 1
subprojects/scufeng-x86_64/include/scufeng/text.h

@@ -9,6 +9,16 @@
 #include "scufeng/Mesh.h"
 #include "scufcont/DString.h"
 
+typedef struct {
+    unsigned int textures[128];
+    ivec2 sizes[128];
+    ivec2 bearings[128];
+    unsigned int advances[128];
+    int size_x_max;
+    int size_y_max;
+    int bearing_y_max;
+} SEFont;
+
 typedef enum : uint8_t {
     TI_FIXED_WIDTH = 1,
 } SETextInputOptions;
@@ -30,8 +40,12 @@ typedef struct {
 SEResult se_font_load(
     const char *path,
     const unsigned int pixel_w,
-    const unsigned int pixel_h
+    const unsigned int pixel_h,
+    size_t *const font_id
 );
+void se_font_set(const size_t font_id);
+SEFont *se_font_current_get();
+size_t se_font_current_get_id();
 int se_text_render(
     const char *text,
     int x,
@@ -52,6 +66,9 @@ SETextInput SETextInput_alloc(
 void SETextInput_free(SETextInput *const ti);
 void SETextInput_deactivate();
 void SETextInput_activate(SETextInput *const ti);
+SCDString *SETextInput_string_get_ref(SETextInput *const ti);
+SEResult SETextInput_text_copy(const SETextInput *const ti, SCDString *const dst);
+const char *SETextInput_c_str(const SETextInput *const ti);
 SEResult SETextInput_char_insert_at_cursor(SETextInput *const ti, const char ch);
 void SETextInput_char_remove_at_cursor(SETextInput *const ti);
 bool SETextInput_is_pos_inside(const SETextInput *const ti, const float x, const float y);

BIN
subprojects/scufeng-x86_64/libscufeng.a