Browse Source

Settings: use json util, update window size to uint32_t

master
Riyyi 4 years ago
parent
commit
7042a42032
  1. 3
      inferno/src/inferno/assert.h
  2. 27
      inferno/src/inferno/settings.cpp
  3. 4
      inferno/src/inferno/window.cpp
  4. 9
      inferno/src/inferno/window.h

3
inferno/src/inferno/assert.h

@ -2,6 +2,7 @@
#define ASSERT_H #define ASSERT_H
#include <csignal> // raise #include <csignal> // raise
#include <cstdint> // uint32_t
#include "inferno/core.h" #include "inferno/core.h"
#include "inferno/io/log.h" #include "inferno/io/log.h"
@ -43,7 +44,7 @@ namespace Inferno {
#ifdef NF_ENABLE_ASSERTS #ifdef NF_ENABLE_ASSERTS
template<typename... P> template<typename... P>
inline void __assert_fail(const char* assertion, const char* file, unsigned int line, const char* function, P&&... parameters) inline void __assert_fail(const char* assertion, const char* file, uint32_t line, const char* function, P&&... parameters)
{ {
dangerln(false, "ASSERTION `{}' FAILED.", assertion); dangerln(false, "ASSERTION `{}' FAILED.", assertion);

27
inferno/src/inferno/settings.cpp

@ -1,8 +1,10 @@
#include <cstdint> // uint32_t
#include <string> // std::string #include <string> // std::string
#include "inferno/io/file.h" #include "inferno/io/file.h"
#include "inferno/io/log.h" #include "inferno/io/log.h"
#include "inferno/settings.h" #include "inferno/settings.h"
#include "inferno/util/json.h"
namespace Inferno { namespace Inferno {
@ -21,16 +23,23 @@ namespace Inferno {
nlohmann::json json; nlohmann::json json;
Settings::load(json); Settings::load(json);
try { if (!Json::hasProperty(json, "window")) {
m_properties.window.title = json["window"]["title"].get<std::string>(); warn() << "Settings has no window section, using default values";
m_properties.window.width = json["window"]["width"].get<int>(); return;
m_properties.window.height = json["window"]["height"].get<int>();
m_properties.window.fullscreen = json["window"]["fullscreen"].get<std::string>();
m_properties.window.vsync = json["window"]["vsync"].get<bool>();
}
catch (...) {
warn() << "Settings syntax error: using default values";
} }
auto window = json["window"];
auto title = Json::parseStringProperty(window, "title", false);
auto width = Json::parseUnsignedProperty(window, "width", false);
auto height = Json::parseUnsignedProperty(window, "height", false);
auto fullscreen = Json::parseStringProperty(window, "fullscreen", false);
auto vsync = Json::parseBoolProperty(window, "vsync", false);
if (title) m_properties.window.title = title.value();
if (width) m_properties.window.width = width.value();
if (height) m_properties.window.height = height.value();
if (fullscreen) m_properties.window.fullscreen = fullscreen.value();
if (vsync) m_properties.window.vsync = vsync.value();
} }
void Settings::destroy() void Settings::destroy()

4
inferno/src/inferno/window.cpp

@ -43,8 +43,8 @@ namespace Inferno {
void Window::initialize() void Window::initialize()
{ {
std::string title = m_properties.title; std::string title = m_properties.title;
unsigned int width = m_properties.width; uint32_t width = m_properties.width;
unsigned int height = m_properties.height; uint32_t height = m_properties.height;
bool vsync = m_properties.vsync; bool vsync = m_properties.vsync;
// Only init once // Only init once

9
inferno/src/inferno/window.h

@ -1,6 +1,7 @@
#ifndef WINDOW_H #ifndef WINDOW_H
#define WINDOW_H #define WINDOW_H
#include <cstdint> // uint32_t
#include <functional> // std::function #include <functional> // std::function
#include <memory> // std::shared_ptr #include <memory> // std::shared_ptr
#include <string> // std::string #include <string> // std::string
@ -14,8 +15,8 @@ namespace Inferno {
struct WindowProperties { struct WindowProperties {
std::string title { "Inferno" }; std::string title { "Inferno" };
int width = 1280; uint32_t width = 1280;
int height = 720; uint32_t height = 720;
std::string fullscreen { "windowed" }; // windowed/fullscreen/borderless std::string fullscreen { "windowed" }; // windowed/fullscreen/borderless
bool vsync = true; bool vsync = true;
}; };
@ -42,8 +43,8 @@ namespace Inferno {
bool shouldClose() const; bool shouldClose() const;
inline float getAspect() const { return static_cast<float>(m_properties.width) / static_cast<float>(m_properties.height); } inline float getAspect() const { return static_cast<float>(m_properties.width) / static_cast<float>(m_properties.height); }
inline int getWidth() const { return m_properties.width; } inline uint32_t getWidth() const { return m_properties.width; }
inline int getHeight() const { return m_properties.height; } inline uint32_t getHeight() const { return m_properties.height; }
inline GLFWwindow* getWindow() const { return m_window; } inline GLFWwindow* getWindow() const { return m_window; }
inline const std::shared_ptr<Context>& getContext() const { return m_context; } inline const std::shared_ptr<Context>& getContext() const { return m_context; }

Loading…
Cancel
Save