SDL Pong Issues

Compiles but doesn't run right....

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 4155 Views - Last Post: 15 March 2009 - 05:15 PM Rate Topic: -----

#1 blackzer0  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 03-March 09

SDL Pong Issues

Posted 13 March 2009 - 05:54 PM

Hi all,

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




Is This A Good Question/Topic? 0
  • +

Replies To: SDL Pong Issues

#2 OrganizedChaos  Icon User is offline

  • D.I.C Head

Reputation: 39
  • View blog
  • Posts: 153
  • Joined: 29-November 08

Re: SDL Pong Issues

Posted 13 March 2009 - 06:04 PM

At the end of your main() before return 0, add the line
system("pause");

That should keep the window from closing after it compiles.
Was This Post Helpful? 0
  • +
  • -

#3 blackzer0  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 03-March 09

Re: SDL Pong Issues

Posted 13 March 2009 - 06:09 PM

Thank you, but that was the very first thing I tried and still no luck...
Was This Post Helpful? 0
  • +
  • -

#4 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: SDL Pong Issues

Posted 13 March 2009 - 06:18 PM

Unless it's crashing on you, the only line that'd make it exit is this:
            //If the user has Xed the window
            if (event.type == SDL_QUIT)
            {
                //Quit the program
                quit == true;
            }


Try (for now) making the while loop be "while (true) {" or commenting out the "quit == true," PS that should be "quit = true" because "==" is a comparison

EDIT: As for non-crashes and it never reaching the while loop, you wouldn't know...
    //Initialize
    if(init() == false)
    {
        return 1;
    }
    
    //Load the files
    if(load_files() == false)
    {
        return 1;
    }


You might try putting a MessageBox() before each return so you'll know if/when it's exiting prematurely.

This post has been edited by Hyper: 13 March 2009 - 06:19 PM

Was This Post Helpful? 0
  • +
  • -

#5 blackzer0  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 03-March 09

Re: SDL Pong Issues

Posted 13 March 2009 - 06:24 PM

Thank you,

Changing the while loop to while(true) stopped the program from closing but now neither my background nor my ball is blitted to the screen.
Was This Post Helpful? 0
  • +
  • -

#6 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: SDL Pong Issues

Posted 13 March 2009 - 06:57 PM

Welcome and I don't know what to say other than: Have fun! :)
I don't use SDL.
Was This Post Helpful? 0
  • +
  • -

#7 blackzer0  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 03-March 09

Re: SDL Pong Issues

Posted 14 March 2009 - 10:46 PM

I fixed it... The problem was in my bool init() function...

I changed this:
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;
}


to this:
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 1;
	}
	
	//Set the window caption
	SDL_WM_SetCaption("Move the Ball", NULL);
	
	//If everything initialized fine
	return true;
}


I'm not entirely clear on why it now works and why it wouldn't work before. If someone could explain it that would be wonderful...
Thanks
Was This Post Helpful? 0
  • +
  • -

#8 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5322
  • View blog
  • Posts: 27,232
  • Joined: 10-May 07

Re: SDL Pong Issues

Posted 14 March 2009 - 10:51 PM

You have this in your 1st set of code :

 if(screen = NULL)



But then you changed it to this :

 if(screen == NULL)



If we take a look at what that comparison is actually doing, logically, it'll make sense.

In the 1st example, if screen can be set to the value of NULL, then...

In the 2nd example, if screen equals the value of NULL, then...

The reason why this is so, is because the single equals sign means push the value to the left.

this = that

This equals that.

So what you were testing is if the value of screen can be set to NULL (which it can), then do the following.

The double equals indicates, compare the value of the left variable to the value of the right variable.

1 == 2

I hope this makes sense :D :^:
Was This Post Helpful? 1
  • +
  • -

#9 blackzer0  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 03-March 09

Re: SDL Pong Issues

Posted 14 March 2009 - 11:18 PM

That does make sense. Thank you... I thought it had something to do with the return value being changed from false to true, but it compiles and runs the same with both returns. Why does the return value not matter in this case??
Was This Post Helpful? 0
  • +
  • -

#10 blackzer0  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 03-March 09

Re: SDL Pong Issues

Posted 15 March 2009 - 09:46 AM

I now realize that that is a very simple question to answer. I was just overthinking the basic reason for an if statement... Since, my program didn't find the screen to == NULL, it didn't consider either return.

