Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions include/asw/modules/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
19 changes: 19 additions & 0 deletions include/asw/modules/display.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
23 changes: 23 additions & 0 deletions src/modules/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
32 changes: 30 additions & 2 deletions src/modules/display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -161,3 +179,13 @@ void asw::display::set_blend_mode(asw::BlendMode mode)
{
SDL_SetRenderDrawBlendMode(renderer, static_cast<SDL_BlendMode>(mode));
}

void asw::display::warp_mouse(float x, float y)
{
SDL_WarpMouseInWindow(window, x, y);
}

void asw::display::swap_window()
{
SDL_GL_SwapWindow(window);
}
Loading