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
game.h
Go to the documentation of this file.
1
8
9#ifndef ASW_COMPONENTS_H
10#define ASW_COMPONENTS_H
11
12#include <string>
13
14#include "./color.h"
15#include "./draw.h"
16#include "./geometry.h"
17#include "./util.h"
18
19namespace asw::game {
22
25class Physics {
26public:
27 // Velocity in pixels per second
29 // Acceleration in pixels per second squared
31 // Angular velocity in radians per second
32 float angular_velocity { 0.0F };
33 // Angular acceleration in radians per second squared
34 float angular_acceleration { 0.0F };
35};
36
39
43public:
44 GameObject() = default;
45
46 virtual ~GameObject() = default;
47
52 virtual void update(float dt)
53 {
58 };
59
62 virtual void draw() {
63 // Do nothing by default
64 };
65
71 {
72 return transform;
73 }
74
78
81 float rotation { 0.0F };
82
87 int z_index { 0 };
88
92 bool active { true };
93
96 float alpha { 1.0F };
97
101
104 bool alive { true };
105};
106
109class Sprite : public GameObject {
110public:
117 void set_texture(const asw::Texture& texture, bool auto_size = true)
118 {
119 this->texture_ = texture;
120
121 if (auto_size) {
123 }
124 }
125
130 void draw() override
131 {
132 if (texture_ == nullptr) {
133 return;
134 }
135
136 if (alpha < 1.0F) {
138 }
139
140 if (rotation != 0.0F) {
142 } else {
144 }
145
146 if (alpha < 1.0F) {
148 }
149 }
150
151private:
155};
156
159class Text : public GameObject {
160public:
165 void set_font(const asw::Font& font)
166 {
167 this->font_ = font;
168 }
169
174 void set_text(const std::string_view& text)
175 {
176 this->text_ = text;
177 }
178
183 void set_color(const asw::Color& color)
184 {
185 this->color_ = color;
186 }
187
193 {
194 this->justify_ = justify;
195 }
196
199 void draw() override
200 {
202 }
203
204private:
205 std::string text_;
209};
210
211}; // namespace asw::game
212
213#endif // ASW_COMPONENTS_H
A 2D rectangle in space.
Definition geometry.h:433
Vec2< T > size
The size of the rectangle.
Definition geometry.h:600
Vec2< T > position
The position of the rectangle.
Definition geometry.h:597
A 2D vector in space.
Definition geometry.h:21
Game Object.
Definition game.h:42
const asw::Quad< float > & get_transform() const
Get transform.
Definition game.h:70
virtual void update(float dt)
Update the object.
Definition game.h:52
bool active
Whether or not the object is active.
Definition game.h:92
float alpha
Opacity of the object.
Definition game.h:96
asw::Quad< float > transform
The transform of the object.
Definition game.h:77
Physics body
Physics body of the object.
Definition game.h:100
virtual ~GameObject()=default
int z_index
The layer that the object is on.
Definition game.h:87
float rotation
The rotation of the object in radians.
Definition game.h:81
virtual void draw()
Draw the object to the screen.
Definition game.h:62
bool alive
Alive state.
Definition game.h:104
Physics Component.
Definition game.h:25
asw::Vec2< float > acceleration
Definition game.h:30
asw::Vec2< float > velocity
Definition game.h:28
float angular_acceleration
Definition game.h:34
float angular_velocity
Definition game.h:32
Sprite Object.
Definition game.h:109
void set_texture(const asw::Texture &texture, bool auto_size=true)
Set the texture of the sprite.
Definition game.h:117
asw::Texture texture_
The texture of the sprite.
Definition game.h:154
void draw() override
Draw the sprite to the screen.
Definition game.h:130
Text Object.
Definition game.h:159
void set_text(const std::string_view &text)
Set the text of the text object.
Definition game.h:174
void draw() override
Draw the text to the screen.
Definition game.h:199
void set_justify(asw::TextJustify justify)
Set the justification of the text.
Definition game.h:192
void set_font(const asw::Font &font)
Set the font of the text.
Definition game.h:165
void set_color(const asw::Color &color)
Set the color of the text.
Definition game.h:183
asw::Font font_
Definition game.h:206
asw::Color color_
Definition game.h:207
asw::TextJustify justify_
Definition game.h:208
std::string text_
Definition game.h:205
Routines for drawing sprites and primitives to the screen.
Common geometry types.
void rotate_sprite(const asw::Texture &tex, const asw::Vec2< float > &position, float angle)
Draw a sprite with the option to rotate it.
Definition draw.cpp:86
void stretch_sprite(const asw::Texture &tex, const asw::Quad< float > &position)
Draw a sprite with the option to stretch it.
Definition draw.cpp:70
void text(const asw::Font &font, const std::string &text, const asw::Vec2< float > &position, asw::Color color, asw::TextJustify justify=asw::TextJustify::Left)
Draw text.
Definition draw.cpp:156
void set_alpha(const asw::Texture &texture, float alpha)
Set the alpha of a texture.
Definition draw.cpp:317
asw::Vec2< float > get_texture_size(const asw::Texture &tex)
Get texture size.
Definition util.cpp:15
std::shared_ptr< TTF_Font > Font
Alias for a shared pointer to an TTF_Font.
Definition types.h:41
std::shared_ptr< SDL_Texture > Texture
Alias for a shared pointer to an SDL_Texture.
Definition types.h:38
TextJustify
Text justification options for text rendering.
Definition types.h:31
RGBA color struct with 8-bit channels.
Definition color.h:11
General utility functions.