diff --git a/deerith_client/include/deerith/client/cluster.h b/deerith_client/include/deerith/client/cluster.h index 1d3b032..415c738 100644 --- a/deerith_client/include/deerith/client/cluster.h +++ b/deerith_client/include/deerith/client/cluster.h @@ -18,8 +18,6 @@ namespace deerith void generate_cluster(voxel_cluster_t *cluster); void render_clusters(glm::ivec3 position, int rad, const camera_t &); - void flush_cluster_to_gpu(); - void shutdown_environment(); extern std::mutex cluster_workers_mutex; extern std::vector> cluster_workers; diff --git a/deerith_client/include/deerith/client/graphics/graphics_function_queue.h b/deerith_client/include/deerith/client/graphics/graphics_function_queue.h new file mode 100644 index 0000000..654eea7 --- /dev/null +++ b/deerith_client/include/deerith/client/graphics/graphics_function_queue.h @@ -0,0 +1,7 @@ +#pragma once +#include +namespace deerith +{ + void add_graphics_function(const std::function); + void execute_graphics_functions(); +} \ No newline at end of file diff --git a/deerith_client/include/deerith/client/internals/cluster_generation.h b/deerith_client/include/deerith/client/internals/cluster_generation.h index 31e62a9..e7daf50 100644 --- a/deerith_client/include/deerith/client/internals/cluster_generation.h +++ b/deerith_client/include/deerith/client/internals/cluster_generation.h @@ -3,7 +3,7 @@ namespace deerith { - glm::ivec3 voxel_direction_order[6] = { + inline constexpr glm::ivec3 voxel_direction_order[6] = { glm::ivec3(-1, 0, 0), glm::ivec3(1, 0, 0), glm::ivec3(0, -1, 0), @@ -12,13 +12,14 @@ namespace deerith glm::ivec3(0, 0, 1), }; - glm::vec2 voxel_uv_order[4] = { + inline constexpr glm::vec2 voxel_uv_order[4] = { glm::vec2(0.0f, 0.0f), glm::vec2(1.0f, 0.0f), glm::vec2(0.0f, 1.0f), glm::vec2(1.0f, 1.0f), }; - glm::vec3 voxel_direction_vertices[6][4] = { + + inline constexpr glm::vec3 voxel_direction_vertices[6][4] = { // -X { {0, 0, 1}, @@ -68,7 +69,7 @@ namespace deerith }, }; - uint32_t face_indices_order[6] = { + inline constexpr uint32_t face_indices_order[6] = { 0, 2, 1, diff --git a/deerith_client/include/deerith/client/internals/primitive.h b/deerith_client/include/deerith/client/internals/primitive.h index 9f70acd..d3776ca 100644 --- a/deerith_client/include/deerith/client/internals/primitive.h +++ b/deerith_client/include/deerith/client/internals/primitive.h @@ -8,17 +8,17 @@ namespace deerith void init_primitives(); void shutdown_primitives(); - const glm::vec2 rectangle_vertices[4] = { + inline constexpr glm::vec2 rectangle_vertices[4] = { glm::vec2(0, 0), glm::vec2(1, 0), glm::vec2(0, 1), glm::vec2(1, 1), }; - const unsigned int rectangle_indices[6] = { + inline constexpr unsigned int rectangle_indices[6] = { 0, 2, 1, 1, 2, 3}; - const glm::vec3 cube_vertices[8] = { + inline constexpr glm::vec3 cube_vertices[8] = { // Front face (Z = 1) {0, 0, 1}, // 0: Bottom-left {1, 0, 1}, // 1: Bottom-right @@ -32,7 +32,7 @@ namespace deerith {1, 1, 0}, // 7: Top-right }; - const unsigned int cube_indices[36] = { + inline constexpr unsigned int cube_indices[36] = { // Front (+Z) 0, 2, diff --git a/deerith_client/include/deerith/client/internals/world.h b/deerith_client/include/deerith/client/internals/world.h index 4348e35..8607bdc 100644 --- a/deerith_client/include/deerith/client/internals/world.h +++ b/deerith_client/include/deerith/client/internals/world.h @@ -13,5 +13,5 @@ namespace deerith void generate_voxel_texture(); void init_voxel_shader(); - void shutdown_world_variables(); + void shutdown_environment(); } diff --git a/deerith_client/include/deerith/client/world.h b/deerith_client/include/deerith/client/world.h index 2b85846..0f51636 100644 --- a/deerith_client/include/deerith/client/world.h +++ b/deerith_client/include/deerith/client/world.h @@ -12,7 +12,6 @@ namespace deerith bool connect_to_world(const char *address, int port); void shutdown_world(); - bool is_world_running(); void render_world(); void generate_cluster(voxel_cluster_t *_cluster); diff --git a/deerith_client/src/engine.cpp b/deerith_client/src/engine.cpp index 8a4cf96..826dd44 100644 --- a/deerith_client/src/engine.cpp +++ b/deerith_client/src/engine.cpp @@ -16,7 +16,7 @@ void deerith::init_engine() void deerith::shutdown_engine() { - shutdown_executing_servers(); + shutdown_executing_programs(); shutdown_network(); deerith_trace("shutdown engine"); shutdown_log(); @@ -35,5 +35,4 @@ void deerith::shutdown_engine_render_specific() shutdown_primitives(); shutdown_world(); shutdown_network(); - shutdown_world_variables(); -} \ No newline at end of file +} diff --git a/deerith_client/src/graphics/function_queue.cpp b/deerith_client/src/graphics/function_queue.cpp new file mode 100644 index 0000000..52698c2 --- /dev/null +++ b/deerith_client/src/graphics/function_queue.cpp @@ -0,0 +1,29 @@ +#include + +#include +#include + +namespace deerith +{ + std::vector> functions; + std::mutex function_usage_mutex; +} + +void deerith::add_graphics_function(const std::function fun) +{ + std::lock_guard lock(function_usage_mutex); + + functions.push_back(fun); +} + +void deerith::execute_graphics_functions() +{ + std::lock_guard lock(function_usage_mutex); + + for (std::function &fun : functions) + { + fun(); + } + + functions.clear(); +} \ No newline at end of file diff --git a/deerith_client/src/graphics/window.cpp b/deerith_client/src/graphics/window.cpp index a00ee79..db105cf 100644 --- a/deerith_client/src/graphics/window.cpp +++ b/deerith_client/src/graphics/window.cpp @@ -6,6 +6,8 @@ #include #include +#include + #include #include @@ -96,6 +98,8 @@ bool deerith::begin_window_display() return false; } + execute_graphics_functions(); + int display_w, display_h; glfwGetFramebufferSize(window, &display_w, &display_h); diff --git a/deerith_client/src/main.cpp b/deerith_client/src/main.cpp index 692a0d7..cde7b96 100644 --- a/deerith_client/src/main.cpp +++ b/deerith_client/src/main.cpp @@ -30,6 +30,6 @@ int main(int argc, const char **argv) deerith::shutdown_window(); } - deerith::shutdown_executing_servers(); + deerith::shutdown_executing_programs(); deerith::shutdown_engine(); } \ No newline at end of file diff --git a/deerith_client/src/world/cluster_generation.cpp b/deerith_client/src/world/cluster_generation.cpp index 7452834..e760a65 100644 --- a/deerith_client/src/world/cluster_generation.cpp +++ b/deerith_client/src/world/cluster_generation.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include #include @@ -11,15 +12,28 @@ namespace deerith { std::mutex cluster_workers_mutex; std::vector> cluster_workers; + + extern std::unordered_map, ivec3_hash_t> loaded_clusters; } void deerith::generate_cluster(voxel_cluster_t *cluster) { std::lock_guard lock(cluster_workers_mutex); - cluster_workers.push_back(std::make_unique()); - cluster_generator_t *generator = cluster_workers.back().get(); - generator->generate_cluster(cluster); + cluster_generator_t generator; + generator.generate_cluster(cluster); + + std::function upload_mesh_to_gpu_function = [generator]() + { + glm::ivec3 cluster_position; + cluster_position.x = generator.cluster->surface_id.x; + cluster_position.z = generator.cluster->surface_id.y; + cluster_position.y = generator.cluster->layer; + + loaded_clusters[cluster_position] = std::make_unique(generator.build_data); + }; + + add_graphics_function(upload_mesh_to_gpu_function); } void deerith::cluster_generator_t::generate_cluster(voxel_cluster_t *_cluster) diff --git a/deerith_client/src/world/cluster_storage.cpp b/deerith_client/src/world/cluster_storage.cpp index 2098613..64d377e 100644 --- a/deerith_client/src/world/cluster_storage.cpp +++ b/deerith_client/src/world/cluster_storage.cpp @@ -16,28 +16,6 @@ namespace deerith void render_cluster(glm::ivec3); } -void deerith::shutdown_environment() -{ - loaded_clusters.clear(); -} - -void deerith::flush_cluster_to_gpu() -{ - std::lock_guard lock(cluster_workers_mutex); - - for (std::unique_ptr &cluster_generator : cluster_workers) - { - glm::ivec3 cluster_position; - cluster_position.x = cluster_generator->cluster->surface_id.x; - cluster_position.z = cluster_generator->cluster->surface_id.y; - cluster_position.y = cluster_generator->cluster->layer; - - loaded_clusters[cluster_position] = std::make_unique(cluster_generator->build_data); - } - - cluster_workers.clear(); -} - void deerith::render_clusters(glm::ivec3 position, int rad, const camera_t &camera) { diff --git a/deerith_client/src/world/server_communication.cpp b/deerith_client/src/world/server_communication.cpp index 66e16fb..769b665 100644 --- a/deerith_client/src/world/server_communication.cpp +++ b/deerith_client/src/world/server_communication.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include @@ -110,4 +111,11 @@ void deerith::process_packet(deerith::packet_voxel_texture_atlas_t &packet, clie { packet.texture_atlas->save_atlas("client_generated/voxel_texture_atlas.png"); set_voxel_texture_atlas(std::move(packet.texture_atlas)); + + auto upload_texture_atlas_function = [] + { + generate_voxel_texture(); + }; + + add_graphics_function(upload_texture_atlas_function); } \ No newline at end of file diff --git a/deerith_client/src/world/world.cpp b/deerith_client/src/world/world.cpp index 0c9e3fd..8fb236f 100644 --- a/deerith_client/src/world/world.cpp +++ b/deerith_client/src/world/world.cpp @@ -35,11 +35,6 @@ bool deerith::connect_to_world(const char *address, int port) return true; } -bool deerith::is_world_running() -{ - return world_running; -} - void deerith::world_simulation() { while (world_running) diff --git a/deerith_client/src/world/world_render.cpp b/deerith_client/src/world/world_render.cpp index 86e4191..eacf24f 100644 --- a/deerith_client/src/world/world_render.cpp +++ b/deerith_client/src/world/world_render.cpp @@ -20,7 +20,6 @@ namespace deerith void deerith::render_world() { process_player_input(); - flush_cluster_to_gpu(); get_lit_shader()->set_vec3("u_color", glm::vec3(0.8f, 0.8f, 0.8f)); set_primitive_viewspace(glm::vec2(-10, -10), glm::vec2(10, 10)); diff --git a/deerith_client/src/world/world_variables.cpp b/deerith_client/src/world/world_variables.cpp index 03f657c..565d52d 100644 --- a/deerith_client/src/world/world_variables.cpp +++ b/deerith_client/src/world/world_variables.cpp @@ -1,12 +1,23 @@ #include #include #include +#include +#include #include namespace deerith { std::unique_ptr voxel_shader; std::unique_ptr voxel_texture; + + extern std::unordered_map, ivec3_hash_t> loaded_clusters; +} + +void deerith::shutdown_environment() +{ + loaded_clusters.clear(); + voxel_shader.reset(); + voxel_texture.reset(); } deerith::shader_t *deerith::get_voxel_shader() @@ -16,11 +27,6 @@ deerith::shader_t *deerith::get_voxel_shader() deerith::texture_t *deerith::get_voxel_texture() { - if (voxel_texture == nullptr) - { - generate_voxel_texture(); - } - return voxel_texture.get(); } @@ -46,8 +52,3 @@ void deerith::init_voxel_shader() "shaders/voxel.vert.glsl", "shaders/voxel.frag.glsl"); } - -void deerith::shutdown_world_variables() -{ - voxel_shader.reset(); -} \ No newline at end of file diff --git a/deerith_engine_core/include/deerith/engine.h b/deerith_engine_core/include/deerith/engine.h index ee623e8..956e61b 100644 --- a/deerith_engine_core/include/deerith/engine.h +++ b/deerith_engine_core/include/deerith/engine.h @@ -15,7 +15,7 @@ namespace deerith void shutdown_engine(); bool execute_client(const char *server_executable_path, int port); - void shutdown_executing_servers(); + void shutdown_executing_programs(); void start_chrono(); float get_chrono_ms(); diff --git a/deerith_engine_core/include/deerith/world/environment.h b/deerith_engine_core/include/deerith/world/environment.h index aad461a..dceccfa 100644 --- a/deerith_engine_core/include/deerith/world/environment.h +++ b/deerith_engine_core/include/deerith/world/environment.h @@ -1,7 +1,7 @@ #pragma once #include #include -#include + #include namespace deerith @@ -13,11 +13,9 @@ namespace deerith struct voxel_cluster_t { - voxel_cluster_t() : last_update_tick_id(get_world_tick()) {} - - int32_t layer; + int32_t layer = 0; glm::ivec2 surface_id; - uint32_t last_update_tick_id; + uint32_t last_update_tick_id = -1; cluster_voxel_type_id_data_t __data = {}; }; @@ -32,6 +30,7 @@ namespace deerith }; } + namespace deerith { void set_voxel(voxel_type_id_t voxel_id, glm::ivec3 coordinates); diff --git a/deerith_engine_core/src/core/execution.cpp b/deerith_engine_core/src/core/execution.cpp index b747e4f..eae0a49 100644 --- a/deerith_engine_core/src/core/execution.cpp +++ b/deerith_engine_core/src/core/execution.cpp @@ -41,7 +41,7 @@ bool deerith::execute_client(const char *client_executable_path, int port) } } -void deerith::shutdown_executing_servers() +void deerith::shutdown_executing_programs() { for (pid_t pid : executing_servers) { diff --git a/deerith_engine_core/src/world/environment.cpp b/deerith_engine_core/src/world/environment.cpp index ce2c4cd..ebd9d02 100644 --- a/deerith_engine_core/src/world/environment.cpp +++ b/deerith_engine_core/src/world/environment.cpp @@ -49,6 +49,7 @@ deerith::voxel_cluster_t *deerith::surface_t::get_create_cluster(int layer) cluster->layer = layer; cluster->surface_id = surface_id; + cluster->last_update_tick_id = get_world_tick(); return cluster; } diff --git a/deerith_engine_server/include/deerith/server/internals/network.h b/deerith_engine_server/include/deerith/server/internals/network.h index b491512..0c44663 100644 --- a/deerith_engine_server/include/deerith/server/internals/network.h +++ b/deerith_engine_server/include/deerith/server/internals/network.h @@ -2,7 +2,11 @@ namespace deerith { - bool init_server(int port); void flush_network_events(); - void load_base_protocols(); + bool init_server(int port); + + int init_network(); + void shutdown_network(); + + void load_core_protocols(); } \ No newline at end of file diff --git a/deerith_engine_server/include/deerith/server/internals/world.h b/deerith_engine_server/include/deerith/server/internals/world.h index 3b43aa0..473a8e8 100644 --- a/deerith_engine_server/include/deerith/server/internals/world.h +++ b/deerith_engine_server/include/deerith/server/internals/world.h @@ -1,10 +1,14 @@ #pragma once #include -#include -#include +#include namespace deerith { + void init_world(); + void shutdown_world(); + + void init_voxel_data(); + // Client lifecycle void client_connects(client_t *client); void client_disconnects(client_t *client); diff --git a/deerith_engine_server/include/deerith/server/network.h b/deerith_engine_server/include/deerith/server/network.h deleted file mode 100644 index 8c882c7..0000000 --- a/deerith_engine_server/include/deerith/server/network.h +++ /dev/null @@ -1,7 +0,0 @@ -#pragma once - -namespace deerith -{ - int init_network(); - void shutdown_network(); -} \ No newline at end of file diff --git a/deerith_engine_server/include/deerith/server/runtime_settings.h b/deerith_engine_server/include/deerith/server/runtime_settings.h deleted file mode 100644 index 3d370e5..0000000 --- a/deerith_engine_server/include/deerith/server/runtime_settings.h +++ /dev/null @@ -1,18 +0,0 @@ -#pragma once -#include - -#include - -namespace deerith -{ - struct world_settings_t - { - int port = 8080; - - std::function define_voxel_texture_atlas_function; - std::function define_voxels_materials_function; - std::function define_voxels_types_function; - - std::function generate_voxel_cluster; - }; -} \ No newline at end of file diff --git a/deerith_engine_server/include/deerith/server/world.h b/deerith_engine_server/include/deerith/server/world.h index 000d968..073ba3f 100644 --- a/deerith_engine_server/include/deerith/server/world.h +++ b/deerith_engine_server/include/deerith/server/world.h @@ -1,11 +1,22 @@ #pragma once -#include -#include namespace deerith { + struct atlas_builder_t; + struct texture_atlas_t; + struct voxel_cluster_t; + + struct world_settings_t + { + int port = 8080; + + std::function define_voxel_texture_atlas_function; + std::function define_voxels_materials_function; + std::function define_voxels_types_function; + + std::function generate_voxel_cluster; + }; + void run_world(const world_settings_t &); void shutdown_world(); - - bool is_world_running(); } \ No newline at end of file diff --git a/deerith_engine_server/src/core.cpp b/deerith_engine_server/src/core.cpp index 676ad15..49a77ed 100644 --- a/deerith_engine_server/src/core.cpp +++ b/deerith_engine_server/src/core.cpp @@ -1,16 +1,13 @@ #include - -#include - -namespace deerith -{ - -} +#include +#include void deerith::init_engine() { init_log("server"); + init_network(); + load_core_protocols(); deerith_trace("init server engine"); } @@ -21,4 +18,13 @@ void deerith::shutdown_engine() deerith_trace("shutdown server engine"); shutdown_log(); -} \ No newline at end of file +} + +void deerith::init_world() +{ + init_voxel_data(); +} + +void deerith::shutdown_world() +{ +} diff --git a/deerith_engine_server/src/network/network.cpp b/deerith_engine_server/src/network/network.cpp index 331f7a7..8887f8e 100644 --- a/deerith_engine_server/src/network/network.cpp +++ b/deerith_engine_server/src/network/network.cpp @@ -4,8 +4,6 @@ #include #include -#include -#include #include #include @@ -39,8 +37,6 @@ bool deerith::init_server(int port) return false; } - load_base_protocols(); - return true; } diff --git a/deerith_engine_server/src/readme.md b/deerith_engine_server/src/readme.md new file mode 100644 index 0000000..393d6fa --- /dev/null +++ b/deerith_engine_server/src/readme.md @@ -0,0 +1,7 @@ +# Server Library Documentation + +core.cpp // Init and shutdown all sistems + +World/ + client_managment.cpp Client managment when connected / disconencted + world.cpp Main Loop \ No newline at end of file diff --git a/deerith_engine_server/src/world/client_managment.cpp b/deerith_engine_server/src/world/client_managment.cpp index 60225f9..2607d3c 100644 --- a/deerith_engine_server/src/world/client_managment.cpp +++ b/deerith_engine_server/src/world/client_managment.cpp @@ -6,7 +6,6 @@ #include -#include #include void deerith::client_connects(client_t *client) diff --git a/deerith_engine_server/src/world/player_communication.cpp b/deerith_engine_server/src/world/player_communication.cpp index 7f0da5e..a9e6d7b 100644 --- a/deerith_engine_server/src/world/player_communication.cpp +++ b/deerith_engine_server/src/world/player_communication.cpp @@ -8,7 +8,7 @@ #include #include -void deerith::load_base_protocols() +void deerith::load_core_protocols() { router_add_packet_processor(); } diff --git a/deerith_engine_server/src/world/init_world.cpp b/deerith_engine_server/src/world/voxel_data.cpp similarity index 73% rename from deerith_engine_server/src/world/init_world.cpp rename to deerith_engine_server/src/world/voxel_data.cpp index 0434dbc..df33f1b 100644 --- a/deerith_engine_server/src/world/init_world.cpp +++ b/deerith_engine_server/src/world/voxel_data.cpp @@ -1,17 +1,7 @@ #include -#include -namespace deerith -{ - extern world_settings_t world_settings; - - void init_voxel_data(); -} - -void deerith::init_world() -{ - init_voxel_data(); -} +#include +#include void deerith::init_voxel_data() { diff --git a/deerith_engine_server/src/world/voxel_data_managment.cpp b/deerith_engine_server/src/world/voxel_data_comunication.cpp similarity index 96% rename from deerith_engine_server/src/world/voxel_data_managment.cpp rename to deerith_engine_server/src/world/voxel_data_comunication.cpp index 252f5d1..3231445 100644 --- a/deerith_engine_server/src/world/voxel_data_managment.cpp +++ b/deerith_engine_server/src/world/voxel_data_comunication.cpp @@ -1,9 +1,11 @@ #include -#include #include #include -#include +#include + +#include +#include namespace deerith { @@ -116,5 +118,4 @@ void deerith::send_cluster_info_to_client(client_t *client, voxel_cluster_t *clu *cluster_data.__cluster_data = cluster->__data; router_send_packet(cluster_data, client); - deerith_trace("sent cluster data to client in {} ms", get_chrono_ms()); } \ No newline at end of file diff --git a/deerith_engine_server/src/world/world.cpp b/deerith_engine_server/src/world/world.cpp index 15f860a..3c5e9ea 100644 --- a/deerith_engine_server/src/world/world.cpp +++ b/deerith_engine_server/src/world/world.cpp @@ -4,6 +4,8 @@ #include #include +#include + namespace deerith { void server_loop(); @@ -20,14 +22,19 @@ void deerith::run_world(const world_settings_t &__world_settings) deerith_error("could not init server"); return; } + else + { + deerith_trace("server init on port {}", world_settings.port); + } init_world(); - deerith_trace("server init on port {}", world_settings.port); while (true) { server_loop(); } + + shutdown_world(); } void deerith::server_loop() diff --git a/sandbox/src/build_world.cpp b/sandbox/src/build_world.cpp index 89ece0b..bd5a9b9 100644 --- a/sandbox/src/build_world.cpp +++ b/sandbox/src/build_world.cpp @@ -19,13 +19,11 @@ void sandbox::generate_voxel_cluster(deerith::voxel_cluster_t *cluster) float world_z = static_cast(cluster->surface_id.y * deerith::CLUSTER_SIZE + z) * deerith::VOXEL_SIZE; float noise_height_1 = noise.GetNoise(world_x, world_z); - float noise_height_2 = noise.GetNoise(world_x * 5 + 101, world_z * 5 + 221); - float noise_height_3 = noise.GetNoise(world_x * 8 - 1030, world_z * 8 - 540); + float noise_height_2 = noise.GetNoise(world_x * 3 + 101, world_z * 3 + 221); // Terrain height in voxel units - int terrain_height = static_cast(noise_height_1 * 240.0f * deerith::VOXEL_SIZE); - terrain_height += static_cast(noise_height_2 * 20.0f * deerith::VOXEL_SIZE); - terrain_height += static_cast(noise_height_3 * 5.0f * deerith::VOXEL_SIZE); + int terrain_height = static_cast(noise_height_1 * 121.0f * deerith::VOXEL_SIZE); + // terrain_height += static_cast(noise_height_2 * 20.0f * deerith::VOXEL_SIZE); // Cluster's bottom position in voxel units int cluster_bottom = cluster->layer * deerith::CLUSTER_SIZE; diff --git a/sandbox/src/main.cpp b/sandbox/src/main.cpp index 42d4670..612bacd 100644 --- a/sandbox/src/main.cpp +++ b/sandbox/src/main.cpp @@ -1,7 +1,6 @@ #include #include -#include #include #include "voxels.h" @@ -9,6 +8,7 @@ int main(int argc, const char **argv) { deerith::init_engine(); + auto cmdl = argh::parser(argc, argv); deerith::world_settings_t world_settings; @@ -31,6 +31,6 @@ int main(int argc, const char **argv) deerith::run_world(world_settings); - deerith::shutdown_executing_servers(); + deerith::shutdown_executing_programs(); deerith::shutdown_engine(); } \ No newline at end of file