]> git.localhorst.tv Git - blobs.git/blob - src/app/states.cpp
figure out our creature's metabolism
[blobs.git] / src / app / states.cpp
1 #include "MasterState.hpp"
2
3 #include "../world/Body.hpp"
4 #include "../world/Creature.hpp"
5 #include "../world/Planet.hpp"
6 #include "../world/Simulation.hpp"
7 #include "../world/Sun.hpp"
8
9 #include <glm/gtx/transform.hpp>
10
11
12 namespace blobs {
13 namespace app {
14
15 MasterState::MasterState(Assets &assets, world::Simulation &sim) noexcept
16 : State()
17 , assets(assets)
18 , sim(sim)
19 , cam(sim.Root())
20 , remain(0)
21 , thirds(0)
22 , paused(false) {
23 }
24
25 MasterState::~MasterState() noexcept {
26 }
27
28
29 void MasterState::OnResize(int w, int h) {
30         cam.Aspect(float(w), float(h));
31 }
32
33 void MasterState::OnUpdate(int dt) {
34         remain += dt;
35         while (remain >= FrameMS()) {
36                 Tick();
37         }
38 }
39
40 void MasterState::Tick() {
41         if (!paused) {
42                 sim.Tick();
43         }
44         remain -= FrameMS();
45         thirds = (thirds + 1) % 3;
46 }
47
48 int MasterState::FrameMS() const noexcept {
49         return thirds == 0 ? 16 : 17;
50 }
51
52
53 void MasterState::OnKeyDown(const SDL_KeyboardEvent &e) {
54         if (e.keysym.sym == SDLK_p) {
55                 paused = !paused;
56         }
57 }
58
59 void MasterState::OnRender(graphics::Viewport &viewport) {
60
61         int num_lights = 0;
62         for (auto sun : sim.Suns()) {
63                 // TODO: source sun's light color and strength
64                 glm::vec3 pos(cam.View() * cam.Model(*sun)[3]);
65                 glm::vec3 col(1.0f, 1.0f, 1.0f);
66                 float str = 1.0e6f;
67                 assets.shaders.planet_surface.Activate();
68                 assets.shaders.planet_surface.SetLight(num_lights, pos, col, str);
69                 assets.shaders.creature_skin.Activate();
70                 assets.shaders.creature_skin.SetLight(num_lights, pos, col, str);
71                 ++num_lights;
72                 if (num_lights >= graphics::PlanetSurface::MAX_LIGHTS || num_lights >= graphics::CreatureSkin::MAX_LIGHTS) {
73                         break;
74                 }
75         }
76         for (auto planet : sim.Planets()) {
77                 // TODO: indirect light from planets, calculate strength and get color somehow
78                 glm::vec3 pos(cam.View() * cam.Model(*planet)[3]);
79                 glm::vec3 col(1.0f, 1.0f, 1.0f);
80                 float str = 10.0f;
81                 assets.shaders.planet_surface.Activate();
82                 assets.shaders.planet_surface.SetLight(num_lights, pos, col, str);
83                 assets.shaders.creature_skin.Activate();
84                 assets.shaders.creature_skin.SetLight(num_lights, pos, col, str);
85                 ++num_lights;
86                 if (num_lights >= graphics::PlanetSurface::MAX_LIGHTS || num_lights >= graphics::CreatureSkin::MAX_LIGHTS) {
87                         break;
88                 }
89         }
90         assets.shaders.planet_surface.Activate();
91         assets.shaders.planet_surface.SetNumLights(num_lights);
92         assets.shaders.creature_skin.Activate();
93         assets.shaders.creature_skin.SetNumLights(num_lights);
94
95         assets.shaders.planet_surface.Activate();
96         assets.shaders.planet_surface.SetTexture(assets.textures.tiles);
97         for (auto planet : sim.Planets()) {
98                 assets.shaders.planet_surface.SetMVP(cam.Model(*planet), cam.View(), cam.Projection());
99                 planet->Draw(assets, viewport);
100         }
101
102         assets.shaders.sun_surface.Activate();
103         for (auto sun : sim.Suns()) {
104                 double sun_radius = sun->Radius();
105                 assets.shaders.sun_surface.SetMVP(
106                         cam.Model(*sun) * glm::scale(glm::vec3(sun_radius, sun_radius, sun_radius)),
107                         cam.View(), cam.Projection());
108                 assets.shaders.sun_surface.SetLight(glm::vec3(1.0f, 1.0f, 1.0f), 1.0e6f);
109                 assets.shaders.sun_surface.Draw();
110         }
111
112         assets.shaders.creature_skin.Activate();
113         assets.shaders.creature_skin.SetTexture(assets.textures.skins);
114         // TODO: extend to nearby bodies as well
115         for (auto c : cam.Reference().Creatures()) {
116                 assets.shaders.creature_skin.SetMVP(cam.Model(c->GetBody()) * glm::mat4(c->LocalTransform()), cam.View(), cam.Projection());
117                 c->Draw(assets, viewport);
118         }
119 }
120
121 }
122 }