I thought it would be good to add the source code here so you can take a glance at it !
#include "SDL.h"
#include "SDL_image.h"
#include <string>
#include <stdlib.h>
#include <SDL_TTF.h>
#include <sstream>
#include "SDL_mixer.h"
//Screen attributes
const int SCREEN_WIDTH = 1024;
const int SCREEN_HEIGHT = 768;
const int SCREEN_BPP = 32;
//The dimensions of objects
const int PADDLE_WIDTH = 16;
const int PADDLE_HEIGHT = 100;
const int BALL_WIDTH = 18;
const int BALL_HEIGHT =18;
const int SPEED = 1000;
//The surfaces
SDL_Surface *rightPaddle = NULL;
SDL_Surface *leftPaddle = NULL;
SDL_Surface *ball = NULL;
SDL_Surface *screen = NULL;
SDL_Surface *button = NULL;
SDL_Surface *button2 = NULL;
SDL_Surface *score1 = NULL;
SDL_Surface *score2 = NULL;
SDL_Surface *winningMessage = NULL;
//The event structure
SDL_Event event;
//The font in use
TTF_Font *font = NULL;
SDL_Color textColor = { 255, 250, 250 };
//The collision sound
Mix_Chunk *collisionSound = NULL;
class Ball
{
private:
float xVelocity,yVelocity;
float x,y;
public:
Uint32 Player1Score ;
Uint32 Player2Score ;
Ball();
void move(float xLeftPaddle, float yLeftPaddle,float xRightPaddle, float yRightPaddle, Uint32 deltaticks);
void show();
};
Ball::Ball()
{
x = SCREEN_WIDTH/2;
y = SCREEN_HEIGHT/2;
xVelocity = 300;
yVelocity = 150;
Player1Score = 0;
Player2Score = 0;
}
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 );
}
void Ball::move(float xLeftPaddle, float yLeftPaddle,float xRightPaddle, float yRightPaddle, Uint32 deltaTicks)
{
y += yVelocity * ( deltaTicks / 1000.f );
x += xVelocity * ( deltaTicks / 1000.f );
if( y + BALL_HEIGHT > SCREEN_HEIGHT )//Bottom wall
{
Mix_PlayChannel(-1,collisionSound, 0);
x = x-1;
y = y-1;
yVelocity = -yVelocity;
}
else if( y < 0 )//Top wall
{
Mix_PlayChannel(-1,collisionSound, 0);
x = x+1;
y = y+1;
yVelocity = -yVelocity ;
}
else if(x + BALL_WIDTH >= xRightPaddle && y <= yRightPaddle + PADDLE_HEIGHT && y >=yRightPaddle && x < xRightPaddle + PADDLE_WIDTH )//Right Paddle collision
{
x = x-1;
y = y-1;
xVelocity = -xVelocity;
Mix_PlayChannel(-1,collisionSound, 0);
if( y + BALL_WIDTH < yRightPaddle + (PADDLE_HEIGHT)/2 && yVelocity > 0)
{
yVelocity = -yVelocity;
}
if( y + BALL_WIDTH >= yRightPaddle + (PADDLE_HEIGHT)/2 && yVelocity < 0)
{
yVelocity = -yVelocity;
}
}
else if( x <= xLeftPaddle + PADDLE_WIDTH && y <= yLeftPaddle + PADDLE_HEIGHT && y >= yLeftPaddle && x > xLeftPaddle )//Left Paddle collision
{
x = x + 1;
y = y + 1;
Mix_PlayChannel(-1,collisionSound, 0);
xVelocity = -xVelocity;
if( y < yLeftPaddle + (PADDLE_HEIGHT)/2 && yVelocity > 0)
{
yVelocity = -yVelocity;
}
if( y >= yLeftPaddle + (PADDLE_HEIGHT)/2 && yVelocity < 0)
{
yVelocity = -yVelocity;
}
}
else if(x < 0 )//Reset position if out of bounds
{
Player2Score++;
x = SCREEN_WIDTH/2;
y = SCREEN_HEIGHT/2;
}
else if(x > SCREEN_WIDTH)
{
Player1Score++;
x = SCREEN_WIDTH/2;
y = SCREEN_HEIGHT/2;
}
}
void Ball::show()
{
apply_surface( (int)x, (int)y, ball, screen );
}
class RightPaddle
{
private:
float yVelocity;
public:
float x,y;
RightPaddle();
void handle_right_input(Uint32 deltaTicks);
void move(Uint32 deltaTicks);
void show();
};
RightPaddle::RightPaddle()
{
x = 900;
y = SCREEN_HEIGHT;
yVelocity = 0;
}
void RightPaddle::handle_right_input(Uint32 deltaTicks)
{
if(event.type == SDL_KEYDOWN)
{
switch(event.key.keysym.sym)
{
case SDLK_UP: yVelocity -= SPEED ; break;
case SDLK_DOWN: yVelocity += SPEED ; break;
}
}
else if(event.type == SDL_KEYUP)
{
switch(event.key.keysym.sym)
{
case SDLK_UP: yVelocity += SPEED; break;
case SDLK_DOWN: yVelocity -= SPEED ;break;
}
}
}
void RightPaddle::move(Uint32 deltaTicks)
{
y += yVelocity * ( deltaTicks / 1000.f ) ;
if( y < 0 )
{
y = 0;
}
else if( y + PADDLE_HEIGHT > SCREEN_HEIGHT)
{
y = SCREEN_HEIGHT - PADDLE_HEIGHT;
}
}
void RightPaddle::show()
{
apply_surface( (int)x, (int)y, rightPaddle, screen );
}
class LeftPaddle
{
private:
float yVelocity;
public:
float x,y;
LeftPaddle();
void handle_left_input(Uint32 deltaTicks);
void move(Uint32 deltaTicks);
void show();
};
LeftPaddle::LeftPaddle()
{
x = 100;
y = 0;
yVelocity = 0;
}
void LeftPaddle::handle_left_input(Uint32 deltaTicks)
{
if(event.type == SDL_KEYDOWN)
{
switch(event.key.keysym.sym)
{
case SDLK_w: yVelocity -= SPEED ; break;
case SDLK_s: yVelocity += SPEED ; break;
}
}
else if(event.type == SDL_KEYUP)
{
switch(event.key.keysym.sym)
{
case SDLK_w: yVelocity += SPEED ; break;
case SDLK_s: yVelocity -= SPEED ; break;
}
}
}
void LeftPaddle::move(Uint32 deltaTicks)
{
y += yVelocity * ( deltaTicks / 1000.f );
if( y < 0 )
{
y = 0;
}
else if( y + PADDLE_HEIGHT > SCREEN_HEIGHT)
{
y = SCREEN_HEIGHT - PADDLE_HEIGHT;
}
}
void LeftPaddle::show()
{
apply_surface( (int)x, (int)y, leftPaddle, screen );
}
class Button
{
private:
SDL_Rect box;
public:
Button(int x,int y,int w, int h);
void handle_events(bool *win,Uint32 *score1,Uint32 *score2,bool *menuState,bool *gameState,Uint32 *firstTick);
void handle_events1(bool *gameState,bool *menuState);
void show();
void show1();
};
Button::Button(int x, int y, int w, int h)
{
box.x = x;
box.y = y;
box.w = w;
box.h = h;
}
void Button::handle_events(bool *win,Uint32 *score1,Uint32 *score2,bool *menuState,bool *gameState,Uint32 *firstTick)
{
int x = 0 ;
int y = 0 ;
while( SDL_PollEvent(&event))
{
if(event.type == SDL_MOUSEBUTTONDOWN)
{
if(event.button.button == SDL_BUTTON_LEFT)
{
x = event.button.x;
y = event.button.y;
if( ( x > box.x ) && ( x < box.x + box.w ) && ( y > box.y ) && ( y < box.y + box.h ) )
{
//Reset the values of the match (win and scores) and return to the game state
*win = false;
*score1 = 0;
*score2 = 0;
*menuState = false;
*gameState = true;
*firstTick =0;
}
}
}
}
}
void Button::handle_events1(bool *gameState,bool *menuState)
{
int x = 0 ;
int y = 0 ;
while( SDL_PollEvent(&event))
{
if(event.type == SDL_MOUSEBUTTONDOWN)
{
if(event.button.button == SDL_BUTTON_LEFT)
{
x = event.button.x;
y = event.button.y;
if( ( x > box.x ) && ( x < box.x + box.w ) && ( y > box.y ) && ( y < box.y + box.h ) )
{
//Do after button is pressed
*gameState = true;
*menuState = false;
}
}
}
}
}
void Button::show()
{
apply_surface(box.x, box.y, button, screen);
}
void Button::show1()
{
apply_surface(box.x, box.y, button2, screen);
}
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;
}
//Initialize SDL_ttf
if(TTF_Init() == -1)
{
return false;
}
//Initialize SDL_mixer
if( Mix_OpenAudio( 22050, MIX_DEFAULT_FORMAT, 2, 4096 ) == -1 )
{
return false;
}
//Set the window caption
SDL_WM_SetCaption( "Git Gud Engine", NULL );
//If everything initialized fine
return true;
}
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 )
{
//Color key surface
SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, SDL_MapRGB( optimizedImage->format, 0, 0xFF, 0xFF ) );
}
}
//Return the optimized surface
return optimizedImage;
}
bool load_files()
{
//Load the dot image
leftPaddle = load_image( "Paddle.bmp" );
rightPaddle = load_image( "Paddle.bmp" );
ball = load_image("ball.bmp");
font = TTF_OpenFont("agentorange.ttf",30);
collisionSound = Mix_LoadWAV("bounce.wav");
button = load_image("playAgain.bmp");
button2 = load_image("play.bmp");
if( leftPaddle == NULL )
{
return false;
}
if( rightPaddle == NULL )
{
return false;
}
if( ball == NULL )
{
return false;
}
if(font == NULL)
{
return false;
}
if(collisionSound == NULL)
{
return false;
}
if(button == NULL)
{
return false;
}
if(button2 == NULL)
{
return false;
}
//If everything loaded fine
return true;
}
void clean_up()
{
//Free the surface
SDL_FreeSurface( leftPaddle );
SDL_FreeSurface( rightPaddle );
SDL_FreeSurface( ball );
SDL_FreeSurface(score1);
SDL_FreeSurface(score2);
Mix_FreeChunk(collisionSound);
SDL_FreeSurface(winningMessage);
SDL_FreeSurface(button);
SDL_FreeSurface(button2);
TTF_CloseFont(font);
TTF_Quit();
//Quit SDL
SDL_Quit();
}
class Timer
{
private:
int startTicks;
int pausedTicks;
bool paused;
bool started;
public:
Timer();
void start();
void stop();
void pause();
void unpause();
int get_ticks();
bool is_started();
bool is_paused();
};
Timer::Timer()
{
startTicks = 0;
pausedTicks = 0;
paused = false;
started = false;
}
void Timer::start()
{
started = true;
paused = false;
startTicks = SDL_GetTicks();
}
void Timer::stop()
{
if( (started == true) && (paused == false) )
{
paused = true;
pausedTicks = SDL_GetTicks() - startTicks;
}
}
void Timer::unpause()
{
if(paused == true)
{
paused = false;
startTicks = SDL_GetTicks() - pausedTicks;
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[] )
{
bool quit = false;
bool win = false;
bool menuState = true;
bool gameState = false;
bool isMatchStarted = false;
Uint32 firstTick = 0;
LeftPaddle player1;
RightPaddle player2;
Timer delta;
Ball ball;
Button playButton(SCREEN_WIDTH/2-120, SCREEN_HEIGHT/2 + 200, 280 , 56);
Button playStart(SCREEN_WIDTH/2-65 , SCREEN_HEIGHT/2, 130, 54);
std::stringstream Score1 ;
std::stringstream Score2 ;
if( init() == false)
{
return 1;
}
if(load_files() == false)
{
return 1;
}
delta.start();
while(quit == false )
{
if(menuState == true)
{
if(win == false)//Play button
{
playStart.show1();
playStart.handle_events1(&gameState,&menuState);
}
else if(win == true)//Play again button
{
playButton.show();
playButton.handle_events(&win,&ball.Player1Score,&ball.Player2Score,&menuState,&gameState,&firstTick);
}
if(event.type == SDL_QUIT)
{
quit = true;
}
}
if(gameState == true )
{
firstTick = SDL_GetTicks();
while( SDL_PollEvent(&event))
{
player1.handle_left_input(delta.get_ticks());
player2.handle_right_input(delta.get_ticks());
if(event.type == SDL_QUIT)
{
quit = true;
}
}
player1.move(delta.get_ticks());
player2.move(delta.get_ticks());
if(firstTick >= 3000)
{
ball.move(player1.x,player1.y,player2.x,player2.y, delta.get_ticks());
}
delta.start();
SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0x00, 0x00, 0x00 ) );
if(ball.Player1Score == 0)
{
score1 = TTF_RenderText_Solid( font, "0", textColor );
}
else if( ball.Player1Score == 1)
{
SDL_FreeSurface(score1);
score1 = TTF_RenderText_Solid( font, "1", textColor );
}
else if( ball.Player1Score == 2)
{
SDL_FreeSurface(score1);
score1 = TTF_RenderText_Solid( font, "2", textColor );
}
else if( ball.Player1Score == 3)
{
SDL_FreeSurface(score1);
score1 = TTF_RenderText_Solid( font, "3", textColor );
}
if(ball.Player2Score == 0)
{
score2 = TTF_RenderText_Solid( font, "0", textColor );
}
else if( ball.Player2Score == 1)
{
SDL_FreeSurface(score2);
score2 = TTF_RenderText_Solid( font, "1", textColor );
}
else if( ball.Player2Score == 2)
{
SDL_FreeSurface(score2);
score2 = TTF_RenderText_Solid( font, "2", textColor );
}
else if( ball.Player2Score == 3)
{
SDL_FreeSurface(score2);
score2 = TTF_RenderText_Solid( font, "3", textColor );
}
if(ball.Player1Score == 3)
{
win = true;
winningMessage = TTF_RenderText_Solid( font, "Player 1 wins !", textColor );
}
else if(ball.Player2Score == 3)
{
win = true;
winningMessage = TTF_RenderText_Solid( font, "Player 2 wins !", textColor );
}
apply_surface(SCREEN_WIDTH/2 - 100, 10, score1, screen );
apply_surface(SCREEN_WIDTH/2 + 100, 10, score2, screen );
if( win == false)
{
player1.show();
player2.show();
ball.show();
}
else if(win == true)
{
apply_surface(SCREEN_WIDTH/2-150, SCREEN_HEIGHT/2, winningMessage, screen );
gameState = false;
menuState = true;//Return to the menu if a match is won
}
}
if( SDL_Flip( screen ) == -1 )
{
return 1;
}
}
clean_up();
return 0;
}