From 908232c31eec0923474695c9b556167b5737d696 Mon Sep 17 00:00:00 2001 From: Nathan Chapman Date: Sun, 6 Jul 2025 14:43:59 -0600 Subject: [PATCH] Factor out constructors --- src/camera.cpp | 4 ---- src/camera.h | 19 ++++++++----------- src/light.cpp | 4 +--- src/light.h | 10 ++++------ src/main.cpp | 29 +++++++++++++++++++++-------- src/material.cpp | 2 -- src/material.h | 7 ++----- src/shader.cpp | 11 +++-------- src/shader.h | 23 +++++++++++------------ src/texture.cpp | 3 +-- src/texture.h | 3 +-- src/window.cpp | 14 +------------- src/window.h | 15 +++++++-------- 13 files changed, 60 insertions(+), 84 deletions(-) diff --git a/src/camera.cpp b/src/camera.cpp index 5717e91..799812d 100644 --- a/src/camera.cpp +++ b/src/camera.cpp @@ -1,10 +1,6 @@ #include "camera.h" -Camera::Camera(Vector3 position, Vector3 world_up, f32 yaw, f32 pitch, f32 movement_speed, f32 rotation_speed) : position(position), world_up(world_up), yaw(yaw), pitch(pitch), movement_speed(movement_speed), rotation_speed(rotation_speed) { - update_camera(this); -} - void update_camera(Camera *camera) { Vector3 new_front = Vector3( cos(glm::radians(camera->yaw)) * cos(glm::radians(camera->pitch)), diff --git a/src/camera.h b/src/camera.h index 818424f..a52ed46 100644 --- a/src/camera.h +++ b/src/camera.h @@ -7,19 +7,16 @@ #include "math.h" struct Camera { - Vector3 position; + Vector3 position = Vector3(0.0f, 0.0f, 0.0f); Vector3 front = Vector3(0.0f, 0.0f, -1.0f); - Vector3 up; - Vector3 right; - Vector3 world_up; + Vector3 up = Vector3(0.0f, 0.0f, 0.0f); + Vector3 right = Vector3(0.0f, 0.0f, 0.0f); + Vector3 world_up = Vector3(0.0f, 1.0f, 0.0f); - f32 yaw; - f32 pitch; - f32 movement_speed; - f32 rotation_speed; - - Camera(Vector3 position, Vector3 up, f32 yaw, f32 pitch, f32 movement_speed, f32 rotation_speed); - ~Camera() = default; + f32 yaw = 90.0f; + f32 pitch = 0.0f; + f32 movement_speed = 5.0f; + f32 rotation_speed = 1.0f; }; void update_camera(Camera *camera); diff --git a/src/light.cpp b/src/light.cpp index e74df3f..9fdeee2 100644 --- a/src/light.cpp +++ b/src/light.cpp @@ -1,8 +1,6 @@ #include "light.h" -Light::Light(Vector3 color, f32 ambient_intensity, Vector3 direction, f32 diffuse_intensity) : color(color), ambient_intensity(ambient_intensity), direction(direction), diffuse_intensity(diffuse_intensity) {} - -void use_light(Light *light, f32 color_location, f32 ambient_intensity_location, f32 direction_location, f32 diffuse_intensity_location) { +void use_light(Directional_Light *light, f32 color_location, f32 ambient_intensity_location, f32 diffuse_intensity_location, f32 direction_location) { glUniform3f(color_location, light->color.x, light->color.y, light->color.z); glUniform1f(ambient_intensity_location, light->ambient_intensity); diff --git a/src/light.h b/src/light.h index 8ab0d12..436fc12 100644 --- a/src/light.h +++ b/src/light.h @@ -7,14 +7,12 @@ struct Light { Vector3 color = Vector3(1.0f, 1.0f, 1.0f); f32 ambient_intensity = 1.0f; - - // For a directional light - Vector3 direction = Vector3(0.0f, -1.0f, 0.0f); // Default to down f32 diffuse_intensity = 0.0f; +}; - Light(Vector3 color, f32 ambient_intensity, Vector3 direction, f32 diffuse_intensity); - ~Light() = default; +struct Directional_Light : public Light { + Vector3 direction = Vector3(0.0f, -1.0f, 0.0f); // Default to down }; // Some of these should be u32 -void use_light(Light *light, f32 color_location, f32 ambient_intensity_location, f32 direction_location, f32 diffuse_intensity_location); +void use_light(Directional_Light *light, f32 color_location, f32 ambient_intensity_location, f32 diffuse_intensity_location, f32 direction_location); diff --git a/src/main.cpp b/src/main.cpp index 3ddd520..62b7d3c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -16,9 +16,16 @@ int main() { Mesh *meshes[10] = {nullptr}; Shader shaders[10]; - Window window(1920, 1080); + Window window; + window.width = 1920; + window.height = 1080; bool success = setup_window(&window); + if (!success) { + printf("Failed to setup window"); + return 1; + } + // Delta f32 dt = 0.0f; f32 last_dt = 0.f; @@ -58,20 +65,26 @@ int main() { shaders[0] = *shader; } - Texture clay_texture = Texture((char *)"assets/textures/clay_texture_1k.png"); + Texture clay_texture; + clay_texture.file_path = (char *)"assets/textures/clay_texture_1k.png"; load_texture(&clay_texture); - Texture plaster_texture = Texture((char *)"assets/textures/plaster_texture_1k.png"); + Texture plaster_texture; + plaster_texture.file_path = (char *)"assets/textures/plaster_texture_1k.png"; load_texture(&plaster_texture); - Material shiny_material(1.0f, 32.0f); - Material dull_material(0.3f, 4.0f); + Material shiny_material{32.0f, 1.0f}; + Material dull_material{4.0f, 0.3f}; - Light sun_light(Vector3(1.0f, 1.0f, 1.0f), 0.2f, Vector3(2.0f, -1.0, -2.0f), 1.0f); + Directional_Light sun_light; + sun_light.ambient_intensity = 0.2f; + sun_light.diffuse_intensity = 1.0f; + sun_light.direction = Vector3(2.0f, -1.0, -2.0f); Matrix4 projection = glm::perspective(45.0f, (f32)window.buffer_width / (f32)window.buffer_height, 0.1f, 100.0f); - Camera camera(Vector3(0.0f, 0.0f, 0.0f), Vector3(0.0f, 1.0f, 0.0f), 90.0f, 0.0f, 5.0f, 1.0f); + Camera camera; + update_camera(&camera); // Loop until window is closed @@ -97,7 +110,7 @@ int main() { use_light( &sun_light, shaders[0].uniform_ambient_color, shaders[0].uniform_ambient_intensity, - shaders[0].uniform_diffuse_direction, shaders[0].uniform_diffuse_intensity + shaders[0].uniform_diffuse_intensity, shaders[0].uniform_diffuse_direction ); glUniformMatrix4fv(shaders[0].uniform_projection, 1, GL_FALSE, glm::value_ptr(projection)); diff --git a/src/material.cpp b/src/material.cpp index 23b5caf..d5c5247 100644 --- a/src/material.cpp +++ b/src/material.cpp @@ -1,8 +1,6 @@ #include "material.h" -Material::Material(f32 specular_intensity, f32 shininess) : specular_intensity(specular_intensity), shininess(shininess) {} - void use_material(Material *material, u32 shininess_location, u32 specular_intensity_location) { glUniform1f(shininess_location, material->shininess); glUniform1f(specular_intensity_location, material->specular_intensity); diff --git a/src/material.h b/src/material.h index 7761e47..f187907 100644 --- a/src/material.h +++ b/src/material.h @@ -7,11 +7,8 @@ The kind of a thing */ struct Material { - f32 shininess; - f32 specular_intensity; - - Material(f32 specular_intensity, f32 shininess); - ~Material() = default; + f32 shininess = 2.0f; + f32 specular_intensity = 0.2f; }; void use_material(Material *material, u32 shininess_location, u32 specular_intensity_location); diff --git a/src/shader.cpp b/src/shader.cpp index fe781f0..f805e00 100644 --- a/src/shader.cpp +++ b/src/shader.cpp @@ -1,10 +1,5 @@ #include "shader.h" -Shader::Shader() { - id = 0; - uniform_projection = 0; - uniform_model = 0; -} Shader::~Shader() { clear_shader(this); @@ -47,7 +42,7 @@ bool create_shader(Shader *shader, const char *vertex_shader_path, const char *f glGetProgramiv(shader->id, GL_LINK_STATUS, &result); if (!result) { - glGetProgramInfoLog(shader->id, sizeof(errors), NULL, errors); + glGetProgramInfoLog(shader->id, sizeof(errors), nullptr, errors); printf("Error linking shader: '%s'\n", errors); return false; } @@ -56,7 +51,7 @@ bool create_shader(Shader *shader, const char *vertex_shader_path, const char *f glGetProgramiv(shader->id, GL_VALIDATE_STATUS, &result); if (!result) { - glGetProgramInfoLog(shader->id, sizeof(errors), NULL, errors); + glGetProgramInfoLog(shader->id, sizeof(errors), nullptr, errors); printf("Error validating shader: '%s'\n", errors); return false; } @@ -94,7 +89,7 @@ bool add_shader(Shader *shader, const char *shader_code, Shader_Type shader_type glGetShaderiv(_shader, GL_COMPILE_STATUS, &result); if (!result) { - glGetShaderInfoLog(_shader, sizeof(errors), NULL, errors); + glGetShaderInfoLog(_shader, sizeof(errors), nullptr, errors); printf("Error compiling the %d shader->id: '%s'\n", shader_type, errors); return false; } diff --git a/src/shader.h b/src/shader.h index 0ee38d6..55beaa2 100644 --- a/src/shader.h +++ b/src/shader.h @@ -11,22 +11,21 @@ enum Shader_Type : u32 { }; struct Shader { - u32 id; - u32 uniform_projection; - u32 uniform_model; - u32 uniform_view; - u32 uniform_eye_position; + u32 id = 0; + u32 uniform_projection = 0; + u32 uniform_model = 0; + u32 uniform_view = 0; + u32 uniform_eye_position = 0; - u32 uniform_ambient_color; - u32 uniform_ambient_intensity; + u32 uniform_ambient_color = 0; + u32 uniform_ambient_intensity = 0; - u32 uniform_diffuse_direction; - u32 uniform_diffuse_intensity; + u32 uniform_diffuse_direction = 0; + u32 uniform_diffuse_intensity = 0; - u32 uniform_shininess; - u32 uniform_specular_intensity; + u32 uniform_shininess = 0; + u32 uniform_specular_intensity = 0; - Shader(); ~Shader(); }; diff --git a/src/texture.cpp b/src/texture.cpp index 3c3ac65..ffd8747 100644 --- a/src/texture.cpp +++ b/src/texture.cpp @@ -1,6 +1,5 @@ #include "texture.h" -Texture::Texture(char *file_path) : file_path(file_path) {} Texture::~Texture() { clear_texture(this); @@ -44,6 +43,6 @@ void clear_texture(Texture *texture) { texture->width = 0; texture->height = 0; texture->bit_depth = 0; - texture->file_path = NULL; + texture->file_path = nullptr; } diff --git a/src/texture.h b/src/texture.h index 45bc973..d89c0bd 100644 --- a/src/texture.h +++ b/src/texture.h @@ -9,9 +9,8 @@ struct Texture { int width = 0; int height = 0; int bit_depth = 0; - char *file_path; + char *file_path = nullptr; - Texture(char *file_path); ~Texture(); }; diff --git a/src/window.cpp b/src/window.cpp index 9bc0fe4..0f292ad 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -1,18 +1,6 @@ #include "window.h" - -Window::Window(u32 window_width, u32 window_height) { - width = window_width; - height = window_height; - x_change_position = 0.0f; - y_change_position = 0.0f; - - for (size_t i = 0; i < 1024; i++) { - ascii_keys[i] = 0; - } -} - Window::~Window() { glfwDestroyWindow(gl_window); glfwTerminate(); @@ -36,7 +24,7 @@ bool setup_window(Window *window) { glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); - window->gl_window = glfwCreateWindow(window->width, window->height, "Test Window", NULL, NULL); + window->gl_window = glfwCreateWindow(window->width, window->height, "Test Window", nullptr, nullptr); if (!window->gl_window) { const char *desc; diff --git a/src/window.h b/src/window.h index 5d4bb61..45bfbe6 100644 --- a/src/window.h +++ b/src/window.h @@ -10,18 +10,17 @@ struct Window { u32 width = 1024, height = 768; - s32 buffer_width, buffer_height; + s32 buffer_width = 0, buffer_height = 0; GLFWwindow *gl_window = nullptr; - bool ascii_keys[1024]; + bool ascii_keys[1024] = {false}; // Camera coords - f32 last_x_position; - f32 last_y_position; - f32 x_change_position; - f32 y_change_position; - bool first_mouse_movement; + f32 last_x_position = 0.0f; + f32 last_y_position = 0.0f; + f32 x_change_position = 0.0f; + f32 y_change_position = 0.0f; + bool first_mouse_movement = true; - Window(u32 window_width, u32 window_height); ~Window(); };