Selaa lähdekoodia

Updated Scufeng. Switched to using mouse button callback in MainMenu.

Aaro Saila 1 viikko sitten
vanhempi
sitoutus
a3c6925e2d

+ 32 - 17
src/game/MainMenu.c

@@ -2,6 +2,7 @@
 
 #include "scufeng/engine.h"
 #include "scufeng/Mesh.h"
+#include "scufeng/keys.h"
 #include "scufeng/log.h"
 #include "cglm/vec3.h"
 #include "cglm/io.h"
@@ -15,7 +16,6 @@
         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 });
@@ -123,6 +123,9 @@ SEResult MainMenu_alloc(
     const unsigned int screen_w,
     const unsigned int screen_h
 ) {
+    mm->next_screen = PONG_MAIN_MENU;
+    mm->login_render = false;
+
     SEResult res = SEMesh_string_alloc(&mm->title_text, "Pong", strlen("Pong"), GLM_VEC3_ONE);
     SE_RESULT_CHECK(res);
     SEMesh_rect_textured_scale_set(&mm->title_text, screen_w * 0.16f, 0.0f);
@@ -159,35 +162,44 @@ SEResult MainMenu_alloc(
     return res;
 }
 
-static Screen MainMenu_input_handle(MainMenu *const mm) {
-    if (se_get_mouse_button(SE_MOUSE_BUTTON_LEFT) == SE_KEY_ACTION_PRESS) {
+static void MainMenu_mouse_button_callback(SEMouseButton button, SEKeyAction action, SEKeyMod mods, void *data) {
+    (void) mods;
+
+    if (button == SE_MOUSE_BUTTON_LEFT && action == SE_KEY_ACTION_PRESS) {
+        MainMenu *const mm = (MainMenu *) data;
+
         double x, y;
         se_get_mouse_pos(&x, &y);
 
-        if (!login_render) {
+        if (!mm->login_render) {
             if (SEMesh_rect_is_pos_inside(&mm->sp_text, x, y)) {
                 SE_LOG("SP");
-                return PONG_SINGLEPLAYER;
+                mm->next_screen = PONG_SINGLEPLAYER;
+                return;
             }
 
             if (SEMesh_rect_is_pos_inside(&mm->mp_text, x, y)) {
                 SE_LOG("MP");
-                return PONG_MULTIPLAYER;
+                mm->next_screen = PONG_MULTIPLAYER;
+                return;
             }
 
             if (SEMesh_rect_is_pos_inside(&mm->quit_text, x, y)) {
                 SE_LOG("Quit");
                 se_window_close();
+                return;
             }
 
             if (SEMesh_rect_is_pos_inside(&mm->login_text, x, y)) {
-                login_render = true;
-                return PONG_MAIN_MENU;
+                mm->login_render = true;
+                mm->next_screen = PONG_MAIN_MENU;
+                return;
             }
         } else {
             if (SEMesh_rect_is_pos_inside(&mm->login_prompt.exit_text, x, y)) {
-                login_render = false;
-                return PONG_MAIN_MENU;
+                mm->login_render = false;
+                mm->next_screen = PONG_MAIN_MENU;
+                return;
             }
 
             SETextInput *const tis[] = {
@@ -203,15 +215,18 @@ static Screen MainMenu_input_handle(MainMenu *const mm) {
             }
         }
     }
+}
 
-    return PONG_MAIN_MENU;
+void MainMenu_callbacks_set(MainMenu *const mm) {
+    se_set_mouse_button_callback(MainMenu_mouse_button_callback, (void *) mm);
+}
+
+void MainMenu_callbacks_unset(MainMenu *const mm) {
+    se_set_mouse_button_callback(nullptr, nullptr);
+    mm->next_screen = PONG_MAIN_MENU;
 }
 
 Screen MainMenu_tick(MainMenu *const mm) {
-    const Screen screen = MainMenu_input_handle(mm);
-    if (screen != PONG_MAIN_MENU) {
-        return screen;
-    }
     SEMesh *meshes[] = {
         &mm->title_text,
         &mm->sp_text,
@@ -224,9 +239,9 @@ Screen MainMenu_tick(MainMenu *const mm) {
         SEMesh_bbox_render_2d(meshes[i]);
     }
 
-    if (login_render) {
+    if (mm->login_render) {
         LoginPrompt_render(&mm->login_prompt);
     }
 
-    return PONG_MAIN_MENU;
+    return mm->next_screen; // Possiby modified by callback
 }

+ 4 - 0
src/game/MainMenu.h

@@ -22,6 +22,8 @@ typedef struct {
     SEMesh quit_text;
     SEMesh login_text;
     LoginPrompt login_prompt;
+    bool login_render;
+    Screen next_screen;
 } MainMenu;
 
 /**
@@ -34,4 +36,6 @@ SEResult MainMenu_alloc(
     const unsigned int screen_h
 );
 
+void MainMenu_callbacks_set(MainMenu *const mm);
+void MainMenu_callbacks_unset(MainMenu *const mm);
 Screen MainMenu_tick(MainMenu *const mm);

+ 5 - 0
src/game/pong.c

@@ -29,6 +29,7 @@ int main() {
     if (res != SE_SUCCESS) {
         SE_LOG_FATALF("Failed to allocate MainMenu: %s", SEResult_to_str(res));
     }
+    MainMenu_callbacks_set(&mm);
     SinglePlayer sp;
     MultiPlayer mp;
 
@@ -57,12 +58,14 @@ int main() {
                     SE_LOG_ERRORF("Failed to alloc SinglePlayer: %d", SEResult_to_str(res));
                     current_screen = PONG_MAIN_MENU;
                 }
+                MainMenu_callbacks_unset(&mm);
                 break;
             case PONG_MULTIPLAYER:
             {
                 if (!MultiPlayer_alloc(&mp, screen_w, screen_h, "127.0.0.1", 8080)) {
                     current_screen = PONG_MAIN_MENU;
                 }
+                MainMenu_callbacks_unset(&mm);
                 break;
             }
             default:
@@ -73,6 +76,7 @@ int main() {
             current_screen = SinglePlayer_tick(&sp, delta);
             if (current_screen == PONG_MAIN_MENU) {
                 SinglePlayer_free(&sp);
+                MainMenu_callbacks_set(&mm);
             }
             break;
 
@@ -80,6 +84,7 @@ int main() {
             current_screen = MultiPlayer_tick(&mp, delta);
             if (current_screen == PONG_MAIN_MENU) {
                 MultiPlayer_free(&mp);
+                MainMenu_callbacks_set(&mm);
             }
             break;
         }

+ 1 - 1
subprojects/scufeng-x86_64/GIT_COMMIT

@@ -1 +1 @@
-81e390a835f3ef0f7b6139a0de0d5be043e2cbe5
+d7b098ee4e78c5ca71f843ff794d6d9d064aa493

+ 4 - 2
subprojects/scufeng-x86_64/include/scufeng/engine.h

@@ -37,8 +37,9 @@ typedef enum {
     SE_DOWN
 } SEDirection;
 
-typedef void (*SEKeyCb)(SEKey key, SEKeyAction action, SEKeyMod mods);
+typedef void (*SEKeyCb)(SEKey key, SEKeyAction action, SEKeyMod mods, void *data);
 typedef void (*SECharCb)(unsigned int codepoint);
+typedef void (*SEMouseButtonCb)(SEMouseButton button, SEKeyAction action, SEKeyMod mods, void *data);
 
 void se_init(const bool debug, FILE *log_out, FILE *log_err_out);
 void se_deinit(void);
@@ -62,7 +63,8 @@ void se_handle_events(void);
 double se_get_time(void);
 double se_delta_time(const double current_time);
 void se_set_polygon_mode(const SEPolygonMode mode);
-void se_set_key_callback(SEKeyCb cb);
+void se_set_key_callback(SEKeyCb cb, void *data);
+void se_set_mouse_button_callback(SEMouseButtonCb cb, void *data);
 SEKeyAction se_get_key(const SEKey key);
 SEKeyAction se_get_mouse_button(const SEMouseButton btn);
 void se_get_mouse_pos(double *x, double *y);

BIN
subprojects/scufeng-x86_64/libscufeng.a