Aaro Saila 1 週間 前
親
コミット
2afeb75979

+ 108 - 51
src/game/MainMenu.c

@@ -10,11 +10,13 @@
 #include "game_common.h"
 #include "scufeng/text.h"
 
-#define SE_RESULT_CHECK(res) \
-    if (res != SE_SUCCESS) { \
-        return res;          \
+#define SE_RESULT_CHECK(res)   \
+    if ((res) != SE_SUCCESS) { \
+        return (res);          \
     }
 
+static bool login_render = false;
+
 static SEResult LoginPrompt_alloc(LoginPrompt *const lp, const unsigned int screen_w, const unsigned int screen_h) {
     lp->bg = SEMesh_rect_color_alloc((vec4) { 0.05f, 0.05f, 0.05f, 1.0f });
     vec2 bg_dims = { screen_w * 0.75f, screen_h * 0.75f };
@@ -26,49 +28,75 @@ static SEResult LoginPrompt_alloc(LoginPrompt *const lp, const unsigned int scre
     vec4 bg_bbox;
     SEMesh_bbox_get_2d_v(&lp->bg, bg_bbox);
 
-    SEResult res = SEMesh_string_alloc(&lp->username_text, "Username:", strlen("Username:"), GLM_VEC3_ONE);
+    SEResult res = SEMesh_string_alloc(&lp->exit_text, "X", 1, GLM_VEC3_ONE);
     SE_RESULT_CHECK(res);
+    SEMesh_rect_textured_scale_set(&lp->exit_text, bg_dims[0] * 0.05f, 0.0f);
+    lp->exit_text.transform.pos[0] = bg_bbox[0] + lp->exit_text.transform.scale[0] / 2.0f;
+    lp->exit_text.transform.pos[1] = bg_bbox[1] + lp->exit_text.transform.scale[1] / 2.0f;
+    glm_vec3_print(lp->exit_text.transform.pos, stdout);
 
-    lp->username_input = SETextInput_alloc(
-        "",
-        0,
-        0,
-        0,
-        GLM_VEC3_ONE,
-        (vec4) { 0.0025f, 0.0025f, 0.0025f, 1.0f },
-        TI_FIXED_WIDTH
-    ),
-    SETextInput_fixed_width_set(&lp->username_input, bg_dims[0] * 0.25f);
+    const float label_w = bg_dims[0] * 0.25f;
+
+    res = SEMesh_string_alloc(&lp->login_button, "Login", strlen("Login"), GLM_VEC3_ONE);
+    SE_RESULT_CHECK(res);
+    SEMesh_rect_textured_scale_set(&lp->login_button, label_w, 0.0f);
+    lp->login_button.transform.pos[0] = bg_pos[0];
+    lp->login_button.transform.pos[1] = bg_bbox[3] - lp->login_button.transform.scale[1];
+
+    res = SEMesh_string_alloc(&lp->username_text, "Username:", strlen("Username:"), GLM_VEC3_ONE);
+    SE_RESULT_CHECK(res);
+    SEMesh_rect_textured_scale_set(&lp->username_text, label_w, 0.0f);
+    lp->username_text.transform.pos[0] = bg_bbox[0] + lp->username_text.transform.scale[0] / 2.0f;
+    lp->username_text.transform.pos[1] = bg_bbox[1] + bg_dims[1] / 3.0f;
+    float name_right, name_bottom;
+    SEMesh_bbox_get_2d(&lp->username_text, nullptr, nullptr, &name_right, &name_bottom);
 
     res = SEMesh_string_alloc(&lp->password_text, "Password:", strlen("Password:"), GLM_VEC3_ONE);
     SE_RESULT_CHECK(res);
+    SEMesh_rect_textured_scale_set(&lp->password_text, label_w, 0.0f);
+    lp->password_text.transform.pos[0] = bg_bbox[0] + lp->password_text.transform.scale[0] / 2.0f;
+    lp->password_text.transform.pos[1] = name_bottom + lp->password_text.transform.scale[1];
+    float pw_right;
+    SEMesh_bbox_get_2d(&lp->password_text, nullptr, nullptr, &pw_right, nullptr);
 
-    lp->password_input = SETextInput_alloc(
-        "",
-        0,
-        0,
-        0,
+    lp->username_input = SETextInput_alloc(
+        "username",
+        strlen("username"),
         GLM_VEC3_ONE,
-        (vec4) { 0.0025f, 0.0025f, 0.0025f, 1.0f },
-        TI_FIXED_WIDTH
+        (vec4) { 0.0025f, 0.0025f, 0.0025f, 1.0f }
     );
-    SETextInput_fixed_width_set(&lp->password_input, bg_dims[0] * 0.25f);
+    SETextInput_scale_set(&lp->username_input,
+        (vec2) {
+            lp->bg.transform.scale[0] - lp->username_text.transform.scale[0],
+            lp->username_text.transform.scale[1],
+        });
+    SETextInput_pos_set(&lp->username_input,
+        (vec2) {
+            name_right + SETextInput_scale(lp->username_input)[0] / 2.0f,
+            lp->username_text.transform.pos[1],
+        });
 
-    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);
-
-    res = SEMesh_string_alloc(&lp->exit_text, "X", 1, GLM_VEC3_ONE);
-    SE_RESULT_CHECK(res);
-    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);
+    lp->password_input = SETextInput_alloc(
+        "password",
+        strlen("password"),
+        GLM_VEC3_ONE,
+        (vec4) { 0.0025f, 0.0025f, 0.0025f, 1.0f }
+    );
+    SETextInput_scale_set(&lp->password_input, SETextInput_scale(lp->username_input));
+    SETextInput_pos_set(
+        &lp->password_input,
+        (vec2) {
+            pw_right + SETextInput_scale(lp->password_input)[0] / 2.0f,
+            lp->password_text.transform.pos[1],
+        }
+    );
 
-    res = SEMesh_string_alloc(&lp->login_text, "Login", strlen("Login"), GLM_VEC3_ONE);
-    SE_RESULT_CHECK(res);
+    // 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);
 
     return SE_SUCCESS;
 }
@@ -79,7 +107,7 @@ static void LoginPrompt_render(LoginPrompt *const lp) {
         &lp->username_text,
         &lp->password_text,
         &lp->exit_text,
-        &lp->login_text
+        &lp->login_button,
     };
     SETextInput *const tis[] = {
         &lp->username_input,
@@ -136,19 +164,43 @@ static Screen MainMenu_input_handle(MainMenu *const mm) {
         double x, y;
         se_get_mouse_pos(&x, &y);
 
-        if (SEMesh_rect_is_pos_inside(&mm->sp_text, x, y)) {
-            SE_LOG("SP");
-            return PONG_SINGLEPLAYER;
-        }
-
-        if (SEMesh_rect_is_pos_inside(&mm->mp_text, x, y)) {
-            SE_LOG("MP");
-            return PONG_MULTIPLAYER;
-        }
-
-        if (SEMesh_rect_is_pos_inside(&mm->quit_text, x, y)) {
-            SE_LOG("Quit");
-            se_window_close();
+        if (!login_render) {
+            if (SEMesh_rect_is_pos_inside(&mm->sp_text, x, y)) {
+                SE_LOG("SP");
+                return PONG_SINGLEPLAYER;
+            }
+
+            if (SEMesh_rect_is_pos_inside(&mm->mp_text, x, y)) {
+                SE_LOG("MP");
+                return PONG_MULTIPLAYER;
+            }
+
+            if (SEMesh_rect_is_pos_inside(&mm->quit_text, x, y)) {
+                SE_LOG("Quit");
+                se_window_close();
+            }
+
+            if (SEMesh_rect_is_pos_inside(&mm->login_text, x, y)) {
+                login_render = true;
+                return PONG_MAIN_MENU;
+            }
+        } else {
+            if (SEMesh_rect_is_pos_inside(&mm->login_prompt.exit_text, x, y)) {
+                login_render = false;
+                return PONG_MAIN_MENU;
+            }
+
+            SETextInput *const tis[] = {
+                &mm->login_prompt.username_input,
+                &mm->login_prompt.password_input,
+            };
+            SETextInput_deactivate();
+            for (size_t i = 0; i < sizeof(tis) / sizeof(tis[0]); i++) {
+                if (SETextInput_is_pos_inside(tis[i], x, y)) {
+                    SETextInput_activate(tis[i]);
+                    break;
+                }
+            }
         }
     }
 
@@ -168,8 +220,13 @@ Screen MainMenu_tick(MainMenu *const mm) {
         &mm->login_text
     };
     SEMesh_render_multiple_2d(meshes, sizeof(meshes) / sizeof(meshes[0]));
+    for (size_t i = 0; i < sizeof(meshes) / sizeof(meshes[0]); i++) {
+        SEMesh_bbox_render_2d(meshes[i]);
+    }
 
-    // LoginPrompt_render(&mm->login_prompt);
+    if (login_render) {
+        LoginPrompt_render(&mm->login_prompt);
+    }
 
     return PONG_MAIN_MENU;
 }

+ 1 - 1
src/game/MainMenu.h

@@ -12,7 +12,7 @@ typedef struct {
     SEMesh password_text;
     SETextInput password_input;
     SEMesh exit_text;
-    SEMesh login_text;
+    SEMesh login_button;
 } LoginPrompt;
 
 typedef struct {

+ 1 - 1
src/game/pong.c

@@ -10,7 +10,7 @@
 #include "MultiPlayer.h"
 #include "src/game/game_common.h"
 
-static const unsigned int screen_w = 800;
+static const unsigned int screen_w = 1200;
 static const unsigned int screen_h = 600;
 
 int main() {

+ 1 - 1
subprojects/scufeng-x86_64/GIT_COMMIT

@@ -1 +1 @@
-bd5ecd3d8293b93bae72e5e6d29490585bb6f960
+81e390a835f3ef0f7b6139a0de0d5be043e2cbe5

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

@@ -9,6 +9,9 @@
 #include "scufeng/Mesh.h"
 #include "scufcont/DString.h"
 
+#define SETextInput_pos(ti) (ti.bg_mesh.transform.pos)
+#define SETextInput_scale(ti) (ti.bg_mesh.transform.scale)
+
 typedef struct {
     unsigned int textures[128];
     ivec2 sizes[128];
@@ -19,22 +22,13 @@ typedef struct {
     int bearing_y_max;
 } SEFont;
 
-typedef enum : uint8_t {
-    TI_FIXED_WIDTH = 1,
-} SETextInputOptions;
-
 typedef struct {
-    SETextInputOptions opts;
     bool active;
     size_t cursor;
     SEMesh cursor_mesh;
     SEMesh bg_mesh;
     SCDString text;
-    int x;
-    int y;
-    float scale;
     vec3 text_color;
-    float fixed_width;
 } SETextInput;
 
 SEResult se_font_load(
@@ -46,33 +40,47 @@ SEResult se_font_load(
 void se_font_set(const size_t font_id);
 SEFont *se_font_current_get();
 size_t se_font_current_get_id();
+
+/**
+  @param x Left edge of text.
+  @param y Bottom of text. For example, the bottom edge of a '_' character is at y.
+  @param h Height scale. Values above font.size_y_max scale up, values below it scale down. Set to 0 or font.size_y_max for no scaling.
+*/
 int se_text_render(
     const char *text,
-    int x,
-    int y,
-    float scale,
+    unsigned int x,
+    unsigned int y,
+    unsigned int h,
     const vec3 color
 );
 
 SETextInput SETextInput_alloc(
     const char *text,
     const size_t text_len,
-    const int x,
-    const int y,
     vec3 color,
-    vec4 bg_color,
-    const SETextInputOptions opts
+    vec4 bg_color
 );
 void SETextInput_free(SETextInput *const ti);
+void SETextInput_scale_set(SETextInput *const ti, const vec2 dims);
+void SETextInput_pos_set(SETextInput *const ti, const vec2 pos);
+void SETextInput_color_set(SETextInput *const ti, const vec3 color);
+void SETextInput_bg_color_set(SETextInput *const ti, const vec4 color);
+SCDString *SETextInput_string_get_ref(SETextInput *const ti);
 void SETextInput_deactivate();
 void SETextInput_activate(SETextInput *const ti);
-SCDString *SETextInput_string_get_ref(SETextInput *const ti);
+void SETextInput_bbox_get(
+    const SETextInput *const ti,
+    float *const left,
+    float *const top,
+    float *const right,
+    float *const bottom
+);
+void SETextInput_bbox_get_v(const SETextInput *const ti, vec4 bbox);
 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);
-void SETextInput_fixed_width_set(SETextInput *const ti, const float w);
 void SETextInput_render_2d(SETextInput *const ti);
 void SETextInput_render_multiple_2d(SETextInput *const *const tis, const size_t ti_count);
 

BIN
subprojects/scufeng-x86_64/libscufeng.a