ASW Lib
A.D.S. Games SDL Wrapper Library. A library targeted at Allegro4 users who want to switch to SDL3 and use modern c++.
Loading...
Searching...
No Matches
display.cpp
Go to the documentation of this file.
2
3#include <SDL3/SDL.h>
4#include <SDL3_image/SDL_image.h>
5#include <string>
6
9
10namespace {
13} // namespace
14
15void asw::display::_init(int width, int height, int scale)
16{
17 window = SDL_CreateWindow("", width * scale, height * scale, SDL_WINDOW_RESIZABLE);
18 if (window == nullptr) {
20 }
21
22 SDL_SetHint(SDL_HINT_RENDER_VSYNC, "1");
23
24 renderer = SDL_CreateRenderer(window, nullptr);
25
26 SDL_SetRenderLogicalPresentation(renderer, width, height, SDL_LOGICAL_PRESENTATION_LETTERBOX);
27}
28
29void asw::display::_init_opengl(int width, int height, int scale)
30{
31 window = SDL_CreateWindow(
32 "", width * scale, height * scale, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
33 if (window == nullptr) {
35 }
36
37 SDL_GLContext glcontext = SDL_GL_CreateContext(window);
38 if (glcontext == nullptr) {
39 asw::util::abort_on_error("SDL_GL_CreateContext");
40 }
41
42 if (!SDL_GL_MakeCurrent(window, glcontext)) {
43 asw::util::abort_on_error("SDL_GL_MakeCurrent");
44 }
45}
46
48{
49 auto* r = renderer;
50 renderer = nullptr;
51 if (r != nullptr) {
52 SDL_DestroyRenderer(r);
53 }
54
55 auto* w = window;
56 window = nullptr;
57 if (w != nullptr) {
58 SDL_DestroyWindow(w);
59 }
60}
61
66
71
72void asw::display::set_title(const std::string& title)
73{
74 SDL_SetWindowTitle(window, title.c_str());
75}
76
77void asw::display::set_icon(const std::string& path)
78{
79 SDL_Surface* icon = IMG_Load(path.c_str());
80
81 if (icon == nullptr) {
82 return;
83 }
84
85 SDL_SetWindowIcon(window, icon);
86 SDL_DestroySurface(icon);
87}
88
89void asw::display::set_fullscreen(bool fullscreen)
90{
91 SDL_SetWindowFullscreen(window, fullscreen);
92 SDL_SyncWindow(window);
93}
94
96{
97 SDL_SetWindowSize(window, w, h);
98}
99
100void asw::display::set_resizable(bool resizable)
101{
102 SDL_SetWindowResizable(window, resizable);
103}
104
106{
107 asw::Vec2<int> size;
108 SDL_GetWindowSize(window, &size.x, &size.y);
109 return size;
110}
111
113{
114 asw::Vec2<int> size;
115
116 if (renderer == nullptr) {
117 return size;
118 }
119
120 SDL_GetRenderLogicalPresentation(renderer, &size.x, &size.y, nullptr);
121 return size;
122}
123
125{
126 asw::Vec2<float> scale;
127
128 if (renderer == nullptr) {
129 return scale;
130 }
131
132 SDL_GetRenderScale(renderer, &scale.x, &scale.y);
133 return scale;
134}
135
137{
138 if (renderer == nullptr) {
139 return;
140 }
141
142 SDL_SetRenderTarget(renderer, texture.get());
143}
144
146{
147 if (renderer == nullptr) {
148 return;
149 }
150
151 SDL_SetRenderTarget(renderer, nullptr);
152}
153
155{
156 if (renderer == nullptr) {
157 return;
158 }
159
160 SDL_RenderClear(renderer);
161}
162
164{
165 SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, color.a);
166 SDL_RenderClear(renderer);
167}
168
170{
171 if (renderer == nullptr) {
172 return;
173 }
174
175 SDL_RenderPresent(renderer);
176}
177
179{
180 SDL_SetRenderDrawBlendMode(renderer, static_cast<SDL_BlendMode>(mode));
181}
182
183void asw::display::warp_mouse(float x, float y)
184{
185 SDL_WarpMouseInWindow(window, x, y);
186}
187
189{
190 SDL_GL_SwapWindow(window);
191}
A 2D vector in space.
Definition geometry.h:21
T y
The y component of the vector.
Definition geometry.h:218
T x
The x component of the vector.
Definition geometry.h:215
Display and window routines for the ASW library.
void set_icon(const std::string &path)
Set the icon to display on the window. If it does not exist, this will silently fail.
Definition display.cpp:77
asw::Vec2< float > get_scale()
Get the scale of the window. This is equivalent to the logical size divided by the actual size.
Definition display.cpp:124
void set_fullscreen(bool fullscreen)
Set the window to fullscreen or windowed.
Definition display.cpp:89
asw::Vec2< int > get_logical_size()
Get the logical size of the window. This may differ from the actual size if scaling is enabled.
Definition display.cpp:112
void _init_opengl(int width, int height, int scale)
Initialize the display module with OpenGL support. Called by asw::core::init().
Definition display.cpp:29
void set_blend_mode(asw::BlendMode mode)
Set the blend mode of a texture.
Definition display.cpp:178
void reset_render_target()
Reset the render target to the default.
Definition display.cpp:145
asw::Vec2< int > get_size()
Get the size of the window.
Definition display.cpp:105
void swap_window()
Swap window (gl)
Definition display.cpp:188
void present()
Present the window.
Definition display.cpp:169
void set_render_target(const asw::Texture &texture)
Set the render target to the window.
Definition display.cpp:136
void clear()
Clear the window.
Definition display.cpp:154
void _shutdown()
Shut down the display module. Called by asw::core::shutdown(). Nulls the renderer and window pointers...
Definition display.cpp:47
void warp_mouse(float x, float y)
Warp mouse in window.
Definition display.cpp:183
asw::Renderer * get_renderer()
Get the SDL renderer.
Definition display.cpp:62
asw::Window * get_window()
Get the SDL window.
Definition display.cpp:67
void _init(int width, int height, int scale)
Initialize the display module. Called by asw::core::init().
Definition display.cpp:15
void set_title(const std::string &title)
Set the title of the window.
Definition display.cpp:72
void set_resolution(int w, int h)
Set the resolution of the window.
Definition display.cpp:95
void set_resizable(bool resizable)
Set resizable flag of the window.
Definition display.cpp:100
void abort_on_error(const std::string &message)
Abort program and display error message.
Definition util.cpp:8
std::shared_ptr< SDL_Texture > Texture
Alias for a shared pointer to an SDL_Texture.
Definition types.h:38
BlendMode
Mappings from SDL_BLENDMODE to ASW BlendMode.
Definition types.h:20
SDL_Window Window
Alias for a shared pointer to an SDL_Window.
Definition types.h:53
SDL_Renderer Renderer
Alias for a shared pointer to an SDL_Renderer.
Definition types.h:50
RGBA color struct with 8-bit channels.
Definition color.h:11
uint8_t b
Definition color.h:14
uint8_t r
Definition color.h:12
uint8_t g
Definition color.h:13
uint8_t a
Definition color.h:15
Types used throughout the ASW library.
General utility functions.