Browse Source

Import metaballs project

DricomDragon 4 years ago
parent
commit
53e0652e49

+ 52 - 0
sdl1/metaBalls/MetaBin.cpp

@@ -0,0 +1,52 @@
+#include "MetaBin.h"
+
+/// Construction
+MetaBin::MetaBin()
+:MetaSpace(), m_seuil(128)
+{
+    //ctor
+}
+
+MetaBin::MetaBin(Uint32 seuil)
+:MetaSpace(), m_seuil(seuil)
+{
+    //ctor
+}
+
+MetaBin::~MetaBin()
+{
+    //dtor
+}
+
+/// Méthodes
+void MetaBin::deform(Uint32 y, Uint32 x)
+{
+    for ( Uint32 k(0); k < 50; k++)
+        for (Uint32 i(y-k); i < y+k; i++)
+            for (Uint32 j(x-k); j < x+k; j++)
+                m_tab[i][j] += 140 - k;
+}
+
+void MetaBin::draw()
+{
+    for ( Uint32 i = 0; i < m_h; i++ )
+        for ( Uint32 j = 0; j < m_w; j++ )
+        {
+            if ( m_tab[i][j] > m_seuil )
+                pixelColor(m_buff, j, i, 0xffffff);
+        }
+}
+
+/// Acesseurs
+void MetaBin::setSeuil(Uint32 seuil)
+{
+    m_seuil = seuil;
+}
+
+Uint32 MetaBin::getSeuil()
+{
+    return m_seuil;
+}
+
+
+

+ 23 - 0
sdl1/metaBalls/MetaBin.h

@@ -0,0 +1,23 @@
+#ifndef METABIN_H
+#define METABIN_H
+
+#include "MetaSpace.h"
+
+class MetaBin : public MetaSpace
+{
+    public:
+        MetaBin();
+        MetaBin(Uint32 seuil);
+        virtual ~MetaBin();
+
+        virtual void deform(Uint32 y, Uint32 x);
+        virtual void draw();
+
+        void setSeuil(Uint32 seuil);
+        Uint32 getSeuil();
+
+    protected:
+        Uint32 m_seuil;
+};
+
+#endif // METABIN_H

+ 40 - 0
sdl1/metaBalls/MetaDeg.cpp

@@ -0,0 +1,40 @@
+#include "MetaDeg.h"
+
+/// Construction
+MetaDeg::MetaDeg()
+:MetaSpace()
+{
+    //ctor
+}
+
+MetaDeg::~MetaDeg()
+{
+    //dtor
+}
+
+/// Méthodes
+void MetaDeg::deform(Uint32 y, Uint32 x)
+{
+    Uint32 k(127);
+    for (Uint32 i(y-k); i < y+k; i++)
+        for (Uint32 j(x-k); j < x+k; j++)
+            if ( isInside(i, j) )
+            {
+                m_tab[i][j] += 255 - abs((int)j - (int)x) - abs((int)i - (int)y);
+                if (m_tab[i][j] > 255)
+                    m_tab[i][j] = 255;
+            }
+}
+#include <iostream>
+void MetaDeg::draw()
+{
+    Uint32 val(0);
+    for ( Uint32 i = 0; i < m_h; i++ )
+        for ( Uint32 j = 0; j < m_w; j++ )
+        {
+            val = m_tab[i][j];
+            pixelRGBA(m_buff, j, i, val, 0, 0, 255);
+        }
+}
+
+

+ 17 - 0
sdl1/metaBalls/MetaDeg.h

@@ -0,0 +1,17 @@
+#ifndef METABIN_H
+#define METABIN_H
+
+#include "MetaSpace.h"
+
+class MetaDeg : public MetaSpace
+{
+    public:
+        MetaDeg();
+        MetaDeg(Uint32 seuil);
+        virtual ~MetaDeg();
+
+        virtual void deform(Uint32 y, Uint32 x);
+        virtual void draw();
+};
+
+#endif // METABIN_H

+ 50 - 0
sdl1/metaBalls/MetaSpace.cpp

