Browse Source

Import project

DricomDragon 4 years ago
commit
f33941068b
7 changed files with 557 additions and 0 deletions
  1. 176 0
      Input.cpp
  2. 61 0
      Input.h
  3. 21 0
      intToStr.cpp
  4. 8 0
      intToStr.h
  5. 259 0
      main.cpp
  6. 19 0
      spawn.cpp
  7. 13 0
      spawn.h

+ 176 - 0
Input.cpp

@@ -0,0 +1,176 @@
+#include "Input.h"
+
+
+// Constructeur et Destructeur
+Input::Input()
+: m_x(0), m_y(0), m_xRel(0), m_yRel(0),
+m_terminer(false), m_relativeMouse(false), m_windowHalfHeight(0), m_windowHalfWidth(0)
+{
+    // Initialisation du tableau m_touches[]
+    for(int i(0); i < SDLK_LAST; i++)
+        m_touches[i] = false;
+    std::cout << "Il y a "<<SDLK_LAST<<" touches sur le clavier."<<std::endl;
+
+    // Initialisation du tableau m_boutonsSouris[]
+    for(int i(0); i < 8; i++)
+        m_boutonsSouris[i] = false;
+}
+
+
+Input::~Input()
+{}
+
+
+// Méthodes
+void Input::updateEvenements()
+{
+    // Pour éviter des mouvements fictifs de la souris, on réinitialise les coordonnées relatives
+    m_xRel = 0;
+    m_yRel = 0;
+
+    // Boucle d'évènements
+    while(SDL_PollEvent(&m_evenements))
+    {
+        // Switch sur le type d'évènement
+        switch(m_evenements.type)
+        {
+            // Cas d'une touche enfoncée
+            case SDL_KEYDOWN:
+                m_touches[m_evenements.key.keysym.sym] = true;
+            break;
+
+            // Cas d'une touche relâchée
+            case SDL_KEYUP:
+                m_touches[m_evenements.key.keysym.sym] = false;
+            break;
+
+            // Cas de pression sur un bouton de la souris
+            case SDL_MOUSEBUTTONDOWN:
+                m_boutonsSouris[m_evenements.button.button] = true;
+            break;
+
+            // Cas du relâchement d'un bouton de la souris
+            case SDL_MOUSEBUTTONUP:
+                m_boutonsSouris[m_evenements.button.button] = false;
+            break;
+
+            // Cas d'un mouvement de souris
+            case SDL_MOUSEMOTION:
+                if (m_relativeMouse)
+                {
+                    m_xRel = m_evenements.motion.x-m_windowHalfWidth;
+                    m_yRel = m_evenements.motion.y-m_windowHalfHeight;
+                }
+                else
+                {
+                    m_x = m_evenements.motion.x;
+                    m_y = m_evenements.motion.y;
+                    m_xRel = m_evenements.motion.xrel;
+                    m_yRel = m_evenements.motion.yrel;
+                }
+
+            break;
+
+            // Cas de la fermeture de la fenêtre
+            case SDL_QUIT:
+                m_terminer = true;
+            break;
+
+            // Les autres ne nous interessent pas : on évite de faire râler g++
+            default:
+            break;
+        }
+    }
+
+    // Pour éviter que la souris se barre en mode relative, on la "warp"
+    if (m_relativeMouse)
+         SDL_WarpMouse(m_windowHalfWidth, m_windowHalfHeight);
+}
+
+
+bool Input::terminer() const
+{
+    return m_terminer;
+}
+
+
+void Input::afficherPointeur(bool reponse) const
+{
+    if(reponse)
+        SDL_ShowCursor(SDL_ENABLE);
+
+    else
+        SDL_ShowCursor(SDL_DISABLE);
+}
+
+
+void Input::capturerPointeur(bool reponse)
+{
+    m_relativeMouse = reponse;
+}
+
+
+
+// Getters
+
+bool Input::getTouche(const int touche) const
+{
+    return m_touches[touche];
+}
+
+
+bool Input::getBoutonSouris(const Uint8 bouton) const
+{
+    return m_boutonsSouris[bouton];
+}
+
+
+bool Input::mouvementSouris() const
+{
+    if(m_xRel == 0 && m_yRel == 0)
+        return false;
+
+    else
+        return true;
+}
+
+
+// Getters concernant la position du curseur
+
+int Input::getX() const
+{
+    return m_x;
+}
+
+int Input::getY() const
+{
+    return m_y;
+}
+
+int Input::getXRel() const
+{
+    return m_xRel;
+}
+
+int Input::getYRel() const
+{
+    return m_yRel;
+}
+
+void Input::placerPtr(SDL_Surface* activWindow)
+{
+    // Détermination de l'endroit de capture du pointeur
+    m_windowHalfWidth = activWindow->w / 2;
+    m_windowHalfHeight = activWindow->h / 2;
+}
+
+int Input::getWinHalfH()
+{
+    return m_windowHalfHeight;
+}
+
+int Input::getWinHalfW()
+{
+    return m_windowHalfWidth;
+}
+

