|
@@ -0,0 +1,199 @@
|
|
|
+#include "Jeu.h"
|
|
|
+
|
|
|
+Jeu::Jeu()
|
|
|
+:m_screen(0x0), m_sprites("Textures/"), m_cursor(0x0), m_terrain(0x0),
|
|
|
+m_input(0x0), m_tirsGest(0x0), m_armGest(0x0), m_persosGest(0x0),
|
|
|
+m_support(0x0), m_persoFocus(0x0), m_nbJoueurs(1)
|
|
|
+{}
|
|
|
+
|
|
|
+Jeu::~Jeu()
|
|
|
+{
|
|
|
+ SDL_FreeSurface(m_screen);
|
|
|
+
|
|
|
+ if ( m_terrain != 0x0 ) delete m_terrain;
|
|
|
+ if ( m_input != 0x0 ) delete m_input;
|
|
|
+ if ( m_tirsGest != 0x0 ) delete m_tirsGest;
|
|
|
+ if ( m_persosGest != 0x0 ) delete m_persosGest;
|
|
|
+ if ( m_armGest != 0x0 ) delete m_armGest;
|
|
|
+
|
|
|
+ if ( m_support != 0x0 ) {
|
|
|
+ if ( m_nbJoueurs > 1 )
|
|
|
+ for ( Uint16 i(0); i<m_nbJoueurs; i++ )
|
|
|
+ SDL_FreeSurface( m_support[i] );
|
|
|
+ delete[] m_support;
|
|
|
+ }
|
|
|
+ if ( m_persoFocus != 0x0 ) delete[] m_persoFocus;
|
|
|
+
|
|
|
+ SDL_Quit();
|
|
|
+}
|
|
|
+
|
|
|
+bool Jeu::init()
|
|
|
+{
|
|
|
+ /// [1] Démarrage
|
|
|
+ // [1.1] Démarrages SDL
|
|
|
+ if ( SDL_Init( SDL_INIT_VIDEO ) < 0)
|
|
|
+ {
|
|
|
+ std::cout << "Impossible d'initialiser la SDL: " << SDL_GetError() << std::endl;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // [1.2] Para-fenêtre
|
|
|
+ SDL_WM_SetCaption("Hyper Gunner Bêta", 0);
|
|
|
+
|
|
|
+ // [1.3] Nombres aléatoires
|
|
|
+ srand( time(0) );
|
|
|
+
|
|
|
+
|
|
|
+ /// [2] Préparation des composants
|
|
|
+ // [2.1] Préparation des surfaces
|
|
|
+ const SDL_VideoInfo* ecran = SDL_GetVideoInfo(); // Acquisition des dimensions de l'écran
|
|
|
+ m_screen = SDL_SetVideoMode(/*ecran->current_w*/1366, /*ecran->current_h*/768, 32, SDL_HWSURFACE|SDL_DOUBLEBUF/*|SDL_FULLSCREEN*/);
|
|
|
+ if ( !m_screen )
|
|
|
+ {
|
|
|
+ std::cout << "Impossible de créer une fenêtre " <<ecran->current_w<<'*'<<ecran->current_h<< " : " << SDL_GetError() << std::endl;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ else std::cout << "Création d'une fenêtre " <<ecran->current_w<<'*'<<ecran->current_h<< std::endl;
|
|
|
+
|
|
|
+ m_cursor = m_sprites.takeSprite("ViseurUsiane");
|
|
|
+ SDL_SetColorKey(m_cursor, SDL_SRCCOLORKEY, SDL_MapRGB(m_cursor->format, 255, 255, 255));
|
|
|
+
|
|
|
+ // [2.2] Préparation terrain
|
|
|
+ m_terrain = new Terrain(m_sprites.takeSprite("SiteCrash"));
|
|
|
+ m_sprites.destroySprite("SiteCrash"); // Le terrain s'en servait pour remplir ses chunks, il faut la libérer
|
|
|
+
|
|
|
+ // [2.3] Préparation input
|
|
|
+ m_input = new InputAndJoy;
|
|
|
+ m_input->placerPtr(m_screen);
|
|
|
+ m_input->afficherPointeur(false);
|
|
|
+ if ( m_input->getMainCtrl() == MANETTE ) m_nbJoueurs++;
|
|
|
+
|
|
|
+ // [2.3.1] Préparation écran scindé
|
|
|
+ m_persoFocus = new Perso*[m_nbJoueurs];
|
|
|
+ m_support = new SDL_Surface*[m_nbJoueurs];
|
|
|
+
|
|
|
+ if ( m_nbJoueurs > 1 ) { // Multijoueur
|
|
|
+ for ( Uint16 i(0); i < m_nbJoueurs; i++ ) {
|
|
|
+ m_support[i] = SDL_CreateRGBSurface( SDL_HWSURFACE, ecran->current_w/m_nbJoueurs, ecran->current_h, 32, 0, 0, 0, 0);
|
|
|
+ SDL_FillRect( m_support[i], 0, 0xff0000 );
|
|
|
+ }
|
|
|
+ m_scinde.x = ecran->current_w/m_nbJoueurs;
|
|
|
+ m_scinde.y = 0;
|
|
|
+ }
|
|
|
+ else { // Solo
|
|
|
+ m_support[0] = m_screen;
|
|
|
+ }
|
|
|
+
|
|
|
+ // [2.4.1] Préaparation du gestionnaire de tirs
|
|
|
+ m_tirsGest = new Tirs_Gest(m_sprites.takeSprite("BlueTir"), m_sprites.takeSprite("RedTir"));
|
|
|
+
|
|
|
+ // [2.4.2] Préparation du gestionnaire des armes
|
|
|
+ m_armGest = new Armes_Gest( &m_sprites );
|
|
|
+ /* TEST ARMES
|
|
|
+ m_armGest->drop( Vec( 250.0f, -10.0f ), new Shotgun( m_tirsGest ) );
|
|
|
+ m_armGest->drop( Vec( -250.0f, 10.0f ), new Bomber( m_tirsGest ) );*/
|
|
|
+
|
|
|
+ // [2.5] Préparation perso(s) focus
|
|
|
+ HardCtrl* ctrl(0x0);
|
|
|
+ if ( m_input->getMainCtrl() != CLAVIER_SOURIS && m_input->getMainCtrl() != MANETTE ) return false;
|
|
|
+
|
|
|
+ ctrl = new ClavierCtrl(m_input);
|
|
|
+ m_persoFocus[0] = new Perso(m_sprites.takeSprite("Allie"), m_sprites.takeSprite("Mort"), ctrl, m_tirsGest, true, m_armGest);
|
|
|
+
|
|
|
+ if ( m_nbJoueurs == 2 ) {
|
|
|
+ ctrl = new JoyCtrl(m_input);
|
|
|
+ m_persoFocus[1] = new Perso(m_sprites.takeSprite("Allie"), m_sprites.takeSprite("Mort"), ctrl, m_tirsGest, true, m_armGest);
|
|
|
+ }
|
|
|
+
|
|
|
+ // [2.6] Préparation du gestionnaire de persos
|
|
|
+ m_persosGest = new Persos_Gest();
|
|
|
+ for ( Uint16 i(0); i < m_nbJoueurs; i++ )
|
|
|
+ m_persosGest->addAllie(m_persoFocus[i]);
|
|
|
+ /* TEST IA*/
|
|
|
+ IACtrl* IAFriend( 0x0 );
|
|
|
+ Perso* ami ( 0x0 );
|
|
|
+ for (int i(0); i < 3; i++)
|
|
|
+ {
|
|
|
+ IAFriend = new Brute( m_persosGest, true );
|
|
|
+ ami = new Perso(m_sprites.takeSprite("Allie"), m_sprites.takeSprite("Mort"), IAFriend, m_tirsGest, true, m_armGest, rand() % 1000, rand() % 1000);
|
|
|
+ IAFriend->setPersoMoi( ami );
|
|
|
+ m_persosGest->addAllie( ami );
|
|
|
+ }
|
|
|
+
|
|
|
+ /// [3] Renvoi succès
|
|
|
+ return true;
|
|
|
+}
|
|
|
+
|
|
|
+int Jeu::mainLoop()
|
|
|
+{
|
|
|
+ /// [1] Boucle principale
|
|
|
+ Uint32 tDepart(SDL_GetTicks()), tDif, tFps(1000/50); // 50fps
|
|
|
+ bool done = false;
|
|
|
+ Uint32 nbFrame(0), usedTime(0);///DEBUG
|
|
|
+ while (!done)
|
|
|
+ {
|
|
|
+ // [3.1] Gestion évènements
|
|
|
+ m_input->updateEvenements();
|
|
|
+ if (m_input->terminer()) done = true;
|
|
|
+ if (m_input->getTouche(SDLK_ESCAPE)) done = true;
|
|
|
+
|
|
|
+ // [3.2] Calculs
|
|
|
+ m_persosGest->allMove();
|
|
|
+ m_tirsGest->allMove();
|
|
|
+ m_persosGest->nextWave(m_sprites, m_tirsGest, m_armGest);
|
|
|
+
|
|
|
+ // [3.3] Dessin des composants
|
|
|
+ for ( unsigned int i(0); i<m_nbJoueurs; i++ )
|
|
|
+ afficher(m_persoFocus[i], m_support[i]);
|
|
|
+
|
|
|
+ if ( m_nbJoueurs == 2 ) {
|
|
|
+ SDL_BlitSurface( m_support[0], 0, m_screen, 0);
|
|
|
+ SDL_BlitSurface( m_support[1], 0, m_screen, &m_scinde);
|
|
|
+ }
|
|
|
+
|
|
|
+ SDL_Flip(m_screen);
|
|
|
+
|
|
|
+ // [3.4] Gestion du temps
|
|
|
+ tDif = SDL_GetTicks() - tDepart;
|
|
|
+ nbFrame++;///DEBUG
|
|
|
+ usedTime += tDif;///DEBUG
|
|
|
+ if (tDif < tFps) SDL_Delay(tFps - tDif);
|
|
|
+ tDepart = SDL_GetTicks();
|
|
|
+ }
|
|
|
+
|
|
|
+ std::cout << "Environ " << (float)usedTime/nbFrame <<" ms sont utilisées par frame sur " << tFps << std::endl;
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+void Jeu::afficher( const Perso* focus, SDL_Surface* support )
|
|
|
+{
|
|
|
+ // Focus OU Caméra libre
|
|
|
+ if ( focus->estVivant() ) {
|
|
|
+ m_look = focus->getPos();
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ #define CAM_FREE_SPEED 12.0f
|
|
|
+ if (m_input->getTouche(SDLK_RIGHT)) m_look.setX(m_look.getX() + CAM_FREE_SPEED);
|
|
|
+ if (m_input->getTouche(SDLK_LEFT)) m_look.setX(m_look.getX() - CAM_FREE_SPEED);
|
|
|
+ if (m_input->getTouche(SDLK_UP)) m_look.setY(m_look.getY() + CAM_FREE_SPEED);
|
|
|
+ if (m_input->getTouche(SDLK_DOWN)) m_look.setY(m_look.getY() - CAM_FREE_SPEED);
|
|
|
+ }
|
|
|
+
|
|
|
+ // Nettoyage
|
|
|
+ SDL_FillRect( support, 0, 0x141213 );
|
|
|
+
|
|
|
+ // Elements
|
|
|
+ m_terrain->afficher(m_look, support);
|
|
|
+ m_armGest->allDisplay(m_look, support);
|
|
|
+ m_tirsGest->allDisplay(m_look, support);
|
|
|
+ m_persosGest->allDisplay(m_look, support);
|
|
|
+
|
|
|
+ // Curseur
|
|
|
+ Vec viseur = focus->getVisee();
|
|
|
+ SDL_Rect posCurseur;
|
|
|
+ posCurseur.x = viseur.getX() + support->w/2 - 32;
|
|
|
+ posCurseur.y = support->h/2 - viseur.getY() - 32;
|
|
|
+ SDL_BlitSurface(m_cursor, 0, support, &posCurseur);
|
|
|
+}
|
|
|
+
|
|
|
+///FICHIER END
|