stream world data from pxl8d to pxl8

This commit is contained in:
asrael 2026-01-25 09:26:30 -06:00
parent 39ee0fefb7
commit a71a9840b2
55 changed files with 5290 additions and 2131 deletions

View file

@ -237,6 +237,19 @@ pxl8_result pxl8_init(pxl8* sys, i32 argc, char* argv[]) {
pxl8_rng_seed(&game->rng, (u32)sys->hal->get_ticks());
game->world = pxl8_world_create();
if (!game->world) {
pxl8_error("failed to create world");
return PXL8_ERROR_INITIALIZATION_FAILED;
}
pxl8_net_config net_cfg = { .address = "127.0.0.1", .port = 7777 };
game->net = pxl8_net_create(&net_cfg);
if (game->net) {
pxl8_net_set_chunk_cache(game->net, pxl8_world_chunk_cache(game->world));
pxl8_net_connect(game->net);
}
#ifndef NDEBUG
game->debug_replay = pxl8_replay_create_buffer(60, 60);
pxl8_sfx_mixer_set_event_callback(game->mixer, pxl8_audio_event_callback, game);
@ -338,6 +351,13 @@ pxl8_result pxl8_update(pxl8* sys) {
}
}
if (game->net) {
while (pxl8_net_poll(game->net)) {}
pxl8_net_update(game->net, dt);
pxl8_world_sync(game->world, game->net);
}
pxl8_world_update(game->world, dt);
pxl8_gfx_update(game->gfx, dt);
pxl8_sfx_mixer_process(game->mixer);
@ -418,6 +438,9 @@ void pxl8_quit(pxl8* sys) {
pxl8_replay_destroy(game->debug_replay);
#endif
if (game->net) pxl8_net_destroy(game->net);
if (game->world) pxl8_world_destroy(game->world);
pxl8_sfx_mixer_destroy(game->mixer);
pxl8_gfx_destroy(game->gfx);
pxl8_script_destroy(game->script);
@ -437,6 +460,10 @@ void pxl8_set_running(pxl8* sys, bool running) {
}
}
pxl8_world* pxl8_get_world(pxl8* sys) {
return (sys && sys->game) ? sys->game->world : NULL;
}
f32 pxl8_get_fps(const pxl8* sys) {
return (sys && sys->game) ? sys->game->fps : 0.0f;
}
@ -449,6 +476,10 @@ pxl8_input_state* pxl8_get_input(const pxl8* sys) {
return (sys && sys->game) ? &sys->game->input : NULL;
}
pxl8_net* pxl8_get_net(const pxl8* sys) {
return (sys && sys->game) ? sys->game->net : NULL;
}
pxl8_sfx_mixer* pxl8_get_sfx_mixer(const pxl8* sys) {
return (sys && sys->game) ? sys->game->mixer : NULL;
}

View file

@ -1,37 +1,37 @@
#pragma once
#include "pxl8_gfx.h"
#include "pxl8_net.h"
#include "pxl8_rng.h"
#include "pxl8_script.h"
#include "pxl8_sfx.h"
#include "pxl8_types.h"
#include "pxl8_world.h"
typedef struct pxl8_replay pxl8_replay;
typedef struct pxl8_game {
pxl8_gfx* gfx;
pxl8_script* script;
pxl8_sfx_mixer* mixer;
pxl8_rng rng;
i32 frame_count;
u64 last_time;
f32 time;
f32 fps_accumulator;
i32 fps_frame_count;
f32 fps;
pxl8_input_state input;
pxl8_input_state prev_input;
#ifndef NDEBUG
pxl8_replay* debug_replay;
#endif
f32 fps;
f32 fps_accumulator;
i32 fps_frame_count;
i32 frame_count;
pxl8_gfx* gfx;
pxl8_input_state input;
u64 last_time;
pxl8_sfx_mixer* mixer;
pxl8_net* net;
pxl8_input_state prev_input;
bool repl_mode;
bool repl_started;
pxl8_rng rng;
bool running;
pxl8_script* script;
bool script_loaded;
char script_path[256];
f32 time;
pxl8_world* world;
} pxl8_game;

View file

@ -1,5 +1,5 @@
#include "pxl8_log.h"
#include "pxl8_repl.h"
#include "pxl8_types.h"
#include <stdarg.h>
#include <stdio.h>
@ -18,6 +18,7 @@ static pxl8_log* g_log = NULL;
void pxl8_log_init(pxl8_log* log) {
g_log = log;
g_log->handler = NULL;
g_log->level = PXL8_LOG_LEVEL_DEBUG;
const char* env_level = getenv("PXL8_LOG_LEVEL");
@ -30,6 +31,10 @@ void pxl8_log_init(pxl8_log* log) {
}
}
void pxl8_log_set_handler(pxl8_log_handler handler) {
if (g_log) g_log->handler = handler;
}
void pxl8_log_set_level(pxl8_log_level level) {
if (g_log) g_log->level = level;
}
@ -61,7 +66,7 @@ static void log_output(const char* color, const char* level,
strncat(buffer, "\n", sizeof(buffer) - strlen(buffer) - 1);
if (!pxl8_repl_push_log(buffer)) {
if (!g_log->handler || !g_log->handler(buffer)) {
printf("%s", buffer);
fflush(stdout);
}

View file

@ -10,11 +10,15 @@ typedef enum {
PXL8_LOG_LEVEL_ERROR = 4,
} pxl8_log_level;
typedef bool (*pxl8_log_handler)(const char* message);
typedef struct pxl8_log {
pxl8_log_handler handler;
pxl8_log_level level;
} pxl8_log;
void pxl8_log_init(pxl8_log* log);
void pxl8_log_set_handler(pxl8_log_handler handler);
void pxl8_log_set_level(pxl8_log_level level);
void pxl8_log_write_trace(const char* file, int line, const char* fmt, ...);

View file

@ -3,6 +3,7 @@
#include "pxl8_gfx.h"
#include "pxl8_hal.h"
#include "pxl8_io.h"
#include "pxl8_net.h"
#include "pxl8_sfx.h"
#include "pxl8_types.h"
@ -23,6 +24,7 @@ void pxl8_quit(pxl8* sys);
f32 pxl8_get_fps(const pxl8* sys);
pxl8_gfx* pxl8_get_gfx(const pxl8* sys);
pxl8_input_state* pxl8_get_input(const pxl8* sys);
pxl8_net* pxl8_get_net(const pxl8* sys);
pxl8_size pxl8_get_resolution_dimensions(pxl8_resolution resolution);
pxl8_sfx_mixer* pxl8_get_sfx_mixer(const pxl8* sys);
bool pxl8_is_running(const pxl8* sys);