diff options
author | Clyne Sullivan <clyne@bitgloo.com> | 2019-09-26 11:35:31 -0400 |
---|---|---|
committer | Clyne Sullivan <clyne@bitgloo.com> | 2019-09-26 11:35:31 -0400 |
commit | 5fa8ec8586e3c9087fd68129e1b7d7f94e847a09 (patch) | |
tree | 451946789bd3fc0b79a80b5b3007d26c2159f428 | |
parent | 00da4d690af08d45788d770f9aadc9548438f074 (diff) |
begin config usage; text positioning
-rw-r--r-- | src/config.cpp | 7 | ||||
-rw-r--r-- | src/config.hpp | 4 | ||||
-rw-r--r-- | src/engine.cpp | 6 | ||||
-rw-r--r-- | src/main.cpp | 13 | ||||
-rw-r--r-- | src/render.cpp | 35 | ||||
-rw-r--r-- | src/render.hpp | 6 | ||||
-rw-r--r-- | src/text.cpp | 3 | ||||
-rw-r--r-- | src/text.hpp | 2 |
8 files changed, 39 insertions, 37 deletions
diff --git a/src/config.cpp b/src/config.cpp index 85cc413..c70546c 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -22,9 +22,14 @@ #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) { diff --git a/src/config.hpp b/src/config.hpp index 8b259db..095ca56 100644 --- a/src/config.hpp +++ b/src/config.hpp @@ -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> diff --git a/src/engine.cpp b/src/engine.cpp index e53f266..b1d9a56 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -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) { diff --git a/src/main.cpp b/src/main.cpp index 814a2d4..5f39d95 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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; } diff --git a/src/render.cpp b/src/render.cpp index 9e63a71..cc7ecb1 100644 --- a/src/render.cpp +++ b/src/render.cpp @@ -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: " diff --git a/src/render.hpp b/src/render.hpp index 88668cc..c4456cf 100644 --- a/src/render.hpp +++ b/src/render.hpp @@ -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; diff --git a/src/text.cpp b/src/text.cpp index 9b59f8a..6917a2c 100644 --- a/src/text.cpp +++ b/src/text.cpp @@ -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()) { diff --git a/src/text.hpp b/src/text.hpp index d68613d..7cf90b2 100644 --- a/src/text.hpp +++ b/src/text.hpp @@ -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; |