cleaning up App::config getter and removing App::is_running

This commit is contained in:
Noel Berry 2021-05-09 19:40:50 -07:00
parent c51d397ccc
commit 76525f91c5
6 changed files with 28 additions and 36 deletions

View File

@ -98,16 +98,13 @@ namespace Blah
// Runs the application
bool run(const Config* config);
// Returns whether the application is running
bool is_running();
// Exits the application.
// This only signals for the application to close, it will not stop
// until the current update and render calls are finished.
void exit();
// Gets the config data used to run the application
const Config* config();
const Config& config();
// Gets the working path
const char* path();

View File

@ -127,7 +127,7 @@ bool App::run(const Config* c)
app_is_exiting = false;
// initialize the system
if (!PlatformBackend::init(&app_config))
if (!PlatformBackend::init(app_config))
{
Log::error("Failed to initialize Platform module");
return false;
@ -183,20 +183,15 @@ bool App::run(const Config* c)
return true;
}
bool App::is_running()
{
return app_is_running;
}
void App::exit()
{
if (!app_is_exiting && app_is_running)
app_is_exiting = true;
}
const Config* App::config()
const Config& App::config()
{
return &app_config;
return app_config;
}
const char* App::path()

View File

@ -13,9 +13,9 @@ void Log::info(const char* format, ...)
vsnprintf(msg, sizeof(char) * BLAH_MESSAGE, format, ap);
va_end(ap);
if (App::is_running() && App::config()->on_log)
if (App::config().on_log)
{
App::config()->on_log(msg, Category::Info);
App::config().on_log(msg, Category::Info);
}
else
{
@ -31,9 +31,9 @@ void Log::warn(const char* format, ...)
vsnprintf(msg, sizeof(char) * BLAH_MESSAGE, format, ap);
va_end(ap);
if (App::is_running() && App::config()->on_log)
if (App::config().on_log)
{
App::config()->on_log(msg, Category::Warning);
App::config().on_log(msg, Category::Warning);
}
else
{
@ -49,9 +49,9 @@ void Log::error(const char* format, ...)
vsnprintf(msg, sizeof(char) * BLAH_MESSAGE, format, ap);
va_end(ap);
if (App::is_running() && App::config()->on_log)
if (App::config().on_log)
{
App::config()->on_log(msg, Category::Error);
App::config().on_log(msg, Category::Error);
}
else
{

View File

@ -13,7 +13,7 @@ namespace Blah
typedef void* FileHandle;
// Initialize the System
bool init(const Config* config);
bool init(const Config& config);
// Called after the on_startup callback, but before the update loop begins
void ready();

View File

@ -115,7 +115,7 @@ namespace Blah
}
};
bool PlatformBackend::init(const Config* config)
bool PlatformBackend::init(const Config& config)
{
// Required to call this for Windows
// I'm not sure why SDL2 doesn't do this on Windows automatically?
@ -172,7 +172,7 @@ namespace Blah
}
// create the window
window = SDL_CreateWindow(config->name, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, config->width, config->height, flags);
window = SDL_CreateWindow(config.name, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, config.width, config.height, flags);
if (window == nullptr)
{
Log::error("Failed to create a Window");
@ -195,8 +195,8 @@ namespace Blah
{
SDL_DisplayMode mode;
SDL_GetDesktopDisplayMode(display, &mode);
SDL_SetWindowPosition(window, (int)(mode.w - config->width * dpi) / 2, (int)(mode.h - config->height * dpi) / 2);
SDL_SetWindowSize(window, (int)(config->width * dpi), (int)(config->height * dpi));
SDL_SetWindowPosition(window, (int)(mode.w - config.width * dpi) / 2, (int)(mode.h - config.height * dpi) / 2);
SDL_SetWindowSize(window, (int)(config.width * dpi), (int)(config.height * dpi));
}
}
}
@ -264,9 +264,9 @@ namespace Blah
{
if (event.type == SDL_QUIT)
{
auto config = App::config();
if (config->on_exit_request != nullptr)
config->on_exit_request();
auto& config = App::config();
if (config.on_exit_request != nullptr)
config.on_exit_request();
}
// Mouse
else if (event.type == SDL_MOUSEBUTTONDOWN)
@ -554,8 +554,8 @@ namespace Blah
{
if (userPath == nullptr)
{
const Config* config = App::config();
userPath = SDL_GetPrefPath(nullptr, config->name);
auto& config = App::config();
userPath = SDL_GetPrefPath(nullptr, config.name);
}
return userPath;

View File

@ -157,7 +157,7 @@ namespace Blah
}
};
bool PlatformBackend::init(const Config* config)
bool PlatformBackend::init(const Config& config)
{
// Required to call this for Windows
SetProcessDPIAware();
@ -182,7 +182,7 @@ namespace Blah
RegisterClass(&wc);
// Create the Window Instance
g_hwnd = CreateWindow("BLAH WINDOW", config->name, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, 640, 480, NULL, NULL, hInstance, NULL);
g_hwnd = CreateWindow("BLAH WINDOW", config.name, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, 640, 480, NULL, NULL, hInstance, NULL);
// Failed to create the Window
if (g_hwnd == NULL)
@ -271,7 +271,7 @@ namespace Blah
FilePath result;
result.append_utf16((u16*)path, (u16*)end);
g_user_directory = Path::join(Path::normalize(result), config->name) + "/";
g_user_directory = Path::join(Path::normalize(result), config.name) + "/";
}
CoTaskMemFree(path);
}
@ -288,8 +288,8 @@ namespace Blah
// Setup Window Size
{
auto scale = get_content_scale();
int sw = (int)(App::config()->width * scale);
int sh = (int)(App::config()->height * scale);
int sw = (int)(App::config().width * scale);
int sh = (int)(App::config().height * scale);
set_size(sw, sh);
}
@ -317,9 +317,9 @@ namespace Blah
{
case WM_CLOSE:
{
auto config = App::config();
if (config->on_exit_request != nullptr)
config->on_exit_request();
auto& config = App::config();
if (config.on_exit_request != nullptr)
config.on_exit_request();
return 0;
}