@@ -0,0 +1,50 @@
+#include "MetaSpace.h"
+
+/// Construction
+MetaSpace::MetaSpace()
+: m_buff(0x0), m_w(0), m_h(0)
+{
+    //ctor
+}
+
+MetaSpace::~MetaSpace()
+{
+    if ( !m_buff )
+        SDL_FreeSurface(m_buff) ;
+}
+
+/// Méthodes
+bool MetaSpace::init(Uint32 w, Uint32 h)
+{
+    m_w = w ;
+    m_h = h ;
+
+    m_buff = SDL_CreateRGBSurface(SDL_HWSURFACE, w, h, 32, 0, 0, 0, 0) ;
+
+    if ( !m_buff )
+        return false;
+    else
+        return true;
+
+    clean();
+}
+
+void MetaSpace::clean()
+{
+    SDL_FillRect(m_buff, 0x0, 0x000000);
+
+    for ( Uint32 i = 0; i < m_h; i++ )
+        for ( Uint32 j = 0; j < m_w; j++ )
+            m_tab[i][j] = 0 ;
+}
+
+bool MetaSpace::isInside(Uint32 y, Uint32 x)
+{
+    return y < m_h && x < m_w && x >= 0 && y >=0 ;
+}
+
+/// Acésseurs
+SDL_Surface* MetaSpace::getSurf()
+{
+    return m_buff;
+}

+ 30 - 0
sdl1/metaBalls/MetaSpace.h

@@ -0,0 +1,30 @@
+#ifndef METASPACE_H
+#define METASPACE_H
+
+#include <SDL.h>
+#undef main
+#include <SDL/SDL_gfxPrimitives.h>
+
+class MetaSpace
+{
+    public:
+        MetaSpace();
+        virtual ~MetaSpace();
+
+        virtual bool init(Uint32 w, Uint32 h);
+        void clean();
+        bool isInside(Uint32 y, Uint32 x);
+
+        virtual void deform(Uint32 y, Uint32 x) = 0;
+        virtual void draw() = 0;
+
+        SDL_Surface* getSurf();
+
+    protected:
+    SDL_Surface* m_buff ;
+    Uint32 m_w;
+    Uint32 m_h;
+    Uint32 m_tab[640][480] ;
+};
+
+#endif // METASPACE_H

+ 106 - 0
sdl1/metaBalls/main.cpp

@@ -0,0 +1,106 @@
+#include <iostream>
+
+#include "MetaDeg.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("Application SDL", 0);
+
+    /// [2] Préparation des composants
+    // [2.1] Préparation de la fenêtre
+    int sc_w(640) ;
+    int sc_h(480) ;
+    SDL_Surface* screen = SDL_SetVideoMode(sc_w, sc_h, 32, SDL_HWSURFACE|SDL_DOUBLEBUF);
+    if ( !screen )
+    {
+        std::cout << "Unable to set 640x480 video: " << SDL_GetError() << std::endl;
+        return 1;
+    }
+
+    // [2.2] Préparation de l'espace
+    MetaDeg mySpace;
+    if ( !mySpace.init(sc_w, sc_h) )
+    {
+        std::cout << "Problème initialisation du Metaspace." << std::endl ;
+        return 1;
+    }
+    SDL_Surface* meta_img(mySpace.getSurf());
+
+    // [2.3] Préparation des calculs
+    Uint32 mx(sc_w / 2) ;
+    Uint32 my(sc_w / 2) ;
+
+    /// [3] Boucle principale
+    SDL_EnableKeyRepeat(10,10);
+    bool done = false;
+    while (!done)
+    {
+        // [3.1] Gestion évènements
+        SDL_Event event;
+        while (SDL_PollEvent(&event))
+        {
+            switch (event.type)
+            {
+            case SDL_QUIT:
+                done = true;
+                break;
+            case SDL_MOUSEMOTION:
+                mx = event.motion.x ;
+                my = event.motion.y ;
+                break;
+            case SDL_KEYDOWN:
+                switch (event.key.keysym.sym)
+                {
+                case SDLK_ESCAPE:
+                    done = true;
+                    break;
+                case SDLK_UP:
+                    /*if (seuil < 30000)
+                        seuil += 1 ;
+                    mySpace.setSeuil(seuil);*/
+                    break;
+                case SDLK_DOWN:
+                    /*if (seuil > 10)
+                        seuil -= 1 ;
+                    mySpace.setSeuil(seuil);*/
+                    break;
+                default:
+                    break;
+                }
+                break;
+            } // end switch event type
+        } // end of message processing
+
+        // [3.2] Calculs
+        mySpace.clean();
+        mySpace.deform(my, mx);
+        mySpace.deform(180, 250);
+        mySpace.draw();
+
+        // [3.3] Dessin des composants
+        SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 255, 255));
+
+        SDL_BlitSurface(meta_img, 0, screen, 0);
+
+        SDL_Flip(screen);
+    } //fin bcl principale
+
+    ///[4] Destruction des composants
+    SDL_FreeSurface(screen);
+
+
+    std::cout << "Aucune erreur détectée." << std::endl;
+    return 0;
+}