diff options
Diffstat (limited to 'src/game.cpp')
| -rw-r--r-- | src/game.cpp | 75 |
1 files changed, 75 insertions, 0 deletions
| diff --git a/src/game.cpp b/src/game.cpp new file mode 100644 index 0000000..b3fa9a8 --- /dev/null +++ b/src/game.cpp | |||
| @@ -0,0 +1,75 @@ | |||
| 1 | #include "game.h" | ||
| 2 | #include "components/animatable.h" | ||
| 3 | #include "components/transformable.h" | ||
| 4 | #include "components/controllable.h" | ||
| 5 | #include "components/droppable.h" | ||
| 6 | #include "components/ponderable.h" | ||
| 7 | #include "systems/rendering.h" | ||
| 8 | #include "systems/controlling.h" | ||
| 9 | #include "systems/pondering.h" | ||
| 10 | |||
| 11 | void key_callback(GLFWwindow* window, int key, int, int action, int) | ||
| 12 | { | ||
| 13 | Game& game = *((Game*) glfwGetWindowUserPointer(window)); | ||
| 14 | |||
| 15 | if ((action == GLFW_PRESS) && (key == GLFW_KEY_ESCAPE)) | ||
| 16 | { | ||
| 17 | game.shouldQuit = true; | ||
| 18 | |||
| 19 | return; | ||
| 20 | } | ||
| 21 | |||
| 22 | game.systemManager.getSystem<ControllingSystem>().input(key, action); | ||
| 23 | } | ||
| 24 | |||
| 25 | Game::Game(GLFWwindow* window) : window(window) | ||
| 26 | { | ||
| 27 | systemManager.emplaceSystem<ControllingSystem>(*this); | ||
| 28 | systemManager.emplaceSystem<RenderingSystem>(*this); | ||
| 29 | systemManager.emplaceSystem<PonderingSystem>(*this); | ||
| 30 | |||
| 31 | int player = entityManager.emplaceEntity(); | ||
| 32 | entityManager.emplaceComponent<AnimatableComponent>(player, "res/Starla.png", 10, 12, 6); | ||
| 33 | entityManager.emplaceComponent<TransformableComponent>(player, 203, 44, 10, 12); | ||
| 34 | entityManager.emplaceComponent<DroppableComponent>(player); | ||
| 35 | entityManager.emplaceComponent<PonderableComponent>(player); | ||
| 36 | entityManager.emplaceComponent<ControllableComponent>(player); | ||
| 37 | |||
| 38 | glfwSwapInterval(1); | ||
| 39 | glfwSetWindowUserPointer(window, this); | ||
| 40 | glfwSetKeyCallback(window, key_callback); | ||
| 41 | } | ||
| 42 | |||
| 43 | void Game::execute() | ||
| 44 | { | ||
| 45 | double lastTime = glfwGetTime(); | ||
| 46 | const double dt = 0.01; | ||
| 47 | double accumulator = 0.0; | ||
| 48 | |||
| 49 | while (!(shouldQuit || glfwWindowShouldClose(window))) | ||
| 50 | { | ||
| 51 | double currentTime = glfwGetTime(); | ||
| 52 | double frameTime = currentTime - lastTime; | ||
| 53 | lastTime = currentTime; | ||
| 54 | |||
| 55 | glfwPollEvents(); | ||
| 56 | |||
| 57 | accumulator += frameTime; | ||
| 58 | while (accumulator >= dt) | ||
| 59 | { | ||
| 60 | systemManager.getSystem<ControllingSystem>().tick(dt); | ||
| 61 | systemManager.getSystem<PonderingSystem>().tick(dt); | ||
| 62 | |||
| 63 | accumulator -= dt; | ||
| 64 | } | ||
| 65 | |||
| 66 | systemManager.getSystem<RenderingSystem>().tick(frameTime); | ||
| 67 | } | ||
| 68 | } | ||
| 69 | |||
| 70 | EntityManager& Game::getEntityManager() | ||
| 71 | { | ||
| 72 | return entityManager; | ||
| 73 | } | ||
| 74 | |||
| 75 | |||
