small changes + win32 support

isolation_bkp/dynres
Dominik Madarász 2021-05-03 21:53:28 +02:00
parent a2025bdb35
commit 738b612798
13 changed files with 287 additions and 67 deletions

2
.gitignore vendored
View File

@ -1,4 +1,6 @@
build build
build.bat
work
.vscode .vscode
.ds_store .ds_store

View File

@ -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_DEBUG ${CMAKE_BINARY_DIR})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE ${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) include_directories(code/common code/vendors code/vendors/flecs)
add_subdirectory(code/common) add_subdirectory(code/common)

View File

@ -153,15 +153,15 @@ bool network_client_is_connected() {
return peer ? enet_peer_get_state(peer) == ENET_PEER_STATE_CONNECTED : false; 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); ENetPacket *packet = enet_packet_create(data, datalen, flags);
enet_peer_send(peer, 0, packet); 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); 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); network_msg_send_raw(0, data, datalen, 0);
} }

View File

@ -10,7 +10,7 @@
#include "flecs/flecs.h" #include "flecs/flecs.h"
#include "flecs/flecs_dash.h" #include "flecs/flecs_dash.h"
#include "flecs/flecs_systems_civetweb.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_WORLD_SEED 302097
#define DEFAULT_BLOCK_SIZE 64 /* amount of units within a block (single axis) */ #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(); 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); 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)); IF(world_init(seed, block_size, chunk_size, world_size));

View File

@ -9,6 +9,7 @@
#include "system.h" #include "system.h"
#include "network.h" #include "network.h"
#include "packets/packet.h"
#include "world/world.h" #include "world/world.h"
#include "modules/general.h" #include "modules/general.h"

View File

@ -1,9 +1,10 @@
#include "packet.h" #include "packet.h"
#include "cwpack/cwpack.h"
#define PKT_HEADER_ELEMENTS 2 #define PKT_HEADER_ELEMENTS 2
pkt_handler pkt_handlers[] = { 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) { 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); cw_unpack_next(&uc);
const void *packed_blob = uc.item.as.bin.start; 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->id = pkt_id;
table->data = packed_blob; table->data = packed_blob;

View File

