From 884f10e6ec59d189637519358f3d225ad7d1fc87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Madar=C3=A1sz?= Date: Wed, 12 May 2021 16:42:22 +0200 Subject: [PATCH] code cleanup + fixes --- code/common/game.h | 9 +++- code/common/packets/pkt_send_librg_update.c | 2 +- code/common/world/world.c | 2 +- code/common/world/world.h | 1 + code/game/header/utils/raylib_helpers.h | 49 +++++++++++++++++++++ code/game/source/game.c | 19 ++++---- code/game/source/main.c | 3 +- code/game/source/platform_raylib.c | 42 +----------------- 8 files changed, 72 insertions(+), 55 deletions(-) create mode 100644 code/game/header/utils/raylib_helpers.h diff --git a/code/common/game.h b/code/common/game.h index b744124..90ce234 100644 --- a/code/common/game.h +++ b/code/common/game.h @@ -2,7 +2,14 @@ #include "system.h" #include "world_view.h" -void game_init(int8_t play_mode, uint32_t num_viewers, int32_t seed, uint16_t block_size, uint16_t chunk_size, uint16_t chunk_amount, int8_t is_dash_enabled); +typedef enum { + GAMEKIND_SINGLE, + GAMEKIND_CLIENT, + GAMEKIND_HEADLESS, + FORCE_GAMEKIND_UINT8 = UINT8_MAX +} game_kind; + +void game_init(game_kind play_mode, uint32_t num_viewers, int32_t seed, uint16_t block_size, uint16_t chunk_size, uint16_t chunk_amount, int8_t is_dash_enabled); void game_shutdown(); uint8_t game_is_running(); int8_t game_is_networked(); diff --git a/code/common/packets/pkt_send_librg_update.c b/code/common/packets/pkt_send_librg_update.c index 09e31c9..58cd081 100644 --- a/code/common/packets/pkt_send_librg_update.c +++ b/code/common/packets/pkt_send_librg_update.c @@ -9,7 +9,7 @@ size_t pkt_send_librg_update(uint64_t peer_id, uint8_t ticker, void *data, size_t datalen) { - return pkt_world_write(MSG_ID_LIBRG_UPDATE, pkt_send_librg_update_encode(data, (int32_t)datalen, ticker), 1, view_id, peer_id); + return pkt_world_write(MSG_ID_LIBRG_UPDATE, pkt_send_librg_update_encode(data, (int32_t)datalen, ticker), 1, view_id, (void*)peer_id); } size_t pkt_send_librg_update_encode(void *data, int32_t data_length, uint8_t layer_id) { diff --git a/code/common/world/world.c b/code/common/world/world.c index 30c5907..df5130f 100644 --- a/code/common/world/world.c +++ b/code/common/world/world.c @@ -116,7 +116,7 @@ int32_t world_init(int32_t seed, uint16_t block_size, uint16_t chunk_size, uint1 /* config our world grid */ librg_config_chunksize_set(world.tracker, block_size * world.chunk_size, block_size * world.chunk_size, 0); librg_config_chunkamount_set(world.tracker, world.chunk_amount, world.chunk_amount, 0); - librg_config_chunkoffset_set(world.tracker, LIBRG_OFFSET_BEG, LIBRG_OFFSET_BEG, 0); + librg_config_chunkoffset_set(world.tracker, LIBRG_OFFSET_BEG, LIBRG_OFFSET_BEG, LIBRG_OFFSET_BEG); librg_event_set(world.tracker, LIBRG_WRITE_CREATE, tracker_write_create); librg_event_set(world.tracker, LIBRG_WRITE_REMOVE, tracker_write_remove); diff --git a/code/common/world/world.h b/code/common/world/world.h index fd7ed94..e839112 100644 --- a/code/common/world/world.h +++ b/code/common/world/world.h @@ -44,6 +44,7 @@ void world_setup_pkt_handlers(world_pkt_reader_proc *reader_proc, world_pkt_writ int32_t world_init(int32_t seed, uint16_t block_size, uint16_t chunk_size, uint16_t chunk_amount); int32_t world_destroy(void); int32_t world_update(void); + int32_t world_read(void* data, uint32_t datalen, void *udata); int32_t world_write(pkt_header *pkt, void *udata); diff --git a/code/game/header/utils/raylib_helpers.h b/code/game/header/utils/raylib_helpers.h new file mode 100644 index 0000000..7e9d865 --- /dev/null +++ b/code/game/header/utils/raylib_helpers.h @@ -0,0 +1,49 @@ +#pragma once +#include "system.h" +#include "raylib.h" + +static inline +void DrawTextEco(const char *text, float posX, float posY, int fontSize, Color color, float spacing) { +#if 1 + // Check if default font has been loaded + if (GetFontDefault().texture.id != 0) { + Vector2 position = { (float)posX , (float)posY }; + + int defaultFontSize = 10; // Default Font chars height in pixel + int new_spacing = spacing == 0.0f ? fontSize/defaultFontSize : spacing; + + DrawTextEx(GetFontDefault(), text, position, (float)fontSize , (float)new_spacing , color); + } +#endif +} + +static inline +int MeasureTextEco(const char *text, int fontSize, float spacing) { +#if 1 + Vector2 vec = { 0.0f, 0.0f }; + + // Check if default font has been loaded + if (GetFontDefault().texture.id != 0) { + int defaultFontSize = 10; // Default Font chars height in pixel + int new_spacing = spacing == 0.0f ? fontSize/defaultFontSize : spacing; + + vec = MeasureTextEx(GetFontDefault(), text, (float)fontSize, (float)new_spacing); + } + + return (int)vec.x; +#else + return 0; +#endif +} + +static inline +void DrawCircleEco(float centerX, float centerY, float radius, Color color) +{ + DrawCircleV((Vector2){ (float)centerX , (float)centerY }, radius , color); +} + +static inline +void DrawRectangleEco(float posX, float posY, int width, int height, Color color) +{ + DrawRectangleV((Vector2){ (float)posX , (float)posY }, (Vector2){ (float)width , (float)height }, color); +} diff --git a/code/game/source/game.c b/code/game/source/game.c index 5297420..904e7ae 100644 --- a/code/game/source/game.c +++ b/code/game/source/game.c @@ -23,7 +23,7 @@ #include "packets/pkt_01_welcome.h" #include "packets/pkt_send_keystate.h" -static int8_t is_viewer_only; +static uint8_t game_mode; static world_view *world_viewers; static world_view *active_viewer; @@ -42,10 +42,12 @@ static WORLD_PKT_READER(pkt_reader) { } static WORLD_PKT_WRITER(sp_pkt_writer) { + (void)udata; return world_read(pkt->data, pkt->datalen, (void*)game_world_view_get_active()->owner_id); } static WORLD_PKT_WRITER(mp_pkt_writer) { + (void)udata; if (pkt->is_reliable) { return network_msg_send(pkt->data, pkt->datalen); } @@ -102,14 +104,14 @@ void flecs_dash_init() { ecs_set(world_ecs(), 0, EcsDashServer, {.port = 27001}); } -void game_init(int8_t play_mode, uint32_t num_viewers, int32_t seed, uint16_t block_size, uint16_t chunk_size, uint16_t chunk_amount, int8_t is_dash_enabled) { - is_viewer_only = play_mode; +void game_init(game_kind play_mode, uint32_t num_viewers, int32_t seed, uint16_t block_size, uint16_t chunk_size, uint16_t chunk_amount, int8_t is_dash_enabled) { + game_mode = play_mode; platform_init(); world_viewers_init(num_viewers); active_viewer = &world_viewers[0]; camera_reset(); - if (is_viewer_only) { + if (game_mode == GAMEKIND_CLIENT) { world_setup_pkt_handlers(pkt_reader, mp_pkt_writer); network_init(); network_client_connect("127.0.0.1", 27000); @@ -127,13 +129,13 @@ void game_init(int8_t play_mode, uint32_t num_viewers, int32_t seed, uint16_t bl } int8_t game_is_networked() { - return is_viewer_only; + return game_mode > 0; } void game_shutdown() { world_viewers_destroy(); - if (is_viewer_only) { + if (game_mode == GAMEKIND_CLIENT) { network_client_disconnect(); network_destroy(); } else { @@ -150,7 +152,7 @@ void game_input() { } void game_update() { - if (is_viewer_only) { + if (game_mode == GAMEKIND_CLIENT) { network_client_tick(); } else world_update(); @@ -171,10 +173,9 @@ void game_world_cleanup_entities(void) { entity_view_tbl *view = &world_viewers[i].entities; for (int j = 0; j < zpl_array_count(view->entries); j += 1){ - entity_view *e = &view->entries[j]; + entity_view *e = &view->entries[j].value; if (e->tran_effect == ETRAN_REMOVE) { entity_view_tbl_remove(view, e->ent_id); - j--; } } diff --git a/code/game/source/main.c b/code/game/source/main.c index 4f39d33..c833200 100644 --- a/code/game/source/main.c +++ b/code/game/source/main.c @@ -75,8 +75,7 @@ int main(int argc, char** argv) ECS_IMPORT(world_ecs(), General); ECS_IMPORT(world_ecs(), Controllers); ECS_IMPORT(world_ecs(), Physics); - uint16_t half_world_dim = world_dim() / 2; - for (int i = 0; i < npc_count; i++) { + for (uint32_t i = 0; i < npc_count; i++) { uint64_t e = entity_spawn(NULL); ecs_add(world_ecs(), e, EcsDemoNPC); Position *pos = ecs_get_mut(world_ecs(), e, Position, NULL); diff --git a/code/game/source/platform_raylib.c b/code/game/source/platform_raylib.c index 06898e0..d1b2432 100644 --- a/code/game/source/platform_raylib.c +++ b/code/game/source/platform_raylib.c @@ -7,6 +7,7 @@ #include "prediction.h" #include "camera.h" #include "math.h" +#include "utils/raylib_helpers.h" uint16_t screenWidth = 1600; uint16_t screenHeight = 900; @@ -17,47 +18,6 @@ float zoom_overlay_tran = 0.0f; static Camera2D render_camera; -void DrawTextEco(const char *text, float posX, float posY, int fontSize, Color color, float spacing) { -#if 1 - // Check if default font has been loaded - if (GetFontDefault().texture.id != 0) { - Vector2 position = { (float)posX , (float)posY }; - - int defaultFontSize = 10; // Default Font chars height in pixel - int new_spacing = spacing == 0.0f ? fontSize/defaultFontSize : spacing; - - DrawTextEx(GetFontDefault(), text, position, (float)fontSize , (float)new_spacing , color); - } -#endif -} - -int MeasureTextEco(const char *text, int fontSize, float spacing) { -#if 1 - Vector2 vec = { 0.0f, 0.0f }; - - // Check if default font has been loaded - if (GetFontDefault().texture.id != 0) { - int defaultFontSize = 10; // Default Font chars height in pixel - int new_spacing = spacing == 0.0f ? fontSize/defaultFontSize : spacing; - - vec = MeasureTextEx(GetFontDefault(), text, (float)fontSize, (float)new_spacing); - } - - return (int)vec.x; -#else - return 0; -#endif -} -void DrawCircleEco(float centerX, float centerY, float radius, Color color) -{ - DrawCircleV((Vector2){ (float)centerX , (float)centerY }, radius , color); -} -void DrawRectangleEco(float posX, float posY, int width, int height, Color color) -{ - DrawRectangleV((Vector2){ (float)posX , (float)posY }, (Vector2){ (float)width , (float)height }, color); -} - - void platform_init() { InitWindow(screenWidth, screenHeight, "eco2d - client"); SetWindowState(FLAG_WINDOW_UNDECORATED|FLAG_WINDOW_MAXIMIZED|FLAG_WINDOW_RESIZABLE);