diff --git a/.gitignore b/.gitignore index a3946d5..b00e655 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ build +build.bat +work .vscode .ds_store diff --git a/CMakeLists.txt b/CMakeLists.txt index 1e4b174..d41ede5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,6 +8,10 @@ set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/) set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_DEBUG ${CMAKE_BINARY_DIR}) set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE ${CMAKE_BINARY_DIR}) +if(MSVC) + add_definitions(-D_CRT_SECURE_NO_WARNINGS) +endif() + include_directories(code/common code/vendors code/vendors/flecs) add_subdirectory(code/common) diff --git a/code/apps/client/source/network.c b/code/apps/client/source/network.c index 29c380b..59058f5 100644 --- a/code/apps/client/source/network.c +++ b/code/apps/client/source/network.c @@ -153,15 +153,15 @@ bool network_client_is_connected() { return peer ? enet_peer_get_state(peer) == ENET_PEER_STATE_CONNECTED : false; } -static int32_t network_msg_send_raw(uint16_t /* peer_id */, void *data, size_t datalen, uint32_t flags) { +static int32_t network_msg_send_raw(uint16_t peer_id, void *data, size_t datalen, uint32_t flags) { ENetPacket *packet = enet_packet_create(data, datalen, flags); enet_peer_send(peer, 0, packet); } -int32_t network_msg_send(uint16_t /* peer_id */, void *data, size_t datalen) { +int32_t network_msg_send(uint16_t peer_id, void *data, size_t datalen) { network_msg_send_raw(0, data, datalen, ENET_PACKET_FLAG_RELIABLE); } -int32_t network_msg_send_unreliable(uint16_t /* peer_id */, void *data, size_t datalen) { +int32_t network_msg_send_unreliable(uint16_t peer_id, void *data, size_t datalen) { network_msg_send_raw(0, data, datalen, 0); } diff --git a/code/apps/server/source/main.c b/code/apps/server/source/main.c index 831b828..4c96890 100644 --- a/code/apps/server/source/main.c +++ b/code/apps/server/source/main.c @@ -10,7 +10,7 @@ #include "flecs/flecs.h" #include "flecs/flecs_dash.h" #include "flecs/flecs_systems_civetweb.h" -#include "flecs/flecs_os_api_posix.h" +#include "flecs/flecs_os_api_stdcpp.h" #define DEFAULT_WORLD_SEED 302097 #define DEFAULT_BLOCK_SIZE 64 /* amount of units within a block (single axis) */ @@ -64,7 +64,7 @@ int main(int argc, char** argv) { } sighandler_register(); - posix_set_os_api(); + stdcpp_set_os_api(); zpl_printf("[INFO] Generating world of size: %d x %d\n", world_size, world_size); IF(world_init(seed, block_size, chunk_size, world_size)); diff --git a/code/apps/server/source/network.c b/code/apps/server/source/network.c index e3a56d8..07c624c 100644 --- a/code/apps/server/source/network.c +++ b/code/apps/server/source/network.c @@ -9,6 +9,7 @@ #include "system.h" #include "network.h" +#include "packets/packet.h" #include "world/world.h" #include "modules/general.h" diff --git a/code/common/packets/packet.c b/code/common/packets/packet.c index 2af9cca..609503f 100644 --- a/code/common/packets/packet.c +++ b/code/common/packets/packet.c @@ -1,9 +1,10 @@ #include "packet.h" +#include "cwpack/cwpack.h" #define PKT_HEADER_ELEMENTS 2 pkt_handler pkt_handlers[] = { - {.id = MSG_ID_01_WELCOME, .handler = pkt_01_welcome_handler}, + {.id = MSG_ID_01_WELCOME, .handler = /*pkt_01_welcome_handler*/ NULL}, }; int32_t pkt_header_encode(pkt_header *table) { @@ -28,7 +29,7 @@ int32_t pkt_header_decode(pkt_header *table, void *data, size_t datalen) { cw_unpack_next(&uc); const void *packed_blob = uc.item.as.bin.start; - uin32_t packed_size = uc.item.as.bind.length; + uint32_t packed_size = uc.item.as.bin.length; table->id = pkt_id; table->data = packed_blob; diff --git a/code/common/packets/packet.h b/code/common/packets/packet.h index 35ba9bb..0b0f1dc 100644 --- a/code/common/packets/packet.h +++ b/code/common/packets/packet.h @@ -1,16 +1,13 @@ #pragma once #include "system.h" -#define PKT_HANDLER_PROC(name) int32_t name(pkt_header *header) -typedef PKT_HANDLER_PROC(pkt_handler_proc); - typedef enum { MSG_ID_01_WELCOME, MSG_ID_LIBRG_UPDATE, MSG_ID_FORCE_UINT16 = UINT16_MAX, } pkt_messages; -typedef struct { +typedef struct pkt_header { uint16_t id; uint16_t sender; uint8_t *data; @@ -18,6 +15,9 @@ typedef struct { int8_t ok; } pkt_header; +#define PKT_HANDLER_PROC(name) int32_t name(pkt_header *header) +typedef PKT_HANDLER_PROC(pkt_handler_proc); + typedef struct { uint16_t id; pkt_handler_proc *handler; diff --git a/code/common/packets/pkt_01_welcome.c b/code/common/packets/pkt_01_welcome.c index a15b6ea..72b9752 100644 --- a/code/common/packets/pkt_01_welcome.c +++ b/code/common/packets/pkt_01_welcome.c @@ -1,56 +1,57 @@ // #include "packet.h" // PACKET_GENERATE_ENCODE(1, 2, ) - -#include "packet.h" -#include "cwpack/cwpack.h" - -#define PKT_01_WELCOME_ID 1 -#define PKT_01_WELCOME_ARGS 2 - - -size_t pkt_01_welcome_encode(pkt_01_welcome *table) { - cw_pack_context pc = {0}; - cw_pack_context_init(&pc, buffer, 20, 0); - cw_pack_array_size(&pc, 1 + PKT_01_WELCOME_ARGS); - cw_pack_signed(&pc, PKT_01_WELCOME_ID); - - cw_pack_unsigned(&pc, chunk_size); - cw_pack_unsigned(&pc, chunk_amount); - - return pc.current - pc.start; /* length */ -} - -int32_t pkt_01_welcome_decode(pkt_01_welcome *table, pkt_header *header) { - cw_unpack_context uc = {0}; - cw_unpack_context_init(&uc, header->data, header->datalen, 0); - - cw_unpack_next(&uc); - if (uc.item.type != CWP_ITEM_ARRAY || uc.item.as.array.size != PKT_01_WELCOME_ARGS) { - return -1;//todo: error - } - - cw_unpack_next(&uc); - if (uc.item.type != CWP_ITEM_POSITIVE_INTEGER) return -1; // expected chunk size - table->chunk_size = uc.item.as.u64; - - cw_unpack_next(&uc); - if (uc.item.type != CWP_ITEM_POSITIVE_INTEGER) return -1; // expected chunk amount - table->chunk_amount = uc.item.as.u64; - - if (uc.return_code != CWP_RC_OK) return -1; // unpacking failed somwwhere - cw_unpack_next(&uc); - if (uc.return_code != CWP_RC_END_OF_INPUT) return -1; // not finished yet but should be? - - return 0; -} - -int32_t pkt_01_handler(pkt_header *header) { - pkt_01_welcome table; - pkt_01_welcome_decode(&table, header); - - zpl_printf("we received: chunk_size: %d and chunk_amount: %d\n", table.chunk_size, table.chunk_amount); - +// +//#include "packet.h" +//#include "cwpack/cwpack.h" +// +//#define PKT_01_WELCOME_ID 1 +//#define PKT_01_WELCOME_ARGS 2 +// +// +//size_t pkt_01_welcome_encode(pkt_01_welcome *table) { + //cw_pack_context pc = {0}; + //cw_pack_context_init(&pc, buffer, 20, 0); + //cw_pack_array_size(&pc, 1 + PKT_01_WELCOME_ARGS); + //cw_pack_signed(&pc, PKT_01_WELCOME_ID); +// + //cw_pack_unsigned(&pc, chunk_size); + //cw_pack_unsigned(&pc, chunk_amount); +// + //return pc.current - pc.start; /* length */ +//} +// +//int32_t pkt_01_welcome_decode(pkt_01_welcome *table, pkt_header *header) { + //cw_unpack_context uc = {0}; + //cw_unpack_context_init(&uc, header->data, header->datalen, 0); +// + //cw_unpack_next(&uc); + //if (uc.item.type != CWP_ITEM_ARRAY || uc.item.as.array.size != PKT_01_WELCOME_ARGS) { + //return -1;//todo: error + //} +// + //cw_unpack_next(&uc); + //if (uc.item.type != CWP_ITEM_POSITIVE_INTEGER) return -1; // expected chunk size + //table->chunk_size = uc.item.as.u64; +// + //cw_unpack_next(&uc); + //if (uc.item.type != CWP_ITEM_POSITIVE_INTEGER) return -1; // expected chunk amount + //table->chunk_amount = uc.item.as.u64; +// + //if (uc.return_code != CWP_RC_OK) return -1; // unpacking failed somwwhere + //cw_unpack_next(&uc); + //if (uc.return_code != CWP_RC_END_OF_INPUT) return -1; // not finished yet but should be? +// + //return 0; +//} +// +//int32_t pkt_01_handler(pkt_header *header) { + //pkt_01_welcome table; + //pkt_01_welcome_decode(&table, header); +// + //zpl_printf("we received: chunk_size: %d and chunk_amount: %d\n", table.chunk_size, table.chunk_amount); +// // do STUFF - return 0; -} + //return 0; +//} +// \ No newline at end of file diff --git a/code/vendors/flecs/CMakeLists.txt b/code/vendors/flecs/CMakeLists.txt index 5540a42..6190804 100644 --- a/code/vendors/flecs/CMakeLists.txt +++ b/code/vendors/flecs/CMakeLists.txt @@ -1,2 +1,14 @@ -file(GLOB SRCS *.c *.h) +file(GLOB SRCS *.h + flecs.c + flecs_json.c + flecs_meta.c + flecs_rest.c + flecs_player.c + flecs_rest.c + flecs_systems_civetweb.c + flecs_dash.c + flecs_monitor.c + flecs-os_api-stdcpp.cpp + flecs_components_http.c +) add_library(flecs-bundle STATIC ${SRCS}) diff --git a/code/vendors/flecs/flecs-os_api-stdcpp.cpp b/code/vendors/flecs/flecs-os_api-stdcpp.cpp new file mode 100644 index 0000000..4445f47 --- /dev/null +++ b/code/vendors/flecs/flecs-os_api-stdcpp.cpp @@ -0,0 +1,133 @@ +#include "flecs_os_api_stdcpp.h" +#include +#include +#include + +#include "flecs/flecs.h" + +static +ecs_os_thread_t stdcpp_thread_new( + ecs_os_thread_callback_t callback, + void *arg) +{ + std::thread *thread = new std::thread{callback,arg}; + return reinterpret_cast(thread); +} + +static +void* stdcpp_thread_join( + ecs_os_thread_t thread) +{ + void *arg = nullptr; + std::thread *thr = reinterpret_cast(thread); + thr->join(); + delete thr; + return arg; +} + +static +int32_t stdcpp_ainc(int32_t *count) { + int value; +#ifdef __GNUC__ + value = __sync_add_and_fetch (count, 1); + return value; +#else + /* Unsupported */ + abort(); +#endif +} + +static +int32_t stdcpp_adec(int32_t *count) { + int value; +#ifdef __GNUC__ + value = __sync_sub_and_fetch (count, 1); + return value; +#else + /* Unsupported */ + abort(); +#endif +} + +static +ecs_os_mutex_t stdcpp_mutex_new(void) { + std::mutex *mutex = new std::mutex; + return reinterpret_cast(mutex); +} + +static +void stdcpp_mutex_free(ecs_os_mutex_t m) { + std::mutex*mutex = reinterpret_cast(m); + delete mutex; +} + +static +void stdcpp_mutex_lock(ecs_os_mutex_t m) { + std::mutex*mutex = reinterpret_cast(m); + mutex->lock(); +} + +static +void stdcpp_mutex_unlock(ecs_os_mutex_t m) { + std::mutex *mutex = reinterpret_cast(m); + mutex->unlock(); +} + +static +ecs_os_cond_t stdcpp_cond_new(void) { + std::condition_variable_any* cond = new std::condition_variable_any{}; + return (ecs_os_cond_t)cond; +} + +static +void stdcpp_cond_free(ecs_os_cond_t c) { + std::condition_variable_any *cond = reinterpret_cast(c); + delete cond; +} + +static +void stdcpp_cond_signal(ecs_os_cond_t c) { + std::condition_variable_any *cond = reinterpret_cast(c); + cond->notify_one(); +} + +static +void stdcpp_cond_broadcast(ecs_os_cond_t c) { + std::condition_variable_any*cond = reinterpret_cast(c); + cond->notify_all(); +} + +static +void stdcpp_cond_wait(ecs_os_cond_t c, ecs_os_mutex_t m) { + std::condition_variable_any* cond = reinterpret_cast(c); + std::mutex* mutex = reinterpret_cast(m); + cond->wait(*mutex); +} + +#ifdef __cplusplus +extern "C" { +#endif + void stdcpp_set_os_api(void) { + ecs_os_set_api_defaults(); + + ecs_os_api_t api = ecs_os_api; + + api.thread_new_ = stdcpp_thread_new; + api.thread_join_ = stdcpp_thread_join; + api.ainc_ = stdcpp_ainc; + api.adec_ = stdcpp_adec; + api.mutex_new_ = stdcpp_mutex_new; + api.mutex_free_ = stdcpp_mutex_free; + api.mutex_lock_ = stdcpp_mutex_lock; + api.mutex_unlock_ = stdcpp_mutex_unlock; + api.cond_new_ = stdcpp_cond_new; + api.cond_free_ = stdcpp_cond_free; + api.cond_signal_ = stdcpp_cond_signal; + api.cond_broadcast_ = stdcpp_cond_broadcast; + api.cond_wait_ = stdcpp_cond_wait; + + ecs_os_set_api(&api); + } +#ifdef __cplusplus +} +#endif diff --git a/code/vendors/flecs/flecs_os_api_stdcpp.h b/code/vendors/flecs/flecs_os_api_stdcpp.h new file mode 100644 index 0000000..a411b28 --- /dev/null +++ b/code/vendors/flecs/flecs_os_api_stdcpp.h @@ -0,0 +1,14 @@ +#ifndef FLECS_OS_API_STDCPP_H +#define FLECS_OS_API_STDCPP_H + +#ifdef __cplusplus +extern "C" { +#endif + +extern void stdcpp_set_os_api(void); + +#ifdef __cplusplus +} +#endif + +#endif \ No newline at end of file diff --git a/code/vendors/flecs/flecs_rest.c b/code/vendors/flecs/flecs_rest.c index cd048de..c92d9e6 100644 --- a/code/vendors/flecs/flecs_rest.c +++ b/code/vendors/flecs/flecs_rest.c @@ -92,7 +92,7 @@ bool endpoint_filter( EcsHttpRequest *request, EcsHttpReply *reply) { - ecs_filter_t filter = { }; + ecs_filter_t filter = { 0 }; ecs_type_t select = NULL; if (!parse_filter(world, request, &filter)) { @@ -119,7 +119,7 @@ bool endpoint_scope( EcsHttpReply *reply) { ecs_entity_t e = 0; - ecs_filter_t filter = { }; + ecs_filter_t filter = { 0 }; ecs_type_t select = NULL; if (!parse_entity(world, request, &e)) { @@ -263,7 +263,7 @@ bool endpoint_browse( EcsHttpRequest *request, EcsHttpReply *reply) { - ecs_filter_t filter = { }; + ecs_filter_t filter = { 0 }; ecs_entity_t e; if (!parse_entity(world, request, &e)) { diff --git a/project.4coder b/project.4coder new file mode 100644 index 0000000..9ef5500 --- /dev/null +++ b/project.4coder @@ -0,0 +1,52 @@ +version(1); + +project_name = "eco2d"; + +patterns = +{ + "*.c", + "*.cpp", + "*.jai", + "*.odin", + "*.zig", + "*.h", + "*.inc", + "*.bat", + "*.sh", + "*.4coder", + "*.txt", +}; + +blacklist_patterns = +{ + ".*", + "build/.*", +}; + +load_paths = +{ + { + { {"."}, .recursive = false, .relative = true }, .os = "win" + }, + { + { {"code"}, .recursive = true, .relative = true }, .os = "win" + }, +}; + +command_list = +{ + { + .name = "build", + .out = "*compilation*", + .footer_panel = true, + .save_dirty_files = true, + .cursor_at_end = false, + .cmd = + { + { "cmake --build build", .os = "win" }, + }, + }, + +}; + +fkey_command[1] = "build";