Forráskód Böngészése

Updated Scufeng. Started working on logging in.

Aaro Saila 5 napja
szülő
commit
a9e250bb5c

+ 58 - 1
src/game/MainMenu.c

@@ -1,9 +1,11 @@
 #include <string.h>
 
+#include "scufcont/scufcont.h"
 #include "scufeng/engine.h"
 #include "scufeng/Mesh.h"
 #include "scufeng/keys.h"
 #include "scufeng/log.h"
+#include "scufeng/http.h"
 #include "cglm/vec3.h"
 #include "cglm/io.h"
 
@@ -16,7 +18,6 @@
         return (res);          \
     }
 
-
 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 };
@@ -162,6 +163,51 @@ SEResult MainMenu_alloc(
     return res;
 }
 
+static void login(const char *username, const char *password) {
+#define SC_CHECK(res)                                              \
+    if ((res) != SCUFCONT_SUCCESS) {                               \
+        SE_LOG_FATALF("[login] %s", ScufContResult_to_str((res))); \
+    }
+
+    const size_t host_len = strlen(auth_server_host);
+    SCDString login_dto;
+    ScufContResult sc_res = SCDString_alloc_concat_c_strs(
+        &login_dto,
+        "{\"username\": \"",
+        strlen("{\"username\": \""),
+        username,
+        strlen(username)
+    );
+    SC_CHECK(sc_res);
+
+    sc_res = SCDString_concat_c_str(
+        &login_dto,
+        "\", \"password\": \"",
+        strlen("\", \"password\": \"")
+    );
+    SC_CHECK(sc_res);
+
+    sc_res = SCDString_concat_c_str(
+        &login_dto,
+        password,
+        strlen(password)
+    );
+    SC_CHECK(sc_res);
+
+    sc_res = SCDString_concat_c_str(
+        &login_dto,
+        "\"}",
+        strlen("\"}")
+    );
+    SC_CHECK(sc_res);
+
+    se_http_request_send();
+
+    SCDString_free(&login_dto);
+
+#undef SC_CHECK
+}
+
 static void MainMenu_mouse_button_callback(SEMouseButton button, SEKeyAction action, SEKeyMod mods, void *data) {
     (void) mods;
 
@@ -202,6 +248,17 @@ static void MainMenu_mouse_button_callback(SEMouseButton button, SEKeyAction act
                 return;
             }
 
+            if (SEMesh_rect_is_pos_inside(&mm->login_prompt.login_button, x, y)) {
+                mm->login_render = false;
+                mm->next_screen = PONG_MAIN_MENU;
+                ;
+                login(
+                    SETextInput_c_str(&mm->login_prompt.username_input),
+                    SETextInput_c_str(&mm->login_prompt.password_input)
+                );
+                return;
+            }
+
             SETextInput *const tis[] = {
                 &mm->login_prompt.username_input,
                 &mm->login_prompt.password_input,

+ 4 - 0
src/game/game_common.c

@@ -13,6 +13,10 @@
 #include "game.h"
 
 Screen current_screen = PONG_MAIN_MENU;
+const char *game_server_host = "127.0.0.1";
+const in_port_t game_server_port = 8100;
+const char *auth_server_host = "127.0.0.1";
+const in_port_t auth_server_port = 8080;
 
 static void pos_convert(
     vec3 render_pos,

+ 8 - 2
src/game/game_common.h

@@ -1,5 +1,7 @@
 #pragma once
 
+#include <netdb.h>
+
 #include "scufeng/Mesh.h"
 
 #include "game.h"
@@ -10,8 +12,6 @@ typedef enum {
     PONG_MULTIPLAYER,
 } Screen;
 
-extern Screen current_screen;
-
 typedef struct {
     SEMesh paddle_l;
     SEMesh paddle_r;
@@ -20,6 +20,12 @@ typedef struct {
     SEMesh score_r;
 } GameShapes;
 
+extern Screen current_screen;
+extern const char *game_server_host;
+extern const in_port_t game_server_port;
+extern const char *auth_server_host;
+extern const in_port_t auth_server_port;
+
 /**
   @return SE_OUT_OF_MEMORY
   @return SE_SUCCESS

+ 1 - 1
src/game/pong.c

@@ -62,7 +62,7 @@ int main() {
                 break;
             case PONG_MULTIPLAYER:
             {
-                if (!MultiPlayer_alloc(&mp, screen_w, screen_h, "127.0.0.1", 8080)) {
+                if (!MultiPlayer_alloc(&mp, screen_w, screen_h, game_server_host, game_server_port)) {
                     current_screen = PONG_MAIN_MENU;
                 }
                 MainMenu_callbacks_unset(&mm);

+ 1 - 1
src/server/server.c

@@ -20,7 +20,7 @@
 #include "GameState_net.h"
 
 #define SERVER_HOST "127.0.0.1"
-#define SERVER_PORT 8080
+#define SERVER_PORT 8100
 
 typedef enum {
     SS_LOBBY,

+ 1 - 1
subprojects/scufeng-x86_64/GIT_COMMIT

@@ -1 +1 @@
-d7b098ee4e78c5ca71f843ff794d6d9d064aa493
+06891ee1129ad7ac8acae0628429d019974fef6e

+ 16 - 1
subprojects/scufeng-x86_64/include/scufeng/engine.h

@@ -3,6 +3,9 @@
 
 #include <stdio.h>
 #include <stdbool.h>
+#include <stdint.h>
+
+#include "scufcont/DArray.h"
 
 #include "keys.h" // IWYU pragma: export
 
@@ -11,10 +14,20 @@
     X(SE_OUT_OF_MEMORY) \
     X(SE_FC_FAILURE) \
     X(SE_NOT_FOUND) \
-    X(SE_FT_FAILURE)
+    X(SE_FT_FAILURE) \
+    X(SE_ERRNO) \
+    X(SE_GAI) \
+    X(SE_COULD_NOT_CONNECT_TO_HOST) \
+    X(SE_COULD_NOT_FIND_ADDRESS)
 
 #define X(val) val,
 
+/**
+  \enum SEResult
+
+  \var SEResult::SE_ERRNO Error, errno was set.
+  \var SEResult::SE_GAI Error, a function that returns this should also return an error generated by getaddrinfo as on out parameter.
+*/
 typedef enum {
     SERESULT_X(X)
 } SEResult;
@@ -37,6 +50,8 @@ typedef enum {
     SE_DOWN
 } SEDirection;
 
+SCDARRAY_HEADER(uint8_t, uint8)
+
 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);

+ 146 - 0
subprojects/scufeng-x86_64/include/scufeng/http.h

@@ -0,0 +1,146 @@
+#ifndef SCUFENG_HTTP_H_
+#define SCUFENG_HTTP_H_
+
+#include <netinet/in.h>
+#include <stdint.h>
+
+#include "scufcont/DArray.h"
+#include "scufcont/DString.h"
+
+#include "scufeng/engine.h"
+
+#define SE_HTTP_METHODS_X(X) \
+    X(SE_METHOD_GET)         \
+    X(SE_METHOD_HEAD)        \
+    X(SE_METHOD_POST)        \
+    X(SE_METHOD_PUT)         \
+    X(SE_METHOD_DELETE)      \
+    X(SE_METHOD_CONNECT)     \
+    X(SE_METHOD_OPTIONS)     \
+    X(SE_METHOD_TRACE)       \
+    X(SE_METHOD_PATCH)       \
+    X(SE_METHOD_QUERY)
+
+#define X(method) method,
+
+typedef enum : uint8_t {
+    SE_HTTP_METHODS_X(X)
+} SEHttpMethod;
+
+#undef X
+
+typedef struct {
+    char *key;
+    char *value;
+} SEHttpHeader;
+
+SCDARRAY_HEADER(SEHttpHeader, SEHttpHeader)
+
+typedef struct {
+    in_port_t port;
+    SEHttpMethod method;
+    SCDString uri;
+    SCDArray_SEHttpHeader headers;
+    uint8_t *body;
+    size_t body_len;
+} SEHttpRequest;
+
+// typedef struct {
+//
+// } SEHttpResponse;
+
+const char *SEHttpMethod_to_str(const SEHttpMethod method);
+
+/**
+  @return SE_OUT_OF_MEMORY
+  @return SE_SUCCESS
+*/
+SEResult SEHttpHeader_alloc(
+    SEHttpHeader *const header,
+    const char *key,
+    const size_t key_len,
+    const char *value,
+    const size_t value_len
+);
+
+void SEHttpHeader_free(SEHttpHeader *const header);
+void SEHttpHeader_print(const SEHttpHeader *const header, FILE *o);
+size_t SEHttpHeader_data_size(const SEHttpHeader *const header);
+size_t SEHttpHeader_data(const SEHttpHeader *const header, uint8_t *const buf);
+
+/**
+  @return SE_OUT_OF_MEMORY
+  @return SE_SUCCESS
+*/
+SEResult SEHttpRequest_header_add(SEHttpRequest *const req, const SEHttpHeader *const header);
+
+/**
+  @return SE_OUT_OF_MEMORY
+  @return SE_SUCCESS
+*/
+SEResult SEHttpRequest_header_add_c_str(
+    SEHttpRequest *const req,
+    const char *key,
+    const size_t key_len,
+    const char *value,
+    const size_t value_len
+);
+
+/**
+  @return SE_OUT_OF_MEMORY
+  @return SE_SUCCESS
+*/
+SEResult SEHttpRequest_alloc(
+    SEHttpRequest *const req,
+    const char *host,
+    const size_t host_len,
+    const in_port_t port,
+    const SEHttpMethod method,
+    const char *uri,
+    const size_t uri_len,
+    const uint8_t *body,
+    const size_t body_len
+);
+
+void SEHttpRequest_free(SEHttpRequest *const req);
+void SEHttpRequest_print(const SEHttpRequest *const req, FILE *o);
+size_t SEHttpRequest_headers_data_size(const SEHttpRequest *const req);
+size_t SEHttpRequest_data_size(const SEHttpRequest *const req);
+size_t SEHttpRequest_data(
+    const SEHttpRequest *const req,
+    uint8_t *const buf
+);
+
+/**
+  @return SE_SUCCESS
+  @return SE_ERRNO
+  @return SE_GAI
+  @return SE_COULD_NOT_CONNECT_TO_HOST
+  @return SE_COULD_NOT_FIND_ADDRESS
+  @return SE_OUT_OF_MEMORY
+*/
+SEResult SEHttpRequest_send(const SEHttpRequest *const req, int *const gai_err);
+
+/**
+  @return SE_SUCCESS
+  @return SE_ERRNO
+  @return SE_GAI
+  @return SE_COULD_NOT_CONNECT_TO_HOST
+  @return SE_COULD_NOT_FIND_ADDRESS
+  @return SE_OUT_OF_MEMORY
+*/
+SEResult se_http_request_send(
+    const char *const host,
+    const size_t host_len,
+    const in_port_t port,
+    const SEHttpMethod method,
+    const char *const uri,
+    const size_t uri_len,
+    const char *const *const headers,
+    const size_t headers_len,
+    const uint8_t *const body,
+    const size_t body_len,
+    int *const gai_err
+);
+
+#endif // SCUFENG_HTTP_H_

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

@@ -1,11 +0,0 @@
-#ifndef SE_SCUFCONT_IMPL_H_
-#define SE_SCUFCONT_IMPL_H_
-
-#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_

+ 3 - 0
subprojects/scufeng-x86_64/include/scufeng/text.h

@@ -8,6 +8,7 @@
 #include "scufeng/engine.h"
 #include "scufeng/Mesh.h"
 #include "scufcont/DString.h"
+#include "scufcont/DArray.h"
 
 #define SETextInput_pos(ti) (ti.bg_mesh.transform.pos)
 #define SETextInput_scale(ti) (ti.bg_mesh.transform.scale)
@@ -22,6 +23,8 @@ typedef struct {
     int bearing_y_max;
 } SEFont;
 
+SCDARRAY_HEADER(SEFont, SEFont)
+
 typedef struct {
     bool active;
     size_t cursor;

BIN
subprojects/scufeng-x86_64/libscufeng.a