From 627aa88ad2559f41d5be62d36cdbf536a97e4246 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Mon, 18 Jan 2021 02:08:01 +0000 Subject: Factor to support worlds, objects, windows etc --- application/main.cpp | 65 +++++++++++++++++++++++++++++++++------------------- 1 file changed, 41 insertions(+), 24 deletions(-) (limited to 'application/main.cpp') diff --git a/application/main.cpp b/application/main.cpp index acf3c95..73f337b 100644 --- a/application/main.cpp +++ b/application/main.cpp @@ -1,11 +1,14 @@ #include "gfx/window.h" #include +#include #include +#include +#include +#include +#include #include #include #include -#include -#include #include #include #include @@ -14,6 +17,22 @@ static const int DISPLAY_WIDTH = 800; static const int DISPLAY_HEIGHT = 600; +class Monkey : public WorldObject, public Physical { +public: + Monkey() : Physical {{}, "res/monkey3.obj", "res/bricks.jpg"} { } + + void + tick(TickDuration elapsed) override + { + counter += 0.000625F * elapsed.count(); + location.GetRot().y = std::numbers::pi_v + sin(counter); + location.GetRot().z = 0.3 * cos(counter * 10); + } + +private: + float counter {}; +}; + class SDL_Application { public: SDL_Application() @@ -38,17 +57,21 @@ public: void run() { - Window main_window(DISPLAY_WIDTH, DISPLAY_HEIGHT, "OpenGL"); + Collection windows; + windows.create(DISPLAY_WIDTH, DISPLAY_HEIGHT, "OpenGL"); + + World world; + world.create(); - Mesh monkey("./res/monkey3.obj"); Shader shader("./res/basicShader"); - Texture texture("./res/bricks.jpg"); - Transform transform; - Camera camera(glm::vec3(0.0F, 0.0F, -5.0F), 70.0F, (float)DISPLAY_WIDTH / (float)DISPLAY_HEIGHT, 0.1F, 100.0F); + Camera camera({0.0F, 0.0F, -5.0F}, 70.0F, (float)DISPLAY_WIDTH / (float)DISPLAY_HEIGHT, 0.1F, 100.0F); SDL_Event e; bool isRunning = true; - float counter = 0.0F; + + auto t_start = std::chrono::high_resolution_clock::now(); + const auto framelen = std::chrono::milliseconds {1000} / 120; + while (isRunning) { while (SDL_PollEvent(&e)) { if (e.type == SDL_QUIT) { @@ -56,25 +79,19 @@ public: } } - main_window.Clear(0.0F, 0.0F, 0.0F, 1.0F); - - // float sinCounter = sinf(counter); - // float absSinCounter = abs(sinCounter); - - // transform.GetPos()->x = sinCounter; - transform.GetRot().y = std::numbers::pi_v + sin(counter); - transform.GetRot().z = 0.3 * cos(counter * 10); - // transform.GetScale()->x = absSinCounter; - // transform.GetScale()->y = absSinCounter; + const auto t_end = std::chrono::high_resolution_clock::now(); + const auto t_passed = std::chrono::duration_cast(t_end - t_start); + world.apply(&WorldObject::tick, t_passed); + windows.apply(&Window::Clear, 0.0F, 0.0F, 0.0F, 1.0F); shader.Bind(); - texture.Bind(); - shader.Update(transform, camera); - monkey.Draw(); + world.apply(&Physical::render, shader, camera); + windows.apply(&Window::SwapBuffers); - main_window.SwapBuffers(); - SDL_Delay(1); - counter += 0.01F; + if (const auto snz = framelen - t_passed; snz.count() > 0) { + SDL_Delay(snz.count()); + } + t_start = t_end; } } }; -- cgit v1.2.3