many improvements for the upcoming networking support

isolation_bkp/dynres
Dominik Madarász 2021-09-08 14:13:31 +02:00
parent 664aa09654
commit 096bf8c706
5 changed files with 54 additions and 21 deletions

View File

@ -116,14 +116,21 @@ float game_time() {
void game_init(game_kind play_mode, uint32_t num_viewers, int32_t seed, uint16_t chunk_size, uint16_t chunk_amount, int8_t is_dash_enabled) {
game_mode = play_mode;
if (game_mode != GAMEKIND_HEADLESS) {
platform_init();
world_viewers_init(num_viewers);
active_viewer = &world_viewers[0];
camera_reset();
}
if (game_mode != GAMEKIND_SINGLE) {
network_init();
}
if (game_mode == GAMEKIND_CLIENT) {
world_setup_pkt_handlers(pkt_reader, mp_pkt_writer);
network_init();
network_client_connect("127.0.0.1", 27000);
} else {
stdcpp_set_os_api();
@ -133,33 +140,48 @@ void game_init(game_kind play_mode, uint32_t num_viewers, int32_t seed, uint16_t
//ecs_set_target_fps(world_ecs(), 60);
}
if (game_mode != GAMEKIND_HEADLESS) {
for (uint32_t i = 0; i < num_viewers; i++) {
pkt_00_init_send(i);
}
}
}
int8_t game_is_networked() {
return game_mode != GAMEKIND_SINGLE;
}
void game_shutdown() {
world_viewers_destroy();
if (game_mode == GAMEKIND_CLIENT) {
network_client_disconnect();
network_destroy();
} else {
world_destroy();
}
if (game_mode != GAMEKIND_SINGLE) {
network_destroy();
}
if (game_mode != GAMEKIND_HEADLESS) {
world_viewers_destroy();
platform_shutdown();
}
}
uint8_t game_is_running() {
return platform_is_running();
return game_mode == GAMEKIND_HEADLESS || platform_is_running();
}
game_kind game_get_kind(void) {
return game_mode;
}
void game_input() {
if (game_mode != GAMEKIND_HEADLESS) {
platform_input();
}
}
void game_update() {
if (game_mode == GAMEKIND_CLIENT) {
@ -167,12 +189,16 @@ void game_update() {
}
else world_update();
if (game_mode != GAMEKIND_HEADLESS) {
game_world_cleanup_entities();
}
}
void game_render() {
if (game_mode != GAMEKIND_HEADLESS) {
platform_render();
}
}
void game_action_send_keystate(float x,
float y,

View File

@ -15,6 +15,7 @@ void game_request_close();
uint8_t game_is_running();
int8_t game_is_networked();
float game_time();
game_kind game_get_kind(void);
//~ NOTE(zaklaus): game events
void game_input();

View File

@ -26,6 +26,7 @@ int main(int argc, char** argv) {
zpl_opts_add(&opts, "?", "help", "the HELP section", ZPL_OPTS_FLAG);
zpl_opts_add(&opts, "v", "viewer-only", "run viewer-only client", ZPL_OPTS_FLAG);
zpl_opts_add(&opts, "d", "server-only", "run dedicated server", ZPL_OPTS_FLAG);
zpl_opts_add(&opts, "c", "viewer-count", "number of viewers (detachable clients)", ZPL_OPTS_INT);
zpl_opts_add(&opts, "p", "preview-map", "draw world preview", ZPL_OPTS_FLAG);
zpl_opts_add(&opts, "ed", "enable-dash", "enables flecs dash", ZPL_OPTS_FLAG);
@ -43,12 +44,18 @@ int main(int argc, char** argv) {
}
int8_t is_viewer_only = zpl_opts_has_arg(&opts, "viewer-only");
int8_t is_server_only = zpl_opts_has_arg(&opts, "server-only");
int8_t is_dash_enabled = zpl_opts_has_arg(&opts, "enable-dash");
int32_t seed = zpl_opts_integer(&opts, "seed", DEFAULT_WORLD_SEED);
uint16_t num_viewers = zpl_opts_integer(&opts, "viewer-count", 1);
uint16_t chunk_size = DEFAULT_CHUNK_SIZE; //zpl_opts_integer(&opts, "chunk-size", DEFAULT_CHUNK_SIZE);
uint16_t world_size = zpl_opts_integer(&opts, "world-size", DEFAULT_WORLD_SIZE);
game_kind play_mode = GAMEKIND_SINGLE;
if (is_viewer_only) play_mode = GAMEKIND_CLIENT;
if (is_server_only) play_mode = GAMEKIND_HEADLESS;
if (zpl_opts_has_arg(&opts, "random-seed")) {
zpl_random rnd={0};
zpl_random_init(&rnd);
@ -62,7 +69,7 @@ int main(int argc, char** argv) {
}
sighandler_register();
game_init(is_viewer_only, num_viewers, seed, chunk_size, world_size, is_dash_enabled);
game_init(play_mode, num_viewers, seed, chunk_size, world_size, is_dash_enabled);
while (game_is_running()) {
profile (PROF_MAIN_LOOP) {

View File

@ -27,11 +27,10 @@ static bool request_shutdown;
void platform_init() {
InitWindow(screenWidth, screenHeight, "eco2d");
SetWindowState(FLAG_WINDOW_UNDECORATED|FLAG_WINDOW_MAXIMIZED|FLAG_WINDOW_RESIZABLE|FLAG_MSAA_4X_HINT);
SetTargetFPS(60);
SetTargetFPS(60);
screenWidth = GetScreenWidth();
screenHeight = GetScreenHeight();
renderer_init();
}
@ -136,8 +135,8 @@ void platform_render() {
// NOTE(zaklaus): add-ins
inventory_draw();
}
debug_draw();
display_conn_status();
debug_draw();
}
EndDrawing();

View File

@ -1,6 +1,6 @@
#include <signal.h>
#include "zpl.h"
#include "platform.h"
#include "game.h"
#ifdef ZPL_SYSTEM_WINDOWS
#include <Windows.h>
@ -10,13 +10,13 @@ static BOOL WINAPI _sighandler_win32_control_handler(DWORD control_type)
{
case CTRL_C_EVENT:
case DBG_CONTROL_C:
platform_shutdown();
game_request_close();
return 0;
case CTRL_CLOSE_EVENT:
case CTRL_LOGOFF_EVENT:
case CTRL_BREAK_EVENT:
case CTRL_SHUTDOWN_EVENT:
platform_shutdown();
game_request_close();
return 1;
}
@ -25,7 +25,7 @@ static BOOL WINAPI _sighandler_win32_control_handler(DWORD control_type)
#else //POSIX complaint
#include <sys/types.h>
static void _sighandler_posix_signal_handler(int sig) {
platform_shutdown();
game_request_close();
}
#endif