From d2b5fc5b3bc648afffa42375706429685ac63794 Mon Sep 17 00:00:00 2001 From: Mel Date: Mon, 12 Feb 2024 12:55:11 +0100 Subject: Split rendering into own thread and sync through render action lists --- src/Game.cpp | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 src/Game.cpp (limited to 'src/Game.cpp') diff --git a/src/Game.cpp b/src/Game.cpp new file mode 100644 index 0000000..117ba09 --- /dev/null +++ b/src/Game.cpp @@ -0,0 +1,81 @@ +#include "Game.hpp" + +#include +#include "Time.hpp" +#include "Common/Sizes.hpp" +#include "Entities/Player.hpp" +#include "GFX/Camera.hpp" +#include "GFX/Window.hpp" +#include "World/Clouds.hpp" +#include "World/World.hpp" + +namespace MC { + +void Game::run() const { + World::World world{}; + + GFX::Camera camera{}; + + World::Clouds clouds{}; + Entities::Player player{{0, World::Chunk::Height / 2.0, 0}}; + + Time time; + + while (!m_window.should_close()) { + m_window.poll_events(); + +#ifdef __APPLE__ + // Needs to happen on the main thread + fix_macos_render(); +#endif + + time.start_frame(); + + if (m_window.key(GLFW_KEY_ESCAPE, GLFW_PRESS)) { + m_window.close(); + } + + player.update(time, m_window, camera, world); + clouds.update(time); + + GFX::Actions actions; + + for (auto chunk : world.get_visible_chunks(time, camera.position())) { + auto position = chunk->chunk.value().position(); + actions.add({ + .program = GFX::Resources::Program::Terrain, + .mesh = &chunk->land_mesh.value(), + .transform = Transform(position), + }); + + actions.add({ + .program = GFX::Resources::Program::Terrain, + .mesh = &chunk->water_mesh.value(), + .transform = Transform(position - Vec3{0, 0.2, 0}), + .alpha = 0.4, + }); + } + + player.render(actions); + clouds.render(actions, player.position()); + + m_render_control->send_render_data({actions, camera}); + m_render_control->wait_for_render_finish(); + + time.end_frame(); + } +} + +void Game::fix_macos_render() const { + static Bool moved = false; + + if(!moved) { + I32 x, y; + glfwGetWindowPos(m_window.get(), &x, &y); + glfwSetWindowPos(m_window.get(), ++x, y); + + moved = true; + } +} + +} \ No newline at end of file -- cgit 1.4.1