Parcourir la source

Import rotation test in SDL 1

DricomDragon il y a 4 ans
Parent
commit
175fd426d8

BIN
sdl1/rotationTest/Perso.bmp


+ 234 - 0
sdl1/rotationTest/Vecteur.cpp

@@ -0,0 +1,234 @@
+#include "Vecteur.h"
+
+Vec::Vec() : m_x(0.0), m_y(0.0), m_z(0.0)
+{
+
+}
+Vec::Vec(float x, float y, float z) : m_x(x), m_y(y), m_z(z)
+{
+
+}
+Vec::Vec(const Vec &vecteur) : m_x(vecteur.getX()), m_y(vecteur.getY()), m_z(vecteur.getZ())
+{
+
+}
+Vec::~Vec()
+{
+
+}
+float Vec::getX() const
+{
+    return m_x;
+}
+
+float Vec::getY() const
+{
+    return m_y;
+}
+
+float Vec::getZ() const
+{
+    return m_z;
+}
+
+
+// Setters
+
+void Vec::setVecteur(float x, float y, float z)
+{
+    m_x = x;
+    m_y = y;
+    m_z = z;
+}
+
+void Vec::setX(float x)
+{
+    m_x = x;
+}
+
+void Vec::setY(float y)
+{
+    m_y = y;
+}
+
+void Vec::setZ(float z)
+{
+    m_z = z;
+}
+void Vec::normaliser()
+{
+    // La fonction sqrt() permet de trouver la racine carré d'un nombre
+
+    float longueur (sqrt(m_x * m_x + m_y * m_y + m_z * m_z));
+
+
+    // Normalisation du vecteur
+
+    if(longueur != 0.0)
+    {
+        m_x /= longueur;
+        m_y /= longueur;
+        m_z /= longueur;
+    }
+}
+Vec& Vec::operator=(const Vec &vecteur)
+{
+    // Copie des valeurs
+
+    m_x = vecteur.m_x;
+    m_y = vecteur.m_y;
+    m_z = vecteur.m_z;
+
+
+    // Retour de l'objet
+
+    return *this;
+}
+Vec Vec::operator+(const Vec &vecteur)
+{
+    // Création d'un objet résultat
+
+    Vec resultat;
+
+
+    // Addition des coordonnées
+
+    resultat.m_x = m_x + vecteur.m_x;
+    resultat.m_y = m_y + vecteur.m_y;
+    resultat.m_z = m_z + vecteur.m_z;
+
+
+    // Retour de résultat
+
+    return resultat;
+}
+Vec Vec::operator-(const Vec &vecteur)
+{
+    // Création d'un objet résultat
+
+    Vec resultat;
+
+
+    // Soustraction des coordonnées
+
+    resultat.m_x = m_x - vecteur.m_x;
+    resultat.m_y = m_y - vecteur.m_y;
+    resultat.m_z = m_z - vecteur.m_z;
+
+
+    // Retour de résultat
+
+    return resultat;
+}
+Vec Vec::operator*(float multiplicateur)
+{
+    // Création d'un objet résultat
+
+    Vec resultat;
+
+
+    // Multiplication des coordonnées
+
+    resultat.m_x = m_x * multiplicateur;
+    resultat.m_y = m_y * multiplicateur;
+    resultat.m_z = m_z * multiplicateur;
+
+
+    // Retour du résultat
+
+    return resultat;
+}
+Vec Vec::operator/(float diviseur)
+{
+    // Création d'un objet résultat
+
+    Vec resultat;
+
+
+    // Multiplication des coordonnées
+
+    resultat.m_x = m_x / diviseur;
+    resultat.m_y = m_y / diviseur;
+    resultat.m_z = m_z / diviseur;
+
+
+    // Retour du résultat
+
+    return resultat;
+}
+Vec Vec::operator*(const Vec &vecteur)
+{
+    // Création d'un objet résultat
+
+    Vec resultat;
+
+
+    // Produit Vectoriel
+
+    resultat.m_x = (m_y * vecteur.m_z) - (m_z * vecteur.m_y);
+    resultat.m_y = (m_z * vecteur.m_x) - (m_x * vecteur.m_z);
+    resultat.m_z = (m_x * vecteur.m_y) - (m_y * vecteur.m_x);
+
+
+    // Retour de l'objet
+
+    return resultat;
+}
+void Vec::operator*=(const Vec &vecteur)
+{
+    *this = *this * vecteur;
+}
+void Vec::operator-=(const Vec &vecteur)
+{
+    *this = *this - vecteur;
+}
+void Vec::operator+=(const Vec &vecteur)
+{
+    *this = *this + vecteur;
+}
+void Vec::operator*=(float multiplicateur)
+{
+    *this = *this * multiplicateur;
+}
+void Vec::operator/=(float multiplicateur)
+{
+    *this = *this * multiplicateur;
+}
+float Vec::scalair(const Vec &vecteur)
+{
+    Vec v1(*this), v2(vecteur);
+    v1.normaliser();
+    v2.normaliser();
+    return v1.getX() * v2.getX() + v1.getY() * v2.getY() + v1.getZ() * v2.getZ();
+}
+bool Vec::operator==(const Vec &vecteur)
+{
+    if(m_x == vecteur.getX() && m_y == vecteur.getY() && m_z == vecteur.getZ())
+        return true;
+    else
+        return false;
+}
+bool Vec::operator!=(const Vec &vecteur)
+{
+    if(*this == vecteur)
+        return false;
+    else
+        return true;
+}
+double Vec::norme()
+{
+    return sqrt(m_x*m_x+m_y*m_y+m_z*m_z);
+}
+void Vec::rotateR(float angle)
+{
+    float tmp = m_x;
+    m_x = cos(angle)*m_x-sin(angle)*m_y;
+    m_y = sin(angle)*tmp+cos(angle)*m_y;
+}
+void Vec::rotateD(float angle)
+{
+    float _angle = angle*M_PI/180;
+    float tmp = m_x;
+    m_x = cos(_angle)*m_x-sin(_angle)*m_y;
+    m_y = sin(_angle)*tmp+cos(_angle)*m_y;
+}

