namespace blank {
+class Assets;
class Chunk;
class BlendedSprite;
class DirectionalLighting;
-class Assets;
+class FrameCounter;
class World;
class Interface {
bool visual_disabled = false;
};
- Interface(const Config &, const Assets &, World &);
+ Interface(const Config &, const Assets &, const FrameCounter &, World &);
void HandlePress(const SDL_KeyboardEvent &);
void HandleRelease(const SDL_KeyboardEvent &);
void SelectNext();
void SelectPrevious();
+ void ToggleCounter();
+ void UpdateCounter();
+
void Update(int dt);
void Render(DirectionalLighting &, BlendedSprite &) noexcept;
void CheckAim();
private:
+ const FrameCounter &counter;
World &world;
FPSController ctrl;
Font font;
OutlineModel outline;
glm::mat4 outline_transform;
+ bool show_counter;
+ Texture counter_tex;
+ SpriteModel counter_sprite;
+ glm::mat4 counter_transform;
+ SDL_Color counter_color;
+
Config config;
IntervalTimer place_timer;
#include "Interface.hpp"
#include "../app/Assets.hpp"
+#include "../app/FrameCounter.hpp"
#include "../app/init.hpp"
#include "../graphics/BlendedSprite.hpp"
#include "../graphics/DirectionalLighting.hpp"
#include <algorithm>
#include <cmath>
#include <iostream>
+#include <sstream>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtx/io.hpp>
}
-Interface::Interface(const Config &config, const Assets &assets, World &world)
-: world(world)
+Interface::Interface(
+ const Config &config,
+ const Assets &assets,
+ const FrameCounter &counter,
+ World &world)
+: counter(counter)
+, world(world)
, ctrl(world.Player())
, font(assets.LoadFont("DejaVuSans", 16))
, hud(world.BlockTypes(), font)
, aim_normal()
, outline()
, outline_transform(1.0f)
+, show_counter(false)
+, counter_tex()
+, counter_sprite()
+, counter_transform(1.0f)
+, counter_color{0xFF, 0xFF, 0xFF, 0xFF}
, config(config)
, place_timer(256)
, remove_timer(256)
case SDLK_p:
PrintSelectionInfo();
break;
+
+ case SDLK_F3:
+ ToggleCounter();
+ break;
}
}
<< std::endl;
}
+void Interface::ToggleCounter() {
+ if ((show_counter = !show_counter)) {
+ UpdateCounter();
+ }
+}
+
+void Interface::UpdateCounter() {
+ std::stringstream s;
+ s << std::setprecision(3) << counter.AvgRunning() << "ms";
+ std::string text = s.str();
+ font.Render(text.c_str(), counter_color, counter_tex);
+ glm::vec2 size(font.TextSize(text.c_str()));
+ counter_sprite.LoadRect(size.x, size.y);
+ counter_transform = glm::translate(glm::vec3(
+ 400.0f - size.x,
+ 25.0f,
+ 0.75f
+ ));
+}
+
void Interface::Handle(const SDL_MouseMotionEvent &event) {
if (config.mouse_disabled) return;
PlaceBlock();
CheckAim();
}
+
+ if (show_counter && counter.Changed()) {
+ UpdateCounter();
+ }
}
void Interface::CheckAim() {
outline.Draw();
}
+ if (show_counter) {
+ sprite_prog.Activate();
+ sprite_prog.SetM(counter_transform);
+ sprite_prog.SetTexture(counter_tex);
+ counter_sprite.Draw();
+ }
+
hud.Render(world_prog, sprite_prog);
}