Skip to content

Commit

Permalink
Deprecate WindowConfig, add global Config struct (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoelOtter authored Aug 11, 2024
1 parent b2d384d commit 8c2d322
Show file tree
Hide file tree
Showing 21 changed files with 53 additions and 68 deletions.
6 changes: 3 additions & 3 deletions example/test_app/src/test_app.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "growl/core/config.h"
#include "growl/core/game/game.h"
#ifdef GROWL_DESKTOP
#include "growl/core/graphics/window.h"
#include "growl/platforms/desktop/game_adapter.h"
#elif GROWL_IOS
#include "growl/platforms/ios/app_delegate.h"
Expand All @@ -20,7 +20,7 @@ int main(int argc, char** argv) {
#ifdef GROWL_DESKTOP
auto adapter = Growl::GameAdapter{
std::make_unique<Growl::TestAppGame>(),
Growl::WindowConfig{"Growl Test App", 1000, 1000, true}};
Growl::Config{"Growl Test App", 1000, 1000, true}};
adapter.run();
#elif GROWL_IOS
NSString* appDelegateClassName;
Expand All @@ -35,7 +35,7 @@ int main(int argc, char** argv) {
#elif GROWL_WEB
auto adapter = Growl::GameAdapter{
std::make_unique<Growl::TestAppGame>(),
Growl::WindowConfig{"Growl Test App", 1000, 1000, true}};
Growl::Config{"Growl Test App", 1000, 1000, true}};
adapter.run();
#endif
}
3 changes: 2 additions & 1 deletion src/core/include/growl/core/api/api_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "audio_api.h"
#include "graphics_api.h"
#include "growl/core/config.h"
#include "growl/core/error.h"
#include "growl/core/input/event.h"
#include "scripting_api.h"
Expand Down Expand Up @@ -30,7 +31,7 @@ class SystemAPIInternal : public SystemAPI, public APIInternal {
class GraphicsAPIInternal : public GraphicsAPI, public APIInternal {
public:
virtual ~GraphicsAPIInternal() {}
virtual Error setWindow(const WindowConfig& window_descriptor) = 0;
virtual Error setWindow(const Config& config) = 0;
virtual void onWindowResize(int width, int height) = 0;
virtual void begin() = 0;
virtual void end() = 0;
Expand Down
4 changes: 2 additions & 2 deletions src/core/include/growl/core/api/system_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@

namespace Growl {

struct Config;
class File;
class Window;
class WindowConfig;

class SystemAPI {
public:
Expand All @@ -22,7 +22,7 @@ class SystemAPI {
virtual void stop() {}
virtual void tick() = 0;
virtual Result<std::unique_ptr<Window>>
createWindow(const WindowConfig& window) = 0;
createWindow(const Config& config) = 0;
virtual void setInputProcessor(InputProcessor* processor) {
inputProcessor = processor;
}
Expand Down
14 changes: 14 additions & 0 deletions src/core/include/growl/core/config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once

#include <string>
namespace Growl {

struct Config {
std::string name = "Growl";

int window_width = 1000;
int window_height = 1000;
bool window_centered = true;
};

} // namespace Growl
28 changes: 0 additions & 28 deletions src/core/include/growl/core/graphics/window.h
Original file line number Diff line number Diff line change
@@ -1,36 +1,8 @@
#pragma once

#include "growl/core/error.h"
#include <string>
namespace Growl {

class WindowConfig {
public:
WindowConfig(std::string title, int width, int height, bool centred = true)
: title{title}
, width{width}
, height{height}
, centred{centred} {}
std::string getTitle() const {
return title;
}
int getWidth() const {
return width;
}
int getHeight() const {
return height;
}
bool isCentred() const {
return centred;
}

private:
std::string title;
int width;
int height;
bool centred;
};

class Window {
public:
virtual ~Window() = default;
Expand Down
5 changes: 2 additions & 3 deletions src/platforms/android/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "growl/core/api/api.h"
#include "growl/core/api/api_internal.h"
#include "growl/core/game/game.h"
#include "growl/core/graphics/window.h"
#include "growl/core/log.h"
#include "growl/scene/scene.h"
#include <android/asset_manager.h>
Expand All @@ -12,12 +11,12 @@

using Growl::API;
using Growl::AudioAPIInternal;
using Growl::Config;
using Growl::FrameTimer;
using Growl::GraphicsAPIInternal;
using Growl::LogLevel;
using Growl::ScriptingAPIInternal;
using Growl::SystemAPIInternal;
using Growl::WindowConfig;

void initAndroidPlugin(API& api, android_app* state);
void initSoLoudPlugin(API& api);
Expand Down Expand Up @@ -126,7 +125,7 @@ void android_main(struct android_app* state) {
}

if (auto err = static_cast<GraphicsAPIInternal&>(api->graphics())
.setWindow(WindowConfig{"", 0, 0, false});
.setWindow(Config{"", 0, 0, false});
err) {
api->system().log(
LogLevel::Fatal, "android_main", "Failed to create window: {}",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include "growl/core/graphics/window.h"
#include "growl/core/config.h"
#include <memory>

namespace Growl {
Expand All @@ -10,15 +10,14 @@ class API;

class GameAdapter {
public:
explicit GameAdapter(
std::unique_ptr<Game> game, WindowConfig window_config);
explicit GameAdapter(std::unique_ptr<Game> game, Config window_config);
~GameAdapter();
void run();

private:
std::unique_ptr<API> m_api;
std::unique_ptr<Game> m_game;
WindowConfig m_window_config;
Config m_config;
};

} // namespace Growl
6 changes: 3 additions & 3 deletions src/platforms/desktop/src/game_adapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ void initMetalPlugin(API& api);
void initOpenGLPlugin(API& api);
void initLuaPlugin(API& api);

GameAdapter::GameAdapter(std::unique_ptr<Game> game, WindowConfig window_config)
GameAdapter::GameAdapter(std::unique_ptr<Game> game, Config config)
: m_api(std::make_unique<API>())
, m_game(std::move(game))
, m_window_config(std::move(window_config)) {
, m_config(std::move(config)) {

initSDL2Plugin(*m_api);
initSoLoudPlugin(*m_api);
Expand Down Expand Up @@ -89,7 +89,7 @@ GameAdapter::~GameAdapter() {

void GameAdapter::run() {
if (auto err = static_cast<GraphicsAPIInternal&>(m_api->graphics())
.setWindow(m_window_config);
.setWindow(m_config);
err) {
m_api->system().log(
LogLevel::Fatal, "GameAdapter", "Failed to create window: {}",
Expand Down
3 changes: 1 addition & 2 deletions src/platforms/ios/src/view_controller.mm
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ - (void)viewDidLoad {

api->system().log("ViewController", "iOS view controller created");

if (auto err =
graphicsInternal.setWindow(Growl::WindowConfig{"", 0, 0, false});
if (auto err = graphicsInternal.setWindow(Growl::Config{"", 0, 0, false});
err) {
api->system().log(
Growl::LogLevel::Fatal, "GameAdapter",
Expand Down
7 changes: 3 additions & 4 deletions src/platforms/web/include/growl/platforms/web/game_adapter.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include "growl/core/graphics/window.h"
#include "growl/core/config.h"
#include <memory>

namespace Growl {
Expand All @@ -9,14 +9,13 @@ class Game;

class GameAdapter {
public:
explicit GameAdapter(
std::unique_ptr<Game> game, WindowConfig window_config);
explicit GameAdapter(std::unique_ptr<Game> game, Config config);
~GameAdapter();
void run();

private:
static void doLoopIteration();
WindowConfig window_config;
Config config;
};

} // namespace Growl
7 changes: 4 additions & 3 deletions src/platforms/web/src/game_adapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <iostream>

using Growl::API;
using Growl::Config;
using Growl::Game;
using Growl::GameAdapter;

Expand All @@ -21,8 +22,8 @@ void initLuaPlugin(API& api);
std::unique_ptr<API> g_api;
std::unique_ptr<Game> g_game;

GameAdapter::GameAdapter(std::unique_ptr<Game> game, WindowConfig window_config)
: window_config{window_config} {
GameAdapter::GameAdapter(std::unique_ptr<Game> game, Config config)
: config{config} {
g_api = std::make_unique<API>();
g_game = std::move(game);
initSDL2Plugin(*g_api);
Expand Down Expand Up @@ -96,7 +97,7 @@ void GameAdapter::doLoopIteration() {

void GameAdapter::run() {
if (auto err = static_cast<GraphicsAPIInternal&>(g_api->graphics())
.setWindow(window_config);
.setWindow(config);
err) {
g_api->system().log(
LogLevel::Fatal, "GameAdapter", "Failed to create window: {}",
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/android/src/android_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ void AndroidSystemAPI::onControllerEvent(InputControllerEvent event) {
}

Result<std::unique_ptr<Window>>
AndroidSystemAPI::createWindow(const WindowConfig& config) {
AndroidSystemAPI::createWindow(const Config& config) {
return std::unique_ptr<Window>(
std::make_unique<AndroidWindow>(android_state->window));
}
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/android/src/android_system.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class AndroidSystemAPI : public SystemAPIInternal {
return android_state->destroyRequested == 0;
}
virtual Result<std::unique_ptr<Window>>
createWindow(const WindowConfig& config) override;
createWindow(const Config& config) override;
void setLogLevel(LogLevel log_level) override;
bool didResize(int* width, int* height) override;

Expand Down
2 changes: 1 addition & 1 deletion src/plugins/ios/src/ios_system.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class IOSSystemAPI : public SystemAPIInternal {
return running;
}
virtual Result<std::unique_ptr<Window>>
createWindow(const WindowConfig& config) override;
createWindow(const Config& config) override;
void setLogLevel(LogLevel log_level) override;
Result<std::unique_ptr<File>>
openFile(std::string path, size_t start = 0, size_t end = 0) override;
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/ios/src/ios_system.mm
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
}

Result<std::unique_ptr<Window>>
IOSSystemAPI::createWindow(const WindowConfig& config) {
IOSSystemAPI::createWindow(const Config& config) {
UIWindow* w = [[[UIApplication sharedApplication] delegate] window];
return std::unique_ptr<Window>(std::make_unique<IOSWindow>(w));
}
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/metal/src/metal_graphics.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class MetalGraphicsAPI : public GraphicsAPIInternal {
void dispose() override;
void begin() override;
void end() override;
Error setWindow(const WindowConfig& window_config) override;
Error setWindow(const Config& config) override;
void onWindowResize(int width, int height) override;

std::unique_ptr<Texture>
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/metal/src/metal_graphics.mm
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
[pool release];
}

Error MetalGraphicsAPI::setWindow(const WindowConfig& config) {
Error MetalGraphicsAPI::setWindow(const Config& config) {
auto window_result = api.system().createWindow(config);
if (window_result.hasError()) {
return std::move(window_result.error());
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/opengl/src/opengl_graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ void OpenGLGraphicsAPI::end() {
window->flip();
}

Error OpenGLGraphicsAPI::setWindow(const WindowConfig& config) {
Error OpenGLGraphicsAPI::setWindow(const Config& config) {
auto window_result = api.system().createWindow(config);
if (window_result.hasError()) {
return std::move(window_result.error());
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/opengl/src/opengl_graphics.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class Image;
class ShaderPack;
class Texture;
class TextureAtlas;
struct Config;
struct TextureOptions;

class OpenGLGraphicsAPI : public GraphicsAPIInternal {
Expand All @@ -27,7 +28,7 @@ class OpenGLGraphicsAPI : public GraphicsAPIInternal {
void dispose() override;
void begin() override;
void end() override;
Error setWindow(const WindowConfig& window_config) override;
Error setWindow(const Config& config) override;
void onWindowResize(int width, int height) override;

std::unique_ptr<Texture>
Expand Down
10 changes: 5 additions & 5 deletions src/plugins/sdl2/src/sdl2_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,16 @@ Error SDL2SystemAPI::init() {
}

Result<std::unique_ptr<Window>>
SDL2SystemAPI::createWindow(const WindowConfig& config) {
SDL2SystemAPI::createWindow(const Config& config) {
int flags = SDL_WINDOW_INPUT_FOCUS;
flags |= SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL |
SDL_WINDOW_ALLOW_HIGHDPI;

SDL_Window* win = SDL_CreateWindow(
config.getTitle().c_str(),
config.isCentred() ? SDL_WINDOWPOS_CENTERED : 0,
config.isCentred() ? SDL_WINDOWPOS_CENTERED : 0, config.getWidth(),
config.getHeight(), flags);
config.name.c_str(),
config.window_centered ? SDL_WINDOWPOS_CENTERED : 0,
config.window_centered ? SDL_WINDOWPOS_CENTERED : 0,
config.window_width, config.window_height, flags);
if (win == nullptr) {
return Error(std::make_unique<SDL2Error>(SDL_GetError()));
}
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/sdl2/src/sdl2_system.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Growl {
class API;
class File;
class Window;
class WindowConfig;
struct Config;
enum class ControllerButton;
enum class ControllerEventType;
enum class Key;
Expand Down Expand Up @@ -49,7 +49,7 @@ class SDL2SystemAPI : public SystemAPIInternal {
}
void stop() override;
virtual Result<std::unique_ptr<Window>>
createWindow(const WindowConfig& config) override;
createWindow(const Config& config) override;
void setLogLevel(LogLevel log_level) override;
bool didResize(int* width, int* height) override;

Expand Down

0 comments on commit 8c2d322

Please sign in to comment.