+ 61 - 0
Input.h

@@ -0,0 +1,61 @@
+#ifndef DEF_INPUT
+#define DEF_INPUT
+
+///Jovian
+///Adaptation pour InputAndJoy
+
+// Include
+#include <iostream>
+#include <SDL.h>
+
+
+// Classe
+class Input
+{
+    public:
+
+    Input();
+    virtual ~Input();
+
+    virtual void updateEvenements();
+
+    bool terminer() const;
+    void afficherPointeur(bool reponse) const;
+    void capturerPointeur(bool reponse);
+
+    bool getTouche(const int touche) const;
+    bool getBoutonSouris(const Uint8 bouton) const;
+    bool mouvementSouris() const;
+
+    int getX() const;
+    int getY() const;
+
+    int getXRel() const;
+    int getYRel() const;
+
+    void placerPtr(SDL_Surface* activWindow);
+
+    int getWinHalfH();
+    int getWinHalfW();
+
+
+    protected:
+
+    SDL_Event m_evenements;
+    bool m_touches[SDLK_LAST];
+    bool m_boutonsSouris[8];
+
+    int m_x;
+    int m_y;
+    int m_xRel;
+    int m_yRel;
+
+    bool m_terminer;
+    bool m_relativeMouse;
+
+    int m_windowHalfHeight;
+    int m_windowHalfWidth;
+};
+
+#endif
+

+ 21 - 0
intToStr.cpp

@@ -0,0 +1,21 @@
+#include "intToStr.h"
+
+std::string intToStr( int numero )
+{
+    //[1] Préparer
+    std::string resultat;
+    char caractere(0);
+
+    //[2] S'auto appeler
+    caractere = numero%10;
+    caractere += 48;
+    numero /= 10;
+
+    if ( numero != 0 )
+        resultat = intToStr( numero );
+
+    resultat += caractere;
+
+    //[3] Renvoi
+    return resultat;
+}

+ 8 - 0
intToStr.h

@@ -0,0 +1,8 @@
+#ifndef INTTOSTR_H_INCLUDED
+#define INTTOSTR_H_INCLUDED
+
+#include <string>
+
+std::string intToStr( int numero );
+
+#endif // INTTOSTR_H_INCLUDED

+ 259 - 0
main.cpp

