Sorry MacApp ^^
E' solo che mi danno fastidio le sbrodolate di codice lunghe mezza pagina, perciò di solito tolgo tutte le parti che posso, o se sono poche righe lo riscrivo addirittura al momento (e da bravo programmatore C#, dimentico regolarmente di usare ; dopo una classe anche se nel programma c'è)
Ecco il codice completo:
main.cpp
codice:
#include <cstdlib>
#include <SDL.h>
#include <SDL/SDL_image.h>
#include "GameTypes.h"
bool RectsCollide(SDL_Rect r1, SDL_Rect r2);
class TestGame : public Game
{
SDL_Event evento;
bool tastiera[323];
SDL_Rect screen;
Sprite* bus;
Sprite* roccia;
public:
TestGame(int fps = 60) : Game(fps) { }
void LoadContent()
{
SDL_Rect screenSize = {0, 0, 1024, 768};
screen = screenSize;
this->RenderTarget = SDL_SetVideoMode( screen.w, screen.h, 32,
SDL_HWSURFACE|SDL_DOUBLEBUF);
bus = new Sprite(IMG_Load("bus.jpg"), 0, 0);
roccia = new Sprite(IMG_Load("roccia.jpg"), 500, 0);
}
void Update()
{
while (SDL_PollEvent(&evento))
{
switch (evento.type)
{
case SDL_QUIT:
this->Exit();
break;
case SDL_KEYDOWN: tastiera[evento.key.keysym.sym] = true;
break;
case SDL_KEYUP: tastiera[evento.key.keysym.sym] = false;
break;
}
}
if (tastiera[SDLK_ESCAPE]) this->Exit();
if (tastiera[SDLK_UP]) bus->Rect.y -= 3;
if (tastiera[SDLK_DOWN]) bus->Rect.y += 3;
if (tastiera[SDLK_LEFT]) bus->Rect.x -= 3;
if (tastiera[SDLK_RIGHT]) bus->Rect.x += 3;
//Keeps the bus on the screen
bus->ClampTo(screen);
//Checks collision between the bus and the rock
if (RectsCollide(bus->Rect, roccia->Rect)) this->Exit();
}
void Draw()
{
SDL_FillRect(
this->RenderTarget,
0,
SDL_MapRGB(this->RenderTarget->format, 255, 255, 255));
bus->Draw(this->RenderTarget);
roccia->Draw(this->RenderTarget);
SDL_Flip(this->RenderTarget);
}
};
Game* game;
int main ( int argc, char** argv )
{
//Starts the SDL system
SDL_Init(SDL_INIT_EVERYTHING);
game = new Game(60);
game->Run();
}
bool RectsCollide(SDL_Rect r1, SDL_Rect r2)
{
if (r1.y + r1.h < r2.y
|| r1.y > r2.y + r2.h
|| r1.x + r1.w < r2.x
|| r1.x > r2.x + r2.w)
return false;
else
return true;
}
GameTypes.h
codice:
#ifndef GAMETYPES_H_INCLUDED
#define GAMETYPES_H_INCLUDED
class Sprite
{
public:
SDL_Surface* Texture;
SDL_Rect Rect;
Sprite(SDL_Surface* tex, int x, int y)
{
Texture = tex;
Rect.x = x;
Rect.y = y;
Rect.h = tex->h;
Rect.w = tex->w;
}
void Draw(SDL_Surface* window)
{
SDL_BlitSurface(Texture, NULL, window, &Rect);
}
void ClampTo(SDL_Rect area)
{
int areaR = area.x + area.w;
int areaB = area.y + area.h;
if (Rect.x < area.x) Rect.x = area.x;
if (Rect.y < area.y) Rect.y = area.y;
if ((Rect.x + Rect.w) > areaR) Rect.x = areaR - Rect.w;
if ((Rect.y + Rect.h) > areaB) Rect.y = areaB - Rect.h;
}
};
class Game
{
int fps;
int frame_len;
int lastUpdateCallTime;
int timeDiff;
bool runEnabled;
public:
SDL_Surface* RenderTarget;
Game(int FPS = 60)
{
runEnabled = false;
fps = FPS;
frame_len = 1000 / fps;
lastUpdateCallTime = 0;
timeDiff = 0;
}
virtual void LoadContent();
virtual void Update();
virtual void Draw();
virtual void UnloadContent();
void Run()
{
LoadContent();
while (runEnabled)
{
lastUpdateCallTime = SDL_GetTicks();
Update();
Draw();
timeDiff = SDL_GetTicks() - lastUpdateCallTime;
if(timeDiff < frame_len)
SDL_Delay(frame_len - timeDiff);
}
}
void Exit(bool unloadContent = true)
{
runEnabled = false;
if (unloadContent) UnloadContent();
}
};
#endif // GAMETYPES_H_INCLUDED
mmm... perchè avere Game come classe virtuale dovrebbe risolvere? Non ho capito :master: