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 "./draw.h"
15#include "./geometry.h"
16#include "./util.h"
17
18namespace asw::game {
21
31
34
37 class GameObject {
38 public:
39 GameObject() = default;
40
41 virtual ~GameObject() = default;
42
47 virtual void update(float deltaTime) {
48 body.velocity += body.acceleration * deltaTime;
49 transform.position += body.velocity * deltaTime;
51 rotation += body.angularVelocity * deltaTime;
52 };
53
56 virtual void draw() {};
57
62 const asw::Quad<float>& getTransform() const { return transform; }
63
67
70 float rotation{0};
71
76 int zIndex{0};
77
81 bool active{true};
82
85 float alpha{1.0F};
86
90
93 bool alive{true};
94 };
95
98 class Sprite : public GameObject {
99 public:
106 void setTexture(const asw::Texture& texture, bool autoSize = true) {
107 this->texture = texture;
108
109 if (autoSize) {
111 }
112 }
113
118 void update(float deltaTime) override { GameObject::update(deltaTime); }
119
124 void draw() override {
125 if (texture == nullptr) {
126 return;
127 }
128
129 if (alpha < 1.0F) {
131 }
132
133 if (rotation) {
135 } else {
137 }
138
139 if (alpha < 1.0F) {
141 }
142 }
143
144 private:
148 };
149
152 class Text : public GameObject {
153 public:
158 void setFont(const asw::Font& font) { this->font = font; }
159
164 void setText(const std::string& text) { this->text = text; }
165
170 void setColor(const asw::Color& color) { this->color = color; }
171
176 void update(float deltaTime) override { GameObject::update(deltaTime); }
177
178 void draw() override {
180 }
181
182 private:
183 std::string text;
186 };
187
188}; // namespace asw::game
189
190#endif // ASW_COMPONENTS_H
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
Game Object.
Definition game.h:37
const asw::Quad< float > & getTransform() const
Get transform.
Definition game.h:62
bool active
Whether or not the object is active.
Definition game.h:81
float alpha
Opacity of the object.
Definition game.h:85
asw::Quad< float > transform
The transform of the object.
Definition game.h:66
Physics body
Physics body of the object.
Definition game.h:89
virtual void update(float deltaTime)
Update the object.
Definition game.h:47
virtual ~GameObject()=default
int zIndex
The layer that the object is on.
Definition game.h:76
float rotation
The rotation of the object in degrees.
Definition game.h:70
virtual void draw()
Draw the object to the screen.
Definition game.h:56
bool alive
Alive state.
Definition game.h:93
Physics Component.
Definition game.h:24
asw::Vec2< float > acceleration
Definition game.h:27
float angularAcceleration
Definition game.h:29
asw::Vec2< float > velocity
Definition game.h:26
float angularVelocity
Definition game.h:28
Sprite Object.
Definition game.h:98
void setTexture(const asw::Texture &texture, bool autoSize=true)
Set the texture of the sprite.
Definition game.h:106
void draw() override
Draw the sprite to the screen.
Definition game.h:124
void update(float deltaTime) override
Update the sprite.
Definition game.h:118
asw::Texture texture
The texture of the sprite.
Definition game.h:147
Text Object.
Definition game.h:152
void setText(const std::string &text)
Set the text of the text object.
Definition game.h:164
void draw() override
Draw the object to the screen.
Definition game.h:178
asw::Color color
Definition game.h:185
asw::Font font
Definition game.h:184
std::string text
Definition game.h:183
void setColor(const asw::Color &color)
Set the color of the text.
Definition game.h:170
void setFont(const asw::Font &font)
Set the font of the text.
Definition game.h:158
void update(float deltaTime) override
Set the size of the text.
Definition game.h:176
Routines for drawing sprites and primitives to the screen.
Common geometry types.
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 setAlpha(const asw::Texture &texture, float alpha)
Set the alpha of a texture.
Definition draw.cpp:230
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
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
SDL_Color Color
Alias for an SDL_Color.
Definition types.h:38
General utility functions.