extract orbital camera into separate file

This commit is contained in:
2026-02-22 14:29:04 +01:00
parent 7bc1eaae75
commit 157985df6b
8 changed files with 140 additions and 140 deletions

41
include/camera.hpp Normal file
View File

@ -0,0 +1,41 @@
#ifndef __CAMERA_HPP_
#define __CAMERA_HPP_
#include <raylib.h>
#include <raymath.h>
#include "config.hpp"
class OrbitCamera3D {
friend class Renderer;
private:
Camera camera;
Vector3 target;
float distance;
float angle_x;
float angle_y;
Vector2 last_mouse;
bool dragging;
bool panning;
bool target_lock;
public:
OrbitCamera3D()
: camera({0}), target(Vector3Zero()), distance(CAMERA_DISTANCE),
angle_x(0.0), angle_y(0.0), last_mouse(Vector2Zero()), dragging(false),
panning(false), target_lock(true) {
camera.position = Vector3(0, 0, -1.0 * distance);
camera.target = target;
camera.up = Vector3(0, 1.0, 0);
camera.fovy = CAMERA_FOV;
camera.projection = CAMERA_PERSPECTIVE;
}
~OrbitCamera3D() {}
public:
auto Update(const Vector3 &current_target) -> void;
};
#endif

View File

@ -86,7 +86,6 @@ public:
class MassSpringSystem {
private:
// TODO: Use references
std::vector<Mass *> mass_vec;
std::vector<int> indices;
std::vector<int64_t> cell_ids;

View File

@ -282,6 +282,8 @@ public:
std::vector<std::pair<State, State>>>;
};
// Provide hash functions so we can use State and <State, State> as hash-set
// keys for masses and springs.
template <> struct std::hash<State> {
std::size_t operator()(const State &s) const noexcept { return s.Hash(); }
};

View File

@ -1,57 +1,24 @@
#ifndef __RENDERER_HPP_
#define __RENDERER_HPP_
#include <immintrin.h>
#include <raylib.h>
#include <raymath.h>
#include <unordered_set>
#include "camera.hpp"
#include "config.hpp"
#include "puzzle.hpp"
#include "physics.hpp"
class OrbitCamera3D {
friend class Renderer;
private:
Camera camera;
Vector3 target;
float distance;
float angle_x;
float angle_y;
Vector2 last_mouse;
bool dragging;
bool panning;
bool target_lock;
public:
OrbitCamera3D(Vector3 target, float distance)
: camera({0}), target(target), distance(distance), angle_x(0.0),
angle_y(0.0), last_mouse(Vector2Zero()), dragging(false),
panning(false), target_lock(true) {
camera.position = Vector3(0, 0, -1.0 * distance);
camera.target = target;
camera.up = Vector3(0, 1.0, 0);
camera.fovy = CAMERA_FOV;
camera.projection = CAMERA_PERSPECTIVE;
}
~OrbitCamera3D() {}
public:
auto Update(const Mass &current_mass) -> void;
};
#include "puzzle.hpp"
class Renderer {
private:
OrbitCamera3D camera;
const OrbitCamera3D &camera;
RenderTexture render_target;
RenderTexture klotski_target;
RenderTexture menu_target;
Material vertex_mat;
// Instancing
Material vertex_mat;
int transforms_size;
Matrix *transforms;
Mesh cube_instance;
@ -62,18 +29,14 @@ public:
bool connect_solutions;
public:
Renderer()
: camera(OrbitCamera3D(Vector3(0, 0, 0), CAMERA_DISTANCE)),
mark_solutions(false), connect_solutions(false), transforms_size(0),
transforms(nullptr) {
Renderer(const OrbitCamera3D &camera)
: camera(camera), mark_solutions(false), connect_solutions(false),
transforms_size(0), transforms(nullptr) {
render_target = LoadRenderTexture(GetScreenWidth() / 2.0,
GetScreenHeight() - MENU_HEIGHT);
klotski_target = LoadRenderTexture(GetScreenWidth() / 2.0,
GetScreenHeight() - MENU_HEIGHT);
menu_target = LoadRenderTexture(GetScreenWidth(), MENU_HEIGHT);
vertex_mat = LoadMaterialDefault();
vertex_mat.maps[MATERIAL_MAP_DIFFUSE].color = VERTEX_COLOR;
}
Renderer(const Renderer &copy) = delete;
@ -86,10 +49,9 @@ public:
UnloadRenderTexture(klotski_target);
UnloadRenderTexture(menu_target);
UnloadMaterial(vertex_mat);
// Instancing
if (transforms != nullptr) {
UnloadMaterial(vertex_mat);
MemFree(transforms);
UnloadMesh(cube_instance);
@ -106,9 +68,6 @@ private:
-> void;
public:
auto UpdateCamera(const MassSpringSystem &mass_springs,
const State &current_state) -> void;
auto UpdateTextureSizes() -> void;
auto DrawMassSprings(const MassSpringSystem &mass_springs,