|
|
@@ -78,15 +78,25 @@ static void send_out_msgs() {
|
|
|
OutgoingMsg out_msg = pop_out_msg();
|
|
|
switch (out_msg.type) {
|
|
|
case NET_MSG_PADDLE_POS:
|
|
|
- const PaddlePosMsg msg = {
|
|
|
- .player1 = true,
|
|
|
- .pos_x = game_state.paddle_l.pos[0],
|
|
|
- .pos_y = game_state.paddle_l.pos[1],
|
|
|
+ // p1
|
|
|
+ PaddleUpdateMsg msg = {
|
|
|
+ .l_pos_y = game_state.paddle_l.pos[1],
|
|
|
+ .l_dirv_y = game_state.paddle_l.dirv[1],
|
|
|
+ .r_pos_y = game_state.paddle_r.pos[1],
|
|
|
+ .r_dirv_y = game_state.paddle_r.dirv[1]
|
|
|
};
|
|
|
send_msg(p1_conn, NET_MSG_PADDLE_POS, (uint8_t *) &msg);
|
|
|
+ // p2
|
|
|
+ msg.l_pos_y = game_state.paddle_r.pos[1];
|
|
|
+ msg.l_dirv_y = game_state.paddle_r.dirv[1];
|
|
|
+ msg.r_pos_y = game_state.paddle_l.pos[1];
|
|
|
+ msg.r_dirv_y = game_state.paddle_l.dirv[1];
|
|
|
+ send_msg(p2_conn, NET_MSG_PADDLE_POS, (uint8_t *) &msg);
|
|
|
break;
|
|
|
case NET_MSG_PADDLE_CHANGE_DIR:
|
|
|
case NET_MSG_GAME_STATE:
|
|
|
+ case NET_MSG_GAME_START:
|
|
|
+ case NET_MSG_IS_PLAYER_1:
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
@@ -118,47 +128,65 @@ static void server_lobby(const int sock) {
|
|
|
LOG_FATAL("fdoepn failed for p1");
|
|
|
}
|
|
|
|
|
|
- // int player2 = accept(sock, nullptr, nullptr);
|
|
|
- // if (player2 == -1) {
|
|
|
- // LOG_FATALF("accept failed: %s", strerror(errno));
|
|
|
- // }
|
|
|
- // if (fcntl(player2, F_SETFL, O_NONBLOCK) != 0) {
|
|
|
- // LOG_FATALF("fcntl failed on player2: (%d) %s", errno, strerror(errno));
|
|
|
- // }
|
|
|
- // printf("Player 2 connected\n");
|
|
|
+ IsPlayer1Msg p1_msg = {
|
|
|
+ .player1 = 1u
|
|
|
+ };
|
|
|
+ send_msg(p1_conn, NET_MSG_IS_PLAYER_1, (uint8_t *) &p1_msg);
|
|
|
|
|
|
- // p2_conn = fdopen(player2, "r+");
|
|
|
- // if (p2_conn == nullptr) {
|
|
|
- // LOG_FATAL("fdoepn failed for p2");
|
|
|
- // }
|
|
|
+ int player2 = accept(sock, nullptr, nullptr);
|
|
|
+ if (player2 == -1) {
|
|
|
+ LOG_FATALF("accept failed: %s", strerror(errno));
|
|
|
+ }
|
|
|
+ if (fcntl(player2, F_SETFL, O_NONBLOCK) != 0) {
|
|
|
+ LOG_FATALF("fcntl failed on player2: (%d) %s", errno, strerror(errno));
|
|
|
+ }
|
|
|
+ printf("Player 2 connected\n");
|
|
|
+
|
|
|
+ p2_conn = fdopen(player2, "r+");
|
|
|
+ if (p2_conn == nullptr) {
|
|
|
+ LOG_FATAL("fdoepn failed for p2");
|
|
|
+ }
|
|
|
+
|
|
|
+ p1_msg.player1 = 0u;
|
|
|
+ send_msg(p2_conn, NET_MSG_IS_PLAYER_1, (uint8_t *) &p1_msg);
|
|
|
}
|
|
|
|
|
|
-static void handle_paddle_change_dir_msg() {
|
|
|
+static void handle_paddle_change_dir_msg(FILE* conn, const bool p1) {
|
|
|
PaddleChangeDirMsg msg;
|
|
|
- recv_msg(p1_conn, NET_MSG_PADDLE_CHANGE_DIR, (uint8_t *) &msg);
|
|
|
+ recv_msg(conn, NET_MSG_PADDLE_CHANGE_DIR, (uint8_t *) &msg);
|
|
|
SEDirection dir = (SEDirection) msg.dir;
|
|
|
- GameState_paddle_l_change_dir(&game_state, dir);
|
|
|
+ if (p1) {
|
|
|
+ GameState_paddle_l_change_dir(&game_state, dir);
|
|
|
+ } else {
|
|
|
+ GameState_paddle_r_change_dir(&game_state, dir);
|
|
|
+ }
|
|
|
push_out_msg(NET_MSG_PADDLE_POS, nullptr);
|
|
|
- // bool player1 = (bool) msg.player1;
|
|
|
- // if (player1) {
|
|
|
- // const char *dir_str = dir == SE_UP ? "UP" : (dir == SE_DOWN ? "DOWN" : (dir == SE_NONE ? "NONE" : "INVALID"));
|
|
|
- // printf("Player 1 moves paddle to direction: %s\n", dir_str);
|
|
|
- // GameState_paddle_l_change_dir(&game_state, dir);
|
|
|
- // PaddlePosMsg res_msg = {
|
|
|
- // .pos_x = game_state.paddle_l.pos[0],
|
|
|
- // .pos_y = game_state.paddle_l.pos[1],
|
|
|
- // .player1 = (uint8_t) true
|
|
|
- // };
|
|
|
- // send_msg(p1_conn, NET_MSG_PADDLE_POS, (uint8_t *) &res_msg);
|
|
|
- // } else {
|
|
|
- // printf("Message was not from player1\n");
|
|
|
- // }
|
|
|
+}
|
|
|
+
|
|
|
+static void handle_player_input(const bool p1) {
|
|
|
+ FILE *conn = p1 ? p1_conn : p2_conn;
|
|
|
+ NetMsgType msg_type;
|
|
|
+ if (recv_msg_type(conn, &msg_type)) {
|
|
|
+ switch (msg_type) {
|
|
|
+ case NET_MSG_PADDLE_CHANGE_DIR:
|
|
|
+ handle_paddle_change_dir_msg(conn, p1);
|
|
|
+ break;
|
|
|
+ case NET_MSG_PADDLE_POS:
|
|
|
+ case NET_MSG_GAME_STATE:
|
|
|
+ case NET_MSG_GAME_START:
|
|
|
+ case NET_MSG_IS_PLAYER_1:
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
static void server_game() {
|
|
|
se_fps_lock_set_fps(60);
|
|
|
|
|
|
+ send_msg(p1_conn, NET_MSG_GAME_START, nullptr);
|
|
|
+ send_msg(p2_conn, NET_MSG_GAME_START, nullptr);
|
|
|
send_game_state(p1_conn);
|
|
|
+ send_game_state(p2_conn);
|
|
|
|
|
|
double last_frame_time_s = 0.0;
|
|
|
while (true) {
|
|
|
@@ -168,20 +196,10 @@ static void server_game() {
|
|
|
se_fps_lock_frame_start_s(current_time_s);
|
|
|
|
|
|
// Player input
|
|
|
- NetMsgType msg_type;
|
|
|
- if (recv_msg_type(p1_conn, &msg_type)) {
|
|
|
- switch (msg_type) {
|
|
|
- case NET_MSG_PADDLE_CHANGE_DIR:
|
|
|
- handle_paddle_change_dir_msg();
|
|
|
- break;
|
|
|
- case NET_MSG_PADDLE_POS:
|
|
|
- case NET_MSG_GAME_STATE:
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
+ handle_player_input(true);
|
|
|
+ handle_player_input(false);
|
|
|
|
|
|
// Tick
|
|
|
- printf("delta: %lf\n", delta);
|
|
|
GameState_update(&game_state, delta);
|
|
|
|
|
|
// Player output
|