diff --git a/CMakeLists.txt b/CMakeLists.txt index bbbc6d4..f6e30ea 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,7 +15,7 @@ if(MSVC) endif() if (EMSCRIPTEN) - set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -s USE_GLFW=3 --profiling -s ASSERTIONS=1 -s WASM=1 -s INITIAL_MEMORY=268435456 -s FORCE_FILESYSTEM=1 --preload-file ${CMAKE_SOURCE_DIR}/art@art/ --shell-file ${CMAKE_SOURCE_DIR}/web/eco2d.html") + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -s USE_GLFW=3 --profiling -s ASSERTIONS=1 -s WASM=1 -s INITIAL_MEMORY=268435456 -s FORCE_FILESYSTEM=1 --preload-file ${CMAKE_SOURCE_DIR}/art@art/ --shell-file ${CMAKE_SOURCE_DIR}/web/eco2d.html --post-js ${CMAKE_SOURCE_DIR}/web/eco2d-post.js") set(CMAKE_EXECUTABLE_SUFFIX ".html") endif () diff --git a/code/game/src/assets.c b/code/game/src/assets.c index 58e9f17..0ba81b4 100644 --- a/code/game/src/assets.c +++ b/code/game/src/assets.c @@ -23,6 +23,8 @@ typedef struct { static int64_t assets_frame_counter = 1; static double assets_frame_next_draw = 0.0; +#include + int32_t assets_setup(void) { for (uint32_t i=0; i= zpl_time_rel()) return; + if (playback_time >= get_cached_time()) return; replay_record *r = &records[record_pos]; - playback_time = zpl_time_rel() + r->delay; + playback_time = get_cached_time() + r->delay; switch (r->kind) { case RPKIND_KEY: { @@ -249,7 +249,7 @@ void debug_replay_update(void) { void debug_replay_record_keystate(pkt_send_keystate state) { if (!is_recording) return; - double record_time = zpl_time_rel(); + double record_time = get_cached_time(); replay_record rec = { .kind = RPKIND_KEY, @@ -258,13 +258,13 @@ void debug_replay_record_keystate(pkt_send_keystate state) { }; zpl_array_append(records, rec); - last_record_time = zpl_time_rel(); + last_record_time = get_cached_time(); } void debug_replay_special_action(replay_kind kind) { ZPL_ASSERT(kind != RPKIND_KEY); if (!is_recording || is_playing) return; - double record_time = zpl_time_rel(); + double record_time = get_cached_time(); replay_record rec = { .kind = kind, @@ -272,5 +272,5 @@ void debug_replay_special_action(replay_kind kind) { }; zpl_array_append(records, rec); - last_record_time = zpl_time_rel(); + last_record_time = get_cached_time(); } diff --git a/code/game/src/entity.c b/code/game/src/entity.c index 29ebd8a..be2ddb7 100644 --- a/code/game/src/entity.c +++ b/code/game/src/entity.c @@ -79,7 +79,7 @@ void entity_update_action_timers() { static double last_update_time = 0.0f; if (!ecs_streaminfo) { ecs_streaminfo = ecs_query_new(world_ecs(), "components.StreamInfo"); - last_update_time = zpl_time_rel(); + last_update_time = get_cached_time(); } ecs_iter_t it = ecs_query_iter(world_ecs(), ecs_streaminfo); @@ -88,17 +88,17 @@ void entity_update_action_timers() { StreamInfo *si = ecs_field(&it, StreamInfo, 1); for (int32_t i = 0; i < it.count; i++) { - if (si[i].last_update < zpl_time_rel()) { - si[i].last_update = zpl_time_rel() + si[i].tick_delay; - si[i].tick_delay += (zpl_time_rel() - last_update_time) * 0.5f; + if (si[i].last_update < get_cached_time()) { + si[i].last_update = get_cached_time() + si[i].tick_delay; + si[i].tick_delay += (get_cached_time() - last_update_time) * 0.5f; } } } - last_update_time = zpl_time_rel(); + last_update_time = get_cached_time(); } bool entity_can_stream(uint64_t ent_id) { StreamInfo *si = ecs_get_mut(world_ecs(), ent_id, StreamInfo); - return (si->last_update < zpl_time_rel()); + return (si->last_update < get_cached_time()); } diff --git a/code/game/src/game.c b/code/game/src/game.c index 9e760a6..804fc6c 100644 --- a/code/game/src/game.c +++ b/code/game/src/game.c @@ -122,7 +122,7 @@ void flecs_dash_init() { } float game_time() { - return (float)zpl_time_rel(); + return (float)get_cached_time(); } void game_init(const char *ip, uint16_t port, game_kind play_mode, uint32_t num_viewers, int32_t seed, uint16_t chunk_size, uint16_t chunk_amount, int8_t is_dash_enabled) { @@ -232,15 +232,15 @@ void game_update() { if (game_mode == GAMEKIND_HEADLESS) { network_server_tick(); - static uint64_t ms_report = 2500; - if (ms_report < zpl_time_rel_ms()) { - ms_report = zpl_time_rel_ms() + 5000; - zpl_printf("delta: %f ms.\n", (zpl_time_rel() - last_update)*1000.0f); + static float ms_report = 2.5f; + if (ms_report < get_cached_time()) { + ms_report = get_cached_time() + 5.f; + zpl_printf("delta: %f ms.\n", (get_cached_time() - last_update)*1000.0f); } } } - last_update = zpl_time_rel(); + last_update = get_cached_time(); } void game_render() { diff --git a/code/game/src/gen/texgen.c b/code/game/src/gen/texgen.c index be11a19..e33277f 100644 --- a/code/game/src/gen/texgen.c +++ b/code/game/src/gen/texgen.c @@ -33,11 +33,11 @@ Texture2D GenColorEco(Color color) { Texture2D texgen_build_anim(asset_id id, int64_t counter) { (void)counter; switch (id) { - case ASSET_WATER: { - Image img = LoadImageEco("water"); - ImageColorBrightness(&img, zpl_abs((counter % 64 - 32)*2)); - return Image2TexEco(img); - }break; + // case ASSET_WATER: { + // Image img = LoadImageEco("water"); + // ImageColorBrightness(&img, zpl_abs((counter % 64 - 32)*2)); + // return Image2TexEco(img); + // }break; default: return GenColorEco(PINK); break; } @@ -61,7 +61,8 @@ Texture2D texgen_build_sprite(asset_id id) { case ASSET_LAVA: return LoadTexEco("lava"); case ASSET_WOOD: return LoadTexEco("wood"); case ASSET_TREE: return LoadTexEco("tree"); - + case ASSET_WATER: return LoadTexEco("water"); + case ASSET_BELT: case ASSET_BELT_RIGHT: return LoadTexEco("belt_right"); case ASSET_BELT_LEFT: return LoadTexEco("belt_left"); diff --git a/code/game/src/main.c b/code/game/src/main.c index 67ef512..d2cb10e 100644 --- a/code/game/src/main.c +++ b/code/game/src/main.c @@ -80,6 +80,7 @@ int main(int argc, char** argv) { #if !defined(PLATFORM_WEB) while (game_is_running()) { + reset_cached_time(); profile (PROF_MAIN_LOOP) { game_input(); game_update(); @@ -102,6 +103,7 @@ int main(int argc, char** argv) { #if defined(PLATFORM_WEB) void UpdateDrawFrame(void) { + reset_cached_time(); profile (PROF_MAIN_LOOP) { game_input(); game_update(); @@ -111,3 +113,12 @@ void UpdateDrawFrame(void) { profiler_collate(); } #endif + +static float temp_time = 0.0f; + +float get_cached_time(void) { + return temp_time; +} +void reset_cached_time(void) { + temp_time = zpl_time_rel(); +} diff --git a/code/game/src/network.c b/code/game/src/network.c index bf0322a..ce6e666 100644 --- a/code/game/src/network.c +++ b/code/game/src/network.c @@ -120,7 +120,7 @@ network_client_fetch_stats(void) { static float incoming_bandwidth = 0.0f; static float outgoing_bandwidth = 0.0f; - if (next_measure < zpl_time_rel()) { + if (next_measure < get_cached_time()) { #define MAX_RATE_SAMPLES 8 static uint64_t last_total_sent = 0; static uint64_t last_total_recv = 0; @@ -145,7 +145,7 @@ network_client_fetch_stats(void) { incoming_bandwidth = stats.incoming_bandwidth /= MAX_RATE_SAMPLES; outgoing_bandwidth = stats.outgoing_bandwidth /= MAX_RATE_SAMPLES; - next_measure = zpl_time_rel() + 1.0; + next_measure = get_cached_time() + 1.0; } else { stats.incoming_bandwidth = incoming_bandwidth; stats.outgoing_bandwidth = outgoing_bandwidth; diff --git a/code/game/src/packets/pkt_send_librg_update.c b/code/game/src/packets/pkt_send_librg_update.c index a02d4be..37773e9 100644 --- a/code/game/src/packets/pkt_send_librg_update.c +++ b/code/game/src/packets/pkt_send_librg_update.c @@ -41,7 +41,7 @@ int32_t pkt_send_librg_update_handler(pkt_header *header) { int32_t state = librg_world_read(view->tracker, header->view_id, uc.item.as.bin.start, uc.item.as.bin.length, NULL); if (state < 0) zpl_printf("[ERROR] world read error: %d\n", state); - float now = (float)zpl_time_rel(); + float now = (float)get_cached_time(); view->delta_time[layer_id] = now - view->last_update[layer_id]; view->last_update[layer_id] = now; diff --git a/code/game/src/system.h b/code/game/src/system.h index 63ed0d9..562dcf3 100644 --- a/code/game/src/system.h +++ b/code/game/src/system.h @@ -10,8 +10,11 @@ #define ZPL_ENABLE_MATH #include "zpl.h" +float get_cached_time(void); +void reset_cached_time(void); + #define defer_var ZPL_CONCAT(_i_,__LINE__) #define defer(s,e) for ( \ uint32_t defer_var = (s, 0); \ !defer_var; \ -(defer_var += 1), e) \ No newline at end of file +(defer_var += 1), e) diff --git a/code/game/src/world/world.c b/code/game/src/world/world.c index 41900b2..d0306bf 100644 --- a/code/game/src/world/world.c +++ b/code/game/src/world/world.c @@ -307,8 +307,8 @@ int32_t world_destroy(void) { #define WORLD_LIBRG_BUFSIZ 2000000 static void world_tracker_update(uint8_t ticker, float freq, uint8_t radius) { - if (world.tracker_update[ticker] > (float)zpl_time_rel()) return; - world.tracker_update[ticker] = (float)zpl_time_rel() + freq; + if (world.tracker_update[ticker] > (float)(get_cached_time())) return; + world.tracker_update[ticker] = (float)(get_cached_time()) + freq; profile(PROF_WORLD_WRITE) { ecs_iter_t it = ecs_query_iter(world_ecs(), world.ecs_update); @@ -323,7 +323,7 @@ static void world_tracker_update(uint8_t ticker, float freq, uint8_t radius) { if (!p[i].active) continue; - + int32_t result = librg_world_write(world_tracker(), it.entities[i], radius, buffer, &datalen, NULL); if (result > 0) { diff --git a/code/game/src/world_view.c b/code/game/src/world_view.c index b964177..a4c0d20 100644 --- a/code/game/src/world_view.c +++ b/code/game/src/world_view.c @@ -26,7 +26,7 @@ int32_t tracker_read_update(librg_world *w, librg_event *e) { entity_view *d = entity_view_get(&view->entities, entity_id); #if 1 if (d && d->layer_id < view->active_layer_id) { - if (zpl_time_rel_ms() - d->last_update > WORLD_TRACKER_UPDATE_NORMAL_MS) { + if ((get_cached_time()*1000.0f) - d->last_update > WORLD_TRACKER_UPDATE_NORMAL_MS) { d->layer_id = zpl_min(WORLD_TRACKER_LAYERS-1, d->layer_id+1); } // NOTE(zaklaus): reject updates from slower layers @@ -34,7 +34,7 @@ int32_t tracker_read_update(librg_world *w, librg_event *e) { } #endif - data.last_update = zpl_time_rel_ms(); + data.last_update = get_cached_time()*1000.0f; data.layer_id = view->active_layer_id; predict_receive_update(d, &data); entity_view_update_or_create(&view->entities, entity_id, data); diff --git a/web/eco2d-post.js b/web/eco2d-post.js new file mode 100644 index 0000000..5172c73 --- /dev/null +++ b/web/eco2d-post.js @@ -0,0 +1,2 @@ +// Hack to enforce CLOCK_REALTIME, which is significantly faster for our purposes. +_emscripten_get_now = () => Date.now();