+ 46 - 0
sdl1/rotationTest/Vecteur.h

@@ -0,0 +1,46 @@
+#ifndef VECTEUR_H_INCLUDED
+#define VECTEUR_H_INCLUDED
+
+#include <cmath>
+
+class Vec
+{
+    public:
+
+    Vec();
+    Vec(float x, float y, float z = 0);
+    Vec(const Vec &vecteur);
+    ~Vec();
+    float getX() const;
+    float getY() const;
+    float getZ() const;
+    void setVecteur(float x, float y, float z);
+    void setX(float x);
+    void setY(float y);
+    void setZ(float z);
+    void normaliser();
+    Vec& operator=(const Vec &vecteur);
+    Vec operator+(const Vec &vecteur);
+    Vec operator-(const Vec &vecteur);
+    Vec operator*(float multiplicateur);
+    Vec operator/(float diviseur);
+    Vec operator*(const Vec &vecteur);
+    void operator*=(const Vec &vecteur);
+    void operator-=(const Vec &vecteur);
+    void operator+=(const Vec &vecteur);
+    void operator*=(float multiplicateur);
+    void operator/=(float multiplicateur);
+    float scalair(const Vec &vecteur);
+    bool operator==(const Vec &vecteur);
+    bool operator!=(const Vec &vecteur);
+    double norme();
+    void rotateR(float angle); // angle en radians
+    void rotateD(float angle); // angle en degrées
+
+private:
+    float m_x;
+    float m_y;
+    float m_z;
+};
+
+#endif // VECTEUR_H_INCLUDED

BIN
sdl1/rotationTest/epee.bmp


BIN
sdl1/rotationTest/epee_trans.bmp


+ 107 - 0
sdl1/rotationTest/main.cpp

@@ -0,0 +1,107 @@
+//Basiques
+#include <iostream>
+#include <cmath>
+
+//SDL
+#include <SDL/SDL.h>
+#undef main
+#include <SDL/SDL_rotozoom.h>
+
+//Projet
+#include "Vecteur.h"
+
+int main ( int argc, char** argv )
+{
+    /// [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 1;
+    }
+
+    // [1.2] Préparation de fermeture
+    atexit(SDL_Quit);
+
+    // [1.3] Para-fenêtre
+    SDL_WM_SetCaption("Rotator", 0);
+
+    /// [2] Préparation des composants
+    // [2.1] Préparation de la fenêtre
+    SDL_Surface* screen = SDL_SetVideoMode(1280, 720, 32,
+                                           SDL_HWSURFACE|SDL_DOUBLEBUF);
+    if ( !screen )
+    {
+        std::cout << "Unable to set 640x480 video: " << SDL_GetError() << std::endl;
+        return 1;
+    }
+
+    // [2.2] Préparation surface
+    SDL_Rect pos;
+    SDL_Surface* epee = SDL_LoadBMP("Perso.bmp");
+    SDL_SetColorKey(epee, SDL_SRCCOLORKEY, SDL_MapRGBA(epee->format, 0, 0, 0, 255));
+    SDL_Surface* rotation = rotozoomSurface(epee, 0.0, 1.0, 1);
+    double angle(0.0);
+    Vec dir;
+    Vec axeY( 0.0f, 1.0f );
+
+    /// [3] Boucle principale
+    bool done = false;
+    SDL_Event event;
+    while (!done)
+    {
+        // [3.1] Gestion évènements
+        while (SDL_PollEvent(&event))
+        {
+            switch (event.type)
+            {
+            case SDL_QUIT:
+                done = true;
+                break;
+            case SDL_KEYDOWN:
+                if (event.key.keysym.sym == SDLK_ESCAPE)
+                    done = true;
+            case SDL_MOUSEMOTION:
+                dir.setX( event.motion.x - screen->w/2 );
+                dir.setY( screen->h/2 - event.motion.y );
+                angle = atan((double)(dir.getY()) / (dir.getX()+0.5f)); // Angle en radians
+                angle = angle * 180.0 / M_PI; // Angle en ° + correction pointage
+                if ( dir.getX() < 0 ) angle += 180;
+                break;
+            } // end switch event type
+        } // end of message processing
+
+        // [3.2] Calculs
+        SDL_FreeSurface(rotation);
+        rotation = rotozoomSurface(epee, angle, 1.0, 0);
+
+        pos.x = screen->w/2 - rotation->w/2;
+        pos.y = screen->h/2 - rotation->h/2;
+
+        // [3.3] Dessin des composants
+        SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 255, 0));
+
+        SDL_BlitSurface(rotation, 0, screen, &pos);
+
+        SDL_Flip(screen);
+
+        // [3.4] Temps
+        SDL_Delay(30);
+
+    } //fin bcl principale
+
+    ///Performance
+    /*SDL_Surface* m_surface[1000];
+    Uint32 temps(SDL_GetTicks());///Chrono START
+    for (int i(0); i<1000; i++) m_surface[i] = rotozoomSurface(epee, 35.0, 1.0, 1);
+    temps = SDL_GetTicks() - temps;///Chrono END
+    for (int i(0); i<1000; i++) SDL_FreeSurface(m_surface[i]);
+    std::cout << "Temps de calcul : " << temps << std::endl;*/
+
+    ///[4] Destruction des composants
+    SDL_FreeSurface(screen);
+    SDL_FreeSurface(epee);
+    SDL_FreeSurface(rotation);
+
+    return 0;
+}