Jelajahi Sumber

Updated Scufeng.

Aaro Saila 2 minggu lalu
induk
melakukan
95b77cbf1b

+ 3 - 3
src/common/game.c

@@ -193,7 +193,7 @@ static GameStateEvent ball_paddles_collision_check(GameState *const state, vec2
     //     (vec4) { 0.0f, 1.0f, 0.0f, 1.0f }
     // );
     // if (res != SE_SUCCESS) {
-    //     LOG_ERRORF("Failed to draw segment: %d", res);
+    //     SE_LOG_ERRORF("Failed to draw segment: %d", res);
     // }
     // res = se_render_line_segment_norm_coord(
     //     ball_side_top,
@@ -201,7 +201,7 @@ static GameStateEvent ball_paddles_collision_check(GameState *const state, vec2
     //     (vec4) { 1.0f, 0.0f, 0.0f, 1.0f }
     // );
     // if (res != SE_SUCCESS) {
-    //     LOG_ERRORF("Failed to draw segment: %d", res);
+    //     SE_LOG_ERRORF("Failed to draw segment: %d", res);
     // }
     // res = se_render_line_segment_norm_coord(
     //     (vec2) { paddle_back_x, 0.0f },
@@ -209,7 +209,7 @@ static GameStateEvent ball_paddles_collision_check(GameState *const state, vec2
     //     (vec4) { 0.0f, 1.0f, 0.0f, 1.0f }
     // );
     // if (res != SE_SUCCESS) {
-    //     LOG_ERRORF("Failed to draw segment: %d", res);
+    //     SE_LOG_ERRORF("Failed to draw segment: %d", res);
     // }
 
     const bool inside_paddle = (ball_side_top[0] <= paddle_side_top[0] && ball_side_top[0] >= paddle_back_x)

+ 8 - 8
src/common/networking.c

@@ -39,15 +39,15 @@ size_t NetMsgType_to_buf_size(const NetMsgType type) {
         return sizeof(ScoreUpdateMsg);
     }
 
-    LOG_FATALF("Invalid msg type: %u", type);
+    SE_LOG_FATALF("Invalid msg type: %u", type);
 }
 
 #define FWRITE_CHECK                                                  \
     if ((ferror(s) && errno == ECONNRESET) || feof(s)) {              \
-        LOG("Connection reset");                                      \
+        SE_LOG("Connection reset");                                      \
         return false;                                                 \
     } else {                                                          \
-        LOG_FATALF("fwrite failed: (%d) %s", errno, strerror(errno)); \
+        SE_LOG_FATALF("fwrite failed: (%d) %s", errno, strerror(errno)); \
     }
 
 /**
@@ -77,7 +77,7 @@ bool send_msg(FILE *s, const NetMsgType type, const uint8_t *const buf) {
         FWRITE_CHECK;
     }
     if (fflush(s) != 0) {
-        LOG_ERRORF("[send_msg] Failed to flush stream: (%d) %s", errno, strerror(errno));
+        SE_LOG_ERRORF("[send_msg] Failed to flush stream: (%d) %s", errno, strerror(errno));
         return false;
     }
 
@@ -96,7 +96,7 @@ static int recv_until_fb_or_empty_buf(FILE *s) {
                 return ECONNRESET;
             } else {
                 printf("errno: %d ferror: %d feof: %d\n", errno, ferror(s), feof(s));
-                LOG_FATAL("[recv_until_fb_or_empty_buf] Failed to read _fb");
+                SE_LOG_FATAL("[recv_until_fb_or_empty_buf] Failed to read _fb");
             }
         }
         if (_fb != fb) {
@@ -118,7 +118,7 @@ int recv_msg_type(FILE *s, NetMsgType *const type) {
         return err;
     }
     if (fread(type, sizeof(NetMsgType), 1, s) < 1) {
-        LOG_FATAL("[recv_msg] Failed to read msg type");
+        SE_LOG_FATAL("[recv_msg] Failed to read msg type");
     }
 
     // Taking advantage of 'X not handled in switch' warning to make sure that
@@ -136,7 +136,7 @@ int recv_msg_type(FILE *s, NetMsgType *const type) {
         break;
     }
     if (!valid_type) {
-        LOG_FATALF("[recv_msg_type] Invalid msg type: %u", *type);
+        SE_LOG_FATALF("[recv_msg_type] Invalid msg type: %u", *type);
     }
 
     return 0;
@@ -155,7 +155,7 @@ bool recv_msg(FILE *s, const NetMsgType type, uint8_t *const buf) {
         if ((ferror(s) && errno == ECONNRESET) || feof(s)) {
             return false;
         } else {
-            LOG_FATAL("[recv_msg] Failed to read msg");
+            SE_LOG_FATAL("[recv_msg] Failed to read msg");
         }
     }
     for (uint8_t *p = buf; p < buf + msg_len; p++) {

+ 1 - 1
src/common/util.c

@@ -3,6 +3,6 @@
 
 void assert_scufcont_success(const ScufContResult res) {
     if (res != SCUFCONT_SUCCESS) {
-        LOG_FATALF("Scufcont operation failed: %s", ScufContResult_to_str(res));
+        SE_LOG_FATALF("Scufcont operation failed: %s", ScufContResult_to_str(res));
     }
 }

+ 24 - 24
src/game/MultiPlayer.c

@@ -36,14 +36,14 @@ bool MultiPlayer_alloc(
     struct protoent *proto = getprotobyname("tcp");
     mp->sock = socket(AF_INET, SOCK_STREAM, proto->p_proto);
     if (mp->sock == -1) {
-        LOG_FATALF("socket failed: %d %s", errno, strerror(errno));
+        SE_LOG_FATALF("socket failed: %d %s", errno, strerror(errno));
     }
 
     struct in_addr server_addr;
     int res = inet_aton(server_host, &server_addr);
     if (res == 0) {
         close(mp->sock);
-        LOG_FATAL("inet_aton failed");
+        SE_LOG_FATAL("inet_aton failed");
     }
 
     struct sockaddr_in server_sockaddr = {
@@ -59,16 +59,16 @@ bool MultiPlayer_alloc(
     );
     if (res == -1) {
         close(mp->sock);
-        LOG_ERRORF("connect failed: %d %s", errno, strerror(errno));
+        SE_LOG_ERRORF("connect failed: %d %s", errno, strerror(errno));
         return false;
     }
 
-    LOG("Connected to server");
+    SE_LOG("Connected to server");
 
     mp->conn_s = fdopen(mp->sock, "r+");
     if (mp->conn_s == nullptr) {
         close(mp->sock);
-        LOG_ERRORF("fdopen failed: (%d) %s", errno, strerror(errno));
+        SE_LOG_ERRORF("fdopen failed: (%d) %s", errno, strerror(errno));
         return false;
     }
 
@@ -76,78 +76,78 @@ bool MultiPlayer_alloc(
     if (recv_msg_type(mp->conn_s, &msg_type) == ECONNRESET) {
         fclose(mp->conn_s);
         close(mp->sock);
-        LOG_ERROR("Connection reset");
+        SE_LOG_ERROR("Connection reset");
         return false;
     }
     if (msg_type != NET_MSG_IS_PLAYER_1) {
         fclose(mp->conn_s);
         close(mp->sock);
-        LOG_ERRORF("Did not get IS_PLAYER_1 msg, got: %u", msg_type);
+        SE_LOG_ERRORF("Did not get IS_PLAYER_1 msg, got: %u", msg_type);
         return false;
     }
     IsPlayer1Msg p1_msg = { 0 };
     if (!recv_msg(mp->conn_s, NET_MSG_IS_PLAYER_1, (uint8_t *) &p1_msg)) {
         fclose(mp->conn_s);
         close(mp->sock);
-        LOG_ERROR("Connection reset");
+        SE_LOG_ERROR("Connection reset");
         return false;
     }
     mp->is_player_1 = (bool) p1_msg.player1;
     if (mp->is_player_1) {
-        LOG("I am player 1");
+        SE_LOG("I am player 1");
     } else {
-        LOG("I am player 2");
+        SE_LOG("I am player 2");
     }
 
-    LOG("Waiting for game start");
+    SE_LOG("Waiting for game start");
 
     mp->state = GameState_new(game_default_params, false);
 
     if (recv_msg_type(mp->conn_s, &msg_type) == ECONNRESET) {
         fclose(mp->conn_s);
         close(mp->sock);
-        LOG_ERROR("Connection reset");
+        SE_LOG_ERROR("Connection reset");
         return false;
     }
     if (msg_type != NET_MSG_GAME_STATE) {
         fclose(mp->conn_s);
         close(mp->sock);
-        LOG_FATALF("[MultiPlayer_alloc] Did not get GAME_STATE msg, got: %u", msg_type);
+        SE_LOG_FATALF("[MultiPlayer_alloc] Did not get GAME_STATE msg, got: %u", msg_type);
     }
     if (!handle_game_state_msg(mp)) {
         fclose(mp->conn_s);
         close(mp->sock);
-        LOG_ERROR("Connection reset");
+        SE_LOG_ERROR("Connection reset");
         return false;
     }
 
     if (recv_msg_type(mp->conn_s, &msg_type) == ECONNRESET) {
         fclose(mp->conn_s);
         close(mp->sock);
-        LOG_ERROR("Connection reset");
+        SE_LOG_ERROR("Connection reset");
         return false;
     }
     if (msg_type != NET_MSG_GAME_START) {
         fclose(mp->conn_s);
         close(mp->sock);
-        LOG_FATALF("[MultiPlayer_alloc] Did not get GAME_START msg, got: %u", msg_type);
+        SE_LOG_FATALF("[MultiPlayer_alloc] Did not get GAME_START msg, got: %u", msg_type);
     }
 
     if (!send_msg(mp->conn_s, NET_MSG_GAME_START, nullptr)) {
         fclose(mp->conn_s);
         close(mp->sock);
-        LOG_ERROR("Connection reset");
+        SE_LOG_ERROR("Connection reset");
         return false;
     }
 
     if (fcntl(mp->sock, F_SETFL, O_NONBLOCK) != 0) {
         fclose(mp->conn_s);
         close(mp->sock);
-        LOG_ERRORF("fcntl failed on mp->sock: (%d) %s", errno, strerror(errno));
+        SE_LOG_ERRORF("fcntl failed on mp->sock: (%d) %s", errno, strerror(errno));
         return false;
     }
 
-    LOG("Game started");
+    SE_LOG("Game started");
 
     mp->screen_w = screen_w;
     mp->screen_h = screen_h;
@@ -190,7 +190,7 @@ static void handle_score_update_msg(MultiPlayer *const mp) {
     recv_msg(mp->conn_s, NET_MSG_SCORE_UPDATE, (uint8_t *) &msg);
     mp->state.score_l = msg.l_score;
     mp->state.score_r = msg.r_score;
-    LOGF("score_l: %d score_r: %d", mp->state.score_l, mp->state.score_r);
+    SE_LOGF("score_l: %d score_r: %d", mp->state.score_l, mp->state.score_r);
 }
 
 static void handle_input(
@@ -249,15 +249,15 @@ Screen MultiPlayer_tick(MultiPlayer *const mp, const double delta) {
             }
             break;
         } else if (res == ECONNRESET) {
-            LOG_ERROR("Connection reset");
+            SE_LOG_ERROR("Connection reset");
             return PONG_MAIN_MENU;
         } else if (res == EWOULDBLOCK) {
             break;
         } else {
-            LOG_FATALF("[MultiPlayer_tick] Unhandled return value: %d", res);
+            SE_LOG_FATALF("[MultiPlayer_tick] Unhandled return value: %d", res);
         }
 
-        // LOGF("Got message: %s", NetMsgType_to_str(msg_type));
+        // SE_LOGF("Got message: %s", NetMsgType_to_str(msg_type));
     }
 
     bool dir_changed = false;
@@ -275,7 +275,7 @@ Screen MultiPlayer_tick(MultiPlayer *const mp, const double delta) {
         };
 
         if (!send_msg(mp->conn_s, NET_MSG_PADDLE_CHANGE_DIR, (uint8_t *) &msg)) {
-            LOG_ERROR("Connection reset");
+            SE_LOG_ERROR("Connection reset");
             return PONG_MAIN_MENU;
         }
     }

+ 1 - 1
src/game/pong.c

@@ -14,7 +14,7 @@ static const unsigned int screen_h = 600;
 int main() {
     signal(SIGPIPE, SIG_IGN);
 
-    se_init(true);
+    se_init(true, stdout, stderr);
     se_window_create(screen_w, screen_h, "Pong", -1);
     se_swap_interval(0);
     se_fps_lock_set_fps(60);

+ 20 - 20
src/server/server.c

@@ -124,13 +124,13 @@ static bool server_lobby(const int sock) {
 
     int player1 = accept(sock, nullptr, nullptr);
     if (player1 == -1) {
-        LOG_FATALF("accept failed: %s", strerror(errno));
+        SE_LOG_FATALF("accept failed: %s", strerror(errno));
     }
     printf("Player 1 connected\n");
 
     p1_conn = fdopen(player1, "r+");
     if (p1_conn == nullptr) {
-        LOG_FATAL("fdoepn failed for p1");
+        SE_LOG_FATAL("fdoepn failed for p1");
     }
 
     IsPlayer1Msg p1_msg = {
@@ -144,13 +144,13 @@ static bool server_lobby(const int sock) {
 
     int player2 = accept(sock, nullptr, nullptr);
     if (player2 == -1) {
-        LOG_FATALF("accept failed: %s", strerror(errno));
+        SE_LOG_FATALF("accept failed: %s", strerror(errno));
     }
     printf("Player 2 connected\n");
 
     p2_conn = fdopen(player2, "r+");
     if (p2_conn == nullptr) {
-        LOG_FATAL("fdoepn failed for p2");
+        SE_LOG_FATAL("fdoepn failed for p2");
     }
 
     p1_msg.player1 = 0u;
@@ -229,7 +229,7 @@ static void server_game() {
         goto cleanup;
     }
     if (msg_type != NET_MSG_GAME_START) {
-        LOG_ERRORF("Did not receive GAME_START, got: %s\n", NetMsgType_to_str(msg_type));
+        SE_LOG_ERRORF("Did not receive GAME_START, got: %s\n", NetMsgType_to_str(msg_type));
         goto cleanup;
     }
 
@@ -237,18 +237,18 @@ static void server_game() {
         goto cleanup;
     }
     if (msg_type != NET_MSG_GAME_START) {
-        LOG_ERRORF("Did not receive GAME_START, got: %s\n", NetMsgType_to_str(msg_type));
+        SE_LOG_ERRORF("Did not receive GAME_START, got: %s\n", NetMsgType_to_str(msg_type));
         goto cleanup;
     }
 
     if (fcntl(fileno(p1_conn), F_SETFL, O_NONBLOCK) != 0) {
-        LOG_FATALF("fcntl failed on player1: (%d) %s", errno, strerror(errno));
+        SE_LOG_FATALF("fcntl failed on player1: (%d) %s", errno, strerror(errno));
     }
     if (fcntl(fileno(p2_conn), F_SETFL, O_NONBLOCK) != 0) {
-        LOG_FATALF("fcntl failed on player2: (%d) %s", errno, strerror(errno));
+        SE_LOG_FATALF("fcntl failed on player2: (%d) %s", errno, strerror(errno));
     }
 
-    LOG("Game start");
+    SE_LOG("Game start");
 
     double last_frame_time_s = se_get_time();
     while (true) {
@@ -279,7 +279,7 @@ static void server_game() {
         case GSEVENT_L_SCORE:
         case GSEVENT_R_SCORE:
         {
-            LOG("Goal");
+            SE_LOG("Goal");
             NetMsgType type = NET_MSG_SCORE_UPDATE;
             assert_scufcont_success(SCDArray_NetMsgType_push_back(&out_msgs, &type));
             type = NET_MSG_BALL_UPDATE;
@@ -298,7 +298,7 @@ static void server_game() {
     }
 
 cleanup:
-    LOG("Cleaning up");
+    SE_LOG("Cleaning up");
     fclose(p1_conn);
     fclose(p2_conn);
     SCDArray_NetMsgType_free(&out_msgs);
@@ -307,21 +307,21 @@ cleanup:
 int main() {
     signal(SIGPIPE, SIG_IGN);
 
-    se_init(true);
+    se_init(true, stdout, stderr);
 
     struct protoent *proto = getprotobyname("tcp");
     if (proto == nullptr) {
-        LOG_FATAL("getprotobyname failed");
+        SE_LOG_FATAL("getprotobyname failed");
     }
     const int sock = socket(AF_INET, SOCK_STREAM, proto->p_proto);
     if (sock == -1) {
-        LOG_FATALF("socket failed: %s", strerror(errno));
+        SE_LOG_FATALF("socket failed: %s", strerror(errno));
     }
     {
         int val = 1;
         val = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
         if (val != 0) {
-            LOG_FATALF("setsockopt failed: (%d) %s", errno, strerror(errno));
+            SE_LOG_FATALF("setsockopt failed: (%d) %s", errno, strerror(errno));
         }
     }
 
@@ -329,7 +329,7 @@ int main() {
         struct in_addr addr;
         int res = inet_aton(SERVER_HOST, &addr);
         if (res == 0) {
-            LOG_FATAL("inet_aton failed");
+            SE_LOG_FATAL("inet_aton failed");
         }
         struct sockaddr_in own_addr = {
             .sin_family = AF_INET,
@@ -338,25 +338,25 @@ int main() {
         };
         res = bind(sock, (struct sockaddr *) &own_addr, sizeof(own_addr));
         if (res == -1) {
-            LOG_FATALF("bind failed: %s", strerror(errno));
+            SE_LOG_FATALF("bind failed: %s", strerror(errno));
         }
     }
 
     int res = listen(sock, 5);
     if (res == -1) {
-        LOG_FATALF("listen failed: %s", strerror(errno));
+        SE_LOG_FATALF("listen failed: %s", strerror(errno));
     }
 
     while (true) {
         switch (server_state) {
         case SS_LOBBY:
-            LOG("SS_LOBBY");
+            SE_LOG("SS_LOBBY");
             if (server_lobby(sock)) {
                 server_state = SS_GAME;
             }
             break;
         case SS_GAME:
-            LOG("SS_GAME");
+            SE_LOG("SS_GAME");
             server_game();
             server_state = SS_LOBBY;
             break;

+ 1 - 1
subprojects/scufeng/GIT_COMMIT

@@ -1 +1 @@
-27f3dc720430939d045c6b38047960d2ce253c1a
+47f838af2a4cae2c992ae394f8b7ba7c381bafe0

+ 2 - 1
subprojects/scufeng/include/scufeng/engine.h

@@ -1,6 +1,7 @@
 #ifndef SCUFENG_H_
 #define SCUFENG_H_
 
+#include <stdio.h>
 #include <stdbool.h>
 
 #include "keys.h" // IWYU pragma: export
@@ -39,7 +40,7 @@ typedef enum {
 typedef void (*SEKeyCb)(SEKey key, SEKeyAction action, SEKeyMod mods);
 typedef void (*SECharCb)(unsigned int codepoint);
 
-void se_init(const bool debug);
+void se_init(const bool debug, FILE *log_out, FILE *log_err_out);
 void se_deinit(void);
 void se_window_create(
     const int w,

+ 11 - 10
subprojects/scufeng/include/scufeng/log.h

@@ -5,19 +5,20 @@
 #include <stdlib.h>
 #include <stdio.h>
 
-#define LOG(str) log_with_tag(stdout, __FILE__, __LINE__, "LOG", (str))
-#define LOGF(fmt, ...) logf_with_tag(stdout, __FILE__, __LINE__, "LOG", (fmt), __VA_ARGS__)
+#define SE_LOG(str) se_log_with_tag(false, __FILE__, __LINE__, "LOG", (str))
+#define SE_LOGF(fmt, ...) se_logf_with_tag(false, __FILE__, __LINE__, "LOG", (fmt), __VA_ARGS__)
 
-#define LOG_ERROR(str) log_with_tag(stderr, __FILE__, __LINE__, "ERROR", (str))
-#define LOG_ERRORF(fmt, ...) logf_with_tag(stderr, __FILE__, __LINE__, "ERROR", (fmt), __VA_ARGS__)
+#define SE_LOG_ERROR(str) se_log_with_tag(true, __FILE__, __LINE__, "ERROR", (str))
+#define SE_LOG_ERRORF(fmt, ...) se_logf_with_tag(true, __FILE__, __LINE__, "ERROR", (fmt), __VA_ARGS__)
 
-#define LOG_FATAL(str) LOG_ERROR(str); exit(-1)
-#define LOG_FATALF(fmt, ...) LOG_ERRORF(fmt, __VA_ARGS__); exit(-1)
+#define SE_LOG_FATAL(str) SE_LOG_ERROR(str); exit(-1)
+#define SE_LOG_FATALF(fmt, ...) SE_LOG_ERRORF(fmt, __VA_ARGS__); exit(-1)
 
-#define LOG_FATAL_TODO(str) log_with_tag(stderr, __FILE__, __LINE__, "FATAL TODO", (str)); exit(-1)
-#define LOG_FATAL_TODOF(fmt, ...) logf_with_tag(stderr, __FILE__, __LINE__, "FATAL TODO", (fmt), __VA_ARGS__); exit(-1)
+#define SE_LOG_FATAL_TODO(str) se_log_with_tag(true, __FILE__, __LINE__, "FATAL TODO", (str)); exit(-1)
+#define SE_LOG_FATAL_TODOF(fmt, ...) se_logf_with_tag(true, __FILE__, __LINE__, "FATAL TODO", (fmt), __VA_ARGS__); exit(-1)
 
-void log_with_tag(FILE* out_file, char* file_name, size_t line, const char* tag, char* str);
-void logf_with_tag(FILE* out_file, char* file_name, size_t line, const char* tag, char* fmt, ...);
+void se_log_out_set(FILE *log_out, FILE *log_err_out);
+void se_log_with_tag(const bool is_error, char* file_name, size_t line, const char* tag, char* str);
+void se_logf_with_tag(const bool is_error, char* file_name, size_t line, const char* tag, char* fmt, ...);
 
 #endif // LOG_H_

TEMPAT SAMPAH
subprojects/scufeng/libscufeng.a