@@ -0,0 +1,259 @@
+#include "spawn.h"
+#include "intToStr.h"
+
+#define NB_GAMERS 2
+
+#define NB_GROW 6
+#define NB_BASE 10
+#define NEXT_ROUND spawn( &obs, NB_BASE+lvl*NB_GROW, w, h )
+#define GAMERS_POS (1+t)*(h/(1+NB_GAMERS))
+
+int main ( int argc, char** argv )
+{
+    /// initialize SDL video
+    if ( SDL_Init( SDL_INIT_VIDEO ) < 0 )
+    {
+        printf( "Unable to init SDL: %s\n", SDL_GetError() );
+        return 1;
+    }
+    SDL_ShowCursor( SDL_DISABLE );
+
+    /// make sure SDL cleans up before exit
+    atexit(SDL_Quit);
+
+    /// init random list
+    srand( time(0) );
+
+    /// create a new window
+    Uint16 w, h;
+    SDL_Surface *buffer(0x0), *screen(0x0);
+
+    { // spare useless variables
+        const SDL_VideoInfo* fenetreInfo = SDL_GetVideoInfo();
+        w = fenetreInfo->current_w;
+        h = fenetreInfo->current_h;
+    }
+    screen = SDL_SetVideoMode(w, h, 32, SDL_HWSURFACE|SDL_DOUBLEBUF|SDL_FULLSCREEN);
+    std::cout << "Set " << w << 'x' << h << " video." << std::endl;
+    if ( !screen )
+    {
+        std::cout << "Unable to set video " << SDL_GetError() << std::endl;
+        return 1;
+    }
+    else
+    {
+        buffer = SDL_CreateRGBSurface( SDL_HWSURFACE, w, h, 32, 0, 0, 0, 0 );
+    }
+
+    /// variables
+    const Uint32 delay(18);
+    Uint32 tps( SDL_GetTicks() ), tps_old(0);
+    Uint32 fdis_time(0), fdis_count(0), fdis_rslt(0);
+    bool alive[NB_GAMERS];
+    bool running;
+    int lvl( 1 );
+
+    /// graph
+    int i(0), x[NB_GAMERS], y[NB_GAMERS];
+    Uint8 r(0), g(255), b(255);
+    bool up[NB_GAMERS], down[NB_GAMERS];
+
+    /// init
+    for ( int t(0); t < NB_GAMERS; t++ )
+    {
+        alive[t] = true;
+        x[t] = -1;
+        y[t] = GAMERS_POS;
+        up[t] = down[t] = false;
+    }
+
+    /// obstacles
+    std::vector<SDL_Rect> obs(0);
+    NEXT_ROUND;
+
+    /// program main loop
+    bool done = false;
+    while (!done)
+    {
+        // message processing loop
+        SDL_Event event;
+        while (SDL_PollEvent(&event))
+        {
+            // check for messages
+            switch (event.type)
+            {
+                // exit if the window is closed
+            case SDL_QUIT:
+                done = true;
+                break;
+
+                // check for keypresses
+            case SDL_KEYDOWN:
+                // exit if ESCAPE is pressed
+                switch ( event.key.keysym.sym )
+                {
+                case SDLK_ESCAPE:
+                    done = true;
+                    break;
+                case SDLK_UP:
+                case SDLK_LEFT:
+                    up[0] = true;
+                    break;
+                case SDLK_DOWN:
+                case SDLK_RIGHT:
+                    down[0] = true;
+                    break;
+                case SDLK_BACKSPACE:
+                    alive[0] = true;
+                    x[0] = w+1;
+                    lvl = 0;
+                #if NB_GAMERS > 1
+                    alive[1] = true;
+                    x[1] = w+1;
+                    break;
+                case SDLK_w:
+                case SDLK_a:
+                    up[1] = true;
+                    break;
+                case SDLK_s:
+                case SDLK_d:
+                    down[1] = true;
+                    break;
+                #endif
+                default:
+                    break;
+                }
+                break;
+
+            case SDL_KEYUP:
+                switch ( event.key.keysym.sym )
+                {
+                case SDLK_UP:
+                case SDLK_LEFT:
+                    up[0] = false;
+                    break;
+                case SDLK_DOWN:
+                case SDLK_RIGHT:
+                    down[0] = false;
+                    break;
+                #if NB_GAMERS > 1
+                case SDLK_w:
+                case SDLK_a:
+                    up[1] = false;
+                    break;
+                case SDLK_s:
+                case SDLK_d:
+                    down[1] = false;
+                    break;
+                #endif
+                default:
+                    break;
+                }
+                break;
+
+            } // end switch event tye
+        } // end of message processing
+
+        // color computing
+        if ( i < 256 )
+        {
+            r = i;
+            g = 255 - i%256;
+            // b = 255
+        }
+        else if ( i < 512 )
+        {
+            //r = 255
+            g = i%256;
+            b = 255 - i%256;
+        }
+        else if ( i < 768 )
+        {
+            r = 255 - i%256;
+            //g = 255
+            b = i%256;
+        }
+
+        // still run ?
+        running = alive[0];
+        #if NB_GAMERS > 1
+        running &= alive[1];
+        #endif // NB_GAMERS
+
+        // direction
+        for ( int t(0); t<NB_GAMERS; t++ )
+            for ( int j(0); j < 5 && running; j++ )
+            {
+                x[t]++;
+                if ( up[t] ) y[t]--;
+                else if ( down[t] ) y[t]++;
+                if ( y[t] < 0 ) y[t] = h-1;
+                if ( y[t] >= h ) y[t] = 0;
+                if ( t == 0) pixelRGBA(buffer, x[0], y[0], r, g, b, 255);
+                else pixelRGBA(buffer, x[1], y[1], g, b, r, 255);
+            }
+
+        // next round
+        if ( x[0] > w )
+        {
+            lvl++;
+            obs.clear();
+            NEXT_ROUND;
+            SDL_FillRect( buffer, 0, 0x000000 );
+            for ( int t(0); t<NB_GAMERS; t++ )
+            {
+                x[t] = 0;
+                y[t] = GAMERS_POS;
+            }
+        }
+
+        /// -- Buffer
+            /// calculs
+            i++;
+            for ( int t(0); t<NB_GAMERS; t++ )
+                if ( !alive[t] )
+                    stringRGBA(buffer, x[t]+8, y[t]+8, "GAME OVER", r/3, g/3, b/3, 255);
+            if ( i > 767 ) i = 0;
+
+            /// draw obstacles
+            for ( Uint16 j(0); j < obs.size(); j++ )
+            {
+                SDL_FillRect( buffer, &obs[j], b + (g << 8) + (r << 16) );
+                for ( int t(0); t<NB_GAMERS; t++ )
+                    if (x[t] > obs[j].x &&
+                        x[t] < obs[j].x + obs[j].w &&
+                        y[t] > obs[j].y &&
+                        y[t] < obs[j].y + obs[j].h) alive[t] = false;
+            }
+            stringRGBA(buffer, 12, 12, ("Level "+intToStr( lvl )).c_str(), r, g, b, 255);
+            for ( int t(0); t<NB_GAMERS; t++ )
+                if ( !alive[t] )
+                    stringRGBA(buffer, x[t]+6, y[t]+6, "GAME OVER", 255, 4, 23, 255);
+
+        /// past buffer
+        SDL_BlitSurface( buffer, 0, screen, 0 );
+
+        /// fps count
+        fdis_count++;
+        if ( SDL_GetTicks() - fdis_time > 1000) {
+            fdis_rslt = fdis_count;
+            fdis_count = 0;
+            fdis_time = SDL_GetTicks();
+        }
+        stringRGBA(screen, 12, 24, ("Fps: "+intToStr( fdis_rslt )).c_str(), r, g, b, 255);
+
+        /// finally, update the screen :)
+        SDL_Flip(screen);
+
+        /// time management
+        do
+        {
+            tps = SDL_GetTicks();
+        } while ( tps-tps_old < delay );
+        tps_old = tps;
+    } // end main loop
+
+    /// all is well ;)
+    std::cout << "Exited cleanly." << std::endl;
+    return 0;
+}

