diff options
author | Clyne Sullivan <clyne@bitgloo.com> | 2019-09-26 10:38:12 -0400 |
---|---|---|
committer | Clyne Sullivan <clyne@bitgloo.com> | 2019-09-26 10:38:12 -0400 |
commit | 00da4d690af08d45788d770f9aadc9548438f074 (patch) | |
tree | 171f959df04a4d322888198d5c8f48635f0bf3b6 | |
parent | ff93f760dd50f078779bdabcd41626a45bea6548 (diff) |
made Config static; added fps measure/text
-rw-r--r-- | Shaders/ui.vert | 2 | ||||
-rw-r--r-- | src/config.cpp | 4 | ||||
-rw-r--r-- | src/config.hpp | 15 | ||||
-rw-r--r-- | src/engine.cpp | 18 | ||||
-rw-r--r-- | src/engine.hpp | 8 | ||||
-rw-r--r-- | src/text.cpp | 7 |
6 files changed, 40 insertions, 14 deletions
diff --git a/Shaders/ui.vert b/Shaders/ui.vert index d2e3902..a940ebc 100644 --- a/Shaders/ui.vert +++ b/Shaders/ui.vert @@ -12,7 +12,7 @@ out vec4 color; void main(){ texCoord = tex_coord; - color = vec4(0.0f, 0.0f, 0.0f, 1.0f); + color = vec4(1.0f, 1.0f, 1.0f, 1.0f); //color = tex_color; //gl_Position = ortho * vec4(coord2d.xyz, 1.0f); gl_Position = projection * view * model * vec4(coord2d.xyz, 1.0f); diff --git a/src/config.cpp b/src/config.cpp index 4f5847c..85cc413 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -24,7 +24,9 @@ #include <cereal/archives/json.hpp> #include <fstream> -void Config::save(void) const +std::map<std::string, std::variant<int, double, std::string>> Config::values; + +void Config::save(void) { if (std::ofstream file (fileName); file.good()) { cereal::JSONOutputArchive archive (file); diff --git a/src/config.hpp b/src/config.hpp index a57a5db..8b259db 100644 --- a/src/config.hpp +++ b/src/config.hpp @@ -30,11 +30,11 @@ class Config { private: static constexpr const char *fileName = "settings.json"; - std::map<std::string, std::variant<int, double, std::string>> values; + static std::map<std::string, std::variant<int, double, std::string>> values; public: template<typename T> - std::optional<T> get(const std::string& name) + static std::optional<T> get(const std::string& name) { if (values.count(name) != 0) { if (auto value = std::get_if<T>(&values[name]); value != nullptr) @@ -45,19 +45,20 @@ public: } template<typename T> - void set(const std::string& name, T val) + static void set(const std::string& name, T val) { values[name] = val; } template<typename T> - void add(const std::string& name) + static void require(const std::string& name, T defaultValue = T()) { - values[name] = T(); + if (values.count(name) == 0) + values[name] = defaultValue(); } - void save(void) const; - void load(void); + static void save(void); + static void load(void); }; #endif // CONFIG_HPP_ diff --git a/src/engine.cpp b/src/engine.cpp index a000979..e53f266 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -36,6 +36,7 @@ #include <fstream> +using namespace std::literals::string_literals; using namespace std::chrono_literals; namespace cr = std::chrono; typedef std::chrono::high_resolution_clock mc; @@ -137,17 +138,20 @@ void Engine::physicsLoop(void) } } -void Engine::renderLoop(void) +void Engine::renderLoop(int& fpsCounter) { entityx::TimeDelta dt = 0; /**< Elapsed milliseconds since each loop */ while (shouldRun()) { systems.update<TextSystem>(dt); systems.update<RenderSystem>(dt); + fpsCounter++; } } void Engine::run(void) { + int fpsCounter = 0; + // Start logic thread logicThread = std::thread([this](void) { logicLoop(); @@ -157,12 +161,22 @@ void Engine::run(void) physicsLoop(); }); + debugThread = std::thread([this, &fpsCounter](void) { + while (shouldRun()) { + std::this_thread::sleep_for(1s); + fps = fpsCounter; + fpsCounter = 0; + systems.system<TextSystem>()->put("default", 0, 0, "fps: "s + std::to_string(fps)); + } + }); + // Keep render loop on main thread - renderLoop(); + renderLoop(fpsCounter); // Done, bring logic thread back logicThread.join(); physicsThread.join(); + debugThread.join(); // Save the entities' data GameState::save("save.json", entities); diff --git a/src/engine.hpp b/src/engine.hpp index ab6f16b..c2b3b58 100644 --- a/src/engine.hpp +++ b/src/engine.hpp @@ -36,19 +36,23 @@ private: entityx::EntityManager entities; entityx::SystemManager systems; + int fps; + std::thread logicThread; std::thread physicsThread; + std::thread debugThread; void logicLoop(void); void physicsLoop(void); - void renderLoop(void); + void renderLoop(int& fpsCounter); bool shouldRun(void); public: Engine(void) : entities(events), - systems(entities, events) {} + systems(entities, events), + fps(0) {} /** * Initializes the game engine. diff --git a/src/text.cpp b/src/text.cpp index e0eb158..9b59f8a 100644 --- a/src/text.cpp +++ b/src/text.cpp @@ -30,7 +30,6 @@ void TextSystem::update([[maybe_unused]] entityx::EntityManager& entites, if (d.text.size() == 0) continue; - // TODO make normal events.emit<NewRenderEvent>(d.vbo, d.tex, 0, d.buffer.size()); } } @@ -131,6 +130,12 @@ void TextSystem::put(const std::string& font, if (fontData.find(font) == fontData.end()) return; + auto& vector = fontData[font].text; + if (auto i = std::find_if(vector.begin(), vector.end(), [&x, &y](const Text& t) { + return t.x == x && t.y == y; }); i != vector.end()) { + vector.erase(i); + } + fontData[font].text.emplace_back(text, x, y, -9.0f); shouldUpdateVBOs = true; } |