-problem : Well i return to the realms of using SDL agian, and recently i wanted to just make a simple program that moved an image left, right, up, and down. Well the problem in my program is that after my program executes it instantly flashes and closes...
{ 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;
}
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;
bloodshev version -4.9.9.2
-problem : Well i return to the realms of using SDL agian, and recently i wanted to just make a simple program that moved an image left, right, up, and down. Well the problem in my program is that after my program executes it instantly flashes and closes...
-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:
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;
}
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 know it's bugs out in GameInit so i wanted to hear adivce on this bug.
also
ifdef main
undef main
endif
was use to make the addition files of stderr and stdout from showing when the exe process..