Arkanoid Remake  1.0
ArkanoidRemakeSdl.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 ARKANOID_REMAKE_SDL_H
38 #define ARKANOID_REMAKE_SDL_H
39 
40 
41 #include <ArkanoidRemake.h>
42 #include <sdltk.h>
43 #include <map>
44 
47 
52 {
53 protected:
54 
57  std::string mShipSurf;
58  std::string mBallSurf;
59  std::string mPlayAreaSurf;
60  std::string mBkgndSurf;
61  std::string mSnowfallSurf;
63 
65  double mBkgndScroll;
66 
69  sdltk::Timer mTimer;
71 
73  std::string mBlockMatrixName;
74 
77 
79  unsigned mBkgndColor;
80 
83 
84 
86  double GetTime();
87 
89  virtual void HandleEvents();
90 
92  virtual void Frame();
94 
98  void AddLevel();
99 
101  bool SetCurrentLevel(unsigned int n);
102 
104  void GoToNextLevel();
105 
107 
109  bool InitSdl();
110 
112  bool LoadFiles();
113 
115  bool SetBackground(const std::string& file_name);
116 
120  void DrawBall();
121  void DrawShip();
122  void DrawBonus();
123  void DrawText();
124  void DrawBlocks();
125  void DrawBackground();
126  void DrawPlayArea();
128 
132 
133  void HitBlock(Block& block, int x, int y);
134  void HitShip();
135  void GetBonus(const Bonus& bonus);
136  bool DropBonus(const std::string& name, const VecXy& pos);
137  void BonusLost(const Bonus& bonus);
138  void BallLost();
139 
140  void AddScore(int n);
141 
143  void UpdateScoreText();
144 
146 
148  void PlaySound(const std::string& name);
149 
150 
153  {
154  protected:
157  public:
160 
162  virtual bool operator ()(const sdltk::EventData& evt_data);
163  };
164 
166  friend class AppEventFunctor;
167 
171  {
172  bool FullScreen;
173  int WindowWidth;
174  int WindowHeight;
175  bool Audio;
176  int MusicVolume;
177  int SoundsVolume;
178  };
179 
182 
185 
187  bool LoadConfiguration(const std::string& file_path);
188 
190  bool SaveConfiguration(const std::string& file_path);
191 
192 public:
193 
196 
200  bool Init();
201  void Start();
202  void Stop();
203  void CleanUp();
204 
205  void Update();
207 };
208 
210 
211 #endif//ARKANOID_REMAKE_SDL_H
bool SetCurrentLevel(unsigned int n)
Set up the graphics for the current level.
ArkanoidRemake class definition.
double GetTime()
Get the current time in seconds (definition of the base class abstract method)
virtual void Frame()
Render a frame.
Bonus item, dropped when a wall block is hit.
Definition: Bonus.h:52
Vector with two (double precision) coordinate values.
Definition: VecXy.h:59
bool LoadConfiguration(const std::string &file_path)
Load the game configuration.
void BallLost()
The ship missed the ball.
static Configuration smDefaultConfig
Default configuration used in case the configuration fila is not readable.
Definition: ArkanoidRemakeSdl.h:181
std::string mBlockMatrixName
Identifier of the container for blocks images.
Definition: ArkanoidRemakeSdl.h:73
SDL application manager.
Definition: sdltk.h:188
sdltk::SdlApp mSdlApp
SDL application manager, implementing event management, sound and graphics.
Definition: ArkanoidRemakeSdl.h:76
Configuration mConfig
Configuration loaded from the file (see smDefaultConfig)
Definition: ArkanoidRemakeSdl.h:184
void HitBlock(Block &block, int x, int y)
Actions implementation.
void AddScore(int n)
Add the givent points to the score.
void Update()
Update the application (call it in an iteration loop)
double mBkgndScroll
Value controlling the background scroll.
Definition: ArkanoidRemakeSdl.h:65
Game configuration data structure. It is loaded on startup and saved to disk on exit.
Definition: ArkanoidRemakeSdl.h:170
Implementation of ArkanoidRemake using SDL library.
Definition: ArkanoidRemakeSdl.h:51
ArkanoidRemakeSdl()
Constructor: initialize members values.
Functor class for application events.
Definition: ArkanoidRemakeSdl.h:152
void Start()
Initialize the score and go to the first level.
void BonusLost(const Bonus &bonus)
The ship missed a bonus.
Base class for an implementation of a remake of the old game Arkanoid.
Definition: ArkanoidRemake.h:88
Functor class for events handling.
Definition: sdltk.h:136
ArkanoidRemakeSdl * mApp
Internal reference to the application.
Definition: ArkanoidRemakeSdl.h:156
Wall block.
Definition: Block.h:52
SDL Toolkit.
void HitShip()
The ship hit the ball.
bool DropBonus(const std::string &name, const VecXy &pos)
A bonus item was dropped.
void GoToNextLevel()
Set up the graphics for the next level.
void AddLevel()
Add a (the only one foer now) new level.
AppEventFunctor(ArkanoidRemakeSdl *app)
Constructor: it requires a reference to the owner application.
bool SetBackground(const std::string &file_name)
Use the given image file to fill the background.
void PlaySound(const std::string &name)
Play the named sound.
void GetBonus(const Bonus &bonus)
The ship hit a bonus item.
void CleanUp()
Clean up the application and free memory.
virtual void HandleEvents()
Handle input events.
virtual bool operator()(const sdltk::EventData &evt_data)
Functor operator.
Utility timer (e.g. for dynamics calculations)
Definition: sdltk.h:144
unsigned mBkgndColor
Color used for background if an image is not available.
Definition: ArkanoidRemakeSdl.h:79
bool Init()
App interface implementation.
Event data structure.
Definition: sdltk.h:112
bool SaveConfiguration(const std::string &file_path)
Save the game configuration.
bool LoadFiles()
Load images, sounds, etc. with SDL toolkit.
bool InitSdl()
Initialize SDL with SDL toolkit.
void UpdateScoreText()
Update the GUI text.
void Stop()
Stop the application.