refactor: introduce App class as program core

This commit is contained in:
2026-03-20 22:46:34 +08:00
parent bfc928d91a
commit 3916a72ca3
12 changed files with 492 additions and 288 deletions

43
include/Cubed/app.hpp Normal file
View File

@@ -0,0 +1,43 @@
#pragma once
#include <Cubed/camera.hpp>
#include <Cubed/gameplay/world.hpp>
#include <Cubed/input.hpp>
#include <Cubed/renderer.hpp>
#include <Cubed/texture_manager.hpp>
#include <Cubed/window.hpp>
class App {
public:
App();
~App();
static void cursor_position_callback(GLFWwindow* window, double xpos, double ypos);
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods);
static void mouse_button_callback(GLFWwindow* window, int button, int action, int mods);
static void window_reshape_callback(GLFWwindow* window, int new_width, int new_height);
static int start_cubed_application(int argc, char** argv);
private:
Camera m_camera;
World m_world;
Renderer m_renderer{m_camera, m_world};
Window m_window{m_renderer};
GLuint m_texture_array;
TextureManager m_texture_manager;
void init();
auto init_camera();
auto init_texture();
auto init_world();
void render();
void run();
void update();
};

View File

@@ -3,10 +3,14 @@ constexpr int WORLD_SIZE_X = 32;
constexpr int WORLD_SIZE_Z = 32;
constexpr int WORLD_SIZE_Y = 16;
constexpr int MAX_BLOCK_NUM = 2;
constexpr int CHUCK_SIZE = 16;
constexpr int DISTANCE = 8;
constexpr int MAX_BLOCK_STATUS = 1;
constexpr float FOV = 70.0f;
constexpr float VERTICES_POS[6][6][3] = {
// ===== front (z = +1) =====
0.0f, 0.0f, 1.0f, // bottom left

View File

@@ -1,5 +1,7 @@
#pragma once
#include <GLFW/glfw3.h>
struct MoveState {
bool forward = false;
bool back = false;
@@ -21,4 +23,6 @@ struct InputState {
namespace Input {
InputState& get_input_state();
}

View File

@@ -0,0 +1,39 @@
#pragma once
#include <glad/glad.h>
#include <glm/glm.hpp>
#include <vector>
class Camera;
class World;
class Renderer {
public:
constexpr static int NUM_VAO = 1;
Renderer(const Camera& camera, World& world);
~Renderer();
void init();
void render(GLuint texture_array);
void update_proj_matrix(float aspect);
private:
const Camera& m_camera;
World& m_world;
glm::mat4 m_p_mat, m_v_mat, m_m_mat, m_mv_mat, m_mvp_mat;
GLuint m_mv_loc;
GLuint m_proj_loc;
GLuint m_sky_vbo;
GLuint m_outline_indices_vbo;
GLuint m_outline_vbo;
GLuint m_sky_program;
GLuint m_outline_program;
GLuint m_world_program;
std::vector<GLuint> m_vao;
void render_outline();
void render_sky();
};

View File

@@ -3,12 +3,12 @@
namespace Assert {
inline void msg(const char* condition, const char* file,
int line, const char* func,
const std::string& message = ""
std::string_view message = ""
) {
LOG::error("Assertion failed: {} at {}: {} in function {}",
condition, file, line, func);
if (!message.empty()) {
if (message.size()) {
LOG::error("Message: {}", message);
}
std::abort();

21
include/Cubed/window.hpp Normal file
View File

@@ -0,0 +1,21 @@
#pragma once
#include <GLFW/glfw3.h>
class Renderer;
class Window {
public:
Window(Renderer& renderer);
~Window();
const GLFWwindow* get_glfw_window() const;
GLFWwindow* get_glfw_window();
void init();
void update_viewport();
private:
float m_aspect;
GLFWwindow* m_window;
int m_width;
int m_height;
Renderer& m_renderer;
};