adding application configuration reader/writer
adding application configuration reader/writer. this is used to pull in application level settings, such as Opengl options and location of project/theme/log paths
This commit is contained in:
@@ -188,7 +188,7 @@ INCLUDE_DIRECTORIES(${Qt5Gui_PRIVATE_INCLUDE_DIRS})
|
||||
|
||||
#Load noggit source files
|
||||
collect_files(noggit_root_sources src/noggit FALSE "*.cpp" "")
|
||||
collect_files(noggit_application_sources src/noggit/application FALSE "*.cpp" "")
|
||||
collect_files(noggit_application_sources src/noggit/application TRUE "*.cpp" "")
|
||||
collect_files(noggit_data_sources src/noggit/data FALSE "*.cpp" "")
|
||||
collect_files(noggit_simulation_sources src/noggit/simulation FALSE "*.cpp" "")
|
||||
collect_files(noggit_project_sources src/noggit/project FALSE "*.cpp" "")
|
||||
@@ -215,7 +215,7 @@ collect_files(util_sources src/util TRUE "*.c;*.cpp;" "")
|
||||
collect_files(util_headers src/util TRUE "*.h;*.hpp" "")
|
||||
|
||||
collect_files(noggit_root_headers src/noggit FALSE "*.h;*.hpp;*.inl" "")
|
||||
collect_files(noggit_application_headers src/noggit/application FALSE "*.h;*.hpp" "")
|
||||
collect_files(noggit_application_headers src/noggit/application TRUE "*.h;*.hpp" "")
|
||||
collect_files(noggit_data_sources src/noggit/data FALSE "*.h;*.hpp" "")
|
||||
collect_files(noggit_simulation_sources src/noggit/simulation FALSE "*.h;*.hpp" "")
|
||||
collect_files(noggit_project_sources src/noggit/project FALSE "*.h;*.hpp" "")
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
// This file is part of Noggit3, licensed under GNU General Public License (version 3).
|
||||
#ifndef NOGGIT_APPLICATION_CONFIGURATION_HPP
|
||||
#define NOGGIT_APPLICATION_CONFIGURATION_HPP
|
||||
|
||||
#include <string>
|
||||
#include <QSurfaceFormat>
|
||||
|
||||
namespace Noggit::Application {
|
||||
|
||||
struct NoggitApplicationLoggingConfiguration
|
||||
{
|
||||
std::string ApplicationLoggingPath;
|
||||
bool EnableConsoleLogging;
|
||||
};
|
||||
|
||||
struct NoggitApplicationGraphicsConfiguration
|
||||
{
|
||||
QSurfaceFormat::SwapBehavior SwapChainDepth;
|
||||
char SwapChainInternal;
|
||||
char DepthBufferSize;
|
||||
char SamplesCount;
|
||||
};
|
||||
|
||||
struct NoggitApplicationConfiguration
|
||||
{
|
||||
std::string ApplicationProjectPath;
|
||||
std::string ApplicationThemePath;
|
||||
NoggitApplicationGraphicsConfiguration GraphicsConfiguration;
|
||||
NoggitApplicationLoggingConfiguration LoggingConfiguration;
|
||||
};
|
||||
}
|
||||
|
||||
#endif NOGGIT_APPLICATION_CONFIGURATION_HPP
|
||||
@@ -0,0 +1,68 @@
|
||||
#include <noggit/application/Configuration/NoggitApplicationConfigurationReader.hpp>
|
||||
#include <QJsonObject>
|
||||
#include <QJsonDocument>
|
||||
|
||||
namespace Noggit::Application {
|
||||
|
||||
NoggitApplicationConfiguration NoggitApplicationConfigurationReader::ReadConfigurationState(QFile& inputFile)
|
||||
{
|
||||
inputFile.open(QIODevice::ReadOnly);
|
||||
|
||||
auto document = QJsonDocument().fromJson(inputFile.readAll());
|
||||
auto root = document.object();
|
||||
|
||||
auto noggitApplicationConfiguration = NoggitApplicationConfiguration();
|
||||
if (root.contains("Noggit") && root["Noggit"].isObject())
|
||||
{
|
||||
auto noggitConfiguration = root["Noggit"].toObject();
|
||||
if (noggitConfiguration.contains("ApplicationProjectPath"))
|
||||
noggitApplicationConfiguration.ApplicationProjectPath = noggitConfiguration["ApplicationProjectPath"].toString().toStdString();
|
||||
if (noggitConfiguration.contains("ApplicationThemePath"))
|
||||
noggitApplicationConfiguration.ApplicationThemePath = noggitConfiguration["ApplicationThemePath"].toString().toStdString();
|
||||
|
||||
if (noggitConfiguration.contains("GraphicsConfiguration") && noggitConfiguration["GraphicsConfiguration"].isObject())
|
||||
{
|
||||
auto noggitGraphicsConfiguration = noggitConfiguration["GraphicsConfiguration"].toObject();
|
||||
noggitApplicationConfiguration.GraphicsConfiguration = NoggitApplicationGraphicsConfiguration();
|
||||
|
||||
|
||||
if (noggitGraphicsConfiguration.contains("SwapChainDepth"))
|
||||
{
|
||||
auto swapChainName = noggitGraphicsConfiguration["SwapChainDepth"].toString().toStdString();
|
||||
|
||||
auto swapChainDepth = QSurfaceFormat::DefaultSwapBehavior;
|
||||
if (swapChainName == std::string("DEFAULT"))
|
||||
swapChainDepth = QSurfaceFormat::DefaultSwapBehavior;
|
||||
if (swapChainName == std::string("SINGLE"))
|
||||
swapChainDepth = QSurfaceFormat::SingleBuffer;
|
||||
if (swapChainName == std::string("DOUBLE"))
|
||||
swapChainDepth = QSurfaceFormat::DoubleBuffer;
|
||||
if (swapChainName == std::string("TRIPLE"))
|
||||
swapChainDepth = QSurfaceFormat::TripleBuffer;
|
||||
|
||||
noggitApplicationConfiguration.GraphicsConfiguration.SwapChainDepth = swapChainDepth;
|
||||
}
|
||||
|
||||
if (noggitGraphicsConfiguration.contains("SamplesCount"))
|
||||
noggitApplicationConfiguration.GraphicsConfiguration.SamplesCount = noggitGraphicsConfiguration["SamplesCount"].toInt();
|
||||
if (noggitGraphicsConfiguration.contains("SwapChainInternal"))
|
||||
noggitApplicationConfiguration.GraphicsConfiguration.SwapChainInternal = noggitGraphicsConfiguration["SwapChainInternal"].toInt();
|
||||
if (noggitGraphicsConfiguration.contains("DepthBufferSize"))
|
||||
noggitApplicationConfiguration.GraphicsConfiguration.DepthBufferSize = noggitGraphicsConfiguration["DepthBufferSize"].toInt();
|
||||
}
|
||||
|
||||
if (noggitConfiguration.contains("LoggingConfiguration") && noggitConfiguration["LoggingConfiguration"].isObject())
|
||||
{
|
||||
auto noggitLoggingConfiguration = noggitConfiguration["LoggingConfiguration"].toObject();
|
||||
noggitApplicationConfiguration.LoggingConfiguration = NoggitApplicationLoggingConfiguration();
|
||||
|
||||
if (noggitLoggingConfiguration.contains("ApplicationLoggingPath"))
|
||||
noggitApplicationConfiguration.LoggingConfiguration.ApplicationLoggingPath = noggitLoggingConfiguration["ApplicationLoggingPath"].toString().toStdString();
|
||||
if (noggitLoggingConfiguration.contains("EnableConsoleLogging"))
|
||||
noggitApplicationConfiguration.LoggingConfiguration.EnableConsoleLogging = noggitLoggingConfiguration["EnableConsoleLogging"].toBool();
|
||||
}
|
||||
}
|
||||
|
||||
return noggitApplicationConfiguration;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
#ifndef NOGGIT_APPLICATION_CONFIGURATION_READER_HPP
|
||||
#define NOGGIT_APPLICATION_CONFIGURATION_READER_HPP
|
||||
|
||||
#include <noggit/application/Configuration/NoggitApplicationConfiguration.hpp>
|
||||
#include <QFile>
|
||||
|
||||
namespace Noggit::Application {
|
||||
|
||||
class NoggitApplicationConfigurationReader
|
||||
{
|
||||
public:
|
||||
NoggitApplicationConfiguration ReadConfigurationState(QFile& inputFile);
|
||||
};
|
||||
}
|
||||
#endif NOGGIT_APPLICATION_CONFIGURATION_READER_HPP
|
||||
@@ -0,0 +1,63 @@
|
||||
#include <noggit/application/Configuration/NoggitApplicationConfigurationWriter.hpp>
|
||||
#include <QJsonObject>
|
||||
#include <QJsonDocument>
|
||||
|
||||
namespace Noggit::Application {
|
||||
|
||||
void NoggitApplicationConfigurationWriter::PersistDefaultConfigurationState(QFile& outputFile)
|
||||
{
|
||||
auto noggitApplicationConfiguration = NoggitApplicationConfiguration();
|
||||
noggitApplicationConfiguration.ApplicationProjectPath = std::string("projects");
|
||||
noggitApplicationConfiguration.ApplicationThemePath = std::string("themes");
|
||||
|
||||
noggitApplicationConfiguration.GraphicsConfiguration = NoggitApplicationGraphicsConfiguration();
|
||||
noggitApplicationConfiguration.GraphicsConfiguration.SwapChainDepth = QSurfaceFormat::TripleBuffer;
|
||||
noggitApplicationConfiguration.GraphicsConfiguration.DepthBufferSize = 16;
|
||||
noggitApplicationConfiguration.GraphicsConfiguration.SwapChainInternal = 0;
|
||||
noggitApplicationConfiguration.GraphicsConfiguration.SamplesCount = 0;
|
||||
|
||||
noggitApplicationConfiguration.LoggingConfiguration = NoggitApplicationLoggingConfiguration();
|
||||
noggitApplicationConfiguration.LoggingConfiguration.EnableConsoleLogging = false;
|
||||
noggitApplicationConfiguration.LoggingConfiguration.ApplicationLoggingPath = "log.txt";
|
||||
|
||||
PersistConfigurationState(outputFile, noggitApplicationConfiguration);
|
||||
}
|
||||
void NoggitApplicationConfigurationWriter::PersistConfigurationState(QFile& outputFile, const NoggitApplicationConfiguration& configuration)
|
||||
{
|
||||
outputFile.open(QIODevice::WriteOnly);
|
||||
|
||||
auto document = QJsonDocument();
|
||||
auto root = QJsonObject();
|
||||
auto rootConfiguration = QJsonObject();
|
||||
|
||||
auto swapChainDepth = std::string();
|
||||
if (configuration.GraphicsConfiguration.SwapChainDepth == QSurfaceFormat::DefaultSwapBehavior)
|
||||
swapChainDepth = std::string("DEFAULT");
|
||||
if (configuration.GraphicsConfiguration.SwapChainDepth == QSurfaceFormat::SingleBuffer)
|
||||
swapChainDepth = std::string("SINGLE");
|
||||
if (configuration.GraphicsConfiguration.SwapChainDepth == QSurfaceFormat::DoubleBuffer)
|
||||
swapChainDepth = std::string("DOUBLE");
|
||||
if (configuration.GraphicsConfiguration.SwapChainDepth == QSurfaceFormat::TripleBuffer)
|
||||
swapChainDepth = std::string("TRIPLE");
|
||||
|
||||
auto graphicsConfiguration = QJsonObject();
|
||||
graphicsConfiguration.insert("SwapChainDepth", swapChainDepth.c_str());
|
||||
graphicsConfiguration.insert("DepthBufferSize", configuration.GraphicsConfiguration.DepthBufferSize);
|
||||
graphicsConfiguration.insert("SwapChainInternal", configuration.GraphicsConfiguration.SwapChainInternal);
|
||||
graphicsConfiguration.insert("SamplesCount", configuration.GraphicsConfiguration.SamplesCount);
|
||||
|
||||
auto loggingConfiguration = QJsonObject();
|
||||
loggingConfiguration.insert("EnableConsoleLogging", configuration.LoggingConfiguration.EnableConsoleLogging);
|
||||
loggingConfiguration.insert("ApplicationLoggingPath", configuration.LoggingConfiguration.ApplicationLoggingPath.c_str());
|
||||
|
||||
rootConfiguration.insert("ApplicationProjectPath", configuration.ApplicationProjectPath.c_str());
|
||||
rootConfiguration.insert("ApplicationThemePath", configuration.ApplicationThemePath.c_str());
|
||||
rootConfiguration.insert("GraphicsConfiguration", graphicsConfiguration);
|
||||
rootConfiguration.insert("LoggingConfiguration", loggingConfiguration);
|
||||
|
||||
root.insert("Noggit", rootConfiguration);
|
||||
document.setObject(root);
|
||||
|
||||
outputFile.write(document.toJson(QJsonDocument::Indented));
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
#ifndef NOGGIT_APPLICATION_CONFIGURATION_WRITER_HPP
|
||||
#define NOGGIT_APPLICATION_CONFIGURATION_WRITER_HPP
|
||||
|
||||
#include <QFile>
|
||||
#include <noggit/application/Configuration/NoggitApplicationConfiguration.hpp>
|
||||
|
||||
namespace Noggit::Application {
|
||||
|
||||
class NoggitApplicationConfigurationWriter
|
||||
{
|
||||
public:
|
||||
void PersistDefaultConfigurationState(QFile& outputFile);
|
||||
void PersistConfigurationState(QFile& outputFile, const NoggitApplicationConfiguration& configuration);
|
||||
};
|
||||
}
|
||||
#endif NOGGIT_APPLICATION_CONFIGURATION_WRITER_HPP
|
||||
@@ -7,7 +7,6 @@ namespace Noggit::Application
|
||||
|
||||
Noggit::Noggit()
|
||||
: fullscreen(false)
|
||||
, doAntiAliasing(true)
|
||||
{
|
||||
|
||||
}
|
||||
@@ -15,34 +14,84 @@ namespace Noggit::Application
|
||||
void Noggit::Initalize(int argc, char* argv[])
|
||||
{
|
||||
InitLogging();
|
||||
assert(argc >= 1);
|
||||
(void)argc;
|
||||
|
||||
try
|
||||
{
|
||||
std::filesystem::path startupPath(argv[0]);
|
||||
startupPath.remove_filename();
|
||||
|
||||
if (startupPath.is_relative())
|
||||
{
|
||||
std::filesystem::current_path(std::filesystem::current_path() / startupPath);
|
||||
}
|
||||
else
|
||||
{
|
||||
std::filesystem::current_path(startupPath);
|
||||
}
|
||||
}
|
||||
catch (const std::filesystem::filesystem_error& ex)
|
||||
{
|
||||
LogError << ex.what() << std::endl;
|
||||
}
|
||||
|
||||
//Locate application relative path
|
||||
Log << "Noggit Studio - " << STRPRODUCTVER << std::endl;
|
||||
|
||||
QSettings settings;
|
||||
doAntiAliasing = settings.value("antialiasing", false).toBool();
|
||||
fullscreen = settings.value("fullscreen", false).toBool();
|
||||
auto applicationLocation = std::filesystem::path(argv[0]);
|
||||
Log << "Noggit Application Path: " << applicationLocation << std::endl;
|
||||
|
||||
auto applicationExecutionLocation = std::filesystem::current_path();
|
||||
Log << "Noggit Execution Path: " << applicationExecutionLocation << std::endl;
|
||||
|
||||
if (applicationLocation.remove_filename().is_relative())
|
||||
{
|
||||
std::filesystem::current_path(std::filesystem::current_path() / applicationLocation);
|
||||
}
|
||||
else
|
||||
{
|
||||
std::filesystem::current_path(applicationLocation);
|
||||
}
|
||||
|
||||
auto applicationCurrentPath = std::filesystem::current_path();
|
||||
Log << "Noggit Relative Path: " << applicationCurrentPath << std::endl;
|
||||
|
||||
//Locate application configuration file
|
||||
auto nogginConfigurationPath = applicationCurrentPath / "noggit.json";
|
||||
|
||||
if(!std::filesystem::exists(nogginConfigurationPath))
|
||||
{
|
||||
//Create Default config file
|
||||
Log << "Noggit Configuration File Not Found! Creating New File: " << nogginConfigurationPath << std::endl;
|
||||
|
||||
auto configurationFileStream = QFile(QString::fromStdString(nogginConfigurationPath.generic_string()));
|
||||
auto configurationFileWriter = NoggitApplicationConfigurationWriter();
|
||||
configurationFileWriter.PersistDefaultConfigurationState(configurationFileStream);
|
||||
configurationFileStream.close();
|
||||
}
|
||||
|
||||
//Read config file
|
||||
auto configurationFileStream = QFile(QString::fromStdString( nogginConfigurationPath.generic_string()));
|
||||
auto configurationFileReader = NoggitApplicationConfigurationReader();
|
||||
auto applicationConfiguration = configurationFileReader.ReadConfigurationState(configurationFileStream);
|
||||
|
||||
configurationFileStream.close();
|
||||
|
||||
Log << "Noggit Configuration File Loaded! Creating New File: " << nogginConfigurationPath << std::endl;
|
||||
|
||||
//Initalise OpenGL Context
|
||||
if (!QGLFormat::hasOpenGL())
|
||||
{
|
||||
throw std::runtime_error(
|
||||
"Your system does not support OpenGL. Sorry, this application can't run without it.");
|
||||
}
|
||||
|
||||
QSurfaceFormat format;
|
||||
format.setRenderableType(QSurfaceFormat::OpenGL);
|
||||
format.setVersion(4, 1);
|
||||
format.setProfile(QSurfaceFormat::CoreProfile);
|
||||
format.setSwapBehavior(applicationConfiguration.GraphicsConfiguration.SwapChainDepth);
|
||||
format.setSwapInterval(applicationConfiguration.GraphicsConfiguration.SwapChainInternal);
|
||||
format.setDepthBufferSize(applicationConfiguration.GraphicsConfiguration.DepthBufferSize);
|
||||
format.setSamples(applicationConfiguration.GraphicsConfiguration.SamplesCount);
|
||||
|
||||
QSurfaceFormat::setDefaultFormat(format);
|
||||
QOpenGLContext context;
|
||||
context.create();
|
||||
|
||||
QOffscreenSurface surface;
|
||||
surface.create();
|
||||
|
||||
context.makeCurrent(&surface);
|
||||
|
||||
OpenGL::context::scoped_setter const _(::gl, &context);
|
||||
|
||||
LogDebug << "GL: Version: " << gl.getString(GL_VERSION) << std::endl;
|
||||
LogDebug << "GL: Vendor: " << gl.getString(GL_VENDOR) << std::endl;
|
||||
LogDebug << "GL: Renderer: " << gl.getString(GL_RENDERER) << std::endl;
|
||||
|
||||
//All of the below should be Project Initalisation
|
||||
QSettings settings;
|
||||
|
||||
srand(::time(nullptr));
|
||||
QDir path(settings.value("project/game_path").toString());
|
||||
@@ -58,43 +107,6 @@ namespace Noggit::Application
|
||||
|
||||
settings.setValue("project/game_path", path.absolutePath());
|
||||
settings.setValue("project/path", QString::fromStdString(project_path));
|
||||
|
||||
if (!QGLFormat::hasOpenGL())
|
||||
{
|
||||
throw std::runtime_error(
|
||||
"Your system does not support OpenGL. Sorry, this application can't run without it.");
|
||||
}
|
||||
|
||||
QSurfaceFormat format;
|
||||
|
||||
format.setRenderableType(QSurfaceFormat::OpenGL);
|
||||
format.setVersion(4, 1);
|
||||
format.setProfile(QSurfaceFormat::CoreProfile);
|
||||
//format.setOption(QSurfaceFormat::ResetNotification, true);
|
||||
format.setSwapBehavior(QSurfaceFormat::TripleBuffer);
|
||||
format.setSwapInterval(0);
|
||||
format.setRenderableType(QSurfaceFormat::OpenGL);
|
||||
format.setDepthBufferSize(16);
|
||||
format.setSamples(0);
|
||||
|
||||
if (doAntiAliasing)
|
||||
{
|
||||
format.setSamples(4);
|
||||
}
|
||||
|
||||
QSurfaceFormat::setDefaultFormat(format);
|
||||
|
||||
QOpenGLContext context;
|
||||
context.create();
|
||||
QOffscreenSurface surface;
|
||||
surface.create();
|
||||
context.makeCurrent(&surface);
|
||||
|
||||
OpenGL::context::scoped_setter const _(::gl, &context);
|
||||
|
||||
LogDebug << "GL: Version: " << gl.getString(GL_VERSION) << std::endl;
|
||||
LogDebug << "GL: Vendor: " << gl.getString(GL_VENDOR) << std::endl;
|
||||
LogDebug << "GL: Renderer: " << gl.getString(GL_RENDERER) << std::endl;
|
||||
}
|
||||
|
||||
void Noggit::Start()
|
||||
|
||||
@@ -8,8 +8,12 @@
|
||||
#include <ClientData.hpp>
|
||||
#include <noggit/ui/main_window.hpp>
|
||||
#include <noggit/application/NoggitApplication.hpp>
|
||||
#include <noggit/application/Configuration/NoggitApplicationConfiguration.hpp>
|
||||
#include <noggit/application/Configuration/NoggitApplicationConfigurationReader.hpp>
|
||||
#include <noggit/application/Configuration/NoggitApplicationConfigurationWriter.hpp>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <string_view>
|
||||
#include <QtCore/QSettings>
|
||||
#include <QtGui/QOffscreenSurface>
|
||||
@@ -19,6 +23,8 @@
|
||||
#include <QtWidgets/QFileDialog>
|
||||
#include <QtWidgets/QMessageBox>
|
||||
#include <QSplashScreen>
|
||||
#include <QJsonObject>
|
||||
#include <QJsonDocument>
|
||||
#include <string>
|
||||
#include <revision.h>
|
||||
|
||||
@@ -47,9 +53,8 @@ namespace Noggit::Application {
|
||||
std::string project_path;
|
||||
|
||||
bool fullscreen;
|
||||
bool doAntiAliasing;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif //NOGGIT_APPLICATION_HPP
|
||||
#endif NOGGIT_APPLICATION_HPP
|
||||
Reference in New Issue
Block a user