Game::Game() : mCurrentLevel(0), mScreen(0)
{
+ // Initialize SDL
+ if (SDL_Init(SDL_INIT_VIDEO) < 0) {
+ printf ("Couldn't initialize SDL: %s\n", SDL_GetError ());
+ exit (1);
+ }
+ atexit (SDL_Quit);
+
+ // Set 800x600 video mode
+ mScreen = SDL_SetVideoMode(800, 600, 32, SDL_SWSURFACE | SDL_DOUBLEBUF);
+ if (!mScreen) {
+ printf ("Couldn't set 800x600 32b video mode: %s\n", SDL_GetError ());
+ exit (2);
+ }
+
+ // Set the title of our application window handler
+ SDL_WM_SetCaption("Ivan's Game", NULL);
+
+ // We activate keyboard repetition. Therefore, when a key stays pressed down
+ // it will keep moving the image around the screen
+ // To see prcisely what this toggle does, just comment the line and recompile
+ // the code...
+ SDL_EnableKeyRepeat(25, 20);
}
Game::Game(SDL_Surface* screen) : mCurrentLevel(0), mScreen(screen)
{
}
-Level* Game::getCurrentLevel() {
- if (mCurrentLevel < mLevels.size()) {
+Level* Game::getCurrentLevel()
+{
+ if (mCurrentLevel < (ssize_t)mLevels.size()) {
return mLevels[mCurrentLevel];
} else {
return NULL;
}
}
-int Game::getLevelIndex() {
+int Game::getLevelIndex()
+{
return mCurrentLevel;
}
+
+void Game::run()
+{
+ bool done = false;
+
+ // Game Loop
+ while (!done) {
+ // This will let us track events
+ SDL_Event event;
+
+ // We fill 'event' with the first event in the event queue
+ while (SDL_PollEvent(&event)) {
+ switch (event.type) {
+ // If the event is a click on the close button in the top
+ // right corner of the window, we kill the application
+ case SDL_QUIT:
+ done = 1;
+ break;
+
+ // If our event reports a key being pressed down
+ // we process it
+ case SDL_KEYDOWN: {
+ Uint8 *keys = SDL_GetKeyState(NULL);
+ if (keys[SDLK_ESCAPE]) {
+ done = 1;
+ break;
+ }
+ }
+ default: /* fallthrough if the key wasn't escape */
+ getCurrentLevel()->postEvent(event);
+ break;
+ }
+ }
+
+ getCurrentLevel()->drawScene();
+ }
+}
\return index of the current level
*/
int getLevelIndex();
+
+ void run();
private:
int mCurrentLevel; /**< current index */
#include "Level.h"
#include "fns.h"
+Level::Level(SDL_Surface* screen) :
+ mBackground(0),
+ mScreen(screen)
+{
+ mBackground = LoadImage("Backgrounds/bg.bmp");
+}
+
void Level::DrawIMG(SDL_Surface *img, int x, int y)
{
SDL_Rect dest;
dest.x = x;
dest.y = y;
- SDL_BlitSurface(img, NULL, screen, &dest);
+ SDL_BlitSurface(img, NULL, mScreen, &dest);
}
src.y = y2;
src.w = w;
src.h = h;
- SDL_BlitSurface(img, &src, screen, &dest);
+ SDL_BlitSurface(img, &src, mScreen, &dest);
}
void Level::DrawBG()
{
- DrawIMG(back, 0, 0);
+ DrawIMG(mBackground, 0, 0);
}
-void Level::DrawScene(SDL_Surface *screen)
+void Level::drawScene()
{
Uint32 color;
- // Create a black background using the screen pixel format (32 bpp)
- color = SDL_MapRGB (screen->format, 0, 0, 0);
- SDL_FillRect (screen, NULL, color);
+ // Create a black mBackgroundground using the mScreen pixel format (32 bpp)
+ color = SDL_MapRGB(mScreen->format, 0, 0, 0);
+ SDL_FillRect(mScreen, NULL, color);
//Draw BG
DrawBG();
DrawSprites();
- // Flip the working image buffer with the screen buffer
- SDL_Flip (screen);
+ // Flip the working image buffer with the mScreen buffer
+ SDL_Flip (mScreen);
// Add a little pause...
SDL_Delay (1);
void Level::DrawSprites()
{
- for(int i=0; i<SpriteList.size(); ++i)
- {
- SpriteList[i].clearBG();
+ for (size_t i=0; i<mSprites.size(); ++i) {
+ mSprites[i].clearBG();
}
- for(int i=0; i<SpriteList.size(); ++i)
- {
- SpriteList[i].updateBG();
+ for (size_t i=0; i<mSprites.size(); ++i) {
+ mSprites[i].updateBG();
}
- for(int i=0; i<SpriteList.size(); ++i)
- {
- SpriteList[i].draw();
+ for (size_t i=0; i<mSprites.size(); ++i) {
+ mSprites[i].draw();
}
}
void Level::LoadBG(std::string name)
{
- back=LoadImage("Background"+name);
+ mBackground=LoadImage("Background"+name);
+}
+
+void Level::postEvent(SDL_Event event)
+{
+ switch (event.type) {
+ case SDL_KEYDOWN: {
+ Uint8 *keys = SDL_GetKeyState(NULL);
+ if ( keys[SDLK_LEFT] ) { mSprites[0].xadd(-1);}
+ if ( keys[SDLK_RIGHT] ) { mSprites[0].xadd(1);}
+ if ( keys[SDLK_UP] ) { mSprites[0].yadd(-1);}
+ if ( keys[SDLK_DOWN] ) { mSprites[0].yadd(1);}
+ }
+ }
}
class Level
{
public:
- Level() {}
- Level(SDL_Surface *screen):screen(screen) {}
+ Level(SDL_Surface *screen);
~Level();
- void DrawScene(SDL_Surface *screen);
+ void drawScene();
void LoadBG(string name);
+ virtual void postEvent(SDL_Event event);
+
private:
- SDL_Surface *back;
- SDL_Surface *screen;
+ SDL_Surface *mBackground;
+ SDL_Surface *mScreen;
- vector<Sprite> SpriteList;
+ vector<Sprite> mSprites;
void DrawBG();
void DrawIMG();
void DrawIMG(SDL_Surface *img, int x, int y);
#include <sstream>\r
#include <vector>\r
#include "fns.h"\r
+#include "Game.h"\r
\r
using namespace std;\r
\r
\r
bool init()\r
{\r
- // Initialize SDL \r
- if (SDL_Init (SDL_INIT_VIDEO) < 0)\r
- {\r
- printf ("Couldn't initialize SDL: %s\n", SDL_GetError ());\r
- exit (1);\r
- }\r
- \r
- atexit (SDL_Quit);\r
-\r
- // Set 800x600 32-bits video mode\r
- screen = SDL_SetVideoMode (800, 600, 32, SDL_SWSURFACE | SDL_DOUBLEBUF);\r
- if (screen == NULL)\r
- {\r
- printf ("Couldn't set 800x600 32b video mode: %s\n", SDL_GetError ());\r
- exit (2);\r
- }\r
- \r
- // Set the title of our application window handler\r
- SDL_WM_SetCaption ("SDL MultiMedia Application", NULL);\r
- \r
- // We now load our first bitmap image. \r
- back = LoadImage("Backgrounds/bg.bmp");\r
- image = LoadImage("image.bmp");\r
- \r
- // This lets us set the original coordinates of our image on the screen.\r
- // Don't forget that top left corner is the origin (0,0)\r
- //rect.x = 200;\r
- //rect.y = 200;\r
-\r
- // We activate keyboard repetition. Therefore, when a key stays pressed down\r
- // it will keep moving the image around the screen\r
- // To see prcisely what this toggle does, just comment the line and recompile\r
- // the code...\r
- SDL_EnableKeyRepeat(25, 20);\r
- \r
return true;\r
}\r
\r
// until an escape condition is reached\r
int main (int argc, char *argv[])\r
{\r
- // Used to loop\r
- int done = 0;\r
- // The keys variable is used to store the state of the keyboard at \r
- // each frame and see if which keys are pressed down\r
- Uint8 *keys;\r
- \r
- // Initialise SDL and all the rest...\r
- init();\r
+ Game g;\r
\r
+ g.run();\r
+#if 0\r
//load sprites\r
//Animation vikingAnimation = new Animation("viking.anim");\r
vikingAnimation.loadAnimation("viking.anim");\r
\r
//Hide Cursor\r
SDL_ShowCursor(0);\r
+#endif\r
\r
- // Game Loop\r
- while (!done)\r
- {\r
- // This will let us track events\r
- SDL_Event event;\r
-\r
- // We fill 'event' with the first event in the event queue\r
- while (SDL_PollEvent (&event))\r
- {\r
- switch (event.type)\r
- {\r
- // If our event reports a key being pressed down\r
- // we process it\r
- case SDL_KEYDOWN:\r
- keys = SDL_GetKeyState(NULL);\r
- if ( keys[SDLK_LEFT] ) { SpriteList[0].xadd(-1);}\r
- if ( keys[SDLK_RIGHT] ) { SpriteList[0].xadd(1);}\r
- if ( keys[SDLK_UP] ) { SpriteList[0].yadd(-1);}\r
- if ( keys[SDLK_DOWN] ) { SpriteList[0].yadd(1);}\r
- if ( keys[SDLK_ESCAPE] ) { done = 1;}\r
- break;\r
- // If the event is a click on the close button in the top\r
- // right corner of the window, we kill the application\r
- case SDL_QUIT:\r
- done = 1;\r
- break;\r
- default:\r
- break;\r
- }\r
- }\r
- // Render the scene\r
- DrawScene (screen);\r
- }\r
\r
return 0;\r
}\r