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
draw.cpp
Go to the documentation of this file.
2
3#include <SDL3/SDL.h>
4#include <SDL3_image/SDL_image.h>
5#include <SDL3_ttf/SDL_ttf.h>
6#include <cmath>
7
10
13 return;
14 }
15
16 SDL_SetRenderDrawColor(asw::display::renderer, color.r, color.g, color.b,
17 color.a);
18 SDL_RenderClear(asw::display::renderer);
19}
20
22 const asw::Vec2<float>& position) {
24 return;
25 }
26
27 auto size = asw::util::getTextureSize(tex);
28
29 SDL_FRect dest;
30 dest.x = position.x;
31 dest.y = position.y;
32 dest.w = size.x;
33 dest.h = size.y;
34
35 SDL_RenderTexture(asw::display::renderer, tex.get(), nullptr, &dest);
36}
37
39 const asw::Vec2<float>& position,
40 bool flipX,
41 bool flipY) {
43 return;
44 }
45
46 auto size = asw::util::getTextureSize(tex);
47
48 SDL_FRect dest;
49 dest.x = position.x;
50 dest.y = position.y;
51 dest.w = size.x;
52 dest.h = size.y;
53
54 SDL_FlipMode flip = SDL_FLIP_NONE;
55
56 if (flipX) {
57 flip = static_cast<SDL_FlipMode>(flip | SDL_FLIP_HORIZONTAL);
58 }
59
60 if (flipY) {
61 flip = static_cast<SDL_FlipMode>(flip | SDL_FLIP_VERTICAL);
62 }
63
64 SDL_RenderTextureRotated(asw::display::renderer, tex.get(), nullptr, &dest, 0,
65 nullptr, flip);
66}
67
69 const asw::Quad<float>& position) {
70 SDL_FRect dest;
71 dest.x = position.position.x;
72 dest.y = position.position.y;
73 dest.w = position.size.x;
74 dest.h = position.size.y;
75
76 SDL_RenderTexture(asw::display::renderer, tex.get(), nullptr, &dest);
77}
78
80 const asw::Vec2<float>& position,
81 double angle) {
83 return;
84 }
85
86 auto size = asw::util::getTextureSize(tex);
87
88 SDL_FRect dest;
89 dest.x = position.x;
90 dest.y = position.y;
91 dest.w = size.x;
92 dest.h = size.y;
93
94 SDL_RenderTextureRotated(asw::display::renderer, tex.get(), nullptr, &dest,
95 angle, nullptr, SDL_FLIP_NONE);
96}
97
99 const asw::Quad<float>& source,
100 const asw::Quad<float>& dest) {
102 return;
103 }
104
105 SDL_FRect r_src;
106 r_src.x = source.position.x;
107 r_src.y = source.position.y;
108 r_src.w = source.size.x;
109 r_src.h = source.size.y;
110
111 SDL_FRect r_dest;
112 r_dest.x = dest.position.x;
113 r_dest.y = dest.position.y;
114 r_dest.w = dest.size.x;
115 r_dest.h = dest.size.y;
116
117 SDL_RenderTexture(asw::display::renderer, tex.get(), &r_src, &r_dest);
118}
119
121 const asw::Quad<float>& source,
122 const asw::Quad<float>& dest,
123 double angle) {
125 return;
126 }
127
128 SDL_FRect r_src;
129 r_src.x = source.position.x;
130 r_src.y = source.position.y;
131 r_src.w = source.size.x;
132 r_src.h = source.size.y;
133
134 SDL_FRect r_dest;
135 r_dest.x = dest.position.x;
136 r_dest.y = dest.position.y;
137 r_dest.w = dest.size.x;
138 r_dest.h = dest.size.y;
139
140 SDL_RenderTextureRotated(asw::display::renderer, tex.get(), &r_src, &r_dest,
141 angle, nullptr, SDL_FLIP_NONE);
142}
143
144void asw::draw::text(const asw::Font& font,
145 const std::string& text,
146 const asw::Vec2<float>& position,
147 asw::Color color) {
148 if (text.empty() || !asw::display::renderer) {
149 return;
150 }
151
152 const SDL_Color sdlColor =
153 asw::util::makeColor(color.r, color.g, color.b, color.a);
154 SDL_Surface* textSurface =
155 TTF_RenderText_Solid(font.get(), text.c_str(), 0, sdlColor);
156 SDL_Texture* textTexture =
157 SDL_CreateTextureFromSurface(asw::display::renderer, textSurface);
158
159 SDL_SetTextureBlendMode(textTexture, SDL_BLENDMODE_BLEND);
160 SDL_SetTextureScaleMode(textTexture, SDL_SCALEMODE_NEAREST);
161
162 SDL_FRect dest;
163 dest.x = position.x;
164 dest.y = position.y;
165 dest.w = float(textSurface->w);
166 dest.h = float(textSurface->h);
167
168 SDL_RenderTexture(asw::display::renderer, textTexture, nullptr, &dest);
169 SDL_DestroySurface(textSurface);
170 SDL_DestroyTexture(textTexture);
171}
172
174 const std::string& text,
175 const asw::Vec2<float>& position,
176 asw::Color color) {
178 return;
179 }
180
181 auto size = asw::util::getTextSize(font, text);
182 asw::draw::text(font, text, position - asw::Vec2<float>(size.x / 2, 0),
183 color);
184}
185
187 const std::string& text,
188 const asw::Vec2<float>& position,
189 asw::Color color) {
191 return;
192 }
193
194 auto size = asw::util::getTextSize(font, text);
195 asw::draw::text(font, text, position - asw::Vec2<float>(size.x, 0), color);
196}
197
198void asw::draw::point(const asw::Vec2<float>& position, asw::Color color) {
200 return;
201 }
202
203 SDL_SetRenderDrawColor(asw::display::renderer, color.r, color.g, color.b,
204 color.a);
205 SDL_RenderPoint(asw::display::renderer, position.x, position.y);
206}
207
208void asw::draw::line(const asw::Vec2<float>& position1,
209 const asw::Vec2<float>& position2,
210 asw::Color color) {
212 return;
213 }
214
215 SDL_SetRenderDrawColor(asw::display::renderer, color.r, color.g, color.b,
216 color.a);
217 SDL_RenderLine(asw::display::renderer, position1.x, position1.y, position2.x,
218 position2.y);
219}
220
221void asw::draw::rect(const asw::Quad<float>& position, asw::Color color) {
223 return;
224 }
225
226 SDL_SetRenderDrawColor(asw::display::renderer, color.r, color.g, color.b,
227 color.a);
228 SDL_FRect rect;
229 rect.x = position.position.x;
230 rect.y = position.position.y;
231 rect.w = position.size.x;
232 rect.h = position.size.y;
233
234 SDL_RenderRect(asw::display::renderer, &rect);
235}
236
237void asw::draw::rectFill(const asw::Quad<float>& position, asw::Color color) {
239 return;
240 }
241
242 SDL_SetRenderDrawColor(asw::display::renderer, color.r, color.g, color.b,
243 color.a);
244 SDL_FRect rect;
245 rect.x = position.position.x;
246 rect.y = position.position.y;
247 rect.w = position.size.x;
248 rect.h = position.size.y;
249
250 SDL_RenderFillRect(asw::display::renderer, &rect);
251}
252
254 float radius,
255 asw::Color color) {
257 return;
258 }
259
260 SDL_SetRenderDrawColor(asw::display::renderer, color.r, color.g, color.b,
261 color.a);
262 for (int i = 0; i < 360; i++) {
263 SDL_RenderPoint(asw::display::renderer,
264 position.x + static_cast<float>(radius * std::cos(i)),
265 position.y + static_cast<float>(radius * std::sin(i)));
266 }
267}
268
270 float radius,
271 asw::Color color) {
273 return;
274 }
275
276 SDL_SetRenderDrawColor(asw::display::renderer, color.r, color.g, color.b,
277 color.a);
278 for (int i = 0; i < 360; i++) {
279 SDL_RenderLine(asw::display::renderer, position.x, position.y,
280 position.x + static_cast<float>(radius * std::cos(i)),
281 position.y + static_cast<float>(radius * std::sin(i)));
282 }
283}
284
286 SDL_SetTextureBlendMode(texture.get(), static_cast<SDL_BlendMode>(mode));
287}
288
289void asw::draw::setAlpha(const asw::Texture& texture, float alpha) {
290 SDL_SetTextureAlphaModFloat(texture.get(), alpha);
291}
A 2D rectangle in space.
Definition geometry.h:370
Vec2< T > size
The size of the rectangle.
Definition geometry.h:512
Vec2< T > position
The position of the rectangle.
Definition geometry.h:509
A 2D vector in space.
Definition geometry.h:22
T y
The y component of the vector.
Definition geometry.h:184
T x
The x component of the vector.
Definition geometry.h:181
Display and window routines for the ASW library.
Routines for drawing sprites and primitives to the screen.
asw::Renderer * renderer
The renderer for the display module.
Definition display.cpp:9
void setBlendMode(const asw::Texture &texture, asw::BlendMode mode)
Set the blend mode of a texture.
Definition draw.cpp:285
void sprite(const asw::Texture &tex, const asw::Vec2< float > &position)
Draw a sprite.
Definition draw.cpp:21
void stretchSprite(const asw::Texture &tex, const asw::Quad< float > &position)
Draw a sprite with the option to stretch it.
Definition draw.cpp:68
void rotateSprite(const asw::Texture &tex, const asw::Vec2< float > &position, double angle)
Draw a sprite with the option to rotate it.
Definition draw.cpp:79
void stretchSpriteRotateBlit(const asw::Texture &tex, const asw::Quad< float > &source, const asw::Quad< float > &dest, double angle)
Draw a sprite with the option to stretch and rotate a portion of it.
Definition draw.cpp:120
void textCenter(const asw::Font &font, const std::string &text, const asw::Vec2< float > &position, asw::Color color)
Draw text center aligned.
Definition draw.cpp:173
void line(const asw::Vec2< float > &position1, const asw::Vec2< float > &position2, asw::Color color)
Draw a line.
Definition draw.cpp:208
void rectFill(const asw::Quad< float > &position, asw::Color color)
Draw a filled rectangle.
Definition draw.cpp:237
void stretchSpriteBlit(const asw::Texture &tex, const asw::Quad< float > &source, const asw::Quad< float > &dest)
Draw a sprite with the option to stretch a portion of it.
Definition draw.cpp:98
void setAlpha(const asw::Texture &texture, float alpha)
Set the alpha of a texture.
Definition draw.cpp:289
void rect(const asw::Quad< float > &position, asw::Color color)
Draw a rectangle.
Definition draw.cpp:221
void circle(const asw::Vec2< float > &position, float radius, asw::Color color)
Draw a circle.
Definition draw.cpp:253
void spriteFlip(const asw::Texture &tex, const asw::Vec2< float > &position, bool flipX, bool flipY)
Draw a sprite with the option to flip it.
Definition draw.cpp:38
void circleFill(const asw::Vec2< float > &position, float radius, asw::Color color)
Draw a filled circle.
Definition draw.cpp:269
void point(const asw::Vec2< float > &position, asw::Color color)
Draw a point.
Definition draw.cpp:198
void textRight(const asw::Font &font, const std::string &text, const asw::Vec2< float > &position, asw::Color color)
Draw text right aligned.
Definition draw.cpp:186
void clearColor(asw::Color color)
Clear the screen to a color.
Definition draw.cpp:11
void text(const asw::Font &font, const std::string &text, const asw::Vec2< float > &position, asw::Color color)
Draw text left aligned.
Definition draw.cpp:144
asw::Vec2< float > getTextureSize(const asw::Texture &tex)
Get texture size.
Definition util.cpp:30
asw::Color makeColor(int r, int g, int b)
Make a color from RGB values.
Definition util.cpp:12
asw::Vec2< int > getTextSize(const asw::Font &font, const std::string &text)
Get text size.
Definition util.cpp:36
std::shared_ptr< TTF_Font > Font
Alias for a shared pointer to an TTF_Font.
Definition types.h:23
std::shared_ptr< SDL_Texture > Texture
Alias for a shared pointer to an SDL_Texture.
Definition types.h:20
BlendMode
Mappings from SDL_BLENDMODE to ASW BlendMode.
Definition types.h:41
SDL_Color Color
Alias for an SDL_Color.
Definition types.h:38
General utility functions.