introduce game api layer

isolation_bkp/dynres
Dominik Madarász 2021-05-04 21:45:51 +02:00
parent e85b15d5f1
commit 5d824bbd27
10 changed files with 128 additions and 43 deletions

View File

@ -6,7 +6,6 @@ add_library(client-common STATIC
source/main.c
header/network.h
header/game.h
)
add_executable(eco2d-client

View File

@ -63,6 +63,10 @@ void game_init(int8_t play_mode, int32_t seed, uint16_t block_size, uint16_t chu
}
}
int8_t game_is_networked() {
return is_networked_play;
}
void game_shutdown() {
if (is_networked_play) {
network_client_disconnect();

View File

@ -45,8 +45,7 @@ int main(int argc, char** argv)
sighandler_register();
game_init(is_networked_play, seed, block_size, chunk_size, world_size);
while (game_is_running())
{
while (game_is_running()) {
game_input();
game_update();
game_render();

View File

@ -1,5 +1,6 @@
add_executable(playground
source/main.c
source/game.c
)
include_directories(header ../modules ../common)

View File

@ -0,0 +1,38 @@
#include "game.h"
#include "platform.h"
#include "world/world.h"
#include "packets/packet.h"
#include "signal_handling.h"
#include "flecs/flecs.h"
#include "flecs/flecs_dash.h"
#include "flecs/flecs_systems_civetweb.h"
#include "flecs/flecs_os_api_stdcpp.h"
void game_init(int8_t play_mode, int32_t seed, uint16_t block_size, uint16_t chunk_size, uint16_t world_size) {
}
int8_t game_is_networked() {
return 1;
}
void game_shutdown() {
world_destroy();
}
uint8_t game_is_running() {
return 1;
}
void game_input() {
}
void game_update() {
world_update();
}
void game_render() {
}

View File

@ -1,6 +1,7 @@
add_executable(eco2d-server
source/main.c
source/network.c
source/game.c
source/utils/options.c
header/network.h

View File

@ -0,0 +1,73 @@
#include "game.h"
#include "platform.h"
#include "world/world.h"
#include "packets/packet.h"
#include "signal_handling.h"
#include "network.h"
#include "flecs/flecs.h"
#include "flecs/flecs_dash.h"
#include "flecs/flecs_systems_civetweb.h"
#include "flecs/flecs_os_api_stdcpp.h"
static WORLD_PKT_READER(mp_pkt_reader) {
pkt_header header = {0};
uint32_t ok = pkt_header_decode(&header, data, datalen);
if (ok && header.ok) {
return pkt_handlers[header.id].handler(&header) >= 0;
} else {
zpl_printf("[warn] unknown packet id %d (header %d data %d)\n", header.id, ok, header.ok);
}
return -1;
}
void game_init(int8_t play_mode, int32_t seed, uint16_t block_size, uint16_t chunk_size, uint16_t world_size) {
sighandler_register();
stdcpp_set_os_api();
zpl_printf("[INFO] Generating world of size: %d x %d\n", world_size, world_size);
world_init(seed, block_size, chunk_size, world_size, mp_pkt_reader, mp_pkt_writer);
/* server dashboard */
{
ECS_IMPORT(world_ecs(), FlecsDash);
ECS_IMPORT(world_ecs(), FlecsSystemsCivetweb);
ecs_set(world_ecs(), 0, EcsDashServer, {.port = 27001});
ecs_set_target_fps(world_ecs(), 60);
}
zpl_printf("[INFO] Initializing network...\n");
network_init();
network_server_start("0.0.0.0", 27000);
}
int8_t game_is_networked() {
return 1;
}
void game_shutdown() {
network_server_stop();
network_destroy();
world_destroy();
sighandler_unregister();
zpl_printf("Bye!\n");
}
uint8_t game_is_running() {
return 1;
}
void game_input() {
}
void game_update() {
network_server_tick();
world_update();
}
void game_render() {
}

View File

@ -24,18 +24,6 @@
} \
} while (0)
static WORLD_PKT_READER(mp_pkt_reader) {
pkt_header header = {0};
uint32_t ok = pkt_header_decode(&header, data, datalen);
if (ok && header.ok) {
return pkt_handlers[header.id].handler(&header) >= 0;
} else {
zpl_printf("[warn] unknown packet id %d (header %d data %d)\n", header.id, ok, header.ok);
}
return -1;
}
zpl_global zpl_b32 is_running = true;
int main(int argc, char** argv) {
@ -74,36 +62,14 @@ int main(int argc, char** argv) {
generate_minimap(seed, block_size, chunk_size, world_size);
return 0;
}
sighandler_register();
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, mp_pkt_reader, mp_pkt_writer));
/* server dashboard */
{
ECS_IMPORT(world_ecs(), FlecsDash);
ECS_IMPORT(world_ecs(), FlecsSystemsCivetweb);
ecs_set(world_ecs(), 0, EcsDashServer, {.port = 27001});
ecs_set_target_fps(world_ecs(), 60);
}
zpl_printf("[INFO] Initializing network...\n");
IF(network_init());
IF(network_server_start("0.0.0.0", 27000));
game_init(1, seed, block_size, chunk_size, world_size);
while (is_running) {
network_server_tick();
world_update();
game_update();
}
IF(network_server_stop());
IF(network_destroy());
IF(world_destroy());
sighandler_unregister();
zpl_printf("Bye!\n");
game_shutdown();
return 0;
}

View File

@ -4,6 +4,7 @@
void game_init(int8_t play_mode, int32_t seed, uint16_t block_size, uint16_t chunk_size, uint16_t world_size);
void game_shutdown();
uint8_t game_is_running();
int8_t game_is_networked();
void game_input();
void game_update();

View File

@ -1,5 +1,6 @@
#include "pkt_01_welcome.h"
#include "world.h"
#include "game.h"
pkt_desc pkt_01_welcome_desc[] = {
{ PKT_FIELD(CWP_ITEM_POSITIVE_INTEGER, pkt_01_welcome, block_size) },
@ -21,6 +22,8 @@ int32_t pkt_01_welcome_handler(pkt_header *header) {
zpl_printf("we received: block_size: %d, chunk_size: %d and world_size: %d\n", table.block_size, table.chunk_size, table.world_size);
world_init_minimal(table.block_size, table.chunk_size, table.world_size, NULL, NULL);
if (game_is_networked()) {
world_init_minimal(table.block_size, table.chunk_size, table.world_size, NULL, NULL);
}
return 0;
}