diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2021-01-18 02:08:01 +0000 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2021-01-18 02:08:01 +0000 |
commit | 627aa88ad2559f41d5be62d36cdbf536a97e4246 (patch) | |
tree | 0e98c2e9c39db6ecc88ecf1914af608241d7c18e /application/main.cpp | |
parent | Helpers can be static (diff) | |
download | ilt-627aa88ad2559f41d5be62d36cdbf536a97e4246.tar.bz2 ilt-627aa88ad2559f41d5be62d36cdbf536a97e4246.tar.xz ilt-627aa88ad2559f41d5be62d36cdbf536a97e4246.zip |
Factor to support worlds, objects, windows etc
Diffstat (limited to 'application/main.cpp')
-rw-r--r-- | application/main.cpp | 65 |
1 files changed, 41 insertions, 24 deletions
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 <SDL2/SDL.h> +#include <chrono> #include <cmath> +#include <collection.hpp> +#include <game/physical.h> +#include <game/world.h> +#include <game/worldobject.h> #include <gfx/gl/camera.h> #include <gfx/gl/shader.h> #include <gfx/gl/transform.h> -#include <gfx/models/mesh.h> -#include <gfx/models/texture.h> #include <glm/glm.hpp> #include <memory> #include <numbers> @@ -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<double> + 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<Window> windows; + windows.create(DISPLAY_WIDTH, DISPLAY_HEIGHT, "OpenGL"); + + World world; + world.create<Monkey>(); - 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<double> + 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<std::chrono::milliseconds>(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>(&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; } } }; |