]> code.bitgloo.com Git - clyne/gamedev2.git/commitdiff
begin config usage; text positioning
authorClyne Sullivan <clyne@bitgloo.com>
Thu, 26 Sep 2019 15:35:31 +0000 (11:35 -0400)
committerClyne Sullivan <clyne@bitgloo.com>
Thu, 26 Sep 2019 15:35:31 +0000 (11:35 -0400)
src/config.cpp
src/config.hpp
src/engine.cpp
src/main.cpp
src/render.cpp
src/render.hpp
src/text.cpp
src/text.hpp

index 85cc4135d3fbc57691d1227eca5c725fbabef0b1..c70546c1bc5e5fa31a217e39de63909fe22d1e60 100644 (file)
 
 #include <cereal/cereal.hpp>
 #include <cereal/archives/json.hpp>
+#include <filesystem>
 #include <fstream>
 
-std::map<std::string, std::variant<int, double, std::string>> Config::values;
+std::map<std::string, std::variant<int, double, std::string>> Config::values = {
+    { "title", "gamedev2" },
+    { "screenWidth", 640 },
+    { "screenHeight", 480 }
+};
 
 void Config::save(void)
 {
index 8b259db2a159e801e84d652a06f2317c726e4dad..095ca563061c30f0effa968c534b86e89c09d8f5 100644 (file)
@@ -34,14 +34,14 @@ private:
 
 public:
     template<typename T>
-    static std::optional<T> get(const std::string& name)
+    static T get(const std::string& name)
     {
         if (values.count(name) != 0) {
             if (auto value = std::get_if<T>(&values[name]); value != nullptr)
                 return *value;
         }
     
-        return {};
+        return T(); 
     }
 
     template<typename T>
index e53f266acf2551de03dca53ed5eec406f77edb27..b1d9a562f20552676ca8aa5f0085e966c1e45acf 100644 (file)
@@ -19,6 +19,7 @@
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 
+#include "config.hpp"
 #include "engine.hpp"
 #include "gamestate.hpp"
 #include "gamerun.hpp"
@@ -43,6 +44,8 @@ typedef std::chrono::high_resolution_clock mc;
 
 int Engine::init(void)
 {
+    Config::load();
+
     systems.add<GameRunSystem>();
     systems.add<InputSystem>();
     systems.add<PlayerSystem>(entities);
@@ -178,8 +181,9 @@ void Engine::run(void)
     physicsThread.join();
     debugThread.join();
 
-    // Save the entities' data
+    // Save the entities' data, and settings data
     GameState::save("save.json", entities);
+    Config::save();
 
     // Remove all Lua references from entities
     entities.each<Scripted>([](entityx::Entity, Scripted &f) { 
index 814a2d49f651435719015af7d056e4809ae5aace..5f39d952cfb9538cb40bd7977676e2eb03ec6f78 100644 (file)
@@ -21,7 +21,6 @@
 #define GLEW_STATIC
 #define SOL_ALL_SAFETIES_ON = 1
 
-#include "config.hpp"
 #include "engine.hpp"
 
 #include <SDL2/SDL.h>
@@ -43,21 +42,9 @@ int main([[maybe_unused]] int argc, [[maybe_unused]] char *argv[])
     Engine engine;
     engine.init();
 
-    //Config config;
-    //config.add<int>("screenWidth");
-    //config.add<int>("screenHeight");
-    //config.add<std::string>("title");
-    //config.load();
-    //std::cout << *config.get<int>("screenWidth") << std::endl;
-
     // Go go go!
     engine.run();
 
-    //config.set("screenWidth", 10000);
-    //config.set("screenHeight", 481);
-    //config.set("title", "gamEdevvv2");
-    //config.save();
-
     return 0;
 }
 
index 9e63a712debb4e60844d05b5622a38f34456d010..cc7ecb15958664d2dadb2e099c36569df09d42f9 100644 (file)
@@ -18,6 +18,7 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include <config.hpp>
 #include <render.hpp>
 #include <components/Render.hpp>
 #include <components/Position.hpp>
@@ -31,6 +32,10 @@ void RenderSystem::configure([[maybe_unused]] entityx::EntityManager& entities,
     events.subscribe<WorldMeshUpdateEvent>(*this);
     events.subscribe<entityx::ComponentAddedEvent<Player>>(*this);
 
+    title = Config::get<std::string>("title");
+    width = Config::get<int>("screenWidth");
+    height = Config::get<int>("screenHeight");
+
     init();
 }
 
@@ -74,9 +79,9 @@ void RenderSystem::update([[maybe_unused]] entityx::EntityManager& entities,
     //                                        0.01f, 
     //                                        2048.0f);
 
-    float scale = 40.0f;
-    float scaleWidth = static_cast<float>(width) / scale;
-    float scaleHeight = static_cast<float>(height) / scale;
+    //float scale = 40.0f;
+    //float scaleWidth = static_cast<float>(width) / scale;
+    //float scaleHeight = static_cast<float>(height) / scale;
 
     //glm::mat4 projection = glm::ortho(-(scaleWidth/2),    // Left
     //                                   (scaleWidth/2),    // Right
@@ -257,16 +262,12 @@ void RenderSystem::update([[maybe_unused]] entityx::EntityManager& entities,
                        glm::vec3(0.0f, 0.0f, 1.0f),  // Facing
                        glm::vec3(0.0f, 1.0f, 0.0f)); // Up
 
-    scale = 1.0f;
-    scaleWidth = static_cast<float>(width) / scale;
-    scaleHeight = static_cast<float>(height) / scale;
-
-    projection = glm::ortho(-scaleWidth/2.0f, // Left
-                             scaleWidth/2,    // Right
-                            -scaleHeight/2,   // Bottom
-                             scaleHeight/2,   // Top
-                             100.0f,          // zFar
-                            -100.0f);         // zNear
+    projection = glm::ortho(0.0f,  // Left
+                            static_cast<float>(width),  // Right
+                            -static_cast<float>(height), // Top
+                            0.0f,
+                             100.0f,      // zFar
+                            -100.0f);     // zNear
 
     model = glm::mat4(1.0f);
 
@@ -318,10 +319,10 @@ int RenderSystem::init(void)
     }
 
     // Create window, managed by the unique_ptr
-    window.reset(SDL_CreateWindow(title,
-        SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
-        width, height,
-        SDL_WINDOW_OPENGL));
+    window.reset(SDL_CreateWindow(title.c_str(),
+                 SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
+                 width, height,
+                 SDL_WINDOW_OPENGL));
 
     if (window.get() == nullptr) {
         std::cerr << "SDL window creation failed: "
index 88668cc459f2672220578905cccedb67a877613b..c4456cf20327a611923d1539a8b6e3321d698c7b 100644 (file)
@@ -56,9 +56,9 @@ class RenderSystem : public entityx::System<RenderSystem>,
                      public entityx::Receiver<RenderSystem>
 {
 private:
-    constexpr static const char *title = "gamedev2";
-    constexpr static int width = 1280;
-    constexpr static int height = 720;
+    std::string title;
+    int width;
+    int height;
 
     std::unique_ptr<SDL_Window, void (*)(SDL_Window *)> window;
     SDL_GLContext context;
index 9b59f8ad383d6714b727c15db2774ecc50090697..6917a2cd26ca026c4a6a4ce220e1703dfc89054f 100644 (file)
@@ -53,6 +53,7 @@ void TextSystem::loadFont(const std::string& name,
     auto& face = fonts[file];
     FT_Set_Pixel_Sizes(face, 0, size);
     fontData.try_emplace(name);
+    fontData[name].fontSize = size;
 
     // Calculate dimensions of final texture
     //
@@ -130,6 +131,8 @@ void TextSystem::put(const std::string& font,
     if (fontData.find(font) == fontData.end())
         return;
 
+    y -= fontData[font].fontSize;
+
     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()) {
index d68613dad4e6cfc922db17adbbcefd691dd48f94..7cf90b2fd7dbb3c783133a1b1734667eb6adf6c0 100644 (file)
@@ -60,6 +60,8 @@ struct Text {
 
 // Stores texture and placement data for a font at a size.
 struct Font {
+    int fontSize;
+
     GLuint tex;
     GLuint vbo;