//Main.cpp
//Rocket Pong
#include <string>
#include <sstream>
#include <iostream>
using namespace std;
#include "SDL.h"
#include "SDL_ttf.h"
#include "SDL_Mixer.h"
//Inclusione delle classi create
#include "BluePaddle.h"
#include "RedPaddle.h"
#include "Ball.h"
#include "BlueRocket.h"
#include "RedRocket.h"
//Istanziamo gli oggetti
BluePaddle bluePaddle;
RedPaddle redPaddle;
Ball ball;
BlueRocket blueRocket;
RedRocket redRocket;
//Le Surface
SDL_Surface *screen = NULL;
SDL_Surface *weaponReady = NULL;
SDL_Surface *weaponReloading = NULL;
SDL_Surface *background = NULL;
SDL_Surface *line = NULL;
SDL_Surface *bluePointsSur = NULL;
SDL_Surface *redPointsSur = NULL;
TTF_Font *font = NULL;
SDL_Color blueColor = { 0, 0, 255 };
SDL_Color redColor = { 255, 0, 0 };
Mix_Music *music = NULL;
enum theWinner { NONE, BLUE, RED };
theWinner winner = NONE;
//Prototipi di funzione
bool initializeAll ( void );
void makeBackground ( void );
void makeLine ( void );
void makeWeaponReady ( void );
void makeWeaponReloading ( void );
void makeTexts ( void );
void freeSurfaces ( void );
//Variabili
int xDialog = 640;
int yDialog = 400;
int bits = 32;
int flags = NULL;
int bluePoints = 0;
int redPoints = 0;
bool reloadingB = false;
bool reloadingR = false;
SDL_Rect WCoordinates;
SDL_Rect lineP;
SDL_Rect redPointsPos;
//Definizione delle funzioni
bool initializeAll ( void )
{
//Inizializza SDL
if ( SDL_Init ( SDL_INIT_EVERYTHING) != 0 )
return false;
//Setta lo schermo
screen = SDL_SetVideoMode ( xDialog, yDialog, bits, flags );
//Se c'è un errore nel settare lo schermo
if ( screen == NULL )
return false;
//Inizializza SDL_TTF
if ( TTF_Init() == -1 )
return false;
//Inizializza SDL_Mixer
if ( Mix_OpenAudio ( 44100, MIX_DEFAULT_FORMAT, 2, 2048 ) == -1 )
return false;
//Se tutto va per il verso giusto
return true;
}
void makeBackground ( void )
{
SDL_BlitSurface ( background, NULL, screen, NULL );
}
void makeLine ( void )
{
SDL_BlitSurface ( line, NULL, screen, &lineP );
}
void makeWeaponReady ( void )
{
SDL_BlitSurface ( weaponReady, NULL, screen, &WCoordinates );
}
void makeWeaponReloading ( void )
{
SDL_BlitSurface ( weaponReloading, NULL, screen, &WCoordinates );
}
void makeTexts ( void )
{
SDL_BlitSurface ( bluePointsSur, NULL, screen, NULL );
SDL_BlitSurface ( redPointsSur, NULL, screen, &redPointsPos );
}
void freeSurfaces ( void )
{
//Liberiamo tutte le surface
SDL_FreeSurface ( screen );
SDL_FreeSurface ( line );
SDL_FreeSurface ( background );
SDL_FreeSurface ( weaponReady );
SDL_FreeSurface ( weaponReloading );
SDL_FreeSurface ( bluePointsSur );
SDL_FreeSurface ( redPointsSur );
}
//FUNZIONE MAIN
int main ( int argc, char* argv[] )
{
//Controlla
if ( initializeAll() == false )
{
freeSurfaces();
return -1;
}
//Le coordinate dei punti del paddle rosso
redPointsPos.x = 600;
redPointsPos.y = 0;
stringstream soutB;
soutB.str ( "" );
stringstream soutR;
soutR.str ( "" );
//Setta la grandezza della dialog
screen = SDL_SetVideoMode ( xDialog, yDialog, bits, flags );
//Setta il nome della dialog
SDL_WM_SetCaption ( "Rocket Pong", "rocket.ico" );
//Carica tutto il necessario
background = SDL_LoadBMP ( "Background.bmp" );
weaponReady = SDL_LoadBMP ( "WeaponReady.bmp" );
weaponReloading = SDL_LoadBMP ( "WeaponReloading.bmp" );
line = SDL_LoadBMP ( "Line.bmp" );
font = TTF_OpenFont ( "PointsFont.ttf", 36 );
music = Mix_LoadMUS ( "BigBlue.mid" );
//Gestione del possibile errore
if ( !screen )
{
freeSurfaces();
SDL_Quit();
return -1;
}
//Per non correre il rischio di modificare una Surface quando non è richiesto
//si ricorre al blocco della Surface, quindi sarà però necessario sbloccarla
//ad ogni nuova operazione di disegno (blit)
SDL_LockSurface ( screen );
SDL_Event evento, testEvent;
//Il tempo di attesa di ogni ciclo di gioco
int frameSkip = 10;
Uint8 *keys;
lineP.x = 0;
lineP.y = 50;
WCoordinates.x = 280;
WCoordinates.y = 5;
//Suona la musica per infinite volte
Mix_PlayMusic ( music, -1 );
//Ciclo principale di gioco
while ( winner == NONE )
{
//Tempo di attesa
SDL_Delay ( frameSkip );
//Sblocca screen, così possiamo modificarlo
SDL_UnlockSurface ( screen );
//Modifichiamo screen
makeBackground();
makeLine();
bluePaddle.makeBluePaddle ( screen );
redPaddle.makeRedPaddle ( screen );
redPaddle.moveRedPaddle ( ball.giveYBall() );
ball.makeBall ( screen );
ball.moveBall();
ball.bounce ( bluePoints, redPoints );
ball.bounceOnBlue ( bluePaddle.giveYBluePaddle() );
ball.bounceOnRed ( redPaddle.giveYRedPaddle() );
//RedRocket
if ( reloadingR == false )
{
reloadingR = true;
redRocket.setCoordinates ( redPaddle.giveYRedPaddle() );
}
if ( reloadingR == true )
{
redRocket.makeRedRocket ( screen );
redRocket.moveRedRocket();
redPoints = redRocket.touchWithBluePaddle ( redPoints, bluePaddle.giveYBluePaddle() );
}
if ( redRocket.giveXRedRocket() <= 0 )
reloadingR = false;
if ( SDL_PushEvent ( &testEvent ) == 0 )
{
SDL_PollEvent ( &evento );
keys = SDL_GetKeyState ( NULL );
//Se viene premuto il tasto escape esce dal gioco
if ( keys[SDLK_ESCAPE] == SDL_PRESSED )
{
freeSurfaces();
TTF_Quit();
SDL_Quit();
return 0;
}
//Muove il Paddle blue
bluePaddle.moveBluePaddle ( keys );
//BlueRocket
if ( blueRocket.giveXBlueRocket() > 640 )
reloadingB = false;
if ( ( keys[SDLK_RIGHT] == SDL_PRESSED ) && ( reloadingB == false ) )
{
blueRocket.setCoordinates ( bluePaddle.giveYBluePaddle() );
reloadingB = true;
}
if ( reloadingB == true )
{
blueRocket.makeBlueRocket ( screen );
blueRocket.moveBlueRocket();
bluePoints = blueRocket.touchWithRedPaddle ( bluePoints, redPaddle.giveYRedPaddle() );
makeWeaponReloading();
}
else
makeWeaponReady();
}
soutB.str ( "" );
soutR.str ( "" );
soutB << bluePoints;
bluePointsSur = TTF_RenderText_Solid ( font, soutB.str().c_str(), blueColor );
soutR << redPoints;
redPointsSur = TTF_RenderText_Solid ( font, soutR.str().c_str(), redColor );
makeTexts();
//Riblocchiamo screen
SDL_LockSurface ( screen );
//Aggiorniamo tutta la superfice di screen
SDL_UpdateRect ( screen, 0, 0, 0, 0 );
//Stabilire se qualcuno ha vinto
if ( bluePoints >= 21 )
winner = BLUE;
else if ( redPoints >= 21 )
winner = RED;
}
freeSurfaces();
TTF_Quit();
SDL_Quit();
return 0;
}