code cleanup + fixes
parent
1c72255e09
commit
884f10e6ec
|
@ -2,7 +2,14 @@
|
||||||
#include "system.h"
|
#include "system.h"
|
||||||
#include "world_view.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();
|
void game_shutdown();
|
||||||
uint8_t game_is_running();
|
uint8_t game_is_running();
|
||||||
int8_t game_is_networked();
|
int8_t game_is_networked();
|
||||||
|
|
|
@ -9,7 +9,7 @@ size_t pkt_send_librg_update(uint64_t peer_id,
|
||||||
uint8_t ticker,
|
uint8_t ticker,
|
||||||
void *data,
|
void *data,
|
||||||
size_t datalen) {
|
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) {
|
size_t pkt_send_librg_update_encode(void *data, int32_t data_length, uint8_t layer_id) {
|
||||||
|
|
|
@ -116,7 +116,7 @@ int32_t world_init(int32_t seed, uint16_t block_size, uint16_t chunk_size, uint1
|
||||||
/* config our world grid */
|
/* config our world grid */
|
||||||
librg_config_chunksize_set(world.tracker, block_size * world.chunk_size, block_size * world.chunk_size, 0);
|
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_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_CREATE, tracker_write_create);
|
||||||
librg_event_set(world.tracker, LIBRG_WRITE_REMOVE, tracker_write_remove);
|
librg_event_set(world.tracker, LIBRG_WRITE_REMOVE, tracker_write_remove);
|
||||||
|
|
|
@ -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_init(int32_t seed, uint16_t block_size, uint16_t chunk_size, uint16_t chunk_amount);
|
||||||
int32_t world_destroy(void);
|
int32_t world_destroy(void);
|
||||||
int32_t world_update(void);
|
int32_t world_update(void);
|
||||||
|
|
||||||
int32_t world_read(void* data, uint32_t datalen, void *udata);
|
int32_t world_read(void* data, uint32_t datalen, void *udata);
|
||||||
int32_t world_write(pkt_header *pkt, void *udata);
|
int32_t world_write(pkt_header *pkt, void *udata);
|
||||||
|
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
|
@ -23,7 +23,7 @@
|
||||||
#include "packets/pkt_01_welcome.h"
|
#include "packets/pkt_01_welcome.h"
|
||||||
#include "packets/pkt_send_keystate.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 *world_viewers;
|
||||||
static world_view *active_viewer;
|
static world_view *active_viewer;
|
||||||
|
@ -42,10 +42,12 @@ static WORLD_PKT_READER(pkt_reader) {
|
||||||
}
|
}
|
||||||
|
|
||||||
static WORLD_PKT_WRITER(sp_pkt_writer) {
|
static WORLD_PKT_WRITER(sp_pkt_writer) {
|
||||||
|
(void)udata;
|
||||||
return world_read(pkt->data, pkt->datalen, (void*)game_world_view_get_active()->owner_id);
|
return world_read(pkt->data, pkt->datalen, (void*)game_world_view_get_active()->owner_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
static WORLD_PKT_WRITER(mp_pkt_writer) {
|
static WORLD_PKT_WRITER(mp_pkt_writer) {
|
||||||
|
(void)udata;
|
||||||
if (pkt->is_reliable) {
|
if (pkt->is_reliable) {
|
||||||
return network_msg_send(pkt->data, pkt->datalen);
|
return network_msg_send(pkt->data, pkt->datalen);
|
||||||
}
|
}
|
||||||
|
@ -102,14 +104,14 @@ void flecs_dash_init() {
|
||||||
ecs_set(world_ecs(), 0, EcsDashServer, {.port = 27001});
|
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) {
|
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) {
|
||||||
is_viewer_only = play_mode;
|
game_mode = play_mode;
|
||||||
platform_init();
|
platform_init();
|
||||||
world_viewers_init(num_viewers);
|
world_viewers_init(num_viewers);
|
||||||
active_viewer = &world_viewers[0];
|
active_viewer = &world_viewers[0];
|
||||||
camera_reset();
|
camera_reset();
|
||||||
|
|
||||||
if (is_viewer_only) {
|
if (game_mode == GAMEKIND_CLIENT) {
|
||||||
world_setup_pkt_handlers(pkt_reader, mp_pkt_writer);
|
world_setup_pkt_handlers(pkt_reader, mp_pkt_writer);
|
||||||
network_init();
|
network_init();
|
||||||
network_client_connect("127.0.0.1", 27000);
|
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() {
|
int8_t game_is_networked() {
|
||||||
return is_viewer_only;
|
return game_mode > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void game_shutdown() {
|
void game_shutdown() {
|
||||||
world_viewers_destroy();
|
world_viewers_destroy();
|
||||||
|
|
||||||
if (is_viewer_only) {
|
if (game_mode == GAMEKIND_CLIENT) {
|
||||||
network_client_disconnect();
|
network_client_disconnect();
|
||||||
network_destroy();
|
network_destroy();
|
||||||
} else {
|
} else {
|
||||||
|
@ -150,7 +152,7 @@ void game_input() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void game_update() {
|
void game_update() {
|
||||||
if (is_viewer_only) {
|
if (game_mode == GAMEKIND_CLIENT) {
|
||||||
network_client_tick();
|
network_client_tick();
|
||||||
}
|
}
|
||||||
else world_update();
|
else world_update();
|
||||||
|
@ -171,10 +173,9 @@ void game_world_cleanup_entities(void) {
|
||||||
entity_view_tbl *view = &world_viewers[i].entities;
|
entity_view_tbl *view = &world_viewers[i].entities;
|
||||||
|
|
||||||
for (int j = 0; j < zpl_array_count(view->entries); j += 1){
|
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) {
|
if (e->tran_effect == ETRAN_REMOVE) {
|
||||||
entity_view_tbl_remove(view, e->ent_id);
|
entity_view_tbl_remove(view, e->ent_id);
|
||||||
j--;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -75,8 +75,7 @@ int main(int argc, char** argv)
|
||||||
ECS_IMPORT(world_ecs(), General);
|
ECS_IMPORT(world_ecs(), General);
|
||||||
ECS_IMPORT(world_ecs(), Controllers);
|
ECS_IMPORT(world_ecs(), Controllers);
|
||||||
ECS_IMPORT(world_ecs(), Physics);
|
ECS_IMPORT(world_ecs(), Physics);
|
||||||
uint16_t half_world_dim = world_dim() / 2;
|
for (uint32_t i = 0; i < npc_count; i++) {
|
||||||
for (int i = 0; i < npc_count; i++) {
|
|
||||||
uint64_t e = entity_spawn(NULL);
|
uint64_t e = entity_spawn(NULL);
|
||||||
ecs_add(world_ecs(), e, EcsDemoNPC);
|
ecs_add(world_ecs(), e, EcsDemoNPC);
|
||||||
Position *pos = ecs_get_mut(world_ecs(), e, Position, NULL);
|
Position *pos = ecs_get_mut(world_ecs(), e, Position, NULL);
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
#include "prediction.h"
|
#include "prediction.h"
|
||||||
#include "camera.h"
|
#include "camera.h"
|
||||||
#include "math.h"
|
#include "math.h"
|
||||||
|
#include "utils/raylib_helpers.h"
|
||||||
|
|
||||||
uint16_t screenWidth = 1600;
|
uint16_t screenWidth = 1600;
|
||||||
uint16_t screenHeight = 900;
|
uint16_t screenHeight = 900;
|
||||||
|
@ -17,47 +18,6 @@ float zoom_overlay_tran = 0.0f;
|
||||||
|
|
||||||
static Camera2D render_camera;
|
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() {
|
void platform_init() {
|
||||||
InitWindow(screenWidth, screenHeight, "eco2d - client");
|
InitWindow(screenWidth, screenHeight, "eco2d - client");
|
||||||
SetWindowState(FLAG_WINDOW_UNDECORATED|FLAG_WINDOW_MAXIMIZED|FLAG_WINDOW_RESIZABLE);
|
SetWindowState(FLAG_WINDOW_UNDECORATED|FLAG_WINDOW_MAXIMIZED|FLAG_WINDOW_RESIZABLE);
|
||||||
|
|
Loading…
Reference in New Issue