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_SetTextureBlendMode(textTexture, SDL_BLENDMODE_BLEND);
136 SDL_SetTextureScaleMode(textTexture, SDL_SCALEMODE_NEAREST);
137
138 SDL_FRect dest;
139 dest.x = position.x;
140 dest.y = position.y;
141 dest.w = float(textSurface->w);
142 dest.h = float(textSurface->h);
143
144 SDL_RenderTexture(asw::display::renderer, textTexture, nullptr, &dest);
145 SDL_DestroySurface(textSurface);
146 SDL_DestroyTexture(textTexture);
147}
148
150 const std::string& text,
151 const asw::Vec2<float>& position,
152 asw::Color color) {
153 auto size = asw::util::getTextSize(font, text);
154 asw::draw::text(font, text, position - asw::Vec2<float>(size.x / 2, 0),
155 color);
156}
157
159 const std::string& text,
160 const asw::Vec2<float>& position,
161 asw::Color color) {
162 auto size = asw::util::getTextSize(font, text);
163 asw::draw::text(font, text, position - asw::Vec2<float>(size.x, 0), color);
164}
165
166void asw::draw::point(const asw::Vec2<float>& position, asw::Color color) {
167 SDL_SetRenderDrawColor(asw::display::renderer, color.r, color.g, color.b,
168 color.a);
169 SDL_RenderPoint(asw::display::renderer, position.x, position.y);
170}
171
172void asw::draw::line(const asw::Vec2<float>& position1,
173 const asw::Vec2<float>& position2,
174 asw::Color color) {
175 SDL_SetRenderDrawColor(asw::display::renderer, color.r, color.g, color.b,
176 color.a);
177 SDL_RenderLine(asw::display::renderer, position1.x, position1.y, position2.x,
178 position2.y);
179}
180
181void asw::draw::rect(const asw::Quad<float>& position, asw::Color color) {
182 SDL_SetRenderDrawColor(asw::display::renderer, color.r, color.g, color.b,
183 color.a);
184 SDL_FRect rect;
185 rect.x = position.position.x;
186 rect.y = position.position.y;
187 rect.w = position.size.x;
188 rect.h = position.size.y;
189
190 SDL_RenderRect(asw::display::renderer, &rect);
191}
192
193void asw::draw::rectFill(const asw::Quad<float>& position, asw::Color color) {
194 SDL_SetRenderDrawColor(asw::display::renderer, color.r, color.g, color.b,
195 color.a);
196 SDL_FRect rect;
197 rect.x = position.position.x;
198 rect.y = position.position.y;
199 rect.w = position.size.x;
200 rect.h = position.size.y;
201
202 SDL_RenderFillRect(asw::display::renderer, &rect);
203}
204
206 float radius,
207 asw::Color color) {
208 SDL_SetRenderDrawColor(asw::display::renderer, color.r, color.g, color.b,
209 color.a);
210 for (int i = 0; i < 360; i++) {
211 SDL_RenderPoint(asw::display::renderer,
212 position.x + static_cast<float>(radius * std::cos(i)),
213 position.y + static_cast<float>(radius * std::sin(i)));
214 }
215}
216
218 float radius,
219 asw::Color color) {
220 SDL_SetRenderDrawColor(asw::display::renderer, color.r, color.g, color.b,
221 color.a);
222 for (int i = 0; i < 360; i++) {
223 SDL_RenderLine(asw::display::renderer, position.x, position.y,
224 position.x + static_cast<float>(radius * std::cos(i)),
225 position.y + static_cast<float>(radius * std::sin(i)));
226 }
227}
228
230 SDL_SetTextureBlendMode(texture.get(), static_cast<SDL_BlendMode>(mode));
231}
232
233void asw::draw::setAlpha(const asw::Texture& texture, float alpha) {
234 SDL_SetTextureAlphaModFloat(texture.get(), alpha);
235}
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:229
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:149
void line(const asw::Vec2< float > &position1, const asw::Vec2< float > &position2, asw::Color color)
Draw a line.
Definition draw.cpp:172
void rectFill(const asw::Quad< float > &position, asw::Color color)
Draw a filled rectangle.
Definition draw.cpp:193
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:233
void rect(const asw::Quad< float > &position, asw::Color color)
Draw a rectangle.
Definition draw.cpp:181
void circle(const asw::Vec2< float > &position, float radius, asw::Color color)
Draw a circle.
Definition draw.cpp:205
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:217
void point(const asw::Vec2< float > &position, asw::Color color)
Draw a point.
Definition draw.cpp:166
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:158
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.