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. 29
      inferno/src/inferno/settings.cpp
  3. 8
      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
#include <csignal> // raise
#include <cstdint> // uint32_t
#include "inferno/core.h"
#include "inferno/io/log.h"
@ -43,7 +44,7 @@ namespace Inferno {
#ifdef NF_ENABLE_ASSERTS
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);

29
inferno/src/inferno/settings.cpp

@ -1,8 +1,10 @@
#include <string> // std::string
#include <cstdint> // uint32_t
#include <string> // std::string
#include "inferno/io/file.h"
#include "inferno/io/log.h"
#include "inferno/settings.h"
#include "inferno/util/json.h"
namespace Inferno {
@ -21,16 +23,23 @@ namespace Inferno {
nlohmann::json json;
Settings::load(json);
try {
m_properties.window.title = json["window"]["title"].get<std::string>();
m_properties.window.width = json["window"]["width"].get<int>();
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";
if (!Json::hasProperty(json, "window")) {
warn() << "Settings has no window section, using default values";
return;
}
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()

8
inferno/src/inferno/window.cpp

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

9
inferno/src/inferno/window.h

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

Loading…
Cancel
Save