Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Rocket Pong - Main.cpp

Main.cpp

Caricato da: RiccardoG97
Scarica il programma completo

  1. //Main.cpp
  2. //Rocket Pong
  3.  
  4. #include <string>
  5. #include <sstream>
  6. #include <iostream>
  7. using namespace std;
  8.  
  9. #include "SDL.h"
  10. #include "SDL_ttf.h"
  11. #include "SDL_Mixer.h"
  12.  
  13. //Inclusione delle classi create
  14. #include "BluePaddle.h"
  15. #include "RedPaddle.h"
  16. #include "Ball.h"
  17. #include "BlueRocket.h"
  18. #include "RedRocket.h"
  19.  
  20. //Istanziamo gli oggetti
  21. BluePaddle bluePaddle;
  22. RedPaddle redPaddle;
  23. Ball ball;
  24. BlueRocket blueRocket;
  25. RedRocket redRocket;
  26.  
  27. //Le Surface
  28. SDL_Surface *screen = NULL;
  29. SDL_Surface *weaponReady = NULL;
  30. SDL_Surface *weaponReloading = NULL;
  31. SDL_Surface *background = NULL;
  32. SDL_Surface *line = NULL;
  33. SDL_Surface *bluePointsSur = NULL;
  34. SDL_Surface *redPointsSur = NULL;
  35.  
  36. TTF_Font *font = NULL;
  37. SDL_Color blueColor = { 0, 0, 255 };
  38. SDL_Color redColor = { 255, 0, 0 };
  39.  
  40. Mix_Music *music = NULL;
  41.  
  42. enum theWinner { NONE, BLUE, RED };
  43. theWinner winner = NONE;
  44.  
  45. //Prototipi di funzione
  46. bool initializeAll ( void );
  47. void makeBackground ( void );
  48. void makeLine ( void );
  49. void makeWeaponReady ( void );
  50. void makeWeaponReloading ( void );
  51. void makeTexts ( void );
  52. void freeSurfaces ( void );
  53.  
  54. //Variabili
  55. int xDialog = 640;
  56. int yDialog = 400;
  57. int bits = 32;
  58. int flags = NULL;
  59. int bluePoints = 0;
  60. int redPoints = 0;
  61. bool reloadingB = false;
  62. bool reloadingR = false;
  63.  
  64. SDL_Rect WCoordinates;
  65. SDL_Rect lineP;
  66. SDL_Rect redPointsPos;
  67.  
  68. //Definizione delle funzioni
  69. bool initializeAll ( void )
  70. {
  71.         //Inizializza SDL
  72.         if ( SDL_Init ( SDL_INIT_EVERYTHING) != 0 )
  73.                 return false;
  74.  
  75.         //Setta lo schermo
  76.         screen = SDL_SetVideoMode ( xDialog, yDialog, bits, flags );
  77.  
  78.         //Se c'è un errore nel settare lo schermo
  79.         if ( screen == NULL )
  80.                 return false;
  81.  
  82.         //Inizializza SDL_TTF
  83.         if ( TTF_Init() == -1 )
  84.                 return false;
  85.  
  86.         //Inizializza SDL_Mixer
  87.         if ( Mix_OpenAudio ( 44100, MIX_DEFAULT_FORMAT, 2, 2048 ) == -1 )
  88.                 return false;
  89.  
  90.         //Se tutto va per il verso giusto
  91.         return true;
  92. }
  93.  
  94. void makeBackground ( void )
  95. {
  96.         SDL_BlitSurface ( background, NULL, screen, NULL );
  97. }
  98.  
  99. void makeLine ( void )
  100. {
  101.         SDL_BlitSurface ( line, NULL, screen, &lineP );
  102. }
  103.  
  104. void makeWeaponReady ( void )
  105. {
  106.         SDL_BlitSurface ( weaponReady, NULL, screen, &WCoordinates );
  107. }
  108.  
  109. void makeWeaponReloading ( void )
  110. {
  111.         SDL_BlitSurface ( weaponReloading, NULL, screen, &WCoordinates );
  112. }
  113.  
  114. void makeTexts ( void )
  115. {
  116.         SDL_BlitSurface ( bluePointsSur, NULL, screen, NULL );
  117.         SDL_BlitSurface ( redPointsSur, NULL, screen, &redPointsPos );
  118. }
  119.  
  120. void freeSurfaces ( void )
  121. {
  122.         //Liberiamo tutte le surface
  123.         SDL_FreeSurface ( screen );
  124.         SDL_FreeSurface ( line );
  125.         SDL_FreeSurface ( background );
  126.         SDL_FreeSurface ( weaponReady );
  127.         SDL_FreeSurface ( weaponReloading );
  128.         SDL_FreeSurface ( bluePointsSur );
  129.         SDL_FreeSurface ( redPointsSur );
  130. }
  131.  
  132. //FUNZIONE MAIN
  133. int main ( int argc, char* argv[] )
  134. {
  135.         //Controlla
  136.         if ( initializeAll() == false )
  137.         {
  138.                 freeSurfaces();
  139.                 return -1;
  140.         }
  141.  
  142.         //Le coordinate dei punti del paddle rosso
  143.         redPointsPos.x = 600;
  144.         redPointsPos.y = 0;
  145.  
  146.         stringstream soutB;
  147.         soutB.str ( "" );
  148.  
  149.         stringstream soutR;
  150.         soutR.str ( "" );
  151.  
  152.         //Setta la grandezza della dialog
  153.         screen = SDL_SetVideoMode ( xDialog, yDialog, bits, flags );
  154.  
  155.         //Setta il nome della dialog
  156.         SDL_WM_SetCaption ( "Rocket Pong", "rocket.ico" );
  157.  
  158.         //Carica tutto il necessario
  159.         background = SDL_LoadBMP ( "Background.bmp" );
  160.         weaponReady = SDL_LoadBMP ( "WeaponReady.bmp" );
  161.         weaponReloading = SDL_LoadBMP ( "WeaponReloading.bmp" );
  162.         line = SDL_LoadBMP ( "Line.bmp" );
  163.         font = TTF_OpenFont ( "PointsFont.ttf", 36 );
  164.         music = Mix_LoadMUS ( "BigBlue.mid" );
  165.  
  166.         //Gestione del possibile errore
  167.         if ( !screen )
  168.         {
  169.                 freeSurfaces();
  170.                 SDL_Quit();
  171.                 return -1;
  172.         }
  173.  
  174.         //Per non correre il rischio di modificare una Surface quando non è richiesto
  175.         //si ricorre al blocco della Surface, quindi sarà però necessario sbloccarla
  176.         //ad ogni nuova operazione di disegno (blit)
  177.         SDL_LockSurface ( screen );
  178.  
  179.         SDL_Event evento, testEvent;
  180.  
  181.         //Il tempo di attesa di ogni ciclo di gioco
  182.         int frameSkip = 10;
  183.  
  184.         Uint8 *keys;
  185.  
  186.         lineP.x = 0;
  187.         lineP.y = 50;
  188.         WCoordinates.x = 280;
  189.         WCoordinates.y = 5;
  190.  
  191.         //Suona la musica per infinite volte
  192.         Mix_PlayMusic ( music, -1 );
  193.  
  194.         //Ciclo principale di gioco
  195.         while ( winner == NONE )
  196.         {
  197.                 //Tempo di attesa
  198.                 SDL_Delay ( frameSkip );
  199.  
  200.                 //Sblocca screen, così possiamo modificarlo
  201.                 SDL_UnlockSurface ( screen );
  202.  
  203.                 //Modifichiamo screen
  204.                 makeBackground();
  205.                 makeLine();
  206.                 bluePaddle.makeBluePaddle ( screen );
  207.                 redPaddle.makeRedPaddle ( screen );
  208.                 redPaddle.moveRedPaddle ( ball.giveYBall() );
  209.                 ball.makeBall ( screen );
  210.                 ball.moveBall();
  211.                 ball.bounce ( bluePoints, redPoints );
  212.                 ball.bounceOnBlue ( bluePaddle.giveYBluePaddle() );
  213.                 ball.bounceOnRed ( redPaddle.giveYRedPaddle() );
  214.  
  215.                 //RedRocket
  216.                 if ( reloadingR == false )
  217.                 {
  218.                         reloadingR = true;
  219.                         redRocket.setCoordinates ( redPaddle.giveYRedPaddle() );
  220.                 }
  221.  
  222.                 if ( reloadingR == true )
  223.                 {
  224.                         redRocket.makeRedRocket ( screen );
  225.                         redRocket.moveRedRocket();
  226.                         redPoints = redRocket.touchWithBluePaddle ( redPoints, bluePaddle.giveYBluePaddle() );
  227.                 }
  228.  
  229.                 if ( redRocket.giveXRedRocket() <= 0 )
  230.                         reloadingR = false;
  231.  
  232.                 if ( SDL_PushEvent ( &testEvent ) == 0 )
  233.                 {
  234.                         SDL_PollEvent ( &evento );
  235.  
  236.                         keys = SDL_GetKeyState ( NULL );
  237.                        
  238.                         //Se viene premuto il tasto escape esce dal gioco
  239.                         if ( keys[SDLK_ESCAPE] == SDL_PRESSED )
  240.                         {
  241.                                 freeSurfaces();
  242.                                 TTF_Quit();
  243.                                 SDL_Quit();
  244.                                 return 0;
  245.                         }
  246.  
  247.                         //Muove il Paddle blue
  248.                         bluePaddle.moveBluePaddle ( keys );
  249.  
  250.                         //BlueRocket
  251.                         if ( blueRocket.giveXBlueRocket() > 640 )
  252.                                 reloadingB = false;
  253.  
  254.                         if ( ( keys[SDLK_RIGHT] == SDL_PRESSED ) && ( reloadingB == false ) )
  255.                         {
  256.                                 blueRocket.setCoordinates ( bluePaddle.giveYBluePaddle() );
  257.                                 reloadingB = true;
  258.                         }
  259.  
  260.                         if ( reloadingB == true )
  261.                         {
  262.                                 blueRocket.makeBlueRocket ( screen );
  263.                                 blueRocket.moveBlueRocket();
  264.                                 bluePoints = blueRocket.touchWithRedPaddle ( bluePoints, redPaddle.giveYRedPaddle() );
  265.                                 makeWeaponReloading();
  266.                         }
  267.                         else
  268.                                 makeWeaponReady();
  269.  
  270.                 }
  271.  
  272.                 soutB.str ( "" );
  273.                 soutR.str ( "" );
  274.  
  275.                 soutB << bluePoints;
  276.                 bluePointsSur = TTF_RenderText_Solid ( font, soutB.str().c_str(), blueColor );
  277.  
  278.                 soutR << redPoints;
  279.                 redPointsSur = TTF_RenderText_Solid ( font, soutR.str().c_str(), redColor );
  280.  
  281.                 makeTexts();
  282.  
  283.                 //Riblocchiamo screen
  284.                 SDL_LockSurface ( screen );
  285.  
  286.                 //Aggiorniamo tutta la superfice di screen
  287.                 SDL_UpdateRect ( screen, 0, 0, 0, 0 );
  288.  
  289.                 //Stabilire se qualcuno ha vinto
  290.                 if ( bluePoints >= 21 )
  291.                         winner = BLUE;
  292.                 else if ( redPoints >= 21 )
  293.                         winner = RED;
  294.         }
  295.        
  296.         freeSurfaces();
  297.         TTF_Quit();
  298.         SDL_Quit();
  299.        
  300.         return 0;
  301. }