diff --git a/botcraft/include/botcraft/Renderer/RenderingManager.hpp b/botcraft/include/botcraft/Renderer/RenderingManager.hpp index bcd69ea4..932aea07 100644 --- a/botcraft/include/botcraft/Renderer/RenderingManager.hpp +++ b/botcraft/include/botcraft/Renderer/RenderingManager.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include #include #include @@ -63,6 +64,9 @@ namespace Botcraft // Set a flag to terminate the rendering loop after the current frame void Close(); + void Pause() { paused = true; } + void Unpause() { paused = false; } + // Set mouse and keyboard callbacks to handle user inputs void SetMouseCallback(std::function callback); void SetKeyboardCallback(std::function(KEY_CODE::NUMBER_OF_KEYS)>, double)> callback); @@ -155,6 +159,8 @@ namespace Botcraft std::array(KEY_CODE::NUMBER_OF_KEYS)> is_key_pressed; + std::atomic_bool paused { true }; // default paused + bool inventory_open; bool behaviour_open; diff --git a/botcraft/src/Renderer/RenderingManager.cpp b/botcraft/src/Renderer/RenderingManager.cpp index a67b0314..acbcc8b2 100644 --- a/botcraft/src/Renderer/RenderingManager.cpp +++ b/botcraft/src/Renderer/RenderingManager.cpp @@ -214,47 +214,50 @@ namespace Botcraft glClearColor(current_color[0], current_color[1], current_color[2], 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - //Change view matrix - world_renderer->UpdateViewMatrix(); + if (!paused) + { + //Change view matrix + world_renderer->UpdateViewMatrix(); - if (has_proj_changed) - { - glm::mat4 projection = glm::perspective(glm::radians(45.0f), current_window_width / static_cast(current_window_height), 0.1f, 200.0f); - my_shader->SetMat4("projection", projection); - world_renderer->SetCameraProjection(projection); - has_proj_changed = false; - } + if (has_proj_changed) + { + glm::mat4 projection = glm::perspective(glm::radians(45.0f), current_window_width / static_cast(current_window_height), 0.1f, 200.0f); + my_shader->SetMat4("projection", projection); + world_renderer->SetCameraProjection(projection); + has_proj_changed = false; + } - world_renderer->UpdateFaces(); + world_renderer->UpdateFaces(); - my_shader->Use(); + my_shader->Use(); - //Draw all faces - world_renderer->UseAtlasTextureGL(); + //Draw all faces + world_renderer->UseAtlasTextureGL(); #ifdef USE_IMGUI - int num_chunks, num_rendered_chunks, num_entities, num_rendered_entities, num_faces, num_rendered_faces; - world_renderer->RenderFaces(&num_chunks, &num_rendered_chunks, &num_entities, &num_rendered_entities, &num_faces, &num_rendered_faces); - { - ImGui::SetNextWindowPos(ImVec2(static_cast(current_window_width), 0.0f), 0, ImVec2(1.0f, 0.0f)); - ImGui::SetNextWindowSize(ImVec2(180.0f, 170.0f)); - ImGui::Begin("Rendering", nullptr, ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoCollapse); - ImGui::Text("Lim. FPS: %.1f (%.2fms)", 1.0 / deltaTime, deltaTime * 1000.0); - ImGui::Text("Real FPS: %.1f (%.2fms)", 1.0 / real_fps, real_fps * 1000.0); - ImGui::Text("Loaded sections: %i", num_chunks); - ImGui::Text("Rendered sections: %i", num_rendered_chunks); - ImGui::Text("Num entities: %i", num_entities); - ImGui::Text("Rendered entities: %i", num_rendered_entities); - ImGui::Text("Loaded faces: %i", num_faces); - ImGui::Text("Rendered faces: %i", num_rendered_faces); - ImGui::End(); - } + int num_chunks, num_rendered_chunks, num_entities, num_rendered_entities, num_faces, num_rendered_faces; + world_renderer->RenderFaces(&num_chunks, &num_rendered_chunks, &num_entities, &num_rendered_entities, &num_faces, &num_rendered_faces); + { + ImGui::SetNextWindowPos(ImVec2(static_cast(current_window_width), 0.0f), 0, ImVec2(1.0f, 0.0f)); + ImGui::SetNextWindowSize(ImVec2(180.0f, 170.0f)); + ImGui::Begin("Rendering", nullptr, ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoCollapse); + ImGui::Text("Lim. FPS: %.1f (%.2fms)", 1.0 / deltaTime, deltaTime * 1000.0); + ImGui::Text("Real FPS: %.1f (%.2fms)", 1.0 / real_fps, real_fps * 1000.0); + ImGui::Text("Loaded sections: %i", num_chunks); + ImGui::Text("Rendered sections: %i", num_rendered_chunks); + ImGui::Text("Num entities: %i", num_entities); + ImGui::Text("Rendered entities: %i", num_rendered_entities); + ImGui::Text("Loaded faces: %i", num_faces); + ImGui::Text("Rendered faces: %i", num_rendered_faces); + ImGui::End(); + } #else - world_renderer->RenderFaces(); + world_renderer->RenderFaces(); #endif - glBindVertexArray(0); - glBindTexture(GL_TEXTURE_2D, 0); + glBindVertexArray(0); + glBindTexture(GL_TEXTURE_2D, 0); + } #ifdef USE_IMGUI // Draw the behaviour if it's open @@ -326,27 +329,33 @@ namespace Botcraft } - ImGui::Render(); + if (!paused) + { + ImGui::Render(); - // Render ImGui - ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); + // Render ImGui + ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); + } #endif } - glfwSwapBuffers(window); - glfwPollEvents(); - - { - std::scoped_lock lock(screenshot_mutex); - if (take_screenshot) - { - std::vector pixels(current_window_height * current_window_width * 3); - glReadPixels(0, 0, current_window_width, current_window_height, GL_RGB, GL_UNSIGNED_BYTE, pixels.data()); - - WriteImage(screenshot_path, current_window_height, current_window_width, 3, pixels.data(), true); - take_screenshot = false; - } - } + if (!paused) + { + glfwSwapBuffers(window); + glfwPollEvents(); + + { + std::scoped_lock lock(screenshot_mutex); + if (take_screenshot) + { + std::vector pixels(current_window_height * current_window_width * 3); + glReadPixels(0, 0, current_window_width, current_window_height, GL_RGB, GL_UNSIGNED_BYTE, pixels.data()); + + WriteImage(screenshot_path, current_window_height, current_window_width, 3, pixels.data(), true); + take_screenshot = false; + } + } + } real_fps = static_cast(std::chrono::duration_cast(std::chrono::steady_clock::now() - start).count() / 1e6);