Thanks to all that helped. :)
Was This Post Helpful? 0
  • +
  • -

#11 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: SDL Pong Issues

Posted 15 March 2009 - 01:54 PM

Happens alot... Post the working code so somebody else who's curious enough to run it can (like me)? :)
Was This Post Helpful? 0
  • +
  • -

#12 blackzer0  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 03-March 09

Re: SDL Pong Issues

Posted 15 March 2009 - 02:09 PM

unfortunately, the working code stopped working when I tried to add collision detection. I successfully added both paddles and thought that it would be a good idea to manually control my ball and make sure it is colliding with the paddles before I assigned a random path to the ball. I get a compiler error stating this:
464 C:\Dev-Cpp\Projects\SDL\Pong\Pong.cpp `boxL' undeclared (first use this function) 


Here is my entire code:
/*Pong Clone using C++ and SDL libraries*/

//The headers
#include"SDL/SDL.h"
#include"SDL/SDL_image.h"
#include<string>
#include<stdio.h>


//The screen attributes
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 475;
const int SCREEN_BPP = 32;

//The frame rate
const int FRAMES_PER_SECOND = 20;

//The dimensions of the ball
const int BALL_WIDTH = 35;
const int BALL_HEIGHT = 35;

//The dimensions of the paddles
const int PADDLE_WIDTH = 15;
const int PADDLE_HEIGHT = 85;

//The surfaces
SDL_Surface *ball = NULL;
SDL_Surface *background = NULL;
SDL_Surface *screen = NULL;
SDL_Surface *paddleL = NULL;
SDL_Surface *paddleR = NULL;

//The event structure
SDL_Event event;

//The left paddle that will move
class PaddleL
{
	private:
		//The collision box for the left paddle
		SDL_Rect boxL;
		
		//The velocity of left paddle
		int xVel, yVel;
		
	public:	   
		//Initializes the variables for left paddle
		PaddleL();
			 
		//Takes key presses and adjusts left paddle's velocity
		void handle_input();
		
		//Moves left paddle
		void move();
		
		//Shows left paddle on the screen
		void show();
};

//The right paddle that will move
class PaddleR
{
	private:
		//The collision box of the right paddle
		SDL_Rect boxR;
		
		//The velocity of right paddle
		int xVel, yVel;
		
	public:
		
		//Initializes the variables for right paddle
		PaddleR();
			 
		//Takes key presses and adjusts right paddle's velocity
		void handle_input();
		
		//Moves right paddle
		void move();
		
		//Shows right paddle on the screen
		void show();
};

//The ball that will move around the screen
class Ball
{
	private:
		//The collision box of the ball
		SDL_Rect collbox;
		
		//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 = IMG_Load(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)
		{
			//Map the color key
			Uint32 colorkey = SDL_MapRGB( optimizedImage->format, 255, 255, 255);
			
			//Set all pixels of color R 255, G 255, B 255 (white) to be transparent
			SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, colorkey );
		}
	}
	
	//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 check_collision( SDL_Rect A, SDL_Rect B)
{
	//The sides of the rectangles
	int leftA, leftB;
	int rightA, rightB;
	int topA, topB;
	int bottomA, bottomB;
	
	//Calculate the sides of rect A and rect B
	leftA = A.x;
	rightA = A.x + A.w;
	topA = A.y;
	bottomA = A.y + A.h;
	
	leftB = B.x;
	rightB = B.x + B.w;
	topB = B.y;
	bottomB = B.y + B.h;
	
	//If any of the sides from A are outside of B
	if( bottomA <= topB)
	{
		return false;
	}
	
	if( topA >= bottomB )
	{
		return false;
	}

	if( rightA<=leftB)
	{
		return false;
	}
	
	if( leftA>=rightB)
	{
		return false;
	}
	
	//If none of the sides from A are outside B
	return true;
}

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");
	paddleL = load_image("paddleRed.bmp");
	paddleR = load_image("paddleBlue.bmp");
	
	//If there was a problem loading the ball
	if(ball == NULL)
	{
		return false;
	}
	
	//If there was a problem in loading the background
	if(background == NULL)
	{
		return false;
	}
	
	//If there was a problem in loading a paddle
	if(paddleL == NULL || paddleR == NULL)
	{
		return false;
	}
	
	//If everything loaded fine
	return true;
}

void clean_up()
{
	//Free the surfaces
	SDL_FreeSurface(ball);
	SDL_FreeSurface(background);
	SDL_FreeSurface(paddleL);
	SDL_FreeSurface(paddleR);
	
	//Quit SDL
	SDL_Quit();
}

PaddleL::PaddleL()
{
	//Initialize the offsets
	boxL.x = 0;
	boxL.y = SCREEN_HEIGHT/2 - PADDLE_HEIGHT/2;
	
	//Set the collision box's dimensions
	boxL.w = PADDLE_WIDTH;
	boxL.h = PADDLE_HEIGHT;
	
	//Initialize the velocity
	xVel = 0;
	yVel = 0;
}

PaddleR::PaddleR()
{
	//Initialize the offsets
	boxR.x = 625;
	boxR.y = SCREEN_HEIGHT/2 - PADDLE_HEIGHT/2;
	
	//Set the collision box's dimensions
	boxR.w = PADDLE_WIDTH;
	boxR.h = PADDLE_HEIGHT;
	
	//Initialize the velocity
	xVel = 0;
	yVel = 0;
}

void PaddleL::handle_input()
{
	//If a key was pressed 
	if( event.type == SDL_KEYDOWN )
	{
		//Adjust the velocity
		switch( event.key.keysym.sym )
		{
			case SDLK_a: yVel -= PADDLE_HEIGHT/9; break;
			case SDLK_z: yVel += PADDLE_HEIGHT/9; break;
		}
	}
	//If a key was released
	else if( event.type == SDL_KEYUP )
	{
		//Adjust the velocity
		switch( event.key.keysym.sym )
		{
			case SDLK_a: yVel += PADDLE_HEIGHT/9; break;
			case SDLK_z: yVel -= PADDLE_HEIGHT/9; break;
		}
	}
}

void PaddleR::handle_input()
{
	//If a key was pressed 
	if( event.type == SDL_KEYDOWN )
	{
		//Adjust the velocity
		switch( event.key.keysym.sym )
		{
			case SDLK_UP: yVel -= PADDLE_HEIGHT/9; break;
			case SDLK_DOWN: yVel += PADDLE_HEIGHT/9; break;
		}
	}
	//If a key was released
	else if( event.type == SDL_KEYUP )
	{
		//Adjust the velocity
		switch( event.key.keysym.sym )
		{
			case SDLK_UP: yVel += PADDLE_HEIGHT/9; break;
			case SDLK_DOWN: yVel -= PADDLE_HEIGHT/9; break;
		}
	}
}

void PaddleL::move()
{
	//Move the paddle up or down
	boxL.y += yVel;
	
	//If the paddle went to far up or down
	if( (boxL.y<0) || (boxL.y + PADDLE_HEIGHT > SCREEN_HEIGHT) )
	{
		//Move back
		boxL.y -= yVel;
	}
}

void PaddleR::move()
{
	//Move the paddle up or down
	boxR.y += yVel;
	
	//If the paddle went to far up or down
	if( (boxR.y<0) || (boxR.y + PADDLE_HEIGHT > SCREEN_HEIGHT) )
	{
		//Move back
		boxR.y -= yVel;
	}
}

void PaddleL::show()
{
	//Show the Paddle
	apply_surface(boxL.x, boxL.y, paddleL, screen);
}

void PaddleR::show()
{
	//Show the Paddle
	apply_surface(boxR.x, boxR.y, paddleR, screen);
}

Ball::Ball()
{
	//Initialize the offsets
	collbox.x = SCREEN_WIDTH/2 - BALL_WIDTH/2;
	collbox.y = SCREEN_HEIGHT/2 - BALL_HEIGHT/2;
	
	//Set the square's dimensions
	collbox.w = BALL_WIDTH - 5;
	collbox.h = BALL_HEIGHT - 5;
	
	//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/3; break;
			case SDLK_DOWN: yVel += BALL_HEIGHT/3; break;
			case SDLK_RIGHT: xVel += BALL_WIDTH/3; break;
			case SDLK_LEFT: xVel -= BALL_WIDTH/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/3; break;
			case SDLK_DOWN: yVel -= BALL_HEIGHT/3; break;
			case SDLK_RIGHT: xVel -= BALL_WIDTH/3; break;
			case SDLK_LEFT: xVel += BALL_WIDTH/3; break;
		}
	}
}

void Ball::move()
{
	//Move the ball left or right
	collbox.x += xVel;
	
	//If the ball went too far to the left or right
	if( (collbox.x < 0) || (collbox.x + BALL_WIDTH > SCREEN_WIDTH)  || ( check_collision( collbox, boxL ) ) )
	{
		//Move back
		collbox.x -= xVel;
	}
	
	//Move the ball up or down
	collbox.y += yVel;
	
	//If the ball went to far up or down
	if( (collbox.y<0) || (collbox.y + BALL_HEIGHT > SCREEN_HEIGHT) || (check_collision(collbox, boxL ) )
	{
		//Move back
		collbox.y -= yVel;
	}
}

void Ball::show()
{
	//Show the ball
	apply_surface(collbox.x, collbox.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 the timer
	paused = false;
	
	//Get the current clock time
	startTicks = SDL_GetTicks();
}

void Timer::stop()
{
	//Stop the timer
	started = false;
	
	//Unpause the 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 paddles that will be used
	PaddleL paddleL;
	PaddleR paddleR;
	
	//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();
			
			//Handle events for the paddles
			paddleL.handle_input();
			paddleR.handle_input();
	
			//If the user has Xed the window
			if (event.type == SDL_QUIT)
			{
				//Quit the program
				quit = true;
			}
		}
		
		//Move the dot
		myBall.move();
		
		//Move the paddles
		paddleL.move();
		paddleR.move();
				
		//Apply the background to the screen
		apply_surface(0, 0, background, screen);
	
		//Show the ball on the screen
		myBall.show();
		
		//Show the paddles on the screen
		paddleL.show();
		paddleR.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;
}



However, if you comment out the || (check_collision(collbox, boxL ) ) for each if statement in Ball::Move() the program will compile without collision detection...

Like this:
void Ball::move()
{
	//Move the ball left or right
	collbox.x += xVel;
	
	//If the ball went too far to the left or right
	if( (collbox.x < 0) || (collbox.x + BALL_WIDTH > SCREEN_WIDTH)  )//|| ( check_collision( collbox, boxL ) ) )
	{
		//Move back
		collbox.x -= xVel;
	}
	
	//Move the ball up or down
	collbox.y += yVel;
	
	//If the ball went to far up or down
	if( (collbox.y<0) || (collbox.y + BALL_HEIGHT > SCREEN_HEIGHT) )//|| (check_collision(collbox, boxL ) )
	{
		//Move back
		collbox.y -= yVel;
	}
}


Any and all hellp is greatly appreciated, thanks.

P.S. I can attach my image files if need be.

Also, I realize that both the right paddles and ball are controlled using the same keys but this isn't a problem since the ball will not be controlled by the user once the game is completed.

This post has been edited by blackzer0: 15 March 2009 - 02:12 PM

Was This Post Helpful? 0
  • +
  • -

#13 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: SDL Pong Issues

Posted 15 March 2009 - 02:14 PM

class PaddleL
{
	private:
		//The collision box for the left paddle
		SDL_Rect boxL;


The error you're getting is on line #464, what's the code on that line specifically?

	//If the ball went to far up or down
	if( (collbox.y<0) || (collbox.y + BALL_HEIGHT > SCREEN_HEIGHT) )//|| (check_collision(collbox, boxL ) )


I assume.

This post has been edited by Hyper: 15 March 2009 - 02:15 PM

Was This Post Helpful? 0
  • +
  • -

#14 blackzer0  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 03-March 09

Re: SDL Pong Issues

Posted 15 March 2009 - 02:58 PM

Yes, that is line #464, however the function call for check_collision() isn't commented out when the error occurs
Was This Post Helpful? 0
  • +
  • -

#15 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: SDL Pong Issues

Posted 15 March 2009 - 03:09 PM

Unless I'm missing something here or mis understanding how OOP is:
class Ball
{
    private:
        //The collision box of the ball
        SDL_Rect collbox;


and

class PaddleL
{
    private:
        //The collision box for the left paddle
        SDL_Rect boxL;


Are defined in two seperate classes, thus making the members unaware of each other.
Which would explain why it's saying "UNDEFINED" because in "its mind," it IS undefined.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2