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
12 SDL_SetRenderDrawColor(asw::display::renderer, color.r, color.g, color.b,
13 color.a);
14 SDL_RenderClear(asw::display::renderer);
15}
16
18 const asw::Vec2<float>& position) {
19 auto size = asw::util::getTextureSize(tex);
20
21 SDL_FRect dest;
22 dest.x = position.x;
23 dest.y = position.y;
24 dest.w = size.x;
25 dest.h = size.y;
26
27 SDL_RenderTexture(asw::display::renderer, tex.get(), nullptr, &dest);
28}
29
31 const asw::Vec2<float>& position,
32 bool flipX,
33 bool flipY) {
34 auto size = asw::util::getTextureSize(tex);
35
36 SDL_FRect dest;
37 dest.x = position.x;
38 dest.y = position.y;
39 dest.w = size.x;
40 dest.h = size.y;
41
42 SDL_FlipMode flip = SDL_FLIP_NONE;
43
44 if (flipX) {
45 flip = static_cast<SDL_FlipMode>(flip | SDL_FLIP_HORIZONTAL);
46 }
47
48 if (flipY) {
49 flip = static_cast<SDL_FlipMode>(flip | SDL_FLIP_VERTICAL);
50 }
51
52 SDL_RenderTextureRotated(asw::display::renderer, tex.get(), nullptr, &dest, 0,
53 nullptr, flip);
54}
55
57 const asw::Quad<float>& position) {
58 SDL_FRect dest;
59 dest.x = position.position.x;
60 dest.y = position.position.y;
61 dest.w = position.size.x;
62 dest.h = position.size.y;
63
64 SDL_RenderTexture(asw::display::renderer, tex.get(), nullptr, &dest);
65}
66
68 const asw::Vec2<float>& position,
69 double angle) {
70 auto size = asw::util::getTextureSize(tex);
71
72 SDL_FRect dest;
73 dest.x = position.x;
74 dest.y = position.y;
75 dest.w = size.x;
76 dest.h = size.y;
77
78 SDL_RenderTextureRotated(asw::display::renderer, tex.get(), nullptr, &dest,
79 angle, nullptr, SDL_FLIP_NONE);
80}
81
83 const asw::Quad<float>& source,
84 const asw::Quad<float>& dest) {
85 SDL_FRect r_src;
86 r_src.x = source.position.x;
87 r_src.y = source.position.y;
88 r_src.w = source.size.x;
89 r_src.h = source.size.y;
90
91 SDL_FRect r_dest;
92 r_dest.x = dest.position.x;
93 r_dest.y = dest.position.y;
94 r_dest.w = dest.size.x;
95 r_dest.h = dest.size.y;
96
97 SDL_RenderTexture(asw::display::renderer, tex.get(), &r_src, &r_dest);
98}
99
101 const asw::Quad<float>& source,
102 const asw::Quad<float>& dest,
103 double angle) {
104 SDL_FRect r_src;
105 r_src.x = source.position.x;
106 r_src.y = source.position.y;
107 r_src.w = source.size.x;
108 r_src.h = source.size.y;
109
110 SDL_FRect r_dest;
111 r_dest.x = dest.position.x;
112 r_dest.y = dest.position.y;
113 r_dest.w = dest.size.x;
114 r_dest.h = dest.size.y;
115
116 SDL_RenderTextureRotated(asw::display::renderer, tex.get(), &r_src, &r_dest,
117 angle, nullptr, SDL_FLIP_NONE);
118}
119
120void asw::draw::text(const asw::Font& font,
121 const std::string& text,
122 const asw::Vec2<float>& position,
123 asw::Color color) {
124 if (text.empty()) {
125 return;
126 }
127
128 const SDL_Color sdlColor =
129 asw::util::makeColor(color.r, color.g, color.b, color.a);
130 SDL_Surface* textSurface =
131 TTF_RenderText_Solid(font.get(), text.c_str(), 0, sdlColor);
132 SDL_Texture* textTexture =
133 SDL_CreateTextureFromSurface(asw::display::renderer, textSurface);
134
135 SDL_FRect dest;
136 dest.x = position.x;
137 dest.y = position.y;
138 dest.w = float(textSurface->w);
139 dest.h = float(textSurface->h);
140
141 SDL_RenderTexture(asw::display::renderer, textTexture, nullptr, &dest);
142 SDL_DestroySurface(textSurface);
143 SDL_DestroyTexture(textTexture);
144}
145
147 const std::string& text,
148 const asw::Vec2<float>& position,
149 asw::Color color) {
150 auto size = asw::util::getTextSize(font, text);
151 asw::draw::text(font, text, position - asw::Vec2<float>(size.x / 2, 0),
152 color);
153}
154
156 const std::string& text,
157 const asw::Vec2<float>& position,
158 asw::Color color) {
159 auto size = asw::util::getTextSize(font, text);
160 asw::draw::text(font, text, position - asw::Vec2<float>(size.x, 0), color);
161}
162
163void asw::draw::point(const asw::Vec2<float>& position, asw::Color color) {
164 SDL_SetRenderDrawColor(asw::display::renderer, color.r, color.g, color.b,
165 color.a);
166 SDL_RenderPoint(asw::display::renderer, position.x, position.y);
167}
168
169void asw::draw::line(const asw::Vec2<float>& position1,
170 const asw::Vec2<float>& position2,
171 asw::Color color) {
172 SDL_SetRenderDrawColor(asw::display::renderer, color.r, color.g, color.b,
173 color.a);
174 SDL_RenderLine(asw::display::renderer, position1.x, position1.y, position2.x,
175 position2.y);
176}
177
178void asw::draw::rect(const asw::Quad<float>& position, asw::Color color) {
179 SDL_SetRenderDrawColor(asw::display::renderer, color.r, color.g, color.b,
180 color.a);
181 SDL_FRect rect;
182 rect.x = position.position.x;
183 rect.y = position.position.y;
184 rect.w = position.size.x;
185 rect.h = position.size.y;
186
187 SDL_RenderRect(asw::display::renderer, &rect);
188}
189
190void asw::draw::rectFill(const asw::Quad<float>& position, asw::Color color) {
191 SDL_SetRenderDrawColor(asw::display::renderer, color.r, color.g, color.b,
192 color.a);
193 SDL_FRect rect;
194 rect.x = position.position.x;
195 rect.y = position.position.y;
196 rect.w = position.size.x;
197 rect.h = position.size.y;
198
199 SDL_RenderFillRect(asw::display::renderer, &rect);
200}
201
203 float radius,
204 asw::Color color) {
205 SDL_SetRenderDrawColor(asw::display::renderer, color.r, color.g, color.b,
206 color.a);
207 for (int i = 0; i < 360; i++) {
208 SDL_RenderPoint(asw::display::renderer,
209 position.x + static_cast<float>(radius * std::cos(i)),
210 position.y + static_cast<float>(radius * std::sin(i)));
211 }
212}
213
215 float radius,
216 asw::Color color) {
217 SDL_SetRenderDrawColor(asw::display::renderer, color.r, color.g, color.b,
218 color.a);
219 for (int i = 0; i < 360; i++) {
220 SDL_RenderLine(asw::display::renderer, position.x, position.y,
221 position.x + static_cast<float>(radius * std::cos(i)),
222 position.y + static_cast<float>(radius * std::sin(i)));
223 }
224}
225
227 SDL_SetTextureBlendMode(texture.get(), static_cast<SDL_BlendMode>(mode));
228}
229
230void asw::draw::setAlpha(const asw::Texture& texture, float alpha) {
231 SDL_SetTextureAlphaModFloat(texture.get(), alpha);
232}
A 2D rectangle in space.
Definition geometry.h:193
Vec2< T > size
The size of the rectangle.
Definition geometry.h:335
Vec2< T > position
The position of the rectangle.
Definition geometry.h:332
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:226
void sprite(const asw::Texture &tex, const asw::Vec2< float > &position)
Draw a sprite.
Definition draw.cpp:17
void stretchSprite(const asw::Texture &tex, const asw::Quad< float > &position)
Draw a sprite with the option to stretch it.
Definition draw.cpp:56
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:67
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:100
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:146
void line(const asw::Vec2< float > &position1, const asw::Vec2< float > &position2, asw::Color color)
Draw a line.
Definition draw.cpp:169
void rectFill(const asw::Quad< float > &position, asw::Color color)
Draw a filled rectangle.
Definition draw.cpp:190
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:82
void setAlpha(const asw::Texture &texture, float alpha)
Set the alpha of a texture.
Definition draw.cpp:230
void rect(const asw::Quad< float > &position, asw::Color color)
Draw a rectangle.
Definition draw.cpp:178
void circle(const asw::Vec2< float > &position, float radius, asw::Color color)
Draw a circle.
Definition draw.cpp:202
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:30
void circleFill(const asw::Vec2< float > &position, float radius, asw::Color color)
Draw a filled circle.
Definition draw.cpp:214
void point(const asw::Vec2< float > &position, asw::Color color)
Draw a point.
Definition draw.cpp:163
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:155
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:120
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.