Aspen
2D C++ Class-Based Object Oriented Game Engine
Graphics.hpp
1 #ifndef __GRAPHICS_HPP
2 #define __GRAPHICS_HPP
3 #define SDL_MAIN_HANDLED
4 #include <SDL2/SDL.h>
5 #include <SDL2/SDL_ttf.h>
6 #include "Log.hpp"
7 #include "Object.hpp"
8 #include <map>
9 
11 namespace Aspen
12 {
15 namespace Graphics
16 {
18 namespace UI
19 {
21 class Text;
22 } // namespace UI
23 
25 extern const int DEFAULT_WINDOW_WIDTH;
27 extern const int DEFAULT_WINDOW_HEIGHT;
28 
31 {
33  RED = 0xFF000000,
35  GREEN = 0x00FF0000,
37  BLUE = 0x0000FF00,
39  ALPHA = 0x000000FF
40 };
41 
43 class Color
44 {
45 protected:
47  unsigned _c;
48 
49 public:
53  Color(unsigned color = 0xFFFFFFFF);
60  Color(unsigned char r, unsigned char g, unsigned char b, unsigned char a = 0xFF);
61 
64  Uint8 Red() const;
67  Uint8 Green() const;
70  Uint8 Blue() const;
73  Uint8 Alpha() const;
74 
77  void Red(Uint8 r);
80  void Green(Uint8 g);
83  void Blue(Uint8 b);
86  void Alpha(Uint8 a);
87 
89  operator SDL_Color() const;
90 
95  bool operator==(const Color &rhs) const;
96 };
97 
100 namespace Colors
101 {
103 const Color BLACK = Color(0x000000FF);
105 const Color RED = Color(0xFF0000FF);
107 const Color GREEN = Color(0x00FF00FF);
109 const Color BLUE = Color(0x0000FFFF);
111 const Color WHITE = Color(0xFFFFFFFF);
113 const Color TRANSPARENT = Color(0x00000000);
114 } // namespace Colors
115 
118 class Geometry : public Object::Object
119 {
120 protected:
124  bool _fill;
125 
126 public:
131  Geometry(Object *parent = nullptr, std::string name = "Geometry");
139  Geometry(Color c, bool fill = false, Object *parent = nullptr, std::string name = "Graphics");
141  ~Geometry();
142 
145  Color &Color();
154  void Color(int r, int g, int b, int a);
158  bool Fill();
161  void SetFill(bool fill);
162 
165  void PopulateDebugger();
166 };
167 
169 class Rectangle : public Geometry
170 {
172  SDL_Rect _rect;
173 
174 public:
179  Rectangle(Object *parent = nullptr, std::string name = "Rectangle");
188  Rectangle(SDL_Rect rect, Aspen::Graphics::Color c = Aspen::Graphics::Color(), bool fill = false, Object *parent = nullptr, std::string name = "Rectangle");
190  ~Rectangle();
191 
195  void operator()();
196 
199  SDL_Rect &GetRect();
200 
203  void PopulateDebugger();
204 };
205 
207 class Point : public Geometry
208 {
210  SDL_Point _point;
211 
212 public:
217  Point(Object *parent = nullptr, std::string name = "Point");
225  Point(SDL_Point point, Aspen::Graphics::Color c = Aspen::Graphics::Color(), Object *parent = nullptr, std::string name = "Point");
227  ~Point();
228 
232  void operator()();
233 
236  SDL_Point &GetPoint();
237 
240  void PopulateDebugger();
241 };
242 
244 class Line : public Geometry
245 {
247  SDL_Point _start;
249  SDL_Point _end;
253  float _center;
254 
255 public:
260  Line(Object *parent = nullptr, std::string name = "Line");
270  Line(SDL_Point start, SDL_Point end, float center = 0.5f, Aspen::Graphics::Color c = Aspen::Graphics::Color(), Object *parent = nullptr, std::string name = "Line");
272  ~Line();
273 
277  void operator()();
278 
281  SDL_Point &GetStart();
284  SDL_Point &GetEnd();
287  float &GetCenter();
291  void SetCenter(float center);
292 
295  void PopulateDebugger();
296 };
297 
299 class Sprite : public Object::Object
300 {
302  std::string _path;
304  SDL_Surface *_surface;
306  SDL_Texture *_tex;
308  SDL_Rect _rect;
309 
310 public:
315  Sprite(Object *parent = nullptr, std::string name = "Sprite");
322  Sprite(std::string path, Object *parent = nullptr, std::string name = "Sprite");
324  ~Sprite();
325 
328  void End();
329 
331  void operator()();
332 
335  void SetParent(Object *parent);
336 
338  void GenerateTexture();
339 
342  const std::string &GetPath() const;
345  SDL_Surface *GetSurface();
348  SDL_Texture *GetTexture();
349 
352  SDL_Rect &GetRect();
353 
356  void PopulateDebugger();
357 };
358 
362 {
364  SDL_Rect _frame;
367 
368 public:
373  UniformSpritesheet(Object *parent = nullptr, std::string name = "UniformSpritesheet");
382  UniformSpritesheet(std::string path, unsigned frameCount, Object *parent = nullptr, std::string name = "UniformSpritesheet");
391  UniformSpritesheet(std::string path, unsigned frameWidth, unsigned frameHeight, Object *parent = nullptr, std::string name = "UniformSpritesheet");
401  UniformSpritesheet(std::string path, unsigned frameWidth, unsigned frameHeight, unsigned frameCount, Object *parent = nullptr, std::string name = "UniformSpritesheet");
404 
406  void operator()();
407 
410  int GetFrameCount();
414  SDL_Rect GetClipRectangle(int frame);
415 
418  void PopulateDebugger();
419 };
420 
423 class Animation : public Object::Object
424 {
428  float _delay;
432  bool _done;
433 
434 public:
440  Animation(Object *parent = nullptr, std::string name = "Animation");
448  Animation(UniformSpritesheet *spritesheet, float frameDelay = 0.05f, Object *parent = nullptr, std::string name = "Animation");
450  ~Animation();
451 
453  void operator()();
454 
458  int GetFrameCount();
459 
462  int GetFrame();
465  void SetFrame(int frame);
466 
469  float GetFrameDelay();
472  void SetFrameDelay(float delay);
473 
475  bool Done();
476 
479  void PopulateDebugger();
480 };
481 
484 class FontCache : public Object::Object
485 {
487  std::map<std::string, std::string> _paths;
489  std::map<std::pair<std::string, int>, TTF_Font *> _fonts;
490 
491 public:
497  FontCache(Object *parent = nullptr, std::string name = "FontCache");
499  ~FontCache();
500 
503  void LoadFont(std::string path);
507  void LoadFont(std::string path, std::string name);
510  void UnloadFont(std::string name);
516  TTF_Font *GetFont(std::string name, int size);
517 };
518 
520 class Graphics;
521 
523 class Camera : public Object::Object
524 {
527 
528 public:
534  Camera(Object *parent = nullptr, std::string name = "Camera");
535 
542  void SelectCamera();
543 
549  void SetGraphics(Graphics *gfx);
550 
553  void End();
554 };
555 
557 class Graphics : public Object::Object
558 {
561  static unsigned _gcount;
563  SDL_Window *_window;
565  SDL_Surface *_surface;
567  SDL_Renderer *_renderer;
573  static Graphics *_main;
574 
575 public:
580  Graphics(Object *parent = nullptr, std::string name = "Graphics");
588  Graphics(int w, int h, Object *parent = nullptr, std::string name = "Graphics");
590  ~Graphics();
591 
594  static Graphics *Get();
595 
599  void operator()();
600 
602  void OnEarlyUpdate();
604  void OnLateUpdate();
605 
610  void SetBGColor(int r, int g, int b);
611 
614  SDL_Surface *GetSurface();
617  SDL_Window *GetWindow();
620  SDL_Renderer *GetRenderer();
621 
624  void DrawRectangle(Rectangle *rect);
629  void DrawRectangle(SDL_Rect *rect, Color c, bool fill = true);
630 
633  void DrawPoint(Point *point);
637  void DrawPoint(SDL_Point *point, Color c);
638 
641  void DrawLine(Line *line);
646  void DrawLine(SDL_Point *start, SDL_Point *end, Color c);
647 
650  void DrawSprite(Sprite *sprite);
654  void DrawSprite(Sprite *sprite, SDL_Rect clip);
655 
658  void DrawText(UI::Text *text);
662  void DrawText(UI::Text *text, SDL_Rect clip);
663 
666  void SetCamera(Camera *camera);
669  Camera *GetCamera();
670 
672  void End();
673 
676  void PopulateDebugger();
677 };
678 } // namespace Graphics
679 } // namespace Aspen
680 
681 #endif
Geometry(Object *parent=nullptr, std::string name="Geometry")
Constructor.
void operator()()
Updates this object and all of its children Derived classes should call or reimplement this at some p...
SDL_Point & GetPoint()
Gets the SDL_Point of this object.
SDL_Surface * GetSurface()
Gets the surface of window.
bool _done
True if the animation just looped.
Definition: Graphics.hpp:432
SDL_Rect _rect
Rectangle to represent.
Definition: Graphics.hpp:172
UniformSpritesheet extension of Sprite class Splits a Spritesheet into multiple sprites.
Definition: Graphics.hpp:361
SDL_Rect & GetRect()
Gets the SDL_Rect of this object.
SDL_Surface * _surface
Surface loaded from _path.
Definition: Graphics.hpp:304
int _framecount
Total number of frames of the spritesheet.
Definition: Graphics.hpp:366
bool Done()
True if the animation just looped.
void SetCenter(float center)
Sets the center bias of this object.
TTF_Font * GetFont(std::string name, int size)
Gets a cached font Generates a new TTF_Font if it isn't yet cached.
Sprite(Object *parent=nullptr, std::string name="Sprite")
Constructor.
Uint8 Alpha() const
Gets alpha of the color.
int GetFrameCount()
Gets the total number of frames.
Geometry class Base class for creating basic geometry like rectangles.
Definition: Graphics.hpp:118
SDL_Point _end
Ending point of the line.
Definition: Graphics.hpp:249
void OnEarlyUpdate()
Starts the frame.
SDL_Renderer * GetRenderer()
Gets the hardware accelerated renderer.
bool _fill
Determines if the object is filled or outlined.
Definition: Graphics.hpp:124
void PopulateDebugger()
Fills out the Debugger if it exists with this Object's information Derived classes should call their ...
const Color GREEN
Green.
Definition: Graphics.hpp:107
void SetFill(bool fill)
Sets the fill value of the object.
Object(Object *parent=nullptr, std::string name="Object")
Constructor Derived classes should call this in their constructors' initialization list.
void SetFrame(int frame)
Sets the current frame.
void SetGraphics(Graphics *gfx)
Sets a given Graphics' camera to this.
void PopulateDebugger()
Fills out the Debugger if it exists with this Object's information Derived classes should call their ...
Point Geometry class.
Definition: Graphics.hpp:207
void DrawText(UI::Text *text)
Draws text.
void GenerateTexture()
Generates _tex and _rect when loading the texture from _path.
UniformSpritesheet(Object *parent=nullptr, std::string name="UniformSpritesheet")
Constructor.
SDL_Texture * GetTexture()
Gets the generated texture.
float GetFrameDelay()
Gets the current frame delay.
Color class that is used to store RGBA8 color information.
Definition: Graphics.hpp:43
SDL_Renderer * _renderer
Hardware accelerated renderer.
Definition: Graphics.hpp:567
Color _c
Color of the object.
Definition: Graphics.hpp:122
Camera class.
Definition: Graphics.hpp:523
int GetFrameCount()
Gets the total number of frames Calculates this by combining the frames of all Sprite and UniformSpri...
Sprite class.
Definition: Graphics.hpp:299
General base class Allows for parent/child relationship trees.
Definition: Object.hpp:33
SDL_Texture * _tex
Texture generated from _surface.
Definition: Graphics.hpp:306
const Color BLUE
Blue.
Definition: Graphics.hpp:109
const int DEFAULT_WINDOW_WIDTH
Default created window width.
Graphics(Object *parent=nullptr, std::string name="Graphics")
Constructor.
static unsigned _gcount
Number of Graphics Objects created Used for determining if SDL should be initialized or closed.
Definition: Graphics.hpp:561
Color(unsigned color=0xFFFFFFFF)
Constructor.
SDL_Point _start
Starting point of the line.
Definition: Graphics.hpp:247
SDL_Point & GetStart()
Gets the starting point of this object.
SDL_Surface * GetSurface()
Gets the loaded surface.
const int DEFAULT_WINDOW_HEIGHT
Default created window height.
Graphics class.
Definition: Graphics.hpp:557
void DrawLine(Line *line)
Draws a given line.
Color _background
Background color of the window.
Definition: Graphics.hpp:569
Animation(Object *parent=nullptr, std::string name="Animation")
Constructor Derived classes should call this in their constructors' initialization list.
void SelectCamera()
Finds the graphics in the main expected location and sets its camera to this Engine +— Graphics +— Ga...
Line Geometry class.
Definition: Graphics.hpp:244
SDL_Rect GetClipRectangle(int frame)
Gets the clipping rectangle of the spritesheet at the given frame.
Graphics * GetGraphics()
Gets the Graphics object currently using this camera.
float & GetCenter()
Gets the center bias of this object.
SDL_Point & GetEnd()
Gets the ending point of this object.
void UnloadFont(std::string name)
Removes the font from the recognized list and unloads all cached fonts by that name.
void PopulateDebugger()
Fills out the Debugger if it exists with this Object's information Derived classes should call their ...
std::map< std::string, std::string > _paths
Map of Font names to paths.
Definition: Graphics.hpp:487
void DrawSprite(Sprite *sprite)
Draws a given Sprite.
void operator()()
Updates this object and all of its children Derived classes should call or reimplement this at some p...
Green component of a color.
Definition: Graphics.hpp:35
static Graphics * Get()
Gets the main graphics object.
FontCache class Caches loaded fonts for drawing text with.
Definition: Graphics.hpp:484
const std::string & GetPath() const
Gets the path passed into the constructor.
Uint8 Green() const
Gets green of the color.
Color & Color()
Gets the color of the object.
float _delay
Delay in seconds between each frame.
Definition: Graphics.hpp:428
Camera * GetCamera()
Gets the current camera.
Red component of a color.
Definition: Graphics.hpp:33
const Color WHITE
White.
Definition: Graphics.hpp:111
Uint8 Red() const
Gets red of the color.
void End()
Shuts down and invalidates Object and all of its children An invalid child Object will be deleted by ...
void DrawRectangle(Rectangle *rect)
Draws a given Rectangle.
void SetParent(Object *parent)
Wrapper for Object::SetParent.
std::string _path
Path of file loaded as a sprite.
Definition: Graphics.hpp:302
Point(Object *parent=nullptr, std::string name="Point")
Constructor.
Blue component of a color.
Definition: Graphics.hpp:37
FontCache(Object *parent=nullptr, std::string name="FontCache")
Constructor Derived classes should call this in their constructors' initialization list.
const Color TRANSPARENT
Fully transparent.
Definition: Graphics.hpp:113
Text class.
Definition: UI.hpp:22
void End()
Frees the Window and shuts down SDL if this is the last Graphics object.
SDL_Surface * _surface
Surface of _window.
Definition: Graphics.hpp:565
int _currentFrame
Current frame of the animation.
Definition: Graphics.hpp:426
void DrawPoint(Point *point)
Draws a given point.
void operator()()
Updates this object and all of its children Derived classes should call or reimplement this at some p...
void PopulateDebugger()
Fills out the Debugger if it exists with this Object's information Derived classes should call their ...
Line(Object *parent=nullptr, std::string name="Line")
Constructor.
void operator()()
Draws the sprite to the parent Object's window if parent is of type Graphics.
Uint8 Blue() const
Gets blue of the color.
void PopulateDebugger()
Fills out the Debugger if it exists with this Object's information Derived classes should call their ...
const Color BLACK
Black.
Definition: Graphics.hpp:103
static Graphics * _main
First created Graphics object.
Definition: Graphics.hpp:573
int GetFrame()
Gets the current frame.
bool Fill()
Gets the fill value of the object.
void PopulateDebugger()
Fills out the Debugger if it exists with this Object's information Derived classes should call their ...
void operator()()
Draws the sprite to the parent Object's window if parent is of type Graphics.
SDL_Point _point
Point to represent.
Definition: Graphics.hpp:210
SDL_Rect _frame
Individual frame of the spritesheet.
Definition: Graphics.hpp:364
SDL_Rect _rect
Rectangle to draw _tex with.
Definition: Graphics.hpp:308
float _center
Center bias of the line Range: (0, 1) Center point: _start * _center + _end * (1 - _center)
Definition: Graphics.hpp:253
void PopulateDebugger()
Fills out the Debugger if it exists with this Object's information Derived classes should call their ...
void SetCamera(Camera *camera)
Sets the current camera.
unsigned _c
RGBA8 color.
Definition: Graphics.hpp:47
bool operator==(const Color &rhs) const
Equality operator.
void SetFrameDelay(float delay)
Sets the frame delay.
void LoadFont(std::string path)
Adds the requested font to the list of recognized paths.
Rectangle(Object *parent=nullptr, std::string name="Rectangle")
Constructor.
Camera * _camera
Currently selected camera.
Definition: Graphics.hpp:571
const Color RED
Red.
Definition: Graphics.hpp:105
void OnLateUpdate()
Ends the frame.
void operator()()
Updates this object and all of its children Derived classes should call or reimplement this at some p...
float _remainingDelay
Delay in seconds before the next frame change.
Definition: Graphics.hpp:430
Alpha component of a color.
Definition: Graphics.hpp:39
void End()
Shuts down and invalidates Object and all of its children An invalid child Object will be deleted by ...
Aspen engine namespace.
Definition: Audio.hpp:8
Camera(Object *parent=nullptr, std::string name="Camera")
Constructor Derived classes should call this in their constructors' initialization list.
Graphics * _gfx
Graphics object currently using the Camera.
Definition: Graphics.hpp:526
Rectangle Geometry class.
Definition: Graphics.hpp:169
void operator()()
Draws the sprite to the parent Object's window if parent is of type Graphics.
std::map< std::pair< std::string, int >, TTF_Font * > _fonts
Map of (Font name, Font size) to loaded fonts.
Definition: Graphics.hpp:489
void SetBGColor(int r, int g, int b)
Sets the window's background color.
SDL_Rect & GetRect()
Gets the rectangle _tex is drawn to.
void PopulateDebugger()
Fills out the Debugger if it exists with this Object's information Derived classes should call their ...
Animation class Gets all animation frames from child Sprite and UniformSpritesheet Objects.
Definition: Graphics.hpp:423
SDL_Window * GetWindow()
Gets the window.
COLOR_MASK
Masks used to extract individual color components from a color.
Definition: Graphics.hpp:30
SDL_Window * _window
Window to be displayed.
Definition: Graphics.hpp:563