Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions botcraft/include/botcraft/Renderer/RenderingManager.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <atomic>
#include <unordered_set>
#include <mutex>
#include <functional>
Expand Down Expand Up @@ -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<void(double, double)> callback);
void SetKeyboardCallback(std::function<void(std::array<bool, static_cast<int>(KEY_CODE::NUMBER_OF_KEYS)>, double)> callback);
Expand Down Expand Up @@ -155,6 +159,8 @@ namespace Botcraft

std::array<bool, static_cast<int>(KEY_CODE::NUMBER_OF_KEYS)> is_key_pressed;

std::atomic_bool paused { true }; // default paused

bool inventory_open;
bool behaviour_open;

Expand Down
107 changes: 58 additions & 49 deletions botcraft/src/Renderer/RenderingManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<float>(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<float>(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<float>(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<float>(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
Expand Down Expand Up @@ -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<std::mutex> lock(screenshot_mutex);
if (take_screenshot)
{
std::vector<unsigned char> 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<std::mutex> lock(screenshot_mutex);
if (take_screenshot)
{
std::vector<unsigned char> 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<float>(std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now() - start).count() / 1e6);

Expand Down