SDL Breakout Engine  0.1.0
An implementation of a Breakout game using sdl, implemented with efficient resource management.
GameObject.hpp
Go to the documentation of this file.
1 #ifndef GAMEOBJECT_HPP
2 #define GAMEOBJECT_HPP
3 
4 #include "TinyMath.hpp"
5 #include "SDL2/SDL.h"
6 #include "SDL2/SDL_mixer.h"
7 #include "SoundManager.hpp"
8 #include "GameManager.hpp"
9 
10 #include <string>
11 #include <sstream>
12 #include <iostream>
13 #include <chrono>
14 #include <vector>
15 #include <map>
16 
20 enum class CollisionType
21 {
22  North,
23  South,
24  East,
25  West,
26  NoContact
27 };
28 
29 /*
30 
31 Rectangle collision types:
32  N
33  - - - - - - -
34  | |
35  | |
36  W | | E
37  | |
38  - - - - - - -
39  S
40 */
41 
55 {
56 public:
57 
62  GameObject(const std::string &tag);
63 
69  GameObject(const Vector3D &position);
70 
74  GameObject(const Vector3D &position, const std::string &tag);
75 
79  GameObject(const Vector3D &position, const Vector3D &size);
80 
84  GameObject(const Vector3D &position, const Vector3D &size, const std::string &tag);
85 
91  void Update(const Vector3D &position);
92 
96  Vector3D GetSize();
97 
101  Vector3D GetSize() const;
102 
109  const Vector3D &_GetPosition() const;
110 
118  void DebugRender(SDL_Renderer *renderer);
119 
127  virtual void Hit(const std::string &tag);
128 
129  GameObject();
130 
131  std::string mTag = "";
132  bool alive = true;
133 
134  friend std::ostream &operator<<(std::ostream &os, const GameObject &obj);
135 
136 protected:
137  int mWidth = 0;
138  int mHeight = 0;
139 
142 };
143 
147 class GUIElement : public GameObject
148 {
149 public:
156  GUIElement(const std::shared_ptr<TextureWrapper> &texture);
157 
164  GUIElement(const std::shared_ptr<TextureWrapper> &texture, const std::string &tag);
165 
172  GUIElement(const std::shared_ptr<TextureWrapper> &texture, const Vector3D &startPosition);
173 
180  GUIElement(const std::shared_ptr<TextureWrapper> &texture, const Vector3D &startPosition, const std::string &tag);
181 
189  void Render(SDL_Renderer *renderer);
190 
191  GUIElement();
192 
193 protected:
194  std::shared_ptr<TextureWrapper> mTexture;
195  SDL_Rect mDestination;
196 };
197 
201 class Brick : public GUIElement
202 {
203 public:
210  Brick(const std::shared_ptr<TextureWrapper> &texture, const std::string &tag);
211 
216  void Hit(const std::string &tag);
217 };
218 
223 {
224 public:
232  MovingGUIElement(const std::shared_ptr<TextureWrapper> &texture, const Vector3D &startPosition, Vector3D direction);
233 
242  MovingGUIElement(const std::shared_ptr<TextureWrapper> &texture, const Vector3D &startPosition, Vector3D direction, const std::string &tag);
243 
251  void Move(float dt, int screenWidth, int screenHeight);
252 
258  void UpdateDirection(const Vector3D &direction);
259 
260  MovingGUIElement();
261 
266  const Vector3D &_GetDirection() const;
267 
279  bool Collide(GameObject &el2, bool angle);
280 
281 private:
282  Vector3D mDirection = Vector3D(0, 0, 0);
283 
284  CollisionType _Collide(const GameObject &el2);
285 };
286 
290 class Ball : public MovingGUIElement
291 {
292 public:
297 
304  bool Collide(GameObject &el2, bool angle);
305 
311  bool Collide(Brick &el2, bool angle);
312 };
313 
314 inline std::ostream &operator<<(std::ostream &os, const GameObject &obj)
315 {
316  os << "GameObject: " << obj.mTag << " at: " << obj.mPosition.x << ", " << obj.mPosition.y;
317  return os;
318 }
319 #endif
float y
Definition: TinyMath.hpp:19
float x
Definition: TinyMath.hpp:19
bool Collide(GameObject &el2, bool angle)
Definition: GameObject.cpp:291
Definition: GameObject.hpp:290
bool Collide(GameObject &el2, bool angle)
Definition: GameObject.cpp:149
the north side.
static ostream & operator<<(ostream &os, const Level &l)
Definition: LevelManager.cpp:9
Definition: GameObject.hpp:147
Definition: GameObject.hpp:222
Vector3D GetSize()
Definition: GameObject.cpp:28
no collision occurred.
Definition: GameObject.hpp:201
Definition: GameObject.hpp:54
bool alive
whether this object is alive or not.
Definition: GameObject.hpp:132
const Vector3D & _GetDirection() const
Definition: GameObject.cpp:144
void Hit(const std::string &tag)
Definition: GameObject.cpp:283
GameObject()
default constructor for a gameobject
Brick(const std::shared_ptr< TextureWrapper > &texture, const std::string &tag)
Definition: GameObject.cpp:281
void Update(const Vector3D &position)
Definition: GameObject.cpp:23
const Vector3D & _GetPosition() const
Definition: GameObject.cpp:38
void DebugRender(SDL_Renderer *renderer)
Definition: GameObject.cpp:43
virtual void Hit(const std::string &tag)
Definition: GameObject.cpp:55
std::string mTag
the name of this object, should be unique to other objects unless grouping is desired...
Definition: GameObject.hpp:131
GUIElement()
blank GUIElement
Vector3D startPosition
start position
Definition: GameObject.hpp:141
friend std::ostream & operator<<(std::ostream &os, const GameObject &obj)
overload for << operator
Definition: GameObject.hpp:314
Definition: TinyMath.hpp:15
int mWidth
internal width
Definition: GameObject.hpp:137
CollisionType
Definition: GameObject.hpp:20
Vector3D mPosition
internal position
Definition: GameObject.hpp:140
void Render(SDL_Renderer *renderer)
Definition: GameObject.cpp:87
std::shared_ptr< TextureWrapper > mTexture
internal texture
Definition: GameObject.hpp:194
void Move(float dt, int screenWidth, int screenHeight)
Definition: GameObject.cpp:107
SDL_Rect mDestination
rectangle representing the bounding box
Definition: GameObject.hpp:195
MovingGUIElement()
Blank moving element.
int mHeight
internal height
Definition: GameObject.hpp:138
void UpdateDirection(const Vector3D &direction)
Definition: GameObject.cpp:137