4#include <SDL3_image/SDL_image.h>
52 flags |= SDL_WINDOW_FULLSCREEN;
66 SDL_WindowFlags flags = SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIGH_PIXEL_DENSITY;
71 flags |= SDL_WINDOW_OPENGL;
75 window = SDL_CreateWindow(
"", width * scale, height * scale, flags);
80 SDL_SetHint(SDL_HINT_RENDER_VSYNC,
vsync_override.value_or(
true) ?
"1" :
"0");
85 SDL_SetRenderDrawBlendMode(
renderer, SDL_BLENDMODE_BLEND);
87 SDL_SetRenderLogicalPresentation(
renderer, width, height, SDL_LOGICAL_PRESENTATION_LETTERBOX);
96 SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIGH_PIXEL_DENSITY, scale);
97 window = SDL_CreateWindow(
"", width * scale, height * scale, flags);
124 SDL_DestroyRenderer(r);
135 SDL_DestroyWindow(w);
151 SDL_SetWindowTitle(
window, title.c_str());
156 SDL_Surface* icon = IMG_Load(path.c_str());
158 if (icon ==
nullptr) {
162 SDL_SetWindowIcon(
window, icon);
163 SDL_DestroySurface(icon);
178 return (SDL_GetWindowFlags(
window) & SDL_WINDOW_FULLSCREEN) != 0;
183 SDL_SetWindowSize(
window, w, h);
188 SDL_SetWindowResizable(
window, resizable);
194 SDL_GetWindowSize(
window, &size.
x, &size.
y);
218 SDL_GetRenderOutputSize(
renderer, &out_w, &out_h);
219 const float s = std::min(
static_cast<float>(out_w) /
static_cast<float>(
logical_size.
x),
220 static_cast<float>(out_h) /
static_cast<float>(
logical_size.
y));
230 SDL_SetRenderTarget(
renderer, texture.get());
239 SDL_SetRenderTarget(
renderer,
nullptr);
250 SDL_SetRenderDrawColor(
renderer, 0, 0, 0, 255);
260 SDL_SetRenderDrawColor(
renderer, color.
r, color.
g, color.
b, color.
a);
275 SDL_SetRenderDrawBlendMode(
renderer,
static_cast<SDL_BlendMode
>(mode));
280 SDL_WarpMouseInWindow(
window, x, y);
285 SDL_GL_SwapWindow(
window);
294 SDL_Surface* surface = SDL_RenderReadPixels(
renderer,
nullptr);
295 if (surface ==
nullptr) {
299 const bool saved = IMG_SavePNG(surface, path.c_str());
300 SDL_DestroySurface(surface);
T y
The y component of the vector.
T x
The x component of the vector.
Settings from a config file, set by whoever runs the game.
Display and window routines for the ASW library.
Routines for drawing sprites and primitives to the screen.
Structured logging system.
std::optional< bool > vsync_override
SDL_WindowFlags apply_overrides(SDL_WindowFlags flags, int &scale)
std::optional< int > scale_override
std::optional< bool > fullscreen_override
asw::Vec2< int > logical_size
std::optional< int > get_int(const std::string &key)
Get a setting as a whole number.
std::optional< bool > get_bool(const std::string &key)
Get a setting as true or false. 1, true, yes and on are true. 0, false, no and off are false....
void set_icon(const std::string &path)
Set the icon to display on the window. If it does not exist, this will silently fail.
bool screenshot(const std::string &path)
Save what has been drawn this frame to a PNG file. Call after drawing and before the frame is present...
asw::Vec2< float > get_scale()
Get how many output pixels one logical pixel covers, after letterboxing. 2.0 means the game is drawn ...
void set_fullscreen(bool fullscreen)
Set the window to fullscreen or windowed.
asw::Vec2< int > get_logical_size()
Get the logical size of the window. This may differ from the actual size if scaling is enabled.
void _init_opengl(int width, int height, int scale)
Initialize the display module with OpenGL support. Called by asw::core::init().
void set_blend_mode(asw::BlendMode mode)
Set the blend mode used to draw primitives such as rectangles, lines and circles. Defaults to Blend,...
bool is_fullscreen()
Check if the window is fullscreen.
void reset_render_target()
Reset the render target to the default.
asw::Vec2< int > get_size()
Get the size of the window.
void swap_window()
Swap window (gl)
void present()
Present the window.
void set_render_target(const asw::Texture &texture)
Set the render target to the window.
void clear()
Clear the window to black.
void _shutdown()
Shut down the display module. Called by asw::core::shutdown(). Nulls the renderer and window pointers...
void warp_mouse(float x, float y)
Warp mouse in window.
asw::Renderer * get_renderer()
Get the SDL renderer.
asw::Window * get_window()
Get the SDL window.
void _init(int width, int height, int scale)
Initialize the display module. Called by asw::core::init().
void set_title(const std::string &title)
Set the title of the window.
void set_resolution(int w, int h)
Set the resolution of the window.
void set_resizable(bool resizable)
Set resizable flag of the window.
void clear_text_cache()
Clear cached rendered text textures.
void warn(const std::string &message)
Log a warning message.
void abort_on_error(const std::string &message)
Abort program and display error message.
std::shared_ptr< SDL_Texture > Texture
Alias for a shared pointer to an SDL_Texture.
BlendMode
Mappings from SDL_BLENDMODE to ASW BlendMode.
SDL_Window Window
Alias for a shared pointer to an SDL_Window.
SDL_Renderer Renderer
Alias for a shared pointer to an SDL_Renderer.
RGBA color struct with 8-bit channels.
Types used throughout the ASW library.
General utility functions.