78 lines
2.6 KiB
C++
78 lines
2.6 KiB
C++
#pragma once
|
|
|
|
#include "Camera.hpp"
|
|
|
|
#include <functional>
|
|
|
|
struct GLFWwindow;
|
|
|
|
namespace planetarium {
|
|
|
|
// -----------------------------------------------------------------------
|
|
// Engine
|
|
//
|
|
// Top-level owner of the GLFW window/context, the frame loop, and input
|
|
// routing between raw GLFW callbacks and the Camera. This is the only
|
|
// class that knows about GLFW directly.
|
|
//
|
|
// This is the reusable "engine" half of the original Planetarium project
|
|
// with every game-specific piece (SolarSystem, Planet, UI, picking)
|
|
// removed. Hook your own logic in via setUpdateCallback/setRenderCallback:
|
|
//
|
|
// Engine engine;
|
|
// engine.initialize(1600, 900, "My App");
|
|
// engine.setUpdateCallback([](float dt) { ... });
|
|
// engine.setRenderCallback([&](int fbWidth, int fbHeight) { ... });
|
|
// engine.run();
|
|
//
|
|
// Left-drag orbits the camera, middle-drag pans it, scroll zooms, and R
|
|
// resets it - wire up more input in your own update/render callbacks via
|
|
// window() if you need it.
|
|
// -----------------------------------------------------------------------
|
|
class Engine {
|
|
public:
|
|
using UpdateCallback = std::function<void(float deltaTime)>;
|
|
using RenderCallback = std::function<void(int framebufferWidth, int framebufferHeight)>;
|
|
|
|
~Engine();
|
|
|
|
bool initialize(int width, int height, const char* title);
|
|
void run();
|
|
|
|
void setUpdateCallback(UpdateCallback cb) { updateCallback_ = std::move(cb); }
|
|
void setRenderCallback(RenderCallback cb) { renderCallback_ = std::move(cb); }
|
|
|
|
Camera& camera() { return camera_; }
|
|
const Camera& camera() const { return camera_; }
|
|
GLFWwindow* window() const { return window_; }
|
|
|
|
private:
|
|
void update(float deltaTime);
|
|
void render();
|
|
|
|
// GLFW callback trampolines (static -> instance via glfwGetWindowUserPointer).
|
|
static void cursorPosCallback(GLFWwindow* window, double x, double y);
|
|
static void mouseButtonCallback(GLFWwindow* window, int button, int action, int mods);
|
|
static void scrollCallback(GLFWwindow* window, double xOffset, double yOffset);
|
|
static void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods);
|
|
static void framebufferSizeCallback(GLFWwindow* window, int width, int height);
|
|
|
|
GLFWwindow* window_ = nullptr;
|
|
Camera camera_;
|
|
|
|
UpdateCallback updateCallback_;
|
|
RenderCallback renderCallback_;
|
|
|
|
int framebufferWidth_ = 1600;
|
|
int framebufferHeight_ = 900;
|
|
|
|
double lastMouseX_ = 0.0;
|
|
double lastMouseY_ = 0.0;
|
|
bool rotating_ = false;
|
|
bool panning_ = false;
|
|
|
|
double lastFrameTime_ = 0.0;
|
|
};
|
|
|
|
} // namespace planetarium
|