Original Post
I am having a problem with SDL. Everytime I try to run a program using Visual C++ with SDL it stops responding after a few seconds. If anyone could help this is the code: [Edited by - thejahooli on March 17, 2009 1:17:46 PM]
#include "SDL.h"
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;
bool gamequit = false;
//set surfaces
SDL_Surface *screen;
SDL_Surface *playerimage;
//player class
class Player
{
public:
int w;
int h;
int x;
int y;
int speed;
};
//surface applying
void apply_surface(int x, int y, SDL_Surface* source, SDL_Surface* destination)
{
//creates rect for surface
SDL_Rect offset;
//sets the offset x and y to the given x and y
offset.x = x;
offset.y = y;
//blits the surface
SDL_BlitSurface(source, NULL, destination, &offset);
}
//game end function
void gameend()
{
SDL_FreeSurface(screen);
SDL_FreeSurface(playerimage);
SDL_Quit();
}
int main(int argc, char* argv[])
{
//initialize SDL
SDL_Init(SDL_INIT_VIDEO);
//images and screen
screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP,SDL_SWSURFACE);
playerimage = SDL_LoadBMP("player.bmp");
//game event
SDL_Event movement;
//sets player variables
Player player;
player.x = SCREEN_WIDTH / 2 - 16;
player.y = SCREEN_HEIGHT / 2 - 16;
player.speed = 8;
//main game loop
for(gamequit = false; gamequit != true;)
{
//draws the player to the screen
apply_surface(player.x,player.y,playerimage,screen);
//Polls for an event
SDL_PollEvent(&movement);
//if the event was a key being presses
if(movement.key.state == SDL_PRESSED)
{
//checks which key is being pressed
// and sets the players x and y to the necessery adjustments
if(movement.key.keysym.sym == SDLK_DOWN)
{
player.y = player.y + player.speed;
}
if(movement.key.keysym.sym == SDLK_UP)
{
player.y = player.y - player.speed;
}
if(movement.key.keysym.sym == SDLK_LEFT)
{
player.x = player.x - player.speed;
}
if(movement.key.keysym.sym == SDLK_RIGHT)
{
player.x = player.x + player.speed;
}
}
if(movement.type == SDL_QUIT)
{
gamequit = true;
gameend();
}
//check collision
if(player.x < 0)
{
player.x = 0;
}
if(player.x > SCREEN_WIDTH - player.w)
{
player.x = SCREEN_WIDTH - player.w;
}
if(player.y < 0)
{
player.y = 0;
}
if(player.y > SCREEN_HEIGHT - player.h)
{
player.y = SCREEN_HEIGHT - player.h;
}
SDL_Flip(screen);
}
SDL_Delay(10000);
return 0;
}