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
10
11namespace {
14} // namespace
15
16void asw::display::_init(int width, int height, int scale)
17{
18 window = SDL_CreateWindow("", width * scale, height * scale, SDL_WINDOW_RESIZABLE);
19 if (window == nullptr) {
21 }
22
23 SDL_SetHint(SDL_HINT_RENDER_VSYNC, "1");
24
25 renderer = SDL_CreateRenderer(window, nullptr);
26
27 SDL_SetRenderLogicalPresentation(renderer, width, height, SDL_LOGICAL_PRESENTATION_LETTERBOX);
28}
29
30void asw::display::_init_opengl(int width, int height, int scale)
31{
32 window = SDL_CreateWindow(
33 "", width * scale, height * scale, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
34 if (window == nullptr) {
36 }
37
38 SDL_GLContext glcontext = SDL_GL_CreateContext(window);
39 if (glcontext == nullptr) {
40 asw::util::abort_on_error("SDL_GL_CreateContext");
41 }
42
43 if (!SDL_GL_MakeCurrent(window, glcontext)) {
44 asw::util::abort_on_error("SDL_GL_MakeCurrent");
45 }
46}
47
49{
50 // Release cached text textures while their renderer is still alive.
52
53 auto* r = renderer;
54 renderer = nullptr;
55 if (r != nullptr) {
56 SDL_DestroyRenderer(r);
57 }
58
59 auto* w = window;
60 window = nullptr;
61 if (w != nullptr) {
62 SDL_DestroyWindow(w);
63 }
64}
65
70
75
76void asw::display::set_title(const std::string& title)
77{
78 SDL_SetWindowTitle(window, title.c_str());
79}
80
81void asw::display::set_icon(const std::string& path)
82{
83 SDL_Surface* icon = IMG_Load(path.c_str());
84
85 if (icon == nullptr) {
86 return;
87 }
88
89 SDL_SetWindowIcon(window, icon);
90 SDL_DestroySurface(icon);
91}
92
93void asw::display::set_fullscreen(bool fullscreen)
94{
95 SDL_SetWindowFullscreen(window, fullscreen);
96 SDL_SyncWindow(window);
97}
98
100{
101 SDL_SetWindowSize(window, w, h);
102}
103
104void asw::display::set_resizable(bool resizable)
105{
106 SDL_SetWindowResizable(window, resizable);
107}
108
110{
111 asw::Vec2<int> size;
112 SDL_GetWindowSize(window, &size.x, &size.y);
113 return size;
114}
115
117{
118 asw::Vec2<int> size;
119
120 if (renderer == nullptr) {
121 return size;
122 }
123
124 SDL_GetRenderLogicalPresentation(renderer, &size.x, &size.y, nullptr);
125 return size;
126}
127
129{
130 asw::Vec2<float> scale;
131
132 if (renderer == nullptr) {
133 return scale;
134 }
135
136 SDL_GetRenderScale(renderer, &scale.x, &scale.y);
137 return scale;
138}
139
141{
142 if (renderer == nullptr) {
143 return;
144 }
145
146 SDL_SetRenderTarget(renderer, texture.get());
147}
148
150{
151 if (renderer == nullptr) {
152 return;
153 }
154
155 SDL_SetRenderTarget(renderer, nullptr);
156}
157
159{
160 if (renderer == nullptr) {
161 return;
162 }
163
164 SDL_RenderClear(renderer);
165}
166
168{
169 SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, color.a);
170 SDL_RenderClear(renderer);
171}
172
174{
175 if (renderer == nullptr) {
176 return;
177 }
178
179 SDL_RenderPresent(renderer);
180}
181
183{
184 SDL_SetRenderDrawBlendMode(renderer, static_cast<SDL_BlendMode>(mode));
185}
186
187void asw::display::warp_mouse(float x, float y)
188{
189 SDL_WarpMouseInWindow(window, x, y);
190}
191
193{
194 SDL_GL_SwapWindow(window);
195}
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.
Routines for drawing sprites and primitives to the screen.
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:81
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:128
void set_fullscreen(bool fullscreen)
Set the window to fullscreen or windowed.
Definition display.cpp:93
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:116
void _init_opengl(int width, int height, int scale)
Initialize the display module with OpenGL support. Called by asw::core::init().
Definition display.cpp:30
void set_blend_mode(asw::BlendMode mode)
Set the blend mode of a texture.
Definition display.cpp:182
void reset_render_target()
Reset the render target to the default.
Definition display.cpp:149
asw::Vec2< int > get_size()
Get the size of the window.
Definition display.cpp:109
void swap_window()
Swap window (gl)
Definition display.cpp:192
void present()
Present the window.
Definition display.cpp:173
void set_render_target(const asw::Texture &texture)
Set the render target to the window.
Definition display.cpp:140
void clear()
Clear the window.
Definition display.cpp:158
void _shutdown()
Shut down the display module. Called by asw::core::shutdown(). Nulls the renderer and window pointers...
Definition display.cpp:48
void warp_mouse(float x, float y)
Warp mouse in window.
Definition display.cpp:187
asw::Renderer * get_renderer()
Get the SDL renderer.
Definition display.cpp:66
asw::Window * get_window()
Get the SDL window.
Definition display.cpp:71
void _init(int width, int height, int scale)
Initialize the display module. Called by asw::core::init().
Definition display.cpp:16
void set_title(const std::string &title)
Set the title of the window.
Definition display.cpp:76
void set_resolution(int w, int h)
Set the resolution of the window.
Definition display.cpp:99
void set_resizable(bool resizable)
Set resizable flag of the window.
Definition display.cpp:104
void clear_text_cache()
Clear cached rendered text textures.
Definition draw.cpp:263
void abort_on_error(const std::string &message)
Abort program and display error message.
Definition util.cpp:30
std::shared_ptr< SDL_Texture > Texture
Alias for a shared pointer to an SDL_Texture.
Definition types.h:47
BlendMode
Mappings from SDL_BLENDMODE to ASW BlendMode.
Definition types.h:26
SDL_Window Window
Alias for a shared pointer to an SDL_Window.
Definition types.h:62
SDL_Renderer Renderer
Alias for a shared pointer to an SDL_Renderer.
Definition types.h:59
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.