[SDL] Flashing window
justin
kokotusu at hotmail.com
Sat Aug 18 20:46:00 PDT 2007
Hello my fellow SDL comrades, well i have a problem with my program im confuse
if it's either my code or my linking. im sure it probably not my linking seen
i check it and it's in a GUI form and main( int argc, char* args[] ) is
present.
This is my code :
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include<iostream>
#include<string>
using namespace std;
#define SCREENWIDTH 160
#define SCREENHEIGHT 240
#ifdef main
#undef main
#endif
int const Size = 16;
class Rocket
{
SDL_Surface *Picture;
SDL_Surface *Optimize;
int dx;
int dy;
public:
Rocket();
~Rocket();
SDL_Surface * LoadPicture(std::string filename);
void move( int x, int y);
int GetX();
int GetY();
};
Rocket::Rocket()
{
Picture = NULL;
Optimize = NULL;
dx = SCREENWIDTH / 2;
dy = SCREENHEIGHT / 2;
}
Rocket::~Rocket()
{
Picture = NULL;
Optimize = NULL;
dx = 0;
dy = 0;
}
SDL_Surface * Rocket::LoadPicture(std::string filename)
{
Picture = IMG_Load(filename.c_str());
if(Picture != NULL)
{
Optimize = SDL_DisplayFormat(Picture);
SDL_FreeSurface(Picture);
}
return Optimize;
}
void Rocket::move( int x, int y)
{
dx += x;
dy += y;
}
int Rocket::GetX()
{
return(dx);
}
int Rocket::GetY()
{
return(dy);
}
Rocket rocket;
SDL_Surface * screen;
SDL_Surface * background;
SDL_Surface * user;
SDL_Event event;
int X; int Y;
int pre_x; int pre_y;
int position =0;
Uint32 StartTime;
Uint32 Timediffer;
int XMovement =0;
int YMovement =0;
void DrawMap(int sprite);
void GameInit();
void GameLoop();
void Collision();
void GameDone();
bool GAMESTARTED = false;
int main( int argc, char* args[] )
{
if(SDL_Init(SDL_INIT_EVERYTHING))
return 1;
GameInit();
SDL_WM_SetCaption( "Game test", NULL );
bool quit = false;
while( quit != true)
{
GameLoop();
while( SDL_PollEvent( &event ) )
{
if(event.type == SDL_KEYDOWN)
{ switch(event.key.keysym.sym)
{
case SDLK_UP:
rocket.move(0,-2);
position = 0;
break;
case SDLK_DOWN:
rocket.move(2,0);
position = 1;
break;
case SDLK_RIGHT:
rocket.move(2,0);
position = 2;
break;
case SDLK_LEFT:
rocket.move(-2,0);
position = 3;
break;
}
}
if(event.type == SDL_QUIT)
quit = true;
}
}
GameDone();
SDL_Quit();
return 0;
}
void DrawMap( int sprite)
{
//takes our previous movement's rect
if( GAMESTARTED == true)
{
SDL_Rect * PreRect; //destination postioning
SDL_Rect * PreSRCRect; //which part of our picture to put into the game
PreRect -> x = pre_x;
PreRect -> y = pre_y;
PreRect -> w = Size;
PreRect -> h = Size;
PreSRCRect -> x = 0;
PreSRCRect -> y = 16 * 4;;
PreSRCRect -> w = Size;
PreSRCRect -> h = Size;
SDL_BlitSurface(user,PreSRCRect,background,PreRect);
}
//plots current position
SDL_Rect * Rect;//destination postioning
SDL_Rect * SRCRect;//which part of our picture to put into the game
Rect -> x = X;
Rect -> y = Y;
Rect -> w = Size;
Rect -> h = Size;
SRCRect -> x = position * Size;
SRCRect -> y = position * Size;
SRCRect -> w = Size;
SRCRect -> h = Size;
}
void GameInit()
{
screen = SDL_SetVideoMode(SCREENWIDTH,SCREENHEIGHT,32,
SDL_SWSURFACE|SDL_DOUBLEBUF);
SDL_PixelFormat *fmt = screen->format;
Uint32 desiredcolor = SDL_MapRGB(fmt, 0,0,0);
SDL_FillRect(background,NULL,desiredcolor);
user = rocket.LoadPicture("ship.bmp");
X = rocket.GetX();
Y = rocket.GetY();
StartTime = SDL_GetTicks();
DrawMap(position);
SDL_Flip( screen );
}
void GameLoop()
{
pre_x = XMovement;
pre_y = YMovement;
Timediffer = StartTime - SDL_GetTicks();
float spf = (float) Timediffer / 1000.0;
XMovement = (int) (2 / (1/spf));
YMovement = (int) ( 2 / (1/spf));
rocket.move(XMovement,YMovement);
Collision();
X += rocket.GetX();
Y += rocket.GetY();
DrawMap(position);
SDL_Flip( screen );
}
void Collision()
{
if( X > SCREENWIDTH)
XMovement =-8 * 2;
position = 3;
if(X < 0 )
XMovement = 8;
position = 2;
if( Y > SCREENHEIGHT)
YMovement = -8 * 2;
position = 3;
if(Y < 0 )
YMovement = 8;
position = 2;
}
void GameDone()
{
SDL_FreeSurface(screen);
SDL_FreeSurface(background);
SDL_FreeSurface(user);
SDL_Surface * screen = NULL;;
SDL_Surface * background = NULL;
SDL_Surface * user=NULL;
int X=0;
int Y=0;
int pre_x=0;
int pre_y=0;
int position=0;
Uint32 StartTime=0;
Uint32 Timediffer=0;
int XMovement=0;
int YMovement=0;
}
More information about the SDL
mailing list