This is my first attempt at an actual game in SDL... I'm working on the classic pong type game. I learned SDL from following the tutorials at http://lazyfoo.net/SDL_tutorials/
For now I just load a background and a ball that I'm going to use as the pong ball. I don't have paddles or any collision detection yet... In fact the ball is controlled by the arrow keys, as one of the paddles will be later. This program compiles just fine using dev-C++ but the window immediately closes after it runs, which didn't happen with the tutorial file. What am I doing wrong???
-Thanks
Here is my code:
/*Pong Clone using C++ and SDL libraries*/
#include"SDL/SDL.h"
#include<string>
//The screen attributes
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;
//The frame rate
const int FRAMES_PER_SECOND = 20;
//The dimensions of the ball
const int BALL_WIDTH = 25;
const int BALL_HEIGHT = 25;
//The surfaces
SDL_Surface *ball = NULL;
SDL_Surface *background = NULL;
SDL_Surface *screen = NULL;
//The event structure
SDL_Event event;
//The ball that will move around the screen
class Ball
{
private:
//The x and y offsets of the ball
int x, y;
//The velocity of the ball
int xVel, yVel;
public:
//Initializes the variables
Ball();
//Takes key presses and adjusts the ball's velocity
void handle_input();
//Moves the ball
void move();
//Shows the ball on the screen
void show();
};
//The timer
class Timer
{
private:
//The clock time when the timer started
int startTicks;
//The ticks stored when the timer was paused
int pausedTicks;
//The timer status
bool paused;
bool started;
public:
//Initializes variables
Timer();
//The various clock actions
void start();
void stop();
void pause();
void unpause();
//Gets the timer's time
int get_ticks();
//Checks the status of the timer
bool is_started();
bool is_paused();
};
SDL_Surface *load_image( std::string filename )
{
//The image that's loaded
SDL_Surface* loadedImage = NULL;
//The optimized surface that will be used
SDL_Surface* optimizedImage = NULL;
//Load the image
loadedImage = SDL_LoadBMP(filename.c_str());
//If the image loaded
if(loadedImage != NULL)
{
//Create an optimized surface
optimizedImage = SDL_DisplayFormat(loadedImage);
//Free the old surface
SDL_FreeSurface(loadedImage);
//If the surface was optimized
if(optimizedImage != NULL)
{
//Color key surface
SDL_SetColorKey(optimizedImage, SDL_SRCCOLORKEY, SDL_MapRGB(optimizedImage->format, 0, 0xFF, 0xFF) );
}
}
//Return the optimized surface
return optimizedImage;
}
void apply_surface(int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL)
{
//Holds offsets
SDL_Rect offset;
//Get offsets
offset.x = x;
offset.y = y;
//Blit
SDL_BlitSurface(source, clip, destination, &offset);
}
bool init()
{
//Initialize all SDL subsystems
if(SDL_Init( SDL_INIT_EVERYTHING ) == -1)
{
return false;
}
//Set up the screen
screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE);
//If there was an error in setting up the screen
if(screen = NULL)
{
return false;
}
//Set the window caption
SDL_WM_SetCaption("Move the Ball", NULL);
//If everything initialized fine
return true;
}
bool load_files()
{
//Load the images
ball = load_image("tennisBall.bmp");
background = load_image("background.bmp");
//if there was a problem loading the ball or background
if(ball == NULL || background == NULL)
{
return false;
}
//If everything loaded fine
return true;
}
void clean_up()
{
//Free the surfaces
SDL_FreeSurface(ball);
SDL_FreeSurface(background);
//Quit SDL
SDL_Quit();
}
Ball::Ball()
{
//Initialize the offsets
x = 320;
y = 240;
//Initialize the velocity
xVel = 0;
yVel = 0;
}
void Ball::handle_input()
{
//If a key was pressed
if( event.type == SDL_KEYDOWN )
{
//Adjust the velocity
switch( event.key.keysym.sym )
{
case SDLK_UP: yVel -= BALL_HEIGHT/2; break;
case SDLK_DOWN: yVel += BALL_HEIGHT/3; break;
}
}
//If a key was released
else if( event.type == SDL_KEYUP )
{
//Adjust the velocity
switch( event.key.keysym.sym )
{
case SDLK_UP: yVel += BALL_HEIGHT/2; break;
case SDLK_DOWN: yVel -= BALL_HEIGHT/2; break;
}
}
}
void Ball::move()
{
//Move the ball up or down
y += yVel;
//If the dot went to farr up or down
if( (y<0) || (y + BALL_HEIGHT > SCREEN_HEIGHT) )
{
//move back
y -= yVel;
}
}
void Ball::show()
{
//Show the ball
apply_surface(x, y, ball, screen);
}
Timer::Timer()
{
//Initialize the variables
startTicks = 0;
pausedTicks = 0;
paused = false;
started = false;
}
void Timer::start()
{
//Start the timer
started = true;
//Unpause teh timer
paused = false;
}
void Timer::pause()
{
//If the timer is running and isn't already paused
if( (started == true) && (paused == false) )
{
//Pause the timer
paused == true;
//Calculate the paused ticks
pausedTicks = SDL_GetTicks() - startTicks;
}
}
void Timer::unpause()
{
//If the timer is paused
if(paused == true)
{
//Unpause the timer
paused = false;
//Reset the starting ticks
startTicks = SDL_GetTicks() - pausedTicks;
//Reset the paused ticks
pausedTicks = 0;
}
}
int Timer::get_ticks()
{
//If the timer is running
if(started == true)
{
//If the timer is paused
if(paused == true)
{
//Return the number of ticks when the timer was paused
return pausedTicks;
}
else
{
//Return the current time minus the start time
return SDL_GetTicks() - startTicks;
}
}
//If the timer isn't running
return 0;
}
bool Timer::is_started()
{
return started;
}
bool Timer::is_paused()
{
return paused;
}
int main(int argc, char* args[])
{
//Quit flag
bool quit = false;
//The ball that will be used
Ball myBall;
//The frame rate regulator
Timer fps;
//Initialize
if(init() == false)
{
return 1;
}
//Load the files
if(load_files() == false)
{
return 1;
}
//While the user hasn't quit
while( quit == false )
{
//Start the frame timer
fps.start();
//While there's events to handle
while(SDL_PollEvent( &event ))
{
//Handle events for the ball
myBall.handle_input();
//If the user has Xed the window
if (event.type == SDL_QUIT)
{
//Quit the program
quit == true;
}
}
//Move the dot
myBall.move();
//Apply the background to the screen
apply_surface(0, 0, background, screen);
//Show the ball on the screen
myBall.show();
//Update the screen
if(SDL_Flip(screen) == -1)
{
return 1;
}
//Cap the frame rate
if(fps.get_ticks() < 1000 / FRAMES_PER_SECOND)
{
SDL_Delay( (1000/ FRAMES_PER_SECOND) - fps.get_ticks() );
}
}
//Clean up
clean_up();
return 0;
}

New Topic/Question
Reply




MultiQuote




|