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