Skip to content

Commit

Permalink
Engine now supports window resize
Browse files Browse the repository at this point in the history
  • Loading branch information
BarthPaleologue committed Sep 26, 2023
1 parent ca83c49 commit 3607929
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 2 deletions.
7 changes: 7 additions & 0 deletions core/Engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ Engine::Engine(int windowWidth, int windowHeight, const char *name = "Feather Pr
engine->mouseY = ypos;
});

glfwSetFramebufferSizeCallback(window, [](GLFWwindow *window, int width, int height) {
glViewport(0, 0, width, height);

auto *engine = static_cast<Engine *>(glfwGetWindowUserPointer(window));
engine->onWindowResizeObservable.notifyObservers(width, height);
});

setClearColor(0.1f, 0.1f, 0.1f, 1.0f);

glDepthFunc(GL_LESS); // Specify the depth test for the z-buffer
Expand Down
2 changes: 2 additions & 0 deletions core/Engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ class Engine {

Observable<> onExecuteLoopObservable{};

Observable<int, int> onWindowResizeObservable{};

Observable<int> onKeyPressObservable{};
Observable<int> onKeyReleaseObservable{};
Observable<double, double> onMouseScrollObservable{};
Expand Down
12 changes: 11 additions & 1 deletion core/Scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@

class Scene {
public:
explicit Scene(Engine *engine) {
_engine = engine;

_engine->onWindowResizeObservable.add([this](int width, int height) {
this->_activeCamera->setAspectRatio(static_cast<float>(width) / static_cast<float>(height));
});
}

void addMesh(Mesh *mesh);

void setActiveCamera(Camera *camera);
Expand All @@ -22,7 +30,9 @@ class Scene {
Observable<> onAfterRenderObservable{};

private:
std::vector<Mesh*> _meshes;
Engine *_engine;

std::vector<Mesh *> _meshes;
Camera *_activeCamera = nullptr;
};

Expand Down
2 changes: 1 addition & 1 deletion main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const int WINDOW_HEIGHT = 600;
int main() {
Engine engine(WINDOW_WIDTH, WINDOW_HEIGHT, "Solar System");

Scene scene;
Scene scene(&engine);

OrbitCamera camera(&engine);
camera.setPosition(0, 0, 20);
Expand Down

0 comments on commit 3607929

Please sign in to comment.