refactor: state into namespace

This commit is contained in:
2025-10-13 17:17:28 +02:00
parent 4e2c62aaf4
commit 74a0bfca41
9 changed files with 19 additions and 27 deletions

View File

@@ -13,7 +13,7 @@ Camera::Camera()
void Camera::prepare()
{
// Set projection matrix
State::projection_matrix_ = projection_;
state::projection_matrix = projection_;
// Calculate view matrix
// For a camera, we need the inverse transformation:
@@ -29,7 +29,7 @@ void Camera::prepare()
// Inverse translation (translate in opposite direction)
view = glm::translate(view, -position_);
State::view_matrix_ = view;
state::view_matrix = view;
// Set viewport
glViewport(viewport_.x, viewport_.y, viewport_.z, viewport_.w);

View File

@@ -75,11 +75,11 @@ void Engine::initialize()
}
// Initialize default shader
State::default_shader_ =
state::default_shader =
std::make_shared<Shader>("data/vertex.glsl", "data/fragment.glsl");
if (std::strlen(State::default_shader_->error()) > 0) {
if (std::strlen(state::default_shader->error()) > 0) {
std::cerr << "Failed to initialize shaders: "
<< State::default_shader_->error() << "\n";
<< state::default_shader->error() << "\n";
}
}

View File

@@ -4,6 +4,7 @@
#include <memory>
#include <vector>
#include "../lib/glew/GL/glew.h"
#include "../lib/glfw/glfw3.h"
class World;

View File

@@ -13,15 +13,15 @@ void Mesh::add_buffer(const std::shared_ptr<Buffer>& buffer,
void Mesh::draw()
{
// Calculate MVP matrix
glm::mat4 mvp = State::projection_matrix_ * State::view_matrix_
* State::model_matrix_;
glm::mat4 mvp = state::projection_matrix * state::view_matrix
* state::model_matrix;
// Draw each buffer with its shader
for (size_t i = 0; i < buffers_.size(); ++i) {
// Use buffer's shader if available, otherwise use default
// shader
std::shared_ptr<Shader> shader =
shaders_[i] ? shaders_[i] : State::default_shader_;
shaders_[i] ? shaders_[i] : state::default_shader;
if (shader) {
shader->use();

View File

@@ -27,8 +27,8 @@ void Model::draw()
// Scale
model = glm::scale(model, scale_);
// Set the model matrix in State
State::model_matrix_ = model;
// Set the model matrix in state
state::model_matrix = model;
// Draw the mesh
mesh_->draw();

View File

@@ -1,6 +0,0 @@
#include "state.h"
std::shared_ptr<Shader> State::default_shader_ = nullptr;
glm::mat4 State::projection_matrix_ = glm::mat4(1.0f);
glm::mat4 State::view_matrix_ = glm::mat4(1.0f);
glm::mat4 State::model_matrix_ = glm::mat4(1.0f);

View File

@@ -6,12 +6,13 @@
class Shader;
class State {
public:
static std::shared_ptr<Shader> default_shader_;
static glm::mat4 projection_matrix_;
static glm::mat4 view_matrix_;
static glm::mat4 model_matrix_;
};
namespace state {
inline std::shared_ptr<Shader> default_shader = nullptr;
inline glm::mat4 projection_matrix = glm::mat4(1.0f);
inline glm::mat4 view_matrix = glm::mat4(1.0f);
inline glm::mat4 model_matrix = glm::mat4(1.0f);
} // namespace state
#endif // STATE_H_

View File

@@ -180,7 +180,6 @@
<ClCompile Include="src\mesh.cpp" />
<ClCompile Include="src\model.cpp" />
<ClCompile Include="src\shader.cpp" />
<ClCompile Include="src\state.cpp" />
<ClCompile Include="src\vertex.cpp" />
<ClCompile Include="src\world.cpp" />
</ItemGroup>

View File

@@ -80,9 +80,6 @@
<ClCompile Include="src\buffer.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\state.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\mesh.cpp">
<Filter>Source Files</Filter>
</ClCompile>