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
25 class Physics {
26 public:
27 // Velocity in pixels per second
29 // Acceleration in pixels per second squared
31 // Angular velocity in radians per second
32 float angularVelocity{0.0F};
33 // Angular acceleration in radians per second squared
35 };
36
39
42 class GameObject {
43 public:
44 GameObject() = default;
45
46 virtual ~GameObject() = default;
47
52 virtual void update(float deltaTime) {
53 body.velocity += body.acceleration * deltaTime;
54 transform.position += body.velocity * deltaTime;
56 rotation += body.angularVelocity * deltaTime;
57 };
58
61 virtual void draw() {};
62
67 const asw::Quad<float>& getTransform() const { return transform; }
68
72
75 float rotation{0.0F};
76
81 int zIndex{0};
82
86 bool active{true};
87
90 float alpha{1.0F};
91
95
98 bool alive{true};
99 };
100
103 class Sprite : public GameObject {
104 public:
111 void setTexture(const asw::Texture& texture, bool autoSize = true) {
112 this->texture = texture;
113
114 if (autoSize) {
116 }
117 }
118
123 void update(float deltaTime) override { GameObject::update(deltaTime); }
124
129 void draw() override {
130 if (texture == nullptr) {
131 return;
132 }
133
134 if (alpha < 1.0F) {
136 }
137
138 if (rotation) {
140 } else {
142 }
143
144 if (alpha < 1.0F) {
146 }
147 }
148
149 private:
153 };
154
157 class Text : public GameObject {
158 public:
163 void setFont(const asw::Font& font) { this->font = font; }
164
169 void setText(const std::string& text) { this->text = text; }
170
175 void setColor(const asw::Color& color) { this->color = color; }
176
181 void update(float deltaTime) override { GameObject::update(deltaTime); }
182
183 void draw() override {
185 }
186
187 private:
188 std::string text;
191 };
192
193}; // namespace asw::game
194
195#endif // ASW_COMPONENTS_H
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
Game Object.
Definition game.h:42
const asw::Quad< float > & getTransform() const
Get transform.
Definition game.h:67
bool active
Whether or not the object is active.
Definition game.h:86
float alpha
Opacity of the object.
Definition game.h:90
asw::Quad< float > transform
The transform of the object.
Definition game.h:71
Physics body
Physics body of the object.
Definition game.h:94
virtual void update(float deltaTime)
Update the object.
Definition game.h:52
virtual ~GameObject()=default
int zIndex
The layer that the object is on.
Definition game.h:81
float rotation
The rotation of the object in radians.
Definition game.h:75
virtual void draw()
Draw the object to the screen.
Definition game.h:61
bool alive
Alive state.
Definition game.h:98
Physics Component.
Definition game.h:25
asw::Vec2< float > acceleration
Definition game.h:30
float angularAcceleration
Definition game.h:34
asw::Vec2< float > velocity
Definition game.h:28
float angularVelocity
Definition game.h:32
Sprite Object.
Definition game.h:103
void setTexture(const asw::Texture &texture, bool autoSize=true)
Set the texture of the sprite.
Definition game.h:111
void draw() override
Draw the sprite to the screen.
Definition game.h:129
void update(float deltaTime) override
Update the sprite.
Definition game.h:123
asw::Texture texture
The texture of the sprite.
Definition game.h:152
Text Object.
Definition game.h:157
void setText(const std::string &text)
Set the text of the text object.
Definition game.h:169
void draw() override
Draw the object to the screen.
Definition game.h:183
asw::Color color
Definition game.h:190
asw::Font font
Definition game.h:189
std::string text
Definition game.h:188
void setColor(const asw::Color &color)
Set the color of the text.
Definition game.h:175
void setFont(const asw::Font &font)
Set the font of the text.
Definition game.h:163
void update(float deltaTime) override
Set the size of the text.
Definition game.h:181
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:68
void rotateSprite(const asw::Texture &tex, const asw::Vec2< float > &position, float angle)
Draw a sprite with the option to rotate it.
Definition draw.cpp:83
void setAlpha(const asw::Texture &texture, float alpha)
Set the alpha of a texture.
Definition draw.cpp:333
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:153
asw::Vec2< float > getTextureSize(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:34
std::shared_ptr< SDL_Texture > Texture
Alias for a shared pointer to an SDL_Texture.
Definition types.h:31
RGBA color struct with 8-bit channels.
Definition color.h:11
General utility functions.