]> git.localhorst.tv Git - blobs.git/blob - src/app/states.cpp
concerning orbits
[blobs.git] / src / app / states.cpp
1 #include "MasterState.hpp"
2
3 #include "../world/Body.hpp"
4 #include "../world/Simulation.hpp"
5
6 #include <glm/gtx/transform.hpp>
7
8
9 namespace blobs {
10 namespace app {
11
12 MasterState::MasterState(Assets &assets, world::Simulation &sim) noexcept
13 : State()
14 , assets(assets)
15 , sim(sim)
16 , reference(&sim.Root())
17 , cam()
18 , remain(0)
19 , thirds(0) {
20         cam.View(glm::lookAt(glm::vec3(2.0f, 3.0f, 10.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f)));
21 }
22
23 MasterState::~MasterState() noexcept {
24 }
25
26
27 void MasterState::OnResize(int w, int h) {
28         cam.Aspect(float(w), float(h));
29 }
30
31 void MasterState::OnUpdate(int dt) {
32         remain += dt;
33         while (remain >= FrameMS()) {
34                 Tick();
35         }
36 }
37
38 void MasterState::Tick() {
39         sim.Tick();
40         remain -= FrameMS();
41         thirds = (thirds + 1) % 3;
42 }
43
44 int MasterState::FrameMS() const noexcept {
45         return thirds == 0 ? 16 : 17;
46 }
47
48
49 void MasterState::OnRender(graphics::Viewport &viewport) {
50         glm::mat4 ppos = reference->ToParent();
51         assets.shaders.planet_surface.Activate();
52         assets.shaders.planet_surface.SetMVP(glm::mat4(1.0f), cam.View(), cam.Projection());
53         assets.shaders.planet_surface.SetTexture(assets.textures.tiles);
54         assets.shaders.planet_surface.SetLight(glm::vec3(cam.View() * ppos[3]), glm::vec3(1.0f, 1.0f, 1.0f), 100.0f);
55         reference->Draw(assets, viewport);
56 }
57
58 }
59 }