Arkanoid Remake  1.0
sdltk.h
Go to the documentation of this file.
1 /*
2 ArkanoidRemakeSDL
3 a sample of object-oriented game programming with C++.
4 version 1.0, May 2016
5 
6 Copyright (c) 2016 Giovanni Paolo Vigano'
7 
8 The source code of the SDL library used by ArkanoidRemakeSDL can be found here:
9 https://www.libsdl.org/
10 ---
11 ArkanoidRemakeSDL source code is licensed with the same zlib license:
12 (http://www.gzip.org/zlib/zlib_license.html)
13 
14 This software is provided 'as-is', without any express or implied warranty. In
15 no event will the authors be held liable for any damages arising from the use of
16 this software.
17 
18 Permission is granted to anyone to use this software for any purpose, including
19 commercial applications, and to alter it and redistribute it freely, subject to
20 the following restrictions:
21 
22  1. The origin of this software must not be misrepresented; you must not
23  claim that you wrote the original software. If you use this software in a
24  product, an acknowledgment in the product documentation would be appreciated
25  but is not required.
26 
27  2. Altered source versions must be plainly marked as such, and must not be
28  misrepresented as being the original software.
29 
30  3. This notice may not be removed or altered from any source distribution.
31 */
35 
36 #pragma once
37 #ifndef SDLTK_H
38 #define SDLTK_H
39 
40 #include <string>
41 
42 #include <string>
43 #include <vector>
44 #include <map>
45 
46 
47 
48 // forward declaration for SDL data
49 struct SDL_Surface;
50 struct SDL_Window;
51 struct SDL_PixelFormat;
52 struct SDL_Rect;
53 struct SDL_Color;
54 struct SDL_Renderer;
55 struct _TTF_Font;
56 typedef struct _TTF_Font TTF_Font;
57 struct Mix_Chunk;
58 struct _Mix_Music;
59 typedef struct _Mix_Music Mix_Music;
60 
61 namespace sdltk
62 {
65 
67  enum EventType
68  {
70  APP,
72  KEY,
73  };
74 
77  {
78  NONE,
79  DOWN,
80  UP,
81  MOVE,
82  QUIT,
83  };
84 
86  enum Button
87  {
88  LEFT=0,
89  MIDDLE=1,
90  RIGHT=2,
91 
92  ESC=27,
93  ENTER=13,
94  DEL=127,
95  BACK=8,
96  TAB=9,
97  };
98 
99 
101  struct Coords
102  {
103  int X;
104  int Y;
105  Coords()
106  {
107  X=Y=0;
108  }
109  };
110 
112  struct EventData
113  {
116 
119 
121  int Button;
122 
125 
128  {
129  Type=UNDEFINED;
130  Button=-1;
131  Action=NONE;
132  }
133  };
134 
137  {
138  public:
140  virtual bool operator ()(const sdltk::EventData& evt_data) = 0;
141  };
142 
144  class Timer
145  {
146  private:
147  unsigned int mStartMs;
148 
149  bool mStarted;
150 
151  public:
153  Timer();
154 
156  void Start();
157 
159  void Stop();
160 
162  unsigned int GetTimeMs();
163  };
164 
166  struct ColorRGBA
167  {
168  unsigned char R;
169  unsigned char G;
170  unsigned char B;
171  unsigned char A;
172 
178  ColorRGBA(unsigned char r=0, unsigned char g=0, unsigned char b=0, unsigned char a=0)
179  {
180  R=r;
181  G=g;
182  B=b;
183  A=a;
184  }
185  };
186 
188  class SdlApp
189  {
190 
191  // these classes are defined here, inside the privatre section of SdlApp
192  // to hide the internal implementation for external users of this class
193 
194  class Window
195  {
196  public:
197  Window();
198  SDL_Window* mSdlWin;
199  SDL_Surface *mSdlWinSurf;
200  SDL_PixelFormat *mPixelFormat;
201  int mWinPosX;
202  int mWinPosY;
203  int mWinW;
204  int mWinH;
205  bool Create(std::string title, bool fullscreen, int width, int height, int x=-1, int y=-1);
206  void Destroy();
207  };
208 
209 
210  class SurfaceMatrix
211  {
212  SDL_Surface* mMatrixSurf;
213  int mElemW;
214  int mElemH;
215  int mRows;
216  int mCols;
217  void UpdateClips();
218  public:
219  SurfaceMatrix(SDL_Surface* surf=nullptr, int rows=0, int cols=0);
220  void SetSdlSurface(SDL_Surface* surf, int row, int col);
221  void ApplySdlSurface(int row, int col, SDL_Surface * dst, int dst_x, int dst_y);
222  int GetElemW() { return mElemW; }
223  int GetElemH() { return mElemH; }
224  };
225 
226 
227  class Text
228  {
229  bool mTextChanged;
230  int mShadowOffset;
231  SDL_Surface* mTextSurf;
232  SDL_Surface* mTextShadowSurf;
233  TTF_Font* mFont;
234  ColorRGBA mTextColor;
235  ColorRGBA mShadowColor;
236  std::string mTextString;
237  int mHorizOffset;
238  int mHorizAlign;
239  int mVertOffset;
240  int mVertAlign;
241  void Update();
242  void UpdateAlignment();
243  public:
244  enum {LEFT=-1,CENTER=0,RIGHT=1};
245  enum {TOP=-1,MIDDLE=0,BOTTOM=1};
246  Text( ColorRGBA color=ColorRGBA(255,255,255,255), ColorRGBA shadow_color=ColorRGBA(), int shadow_offset=1 );
247  void Render(SDL_Surface* surf, int x, int y);
248  void SetText(const std::string& text);
249  void SetFont(TTF_Font* font);
250  void SetAlignment(int horiz, int vert);
251  int TextHeight();
252  };
253  public:
254 
256  SdlApp();
257 
259  virtual ~SdlApp();
260 
262  bool CreateWindow(std::string title, bool fullscreen=true, int width=-1, int height=-1, int x=-1, int y=-1);
263 
265  void UpdateWindow();
266 
268  bool HandleEvents();
269 
273  void SetEventFunc(EventFunctor* func, bool auto_delete=true)
274  {
275  mEventCB = func;
276  mAutoDeleteEventCB = auto_delete;
277  }
278 
280  std::string& FontsPath() { return mFontsPath; }
281 
283  std::string& ImagesPath() { return mImagesPath; }
284 
286  std::string& SoundsPath() { return mSoundPath; }
287 
289  std::string& MusicPath() { return mMusicPath; }
290 
292  int WinWidth();
293 
295  int WinHeight();
296 
298  bool InitSdl();
299 
301  void ExitSdl();
302 
304  bool ExistsSurface(const std::string& name) const;
305 
308  bool NewSurface(const std::string& name, int width, int height);
309 
312  bool DeleteSurface(const std::string& name);
313 
317  bool LoadSurface(const std::string& name, const std::string& filename);
318 
320  int GetSurfWidth(const std::string& name);
321 
323  int GetSurfHeight(const std::string& name);
324 
331  bool ApplySurf(int x, int y, const std::string& source, const std::string& destination="");
332 
338  bool TileSurf(int u, int v, const std::string& source, const std::string& destination);
339 
341  void DestroySurface(const std::string& name);
342 
349  bool SetupSurfMatrix(const std::string& surf_mat, const std::string& filename, int rows, int cols);
350 
357  bool ApplySurfMatrixElem(const std::string& surf_mat, int row, int col, int x, int y, const std::string& destination="");
358 
364  bool GetSurfMatrixElemSize(const std::string& surf_mat, int& w, int& h);
365 
367  bool ExistsSurfaceMatrix(const std::string& name) const;
368 
370  void DestroySurfaceMatrix(const std::string& name);
371 
372  // Check if a font with the given name was created
373  bool ExistsFont(const std::string& name);
374 
380  bool LoadFont(const std::string& name, std::string path, int size);
381 
382  // Check if a text with the given name was created
383  bool ExistsText(const std::string& name);
384 
392  bool CreateText(
393  const std::string& text_name,
394  const std::string& font_name,
395  const ColorRGBA& col,
396  const ColorRGBA& shadowcol=ColorRGBA(),
397  int shadow_offset=1);
398 
400  void SetText(const std::string& text_name, const std::string& text);
401 
406  void RenderText(const std::string& text_name, int x, int y, const std::string& dest_surface="");
407 
410  bool LoadSound(const std::string& name, std::string path="");
411 
413  void PlaySound(const std::string& name);
414 
416  void SetSoundsVolume(int vol);
417 
419  int GetSoundsVolume(int vol);
420 
423  bool LoadMusic(const std::string& name, std::string path="");
424 
426  void PlayMusic(unsigned int index);
427 
429  void PlayMusic();
430 
432  void SetMusicVolume(int vol);
433 
435  int GetMusicVolume();
436 
438  void SwitchAudio(bool on);
439 
441  bool AudioEnabled() { return mAudioEnabled; }
442 
444  void Delay(unsigned int ms);
445 
446  protected:
447 
448  // internal window data
449 
450  Window mWin;
451 
452  // paths for loading resource files
453 
454  std::string mFontsPath;
455  std::string mImagesPath;
456  std::string mSoundPath;
457  std::string mMusicPath;
458 
459  // Audio (music/sound) enabling flag
460 
461  bool mAudioEnabled;
462 
463  // Fonts
464  std::map<std::string,TTF_Font*> mFont;
465 
466  // Images
467  std::map<std::string,SDL_Surface*> mSurface;
468 
469  // Image matrices (tables of images)
470  std::map<std::string,SurfaceMatrix*> mSurfaceMatrix;
471 
472  // Sounds
473  std::map<std::string,Mix_Chunk*> mSoundFx;
474 
475  // Music
476  unsigned mMusic;
477  std::vector<Mix_Music*> mPlaylist;
478 
479  // Text
480  std::map<std::string,Text> mText;
481 
482  // reference to the external event manager
483 
484  EventFunctor* mEventCB;
485  bool mAutoDeleteEventCB;
486 
487  // utility methods
488 
489  SDL_Surface* CreateSdlSurface(int w, int h);
490  SDL_Surface* LoadImageAsSurface(const std::string& filename);
491  void ApplySdlSurface(int x, int y, SDL_Surface* source, SDL_Surface* destination=nullptr, SDL_Rect *src_rect=nullptr);
492  void TileSdlSurface(int u, int v, SDL_Surface* source, SDL_Surface* destination);
493  void DestroySdlSurface(SDL_Surface*& surf);
494  };
495 
497 
498 }//namespace sdltk
499 
500 
501 #endif//SDLTK_H
bool ApplySurf(int x, int y, const std::string &source, const std::string &destination="")
int GetSoundsVolume(int vol)
Get the volume for sounds (0 to 128)
bool AudioEnabled()
Check if the audio is on (true) or off (false)
Definition: sdltk.h:441
Application event.
Definition: sdltk.h:70
Mouse event.
Definition: sdltk.h:71
unsigned char R
Red component (0..255)
Definition: sdltk.h:168
bool HandleEvents()
Handle events (mouse, keyboard)
ColorRGBA(unsigned char r=0, unsigned char g=0, unsigned char b=0, unsigned char a=0)
Definition: sdltk.h:178
bool SetupSurfMatrix(const std::string &surf_mat, const std::string &filename, int rows, int cols)
virtual ~SdlApp()
Just trace the deletion of this message with a message.
int WinHeight()
Get the height of the window surface.
void PlaySound(const std::string &name)
Play the named sound.
std::string & ImagesPath()
Access (read/change) the path containing the image files.
Definition: sdltk.h:283
Keyboard event.
Definition: sdltk.h:72
int GetSurfWidth(const std::string &name)
Get the width of the named surface.
EventType Type
Event type (see EventType)
Definition: sdltk.h:115
Mouse middle button.
Definition: sdltk.h:89
EventType
Event type (see EventData)
Definition: sdltk.h:67
int WinWidth()
Get the width of the window surface.
SDL application manager.
Definition: sdltk.h:188
int Button
Mouse/keyboard button (see Button)
Definition: sdltk.h:121
Color data structure (red, green, blue, alpha=opacity)
Definition: sdltk.h:166
std::string & FontsPath()
Access (read/change) the path containing the font files.
Definition: sdltk.h:280
void DestroySurface(const std::string &name)
Destroy a previously created surface.
bool ExistsSurfaceMatrix(const std::string &name) const
Check if a named surface matrix exists.
Event not defined.
Definition: sdltk.h:69
int GetMusicVolume()
Get the music volume (0 to 128)
Delete key (ASCII code)
Definition: sdltk.h:94
void SetText(const std::string &text_name, const std::string &text)
Set the text content for the named text.
int GetSurfHeight(const std::string &name)
Get the width of the named surface.
EventData()
Constructor: set class attributes to default values.
Definition: sdltk.h:127
bool LoadFont(const std::string &name, std::string path, int size)
Mouse left button.
Definition: sdltk.h:88
unsigned char B
Blue component (0..255)
Definition: sdltk.h:170
void DestroySurfaceMatrix(const std::string &name)
Destroy a previously created surface matrix.
bool TileSurf(int u, int v, const std::string &source, const std::string &destination)
bool ApplySurfMatrixElem(const std::string &surf_mat, int row, int col, int x, int y, const std::string &destination="")
bool DeleteSurface(const std::string &name)
bool LoadSurface(const std::string &name, const std::string &filename)
Application quit.
Definition: sdltk.h:82
Button/key up.
Definition: sdltk.h:80
bool CreateWindow(std::string title, bool fullscreen=true, int width=-1, int height=-1, int x=-1, int y=-1)
Create teh application window.
Backspace key (ASCII code)
Definition: sdltk.h:95
bool CreateText(const std::string &text_name, const std::string &font_name, const ColorRGBA &col, const ColorRGBA &shadowcol=ColorRGBA(), int shadow_offset=1)
Coords Point
Coordinates at which the event occurred (see Point)
Definition: sdltk.h:118
Functor class for events handling.
Definition: sdltk.h:136
unsigned char G
Green component (0..255)
Definition: sdltk.h:169
virtual bool operator()(const sdltk::EventData &evt_data)=0
Functor operator.
bool GetSurfMatrixElemSize(const std::string &surf_mat, int &w, int &h)
void SetSoundsVolume(int vol)
Set the volume for sounds (0 to 128)
Coordinates at which the event occurred (see EventData)
Definition: sdltk.h:101
void UpdateWindow()
Redraw window surface.
void Delay(unsigned int ms)
Delay (suspend) the execution for the given milliseconds.
Timer()
Constructor.
SdlApp()
Constructor: just initialize data members.
Button
Mouse/keyboard button (see EventData)
Definition: sdltk.h:86
Mouse move.
Definition: sdltk.h:81
void ExitSdl()
Destroy all and quit SDL library.
Tabulation key (ASCII code)
Definition: sdltk.h:96
bool NewSurface(const std::string &name, int width, int height)
bool LoadMusic(const std::string &name, std::string path="")
Mouse right button.
Definition: sdltk.h:90
std::string & MusicPath()
Access (read/change) the path containing the music files.
Definition: sdltk.h:289
bool InitSdl()
Initialize SDL library.
EventAction
Event action type (see EventData)
Definition: sdltk.h:76
EventAction Action
Event action type (see EventAction)
Definition: sdltk.h:124
Utility timer (e.g. for dynamics calculations)
Definition: sdltk.h:144
void SetMusicVolume(int vol)
Set the music volume (0 to 128)
std::string & SoundsPath()
Access (read/change) the path containing the sound files.
Definition: sdltk.h:286
Event data structure.
Definition: sdltk.h:112
void PlayMusic()
Play the music (start playing, continue, cycle thruogh the playlist)
void SwitchAudio(bool on)
Switch the audio on (true) or off (false)
bool LoadSound(const std::string &name, std::string path="")
void SetEventFunc(EventFunctor *func, bool auto_delete=true)
Definition: sdltk.h:273
Enter key (ASCII code)
Definition: sdltk.h:93
bool ExistsSurface(const std::string &name) const
Check if a named surface exists.
void RenderText(const std::string &text_name, int x, int y, const std::string &dest_surface="")
Escape key (ASCII code)
Definition: sdltk.h:92
unsigned char A
Alpha (opacity) component (0..255)
Definition: sdltk.h:171
void Stop()
Stop the timer.
unsigned int GetTimeMs()
Get the elapsed time in milliseconds.
void Start()
Start the timer.
No action.
Definition: sdltk.h:78
Button/key down.
Definition: sdltk.h:79