diff options
| -rw-r--r-- | .vscode/launch.json | 3 | ||||
| -rw-r--r-- | Makefile | 4 | ||||
| -rw-r--r-- | build/zombies.exe | bin | 1697824 -> 1704017 bytes | |||
| -rw-r--r-- | include/game.h | 32 | ||||
| -rw-r--r-- | include/pathfinding.h | 1 | ||||
| -rw-r--r-- | include/protocol.h | 40 | ||||
| -rw-r--r-- | main.c | 60 | ||||
| -rw-r--r-- | project-base.code-workspace | 4 | ||||
| -rw-r--r-- | src/game.c | 113 | ||||
| -rw-r--r-- | src/objects.c | 3 | ||||
| -rw-r--r-- | src/pathfinding.c | 3 | ||||
| -rw-r--r-- | src/protocol.c | 27 | ||||
| -rw-r--r-- | src/zombies.c | 1 |
13 files changed, 253 insertions, 38 deletions
diff --git a/.vscode/launch.json b/.vscode/launch.json index 570ecaf..7c697de 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -8,7 +8,8 @@ "type": "by-gdb", "request": "launch", "name": "Launch(gdb)", - "program": "build/zombies.exe", + "program": "build/zombies.exe ", + "programArgs": "-ip 127.0.0.1 -port 27015", "cwd": "${workspaceRoot}" } ] @@ -2,4 +2,6 @@ main: rm -rf "build/" mkdir -p "build/" gcc -m64 -g -DMODE_DEBUG main.c -o build/zombies.exe -lprojectbase-debug -Llibs/ -lSDL2 -lSDL2_mixer -lWs2_32 - ./build/zombies.exe + ./build/zombies.exe -ip 127.0.0.1 -port 27015 + # ./build/zombies.exe -ip 172.24.224.1 -port 27015 + diff --git a/build/zombies.exe b/build/zombies.exe Binary files differindex 612a3fb..6832234 100644 --- a/build/zombies.exe +++ b/build/zombies.exe diff --git a/include/game.h b/include/game.h new file mode 100644 index 0000000..944a831 --- /dev/null +++ b/include/game.h @@ -0,0 +1,32 @@ +#ifndef INCLUDE_GAME +#define INCLUDE_GAME + +#include <projectbase/project_base.h> + +#include "../include/protocol.h" + +typedef enum t_game_state { + GAMESTATE_IDLE, + GAMESTATE_LOADING_MAP, + GAMESTATE_PLAYING, +} game_state; + +typedef enum t_network_state { + CONNECTING, + WAITING_FOR_ID, + CONNECTED, + DISCONNECTED, +} network_state; + +typedef struct t_game { + game_state state; + network_state network_state; + network_server *server; + network_client *client; +} game; + +game global_state; + +void init_game(); + +#endif
\ No newline at end of file diff --git a/include/pathfinding.h b/include/pathfinding.h index 6bef101..9dcaab3 100644 --- a/include/pathfinding.h +++ b/include/pathfinding.h @@ -19,6 +19,7 @@ typedef struct t_pathfinding_request array global_pathfinding_queue; +void* pathfinding_thread(void *args); void make_pathfinding_request(vec2f start, vec2f end, array *to_fill, pathfinding_request *request); #endif
\ No newline at end of file diff --git a/include/protocol.h b/include/protocol.h new file mode 100644 index 0000000..316a21f --- /dev/null +++ b/include/protocol.h @@ -0,0 +1,40 @@ +#ifndef INCLUDE_PROTOCOL +#define INCLUDE_PROTOCOL + +#include <projectbase/project_base.h> + +typedef enum t_network_message_type { + MESSAGE_GET_ID_UPSTREAM, + MESSAGE_GET_ID_DOWNSTREAM, +} network_message_type; + +typedef struct t_protocol_generic_message { + network_client client; + network_message_type type; +} protocol_generic_message; + +typedef struct t_protocol_generic_client_message { + network_message_type type; +} protocol_generic_client_message; + +typedef struct t_protocol_get_id_upstream { + network_message_type type; +} protocol_get_id_upstream; + +typedef struct t_protocol_get_id_downstream { + network_message_type type; + u32 id; +} protocol_get_id_downstream; + +#define MAX_NETWORK_BUFFER_SIZE 50000 +u8 network_buffer[50000]; +network_message create_protocol_get_id_up(); +network_message create_protocol_get_id_down(u32 id); + +array messages_received_on_server; +array messages_received_on_client; + +void server_on_message_received(u8* buffer, u32 length, network_client client); +void client_on_message_received(u8* buffer, u32 length); + +#endif
\ No newline at end of file @@ -9,6 +9,8 @@ #include "include/pathfinding.h" #include "include/list.h" #include "include/allocator.h" +#include "include/game.h" +#include "include/protocol.h" #include "src/map.c" #include "src/players.c" @@ -19,50 +21,52 @@ #include "src/pathfinding.c" #include "src/list.c" #include "src/allocator.c" +#include "src/game.c" +#include "src/protocol.c" #define CONFIG_DIRECTORY "zombieshooter" font* fnt; void update_func(platform_window* window) { renderer->render_clear(window, rgb(0,255,0)); - - draw_grid(window); - draw_spawners(window); - - char buf[200]; - sprintf(buf, "QUEUE: %d", global_pathfinding_queue.length); - renderer->render_text(fnt, _global_camera.x, _global_camera.y, buf, rgb(255,255,255)); + update_game(window); } -void resize_func(platform_window* window, u32 change_x,u32 change_y) { - -} -void close_func(platform_window* window) { - +void handle_args(int argc, char **argv) { + char* ip = 0; + char* port = 0; + for (int i = 1; i < argc; i++) { + char* prev_str = argv[i-1]; + char* str = argv[i]; + if (strcmp(prev_str, "-ip") == 0) { + ip = str; + } + if (strcmp(prev_str, "-port") == 0) { + port = str; + } + } + + if (ip && port) { + if (strcmp(ip, "127.0.0.1") == 0) { + start_server(port); + } + connect_to_server(ip, port); + } } int main(int argc, char **argv) { platform_init(argc, argv, CONFIG_DIRECTORY); - platform_window *window = platform_open_window_ex("Zombies!", 700, 700, 1200, 1000, 500, 500, FLAGS_MINIMIZE, update_func, resize_func, close_func, 0, 0); + platform_window *window = platform_open_window_ex("Zombies!", 700, 700, 1200, 1000, 500, 500, FLAGS_MINIMIZE, update_func, 0, 0, 0, 0); settings_set_number("USE_GPU", 1); platform_toggle_vsync(window, true); - - fnt = assets_load_font(mono_ttf, mono_ttf+mono_ttf_len, 16); - load_map_from_data(); - create_objects(); - pathfinding_init(); + init_game(); + handle_args(argc, argv); - network_server *server = networking_create_server(); - - thread t = thread_start(pathfinding_thread, 0); - thread_detach(&t); - - network_client *client = network_connect_to_server("127.0.0.1", "27015"); - network_client_send(client, "Bing Bong"); + fnt = assets_load_font(mono_ttf, mono_ttf+mono_ttf_len, 16); while(platform_keep_running(window)) { platform_handle_events(); @@ -71,10 +75,8 @@ int main(int argc, char **argv) settings_write_to_file(); platform_destroy(); pathfinding_destroy(); - - network_client_close(client); - networking_destroy_server(server); - + destroy_game(); + memory_print_leaks(); return 0; diff --git a/project-base.code-workspace b/project-base.code-workspace index ad85ea4..291e8be 100644 --- a/project-base.code-workspace +++ b/project-base.code-workspace @@ -58,7 +58,9 @@ "pathfinding.h": "c", "unordered_map": "c", "players.h": "c", - "allocator.h": "c" + "allocator.h": "c", + "protocol.h": "c", + "queue": "cpp" } } }
\ No newline at end of file diff --git a/src/game.c b/src/game.c new file mode 100644 index 0000000..55d64a4 --- /dev/null +++ b/src/game.c @@ -0,0 +1,113 @@ +#include "../include/game.h" +#include "../include/pathfinding.h" + +u32 current_id = 0; + +void start_server(char* port) { + messages_received_on_server = array_create(sizeof(protocol_generic_message*)); + array_reserve(&messages_received_on_server, 100); + + global_state.server = networking_create_server(); + global_state.server->on_message = server_on_message_received; +} + +void connect_to_server(char* ip, char* port) { + messages_received_on_client = array_create(sizeof(protocol_generic_message*)); + array_reserve(&messages_received_on_client, 100); + + global_state.network_state = CONNECTING; + global_state.client = network_connect_to_server(ip, port); + global_state.client->on_message = client_on_message_received; + + if (global_state.client->is_connected) { + global_state.network_state = WAITING_FOR_ID; + + network_message message = create_protocol_get_id_up(); + network_client_send(global_state.client, message); + } +} + +void load_map() { + global_state.state = GAMESTATE_LOADING_MAP; + + load_map_from_data(); + create_objects(); + + pathfinding_init(); + + thread t = thread_start(pathfinding_thread, 0); + thread_detach(&t); + + global_state.state = GAMESTATE_PLAYING; + log_info("Done loading map"); +} + +void init_game() { + global_state.state = IDLE; + global_state.network_state = DISCONNECTED; + + load_map(); +} + +void destroy_game() { + if (global_state.server) networking_destroy_server(global_state.server); + if (global_state.client) network_client_close(global_state.client); +} + +void update_server() { + for (int i = 0; i < messages_received_on_server.length; i++) { + protocol_generic_message* msg = *(protocol_generic_message**)array_at(&messages_received_on_server, i); + + switch (msg->type) + { + case MESSAGE_GET_ID_UPSTREAM: { + network_client_send(&msg->client, create_protocol_get_id_down(current_id)); + current_id++; + } break; + + default: + log_info("Unhandled message received"); + break; + } + + array_remove_at(&messages_received_on_server, i); + i--; + } +} + +void update_client() { + for (int i = 0; i < messages_received_on_client.length; i++) { + protocol_generic_client_message* msg = *(protocol_generic_client_message**)array_at(&messages_received_on_client, i); + + switch (msg->type) + { + case MESSAGE_GET_ID_DOWNSTREAM: { + protocol_get_id_downstream* msg_id = (protocol_get_id_downstream*)msg; + my_id = msg_id->id; + global_state.network_state = CONNECTED; + spawn_player(my_id); + log_info("Id received, spawning player"); + } break; + + default: + log_info("Unhandled message received"); + break; + } + + array_remove_at(&messages_received_on_client, i); + i--; + } +} + +void update_game(platform_window* window) { + if (global_state.server) { + update_server(); + } + + update_client(); + + if (global_state.network_state == CONNECTED) { + draw_grid(window); + draw_spawners(window); + } +}
\ No newline at end of file diff --git a/src/objects.c b/src/objects.c index efa128b..ae65af2 100644 --- a/src/objects.c +++ b/src/objects.c @@ -85,7 +85,4 @@ void create_objects() { create_box(14, 10, 0); create_box(13, 10, 0); create_box(11, 10, 0); - - spawn_player(my_id); - //spawn_player(my_id+1); }
\ No newline at end of file diff --git a/src/pathfinding.c b/src/pathfinding.c index 16e5261..effca06 100644 --- a/src/pathfinding.c +++ b/src/pathfinding.c @@ -281,10 +281,7 @@ void* pathfinding_thread(void *args) else { mutex_unlock(&global_pathfinding_queue.mutex); - continue; } - - //thread_sleep(100); } return 0; diff --git a/src/protocol.c b/src/protocol.c new file mode 100644 index 0000000..256e85d --- /dev/null +++ b/src/protocol.c @@ -0,0 +1,27 @@ +#include "../include/protocol.h" + +network_message create_protocol_get_id_up() { + protocol_get_id_upstream* buf = (protocol_get_id_upstream*)network_buffer; + buf->type = MESSAGE_GET_ID_UPSTREAM; + return network_create_message(network_buffer, sizeof(protocol_get_id_upstream), MAX_NETWORK_BUFFER_SIZE); +} + +network_message create_protocol_get_id_down(u32 id) { + protocol_get_id_downstream* buf = (protocol_get_id_downstream*)network_buffer; + buf->type = MESSAGE_GET_ID_DOWNSTREAM; + buf->id = id; + return network_create_message(network_buffer, sizeof(protocol_get_id_downstream), MAX_NETWORK_BUFFER_SIZE); +} + +void server_on_message_received(u8* buffer, u32 length, network_client client) { + u8* allocated_buf = mem_alloc(length + sizeof(network_client)); + memcpy(allocated_buf, &client, sizeof(network_client)); + memcpy(allocated_buf + sizeof(network_client), buffer + 4, length-4); + array_push(&messages_received_on_server, (u8*)&allocated_buf); +} + +void client_on_message_received(u8* buffer, u32 length) { + u8* allocated_buf = mem_alloc(length); + memcpy(allocated_buf, buffer+4, length-4); + array_push(&messages_received_on_client, (u8*)&allocated_buf); +} diff --git a/src/zombies.c b/src/zombies.c index 4bbcf40..3685355 100644 --- a/src/zombies.c +++ b/src/zombies.c @@ -35,6 +35,7 @@ void spawn_zombie(int x, int y) { zombies[i].position = (vec3f){x,y, 0}; zombies[i].size = (vec3f){0.4, 0.4, 1}; zombies[i].time_since_last_path = 0.0f; + zombies[i].request.to_fill = &zombies[i].next_path; zombies[i].request.mutex = mutex_create(); player closest_player = get_closest_player_to_tile(x, y); |
