many improvements for the upcoming networking support
parent
664aa09654
commit
096bf8c706
|
@ -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;
|
||||
platform_init();
|
||||
world_viewers_init(num_viewers);
|
||||
active_viewer = &world_viewers[0];
|
||||
camera_reset();
|
||||
|
||||
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,8 +140,10 @@ void game_init(game_kind play_mode, uint32_t num_viewers, int32_t seed, uint16_t
|
|||
//ecs_set_target_fps(world_ecs(), 60);
|
||||
}
|
||||
|
||||
for (uint32_t i = 0; i < num_viewers; i++) {
|
||||
pkt_00_init_send(i);
|
||||
if (game_mode != GAMEKIND_HEADLESS) {
|
||||
for (uint32_t i = 0; i < num_viewers; i++) {
|
||||
pkt_00_init_send(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -143,22 +152,35 @@ int8_t game_is_networked() {
|
|||
}
|
||||
|
||||
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() {
|
||||
platform_input();
|
||||
if (game_mode != GAMEKIND_HEADLESS) {
|
||||
platform_input();
|
||||
}
|
||||
}
|
||||
|
||||
void game_update() {
|
||||
|
@ -167,11 +189,15 @@ void game_update() {
|
|||
}
|
||||
else world_update();
|
||||
|
||||
game_world_cleanup_entities();
|
||||
if (game_mode != GAMEKIND_HEADLESS) {
|
||||
game_world_cleanup_entities();
|
||||
}
|
||||
}
|
||||
|
||||
void game_render() {
|
||||
platform_render();
|
||||
if (game_mode != GAMEKIND_HEADLESS) {
|
||||
platform_render();
|
||||
}
|
||||
}
|
||||
|
||||
void game_action_send_keystate(float x,
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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();
|
||||
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in New Issue