@ -1,16 +1,13 @@
#pragma once #pragma once
#include "system.h" #include "system.h"
#define PKT_HANDLER_PROC(name) int32_t name(pkt_header *header)
typedef PKT_HANDLER_PROC(pkt_handler_proc);
typedef enum { typedef enum {
MSG_ID_01_WELCOME, MSG_ID_01_WELCOME,
MSG_ID_LIBRG_UPDATE, MSG_ID_LIBRG_UPDATE,
MSG_ID_FORCE_UINT16 = UINT16_MAX, MSG_ID_FORCE_UINT16 = UINT16_MAX,
} pkt_messages; } pkt_messages;
typedef struct { typedef struct pkt_header {
uint16_t id; uint16_t id;
uint16_t sender; uint16_t sender;
uint8_t *data; uint8_t *data;
@ -18,6 +15,9 @@ typedef struct {
int8_t ok; int8_t ok;
} pkt_header; } pkt_header;
#define PKT_HANDLER_PROC(name) int32_t name(pkt_header *header)
typedef PKT_HANDLER_PROC(pkt_handler_proc);
typedef struct { typedef struct {
uint16_t id; uint16_t id;
pkt_handler_proc *handler; pkt_handler_proc *handler;

View File

@ -1,56 +1,57 @@
// #include "packet.h" // #include "packet.h"
// PACKET_GENERATE_ENCODE(1, 2, ) // PACKET_GENERATE_ENCODE(1, 2, )
//
#include "packet.h" //#include "packet.h"
#include "cwpack/cwpack.h" //#include "cwpack/cwpack.h"
//
#define PKT_01_WELCOME_ID 1 //#define PKT_01_WELCOME_ID 1
#define PKT_01_WELCOME_ARGS 2 //#define PKT_01_WELCOME_ARGS 2
//
//
size_t pkt_01_welcome_encode(pkt_01_welcome *table) { //size_t pkt_01_welcome_encode(pkt_01_welcome *table) {
cw_pack_context pc = {0}; //cw_pack_context pc = {0};
cw_pack_context_init(&pc, buffer, 20, 0); //cw_pack_context_init(&pc, buffer, 20, 0);
cw_pack_array_size(&pc, 1 + PKT_01_WELCOME_ARGS); //cw_pack_array_size(&pc, 1 + PKT_01_WELCOME_ARGS);
cw_pack_signed(&pc, PKT_01_WELCOME_ID); //cw_pack_signed(&pc, PKT_01_WELCOME_ID);
//
cw_pack_unsigned(&pc, chunk_size); //cw_pack_unsigned(&pc, chunk_size);
cw_pack_unsigned(&pc, chunk_amount); //cw_pack_unsigned(&pc, chunk_amount);
//
return pc.current - pc.start; /* length */ //return pc.current - pc.start; /* length */
} //}
//
int32_t pkt_01_welcome_decode(pkt_01_welcome *table, pkt_header *header) { //int32_t pkt_01_welcome_decode(pkt_01_welcome *table, pkt_header *header) {
cw_unpack_context uc = {0}; //cw_unpack_context uc = {0};
cw_unpack_context_init(&uc, header->data, header->datalen, 0); //cw_unpack_context_init(&uc, header->data, header->datalen, 0);
//
cw_unpack_next(&uc); //cw_unpack_next(&uc);
if (uc.item.type != CWP_ITEM_ARRAY || uc.item.as.array.size != PKT_01_WELCOME_ARGS) { //if (uc.item.type != CWP_ITEM_ARRAY || uc.item.as.array.size != PKT_01_WELCOME_ARGS) {
return -1;//todo: error //return -1;//todo: error
} //}
//
cw_unpack_next(&uc); //cw_unpack_next(&uc);
if (uc.item.type != CWP_ITEM_POSITIVE_INTEGER) return -1; // expected chunk size //if (uc.item.type != CWP_ITEM_POSITIVE_INTEGER) return -1; // expected chunk size
table->chunk_size = uc.item.as.u64; //table->chunk_size = uc.item.as.u64;
//
cw_unpack_next(&uc); //cw_unpack_next(&uc);
if (uc.item.type != CWP_ITEM_POSITIVE_INTEGER) return -1; // expected chunk amount //if (uc.item.type != CWP_ITEM_POSITIVE_INTEGER) return -1; // expected chunk amount
table->chunk_amount = uc.item.as.u64; //table->chunk_amount = uc.item.as.u64;
//
if (uc.return_code != CWP_RC_OK) return -1; // unpacking failed somwwhere //if (uc.return_code != CWP_RC_OK) return -1; // unpacking failed somwwhere
cw_unpack_next(&uc); //cw_unpack_next(&uc);
if (uc.return_code != CWP_RC_END_OF_INPUT) return -1; // not finished yet but should be? //if (uc.return_code != CWP_RC_END_OF_INPUT) return -1; // not finished yet but should be?
//
return 0; //return 0;
} //}
//
int32_t pkt_01_handler(pkt_header *header) { //int32_t pkt_01_handler(pkt_header *header) {
pkt_01_welcome table; //pkt_01_welcome table;
pkt_01_welcome_decode(&table, header); //pkt_01_welcome_decode(&table, header);
//
zpl_printf("we received: chunk_size: %d and chunk_amount: %d\n", table.chunk_size, table.chunk_amount); //zpl_printf("we received: chunk_size: %d and chunk_amount: %d\n", table.chunk_size, table.chunk_amount);
//
// do STUFF // do STUFF
return 0; //return 0;
} //}
//

View File

@ -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}) add_library(flecs-bundle STATIC ${SRCS})

View File

@ -0,0 +1,133 @@
#include "flecs_os_api_stdcpp.h"
#include <thread>
#include <mutex>
#include <condition_variable>
#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<ecs_os_thread_t>(thread);
}
static
void* stdcpp_thread_join(
ecs_os_thread_t thread)
{
void *arg = nullptr;
std::thread *thr = reinterpret_cast<std::thread*>(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<ecs_os_mutex_t>(mutex);
}
static
void stdcpp_mutex_free(ecs_os_mutex_t m) {
std::mutex*mutex = reinterpret_cast<std::mutex*>(m);
delete mutex;
}
static
void stdcpp_mutex_lock(ecs_os_mutex_t m) {
std::mutex*mutex = reinterpret_cast<std::mutex*>(m);
mutex->lock();
}
static
void stdcpp_mutex_unlock(ecs_os_mutex_t m) {
std::mutex *mutex = reinterpret_cast<std::mutex*>(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<std::condition_variable_any*>(c);
delete cond;
}
static
void stdcpp_cond_signal(ecs_os_cond_t c) {
std::condition_variable_any *cond = reinterpret_cast<std::condition_variable_any*>(c);
cond->notify_one();
}
static
void stdcpp_cond_broadcast(ecs_os_cond_t c) {
std::condition_variable_any*cond = reinterpret_cast<std::condition_variable_any*>(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<std::condition_variable_any*>(c);
std::mutex* mutex = reinterpret_cast<std::mutex*>(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

View File

@ -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

View File

@ -92,7 +92,7 @@ bool endpoint_filter(
EcsHttpRequest *request, EcsHttpRequest *request,
EcsHttpReply *reply) EcsHttpReply *reply)
{ {
ecs_filter_t filter = { }; ecs_filter_t filter = { 0 };
ecs_type_t select = NULL; ecs_type_t select = NULL;
if (!parse_filter(world, request, &filter)) { if (!parse_filter(world, request, &filter)) {
@ -119,7 +119,7 @@ bool endpoint_scope(
EcsHttpReply *reply) EcsHttpReply *reply)
{ {
ecs_entity_t e = 0; ecs_entity_t e = 0;
ecs_filter_t filter = { }; ecs_filter_t filter = { 0 };
ecs_type_t select = NULL; ecs_type_t select = NULL;
if (!parse_entity(world, request, &e)) { if (!parse_entity(world, request, &e)) {
@ -263,7 +263,7 @@ bool endpoint_browse(
EcsHttpRequest *request, EcsHttpRequest *request,
EcsHttpReply *reply) EcsHttpReply *reply)
{ {
ecs_filter_t filter = { }; ecs_filter_t filter = { 0 };
ecs_entity_t e; ecs_entity_t e;
if (!parse_entity(world, request, &e)) { if (!parse_entity(world, request, &e)) {

52
project.4coder 100644
View File

@ -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";