]> code.bitgloo.com Git - clyne/gamedev2.git/commitdiff
made Config static; added fps measure/text
authorClyne Sullivan <clyne@bitgloo.com>
Thu, 26 Sep 2019 14:38:12 +0000 (10:38 -0400)
committerClyne Sullivan <clyne@bitgloo.com>
Thu, 26 Sep 2019 14:38:12 +0000 (10:38 -0400)
Shaders/ui.vert
src/config.cpp
src/config.hpp
src/engine.cpp
src/engine.hpp
src/text.cpp

index d2e390287d829d8087983c86dd61ef1305ca729f..a940ebccaec04c0d24a7b364c1a5533a23af3cee 100644 (file)
@@ -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);
index 4f5847c4970bfbf73c1ef88700221baf1c5fc566..85cc4135d3fbc57691d1227eca5c725fbabef0b1 100644 (file)
@@ -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);
index a57a5db3723287253bbe5ab84823a6ef71466a21..8b259db2a159e801e84d652a06f2317c726e4dad 100644 (file)
@@ -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_
index a00097912867c43f96f9a7f9a03914f54b32b454..e53f266acf2551de03dca53ed5eec406f77edb27 100644 (file)
@@ -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);
index ab6f16b16613ec6be30c255ffe9ca68d18650bac..c2b3b5866820d7d13a332a9862677d78d004f794 100644 (file)
@@ -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.
index e0eb158d4619d4fd2d9c29da4d94813540e7b46a..9b59f8ad383d6714b727c15db2774ecc50090697 100644 (file)
@@ -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;
 }