[SDL] Flashing window

justin kokotusu at hotmail.com
Sun Aug 19 15:55:30 PDT 2007


Brian <brian.ripoff <at> gmail.com> writes:

> 
> Why do you undefine main? SDL #defines main for a reason - unless you
> know what you are doing.
> 
> Also, it makes it easier for us if you give a description of the
> symptoms seen versus expected output. I don't know how "screen
> flashing" relates to your program, you have not described it fully.
> 
> HTH
> 
> On 19/08/07, justin <kokotusu <at> hotmail.com> wrote:
> > 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.
> 

first of all, the use of
#ifdef main
#undef main
#endif

is to take off the additional output of
stdout and stderr....

another thing by mean of flashing window is that the screen fully
creates but closes instantly.. i debug more of it resulting in my code to look 
like this 
#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);

int GameInit();

void GameLoop();

void Collision();

void GameDone();

bool GAMESTARTED = false;


int main( int argc, char* args[] )
{
 
    
 
 GameInit();
 
 
 bool quit = false;
 
 while( quit != true)
 {
  GameLoop();
  
  
  
 while( SDL_PollEvent( &event ) )

{ switch(event.type)
  {case 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;
     }// end of key switch
    }break;
    
  case SDL_QUIT:
  quit = true;
  break;
 }// end of event switch

}  


}
 
  
  
  GameDone();
  
  SDL_Quit();
  
  return 0;

}//end of main      

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 = sprite * Size;
     SRCRect.y = sprite * Size;
     SRCRect.w = Size;
     SRCRect.h = Size;

 SDL_BlitSurface(user,&SRCRect,background,&Rect);

 SDL_BlitSurface(background,NULL,screen,NULL);
 


}


int GameInit()
{
     
 if( SDL_Init(SDL_INIT_EVERYTHING) != 0 ); 
 
 return 1;

 screen = SDL_SetVideoMode(SCREENWIDTH,SCREENHEIGHT,32, 
SDL_HWSURFACE|SDL_DOUBLEBUF);
  if(screen == NULL)
  return 1;
  
 SDL_PixelFormat *fmt = screen->format;
 
 Uint32 desiredcolor = SDL_MapRGB(fmt, 0,0,0);
 
 SDL_FillRect(background,NULL,desiredcolor);
 
 SDL_WM_SetCaption( "Game test", NULL );
 
 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(background);
 
 SDL_FreeSurface(user);
 


 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;
}

i suspect it has  something to do with my gameInit but i would liek advice in 
this thought....




More information about the SDL mailing list