diff --git a/include/asw/modules/core.h b/include/asw/modules/core.h index 941e1c6..7e2cbce 100644 --- a/include/asw/modules/core.h +++ b/include/asw/modules/core.h @@ -23,6 +23,14 @@ void update(); /// void init(int width, int height, int scale = 1); +/// @brief Initializes the core module for opengl. +/// +/// @param width The width of the window. +/// @param height The height of the window. +/// @param scale The scale of the window. +/// +void init_opengl(int width, int height, int scale = 1); + /// @brief Prints information about the core module. /// void print_info(); diff --git a/include/asw/modules/display.h b/include/asw/modules/display.h index 0655696..ecf2286 100644 --- a/include/asw/modules/display.h +++ b/include/asw/modules/display.h @@ -25,6 +25,14 @@ namespace asw::display { /// void _init(int width, int height, int scale); +/// @brief Initialize the display module with OpenGL support. Called by asw::core::init(). +/// +/// @param width The logical width of the display. +/// @param height The logical height of the display. +/// @param scale The initial window scale factor. +/// +void _init_opengl(int width, int height, int scale); + /// @brief Shut down the display module. Called by asw::core::shutdown(). /// Nulls the renderer and window pointers before destroying them, so any /// outstanding shared_ptr asset deleters see a null renderer and become no-ops. @@ -124,6 +132,17 @@ void present(); /// void set_blend_mode(asw::BlendMode mode); +/// @brief Warp mouse in window +/// +/// @param x The x coordinate to warp to. +/// @param y The y coordinate to warp to. +/// +void warp_mouse(float x, float y); + +/// @brief Swap window (gl) +/// +void swap_window(); + } // namespace asw::display #endif // ASW_DISPLAY_H \ No newline at end of file diff --git a/src/modules/core.cpp b/src/modules/core.cpp index 1d731cd..5e41568 100644 --- a/src/modules/core.cpp +++ b/src/modules/core.cpp @@ -150,6 +150,29 @@ void asw::core::init(int width, int height, int scale) asw::display::_init(width, height, scale); } +void asw::core::init_opengl(int width, int height, int scale) +{ + if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMEPAD)) { + asw::util::abort_on_error("SDL_Init"); + } + + if (!TTF_Init()) { + asw::util::abort_on_error("TTF_Init"); + } + + if (!asw::sound::_init()) { + asw::util::abort_on_error("Sound initialization failed"); + } + + // --- Set GL attributes before creating the window --- + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); // Request OpenGL 3.x + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); + SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); + + asw::display::_init_opengl(width, height, scale); +} + void asw::core::print_info() { asw::log::info("ASW Info"); diff --git a/src/modules/display.cpp b/src/modules/display.cpp index 863b8fd..b706300 100644 --- a/src/modules/display.cpp +++ b/src/modules/display.cpp @@ -23,8 +23,25 @@ void asw::display::_init(int width, int height, int scale) renderer = SDL_CreateRenderer(window, nullptr); - SDL_SetRenderLogicalPresentation( - renderer, width, height, SDL_LOGICAL_PRESENTATION_LETTERBOX); + SDL_SetRenderLogicalPresentation(renderer, width, height, SDL_LOGICAL_PRESENTATION_LETTERBOX); +} + +void asw::display::_init_opengl(int width, int height, int scale) +{ + window = SDL_CreateWindow( + "", width * scale, height * scale, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE); + if (window == nullptr) { + asw::util::abort_on_error("WINDOW"); + } + + SDL_GLContext glcontext = SDL_GL_CreateContext(window); + if (glcontext == nullptr) { + asw::util::abort_on_error("SDL_GL_CreateContext"); + } + + if (!SDL_GL_MakeCurrent(window, glcontext)) { + asw::util::abort_on_error("SDL_GL_MakeCurrent"); + } } void asw::display::_shutdown() @@ -72,6 +89,7 @@ void asw::display::set_icon(const std::string& path) void asw::display::set_fullscreen(bool fullscreen) { SDL_SetWindowFullscreen(window, fullscreen); + SDL_SyncWindow(window); } void asw::display::set_resolution(int w, int h) @@ -161,3 +179,13 @@ void asw::display::set_blend_mode(asw::BlendMode mode) { SDL_SetRenderDrawBlendMode(renderer, static_cast(mode)); } + +void asw::display::warp_mouse(float x, float y) +{ + SDL_WarpMouseInWindow(window, x, y); +} + +void asw::display::swap_window() +{ + SDL_GL_SwapWindow(window); +} \ No newline at end of file