| 1 |
#ifndef _CSIMPLEGAME_ |
| 2 |
#define _CSIMPLEGAME_ |
| 3 |
|
| 4 |
#include "CSDLGame.h" |
| 5 |
#include "CSprite.h" |
| 6 |
#include "utils.h" |
| 7 |
|
| 8 |
class CSimpleGame : public CSDLGame |
| 9 |
{ |
| 10 |
public: |
| 11 |
CSimpleGame(int screenX, int screenY, int bitDepth, bool fullscreen) : CSDLGame(screenX, screenY, bitDepth, fullscreen), bgcolor() {} |
| 12 |
~CSimpleGame() |
| 13 |
{ |
| 14 |
#ifndef __GNUC__ |
| 15 |
deleteContainer(sprites); |
| 16 |
#else |
| 17 |
for (list<CParticle*>::iterator i = sprites.begin(); i != sprites.end(); ++i) |
| 18 |
{ |
| 19 |
delete (*i); |
| 20 |
} |
| 21 |
#endif |
| 22 |
} |
| 23 |
|
| 24 |
void addSprite(CParticle* s) { sprites.push_front(s); } |
| 25 |
void removeSprite(CSprite* s) { sprites.remove(s); } |
| 26 |
void setBGColor(CColor c) { bgcolor = c; } |
| 27 |
void x(CSurface* s) { CColor red = CColor(255); for (int x=0; x < 50; x++) for (int y=0; y<50;y++) s->setPixel(x,y,red);} |
| 28 |
bool gameLoop(bool infinite = true) |
| 29 |
{ |
| 30 |
SDL_Event event; |
| 31 |
CTimer timer; |
| 32 |
int i = 0; |
| 33 |
do |
| 34 |
{ |
| 35 |
timer.reset(); |
| 36 |
SDL_PollEvent(&event); |
| 37 |
switch (event.type) |
| 38 |
{ |
| 39 |
case SDL_KEYDOWN: |
| 40 |
switch(event.key.keysym.sym) |
| 41 |
{ |
| 42 |
case SDLK_ESCAPE: |
| 43 |
infinite = false; |
| 44 |
break; |
| 45 |
default: |
| 46 |
break; |
| 47 |
} |
| 48 |
break; |
| 49 |
case SDL_QUIT: |
| 50 |
infinite = false; |
| 51 |
break; |
| 52 |
} |
| 53 |
|
| 54 |
screen->fill(bgcolor); |
| 55 |
|
| 56 |
for(list<CParticle*>::iterator j = sprites.begin(); j != sprites.end(); ++j) |
| 57 |
{ |
| 58 |
(*j)->update(); |
| 59 |
(*j)->draw(*screen); |
| 60 |
} |
| 61 |
screen->doneDrawing(); |
| 62 |
i++; |
| 63 |
if (i) |
| 64 |
{ |
| 65 |
i = 0; |
| 66 |
} |
| 67 |
} |
| 68 |
while(infinite); |
| 69 |
return true; |
| 70 |
} |
| 71 |
|
| 72 |
private: |
| 73 |
list<CParticle*> sprites; |
| 74 |
CColor bgcolor; |
| 75 |
}; |
| 76 |
|
| 77 |
#endif |