125 lines
2.7 KiB
C++
125 lines
2.7 KiB
C++
#include <extra2d/modules/window_module.h>
|
|
#include <extra2d/core/module_macros.h>
|
|
#include <extra2d/platform/backend_factory.h>
|
|
#include <extra2d/utils/logger.h>
|
|
#include <SDL.h>
|
|
|
|
#ifdef __SWITCH__
|
|
#include <switch.h>
|
|
#endif
|
|
|
|
namespace extra2d {
|
|
|
|
WindowModule::WindowModule()
|
|
: Module()
|
|
, sdl2Initialized_(false) {
|
|
}
|
|
|
|
WindowModule::~WindowModule() {
|
|
if (isInitialized()) {
|
|
destroyModule();
|
|
}
|
|
}
|
|
|
|
void WindowModule::setupModule() {
|
|
if (isInitialized()) {
|
|
return;
|
|
}
|
|
|
|
#ifdef __SWITCH__
|
|
windowConfig_.mode = WindowMode::Fullscreen;
|
|
windowConfig_.resizable = false;
|
|
windowConfig_.highDPI = false;
|
|
E2D_LOG_INFO("Switch platform: forcing fullscreen mode");
|
|
#endif
|
|
|
|
if (!initSDL2()) {
|
|
return;
|
|
}
|
|
|
|
extern void initSDL2Backend();
|
|
initSDL2Backend();
|
|
|
|
if (!BackendFactory::has("sdl2")) {
|
|
E2D_LOG_ERROR("SDL2 backend not registered!");
|
|
shutdownSDL2();
|
|
return;
|
|
}
|
|
|
|
if (!createWindow(windowConfig_)) {
|
|
E2D_LOG_ERROR("Failed to create window");
|
|
shutdownSDL2();
|
|
return;
|
|
}
|
|
|
|
setInitialized(true);
|
|
E2D_LOG_INFO("Window module initialized");
|
|
E2D_LOG_INFO(" Window: {}x{}", window_->width(), window_->height());
|
|
E2D_LOG_INFO(" Backend: SDL2");
|
|
E2D_LOG_INFO(" VSync: {}", windowConfig_.vsync);
|
|
E2D_LOG_INFO(" Fullscreen: {}", windowConfig_.isFullscreen());
|
|
}
|
|
|
|
void WindowModule::destroyModule() {
|
|
if (!isInitialized()) {
|
|
return;
|
|
}
|
|
|
|
E2D_LOG_INFO("Window module shutting down");
|
|
|
|
if (window_) {
|
|
window_->destroy();
|
|
window_.reset();
|
|
}
|
|
|
|
shutdownSDL2();
|
|
|
|
setInitialized(false);
|
|
}
|
|
|
|
bool WindowModule::initSDL2() {
|
|
Uint32 initFlags = SDL_INIT_VIDEO | SDL_INIT_EVENTS | SDL_INIT_TIMER;
|
|
|
|
#ifdef __SWITCH__
|
|
initFlags |= SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER;
|
|
#endif
|
|
|
|
if (SDL_Init(initFlags) != 0) {
|
|
E2D_LOG_ERROR("Failed to initialize SDL2: {}", SDL_GetError());
|
|
return false;
|
|
}
|
|
|
|
sdl2Initialized_ = true;
|
|
E2D_LOG_INFO("SDL2 initialized successfully");
|
|
return true;
|
|
}
|
|
|
|
void WindowModule::shutdownSDL2() {
|
|
if (!sdl2Initialized_) {
|
|
return;
|
|
}
|
|
|
|
SDL_Quit();
|
|
sdl2Initialized_ = false;
|
|
E2D_LOG_INFO("SDL2 shutdown");
|
|
}
|
|
|
|
bool WindowModule::createWindow(const WindowConfigData& config) {
|
|
window_ = BackendFactory::createWindow("sdl2");
|
|
if (!window_) {
|
|
E2D_LOG_ERROR("Failed to create SDL2 window");
|
|
return false;
|
|
}
|
|
|
|
if (!window_->create(config)) {
|
|
E2D_LOG_ERROR("Failed to create window with specified config");
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
} // namespace extra2d
|
|
|
|
E2D_MODULE(WindowModule, 20, "ConfigModule")
|