]> git.localhorst.tv Git - blobs.git/blobdiff - src/app/states.cpp
hooray for könig lookup
[blobs.git] / src / app / states.cpp
index 86b2e82c3b71e3d9d9ab630a515facde11573ab9..cbb2309581f05d35f0babf00374b0b97729ccaf0 100644 (file)
@@ -1,5 +1,8 @@
 #include "MasterState.hpp"
 
+#include "../creature/Creature.hpp"
+#include "../graphics/Viewport.hpp"
+#include "../math/const.hpp"
 #include "../world/Body.hpp"
 #include "../world/Planet.hpp"
 #include "../world/Simulation.hpp"
@@ -16,6 +19,13 @@ MasterState::MasterState(Assets &assets, world::Simulation &sim) noexcept
 , assets(assets)
 , sim(sim)
 , cam(sim.Root())
+, cam_dist(5.0)
+, cam_tgt_dist(5.0)
+, cam_orient(PI * 0.375, PI * 0.25, 0.0)
+, cam_dragging(false)
+, cp(assets)
+, rp(sim)
+, tp(sim)
 , remain(0)
 , thirds(0)
 , paused(false) {
@@ -26,22 +36,48 @@ MasterState::~MasterState() noexcept {
 
 
 void MasterState::OnResize(int w, int h) {
+       assets.shaders.canvas.Activate();
+       assets.shaders.canvas.Resize(float(w), float(h));
+       assets.shaders.alpha_sprite.Activate();
+       assets.shaders.alpha_sprite.SetVP(glm::mat4(1.0f), glm::ortho(0.0f, float(w), float(h), 0.0f, 1.0e4f, -1.0e4f));
+
        cam.Aspect(float(w), float(h));
+       assets.shaders.planet_surface.Activate();
+       assets.shaders.planet_surface.SetVP(cam.View(), cam.Projection());
+       assets.shaders.sun_surface.Activate();
+       assets.shaders.sun_surface.SetVP(cam.View(), cam.Projection());
+       assets.shaders.creature_skin.Activate();
+       assets.shaders.creature_skin.SetVP(cam.View(), cam.Projection());
 }
 
 void MasterState::OnUpdate(int dt) {
        remain += dt;
-       while (remain >= FrameMS()) {
+#ifdef NDEBUG
+       int max_tick = 10;
+#else
+       // one tick per frame when debugging so pausing execution doesn't result in more ticks
+       int max_tick = 1;
+#endif
+       while (remain >= FrameMS() && max_tick > 0) {
                Tick();
+               --max_tick;
        }
 }
 
 void MasterState::Tick() {
+       constexpr double dt = 0.01666666666666666666666666666666;
        if (!paused) {
-               sim.Tick();
+               sim.Tick(dt);
        }
        remain -= FrameMS();
        thirds = (thirds + 1) % 3;
+
+       double cam_diff = cam_tgt_dist - cam_dist;
+       if (std::abs(cam_diff) > 0.001) {
+               cam_dist += cam_diff * 0.25;
+       } else {
+               cam_dist = cam_tgt_dist;
+       }
 }
 
 int MasterState::FrameMS() const noexcept {
@@ -55,51 +91,112 @@ void MasterState::OnKeyDown(const SDL_KeyboardEvent &e) {
        }
 }
 
+void MasterState::OnMouseDown(const SDL_MouseButtonEvent &e) {
+       if (e.button == SDL_BUTTON_RIGHT) {
+               SDL_SetRelativeMouseMode(SDL_TRUE);
+               cam_dragging = true;
+       }
+}
+
+void MasterState::OnMouseUp(const SDL_MouseButtonEvent &e) {
+       if (e.button == SDL_BUTTON_RIGHT) {
+               SDL_SetRelativeMouseMode(SDL_FALSE);
+               cam_dragging = false;
+       }
+}
+
+void MasterState::OnMouseMotion(const SDL_MouseMotionEvent &e) {
+       constexpr double pitch_scale = PI * 0.001;
+       constexpr double yaw_scale = PI * 0.002;
+       if (cam_dragging) {
+               cam_orient.x = glm::clamp(cam_orient.x + double(e.yrel) * pitch_scale, 0.0, PI * 0.499);
+               cam_orient.y = std::fmod(cam_orient.y + double(e.xrel) * yaw_scale, PI * 2.0);
+       }
+}
+
+void MasterState::OnMouseWheel(const SDL_MouseWheelEvent &e) {
+       constexpr double roll_scale = PI * 0.0625;
+       constexpr double zoom_scale = -1.0;
+       constexpr double zoom_base = 1.125;
+       cam_orient.z = glm::clamp(cam_orient.z + double(e.x) * roll_scale, PI * -0.5, PI * 0.5);
+       cam_tgt_dist = std::max(cp.GetCreature().Size() * 2.0, cam_tgt_dist * std::pow(zoom_base, double(e.y) * zoom_scale));
+}
+
 void MasterState::OnRender(graphics::Viewport &viewport) {
-       assets.shaders.planet_surface.Activate();
-       assets.shaders.planet_surface.SetTexture(assets.textures.tiles);
+       if (cp.Shown()) {
+               cam.Radial(cp.GetCreature(), cam_dist, cam_orient);
+               assets.shaders.planet_surface.Activate();
+               assets.shaders.planet_surface.SetV(cam.View());
+               assets.shaders.sun_surface.Activate();
+               assets.shaders.sun_surface.SetV(cam.View());
+               assets.shaders.creature_skin.Activate();
+               assets.shaders.creature_skin.SetV(cam.View());
+       }
 
        int num_lights = 0;
        for (auto sun : sim.Suns()) {
                // TODO: source sun's light color and strength
-               assets.shaders.planet_surface.SetLight(
-                       num_lights,
-                       glm::vec3(cam.View() * cam.Model(*sun)[3]),
-                       glm::vec3(1.0f, 1.0f, 1.0f),
-                       1.0e6f);
+               glm::vec3 pos(cam.View() * cam.Model(*sun)[3]);
+               glm::vec3 col(1.0f, 1.0f, 1.0f);
+               float str = 1.0e6f;
+               assets.shaders.planet_surface.Activate();
+               assets.shaders.planet_surface.SetLight(num_lights, pos, col, str);
+               assets.shaders.creature_skin.Activate();
+               assets.shaders.creature_skin.SetLight(num_lights, pos, col, str);
                ++num_lights;
-               if (num_lights >= graphics::PlanetSurface::MAX_LIGHTS) {
+               if (num_lights >= graphics::PlanetSurface::MAX_LIGHTS || num_lights >= graphics::CreatureSkin::MAX_LIGHTS) {
                        break;
                }
        }
        for (auto planet : sim.Planets()) {
                // TODO: indirect light from planets, calculate strength and get color somehow
-               assets.shaders.planet_surface.SetLight(
-                       num_lights,
-                       glm::vec3(cam.View() * cam.Model(*planet)[3]),
-                       glm::vec3(1.0f, 1.0f, 1.0f),
-                       10.0f);
+               glm::vec3 pos(cam.View() * cam.Model(*planet)[3]);
+               glm::vec3 col(1.0f, 1.0f, 1.0f);
+               float str = 10.0f;
+               assets.shaders.planet_surface.Activate();
+               assets.shaders.planet_surface.SetLight(num_lights, pos, col, str);
+               assets.shaders.creature_skin.Activate();
+               assets.shaders.creature_skin.SetLight(num_lights, pos, col, str);
                ++num_lights;
-               if (num_lights >= graphics::PlanetSurface::MAX_LIGHTS) {
+               if (num_lights >= graphics::PlanetSurface::MAX_LIGHTS || num_lights >= graphics::CreatureSkin::MAX_LIGHTS) {
                        break;
                }
        }
+       assets.shaders.planet_surface.Activate();
        assets.shaders.planet_surface.SetNumLights(num_lights);
+       assets.shaders.creature_skin.Activate();
+       assets.shaders.creature_skin.SetNumLights(num_lights);
 
+       assets.shaders.planet_surface.Activate();
+       assets.shaders.planet_surface.SetTexture(assets.textures.tiles);
        for (auto planet : sim.Planets()) {
-               assets.shaders.planet_surface.SetMVP(cam.Model(*planet), cam.View(), cam.Projection());
+               assets.shaders.planet_surface.SetM(cam.Model(*planet));
                planet->Draw(assets, viewport);
        }
 
        assets.shaders.sun_surface.Activate();
        for (auto sun : sim.Suns()) {
                double sun_radius = sun->Radius();
-               assets.shaders.sun_surface.SetMVP(
-                       cam.Model(*sun) * glm::scale(glm::vec3(sun_radius, sun_radius, sun_radius)),
-                       cam.View(), cam.Projection());
+               assets.shaders.sun_surface.SetM(
+                       cam.Model(*sun) * glm::scale(glm::vec3(sun_radius, sun_radius, sun_radius)));
                assets.shaders.sun_surface.SetLight(glm::vec3(1.0f, 1.0f, 1.0f), 1.0e6f);
                assets.shaders.sun_surface.Draw();
        }
+
+       assets.shaders.creature_skin.Activate();
+       assets.shaders.creature_skin.SetTexture(assets.textures.skins);
+       // TODO: extend to nearby bodies as well
+       for (auto c : cam.Reference().Creatures()) {
+               assets.shaders.creature_skin.SetM(cam.Model(cam.Reference()) * glm::mat4(c->LocalTransform()));
+               assets.shaders.creature_skin.SetBaseColor(glm::vec3(c->BaseColor()));
+               assets.shaders.creature_skin.SetHighlightColor(glm::vec4(c->HighlightColor()));
+               c->Draw(viewport);
+       }
+
+       viewport.ClearDepth();
+       cp.Draw(viewport);
+       rp.Draw(viewport);
+       tp.Draw(viewport);
 }
 
 }