|
|
@@ -7,19 +7,27 @@
|
|
|
#include <arpa/inet.h>
|
|
|
#include <unistd.h>
|
|
|
|
|
|
-#include "networking.h"
|
|
|
#include "scufeng/log.h"
|
|
|
+#include "scufeng/Shape.h"
|
|
|
|
|
|
+#include "networking.h"
|
|
|
#include "MultiPlayer.h"
|
|
|
#include "game.h"
|
|
|
+#include "game_common.h"
|
|
|
|
|
|
MultiPlayer MultiPlayer_alloc(
|
|
|
+ const unsigned int screen_w,
|
|
|
+ const unsigned int screen_h,
|
|
|
const char *server_host,
|
|
|
const uint16_t server_port
|
|
|
) {
|
|
|
MultiPlayer mp;
|
|
|
|
|
|
+ mp.screen_w = screen_w;
|
|
|
+ mp.screen_h = screen_h;
|
|
|
+
|
|
|
mp.state = GameState_new(game_default_params);
|
|
|
+ mp.shapes = GameShapes_alloc(screen_w, screen_h, game_default_params);
|
|
|
|
|
|
struct protoent *proto = getprotobyname("tcp");
|
|
|
mp.sock = socket(AF_INET, SOCK_STREAM, proto->p_proto);
|
|
|
@@ -63,6 +71,35 @@ void MultiPlayer_free(MultiPlayer *const mp) {
|
|
|
mp->conn = 0;
|
|
|
close(mp->sock);
|
|
|
mp->sock = 0;
|
|
|
+
|
|
|
+ GameShapes_free(&mp->shapes);
|
|
|
+}
|
|
|
+
|
|
|
+static void pos_convert(
|
|
|
+ vec3 render_pos,
|
|
|
+ const vec3 state_pos,
|
|
|
+ const unsigned int screen_w,
|
|
|
+ const unsigned int screen_h
|
|
|
+) {
|
|
|
+ render_pos[0] = state_pos[0] * screen_w;
|
|
|
+ render_pos[1] = (1.0f - state_pos[1]) * screen_h;
|
|
|
+}
|
|
|
+
|
|
|
+static void update_shapes(
|
|
|
+ const GameState *const state,
|
|
|
+ SEMesh *const paddle_l,
|
|
|
+ SEMesh *const paddle_r,
|
|
|
+ SEMesh *const ball,
|
|
|
+ const unsigned int screen_w,
|
|
|
+ const unsigned int screen_h
|
|
|
+) {
|
|
|
+
|
|
|
+ // Paddles
|
|
|
+ pos_convert(paddle_l->transform.pos, state->paddle_l.pos, screen_w, screen_h);
|
|
|
+ pos_convert(paddle_r->transform.pos, state->paddle_r.pos, screen_w, screen_h);
|
|
|
+
|
|
|
+ // Ball
|
|
|
+ pos_convert(ball->transform.pos, state->ball.pos, screen_w, screen_h);
|
|
|
}
|
|
|
|
|
|
void MultiPlayer_tick(MultiPlayer *const mp, const double delta) {
|
|
|
@@ -94,20 +131,11 @@ void MultiPlayer_tick(MultiPlayer *const mp, const double delta) {
|
|
|
uint8_t buf[PADDLE_CHANGE_DIR_MSG_ENCODED_SIZE];
|
|
|
PaddleChangeDirMsg_encode(&msg, buf, PADDLE_CHANGE_DIR_MSG_ENCODED_SIZE);
|
|
|
send_msg(mp->conn_s, buf, PADDLE_CHANGE_DIR_MSG_ENCODED_SIZE);
|
|
|
-
|
|
|
- // const size_t msg_len = PADDLE_CHANGE_DIR_MSG_ENCODED_SIZE;
|
|
|
- // if (fwrite(&msg_len, sizeof(size_t), 1, mp->conn_s) < 1) {
|
|
|
- // LOG_FATAL("Failed to write msg_len");
|
|
|
- // }
|
|
|
- // if (fwrite(&msg.dir, sizeof(msg.dir), 1, mp->conn_s) < 1) {
|
|
|
- // LOG_FATAL("Failed to write msg");
|
|
|
- // }
|
|
|
- // if (fflush(mp->conn_s) != 0) {
|
|
|
- // LOG_FATAL("Failed to flush stream");
|
|
|
- // }
|
|
|
}
|
|
|
|
|
|
// last_dir = new_direction;
|
|
|
|
|
|
GameState_update(&mp->state, delta);
|
|
|
+ GameShapes_update(&mp->shapes, &mp->state, mp->screen_w, mp->screen_h);
|
|
|
+ GameShapes_render(&mp->shapes);
|
|
|
}
|