+ 19 - 0
spawn.cpp

@@ -0,0 +1,19 @@
+#include "spawn.h"
+
+#define SAMPLER_X 19
+#define SAMPLER_Y 10
+#define WIDE_X w/SAMPLER_X
+#define WIDE_Y h/SAMPLER_Y
+#define WIDE_SUM WIDE_MAX+WIDE_MIN
+#define SAFE_ZONE 200
+
+void spawn( std::vector<SDL_Rect>* obs, Uint16 nb, Uint16 w, Uint16 h )
+{
+    for ( Uint16 i(0); i < nb; i++)
+    {
+        obs->push_back( {   SAFE_ZONE + rand()%(w-SAFE_ZONE-WIDE_X),  // x
+                            rand()%(h-WIDE_Y),                        // y
+                            WIDE_X,                   // w
+                            WIDE_Y} );                // h
+    }
+}

+ 13 - 0
spawn.h

@@ -0,0 +1,13 @@
+#ifndef SPAWN_H_INCLUDED
+#define SPAWN_H_INCLUDED
+
+#include <SDL.h>
+#include "Input.h"
+#include "SDL_gfxPrimitives.h"
+#include <vector>
+#include <cstdlib>
+#include <ctime>
+
+void spawn( std::vector<SDL_Rect>* obs, Uint16 nb, Uint16 w, Uint16 h );
+
+#endif // SPAWN_H_INCLUDED