瀏覽代碼

Import mirror test

DricomDragon 4 年之前
父節點
當前提交
9f90a1717d
共有 4 個文件被更改,包括 145 次插入0 次删除
  1. 二進制
      sdl1/mirrorImage/cb.bmp
  2. 85 0
      sdl1/mirrorImage/main.cpp
  3. 43 0
      sdl1/mirrorImage/retourner.cpp
  4. 17 0
      sdl1/mirrorImage/retourner.h

二進制
sdl1/mirrorImage/cb.bmp


+ 85 - 0
sdl1/mirrorImage/main.cpp

@@ -0,0 +1,85 @@
+#include <iostream>
+#include <SDL.h>
+#include "retourner.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
+    SDL_Surface* screen = SDL_SetVideoMode(640, 480, 16,
+                                           SDL_HWSURFACE|SDL_DOUBLEBUF);
+    if ( !screen )
+    {
+        std::cout << "Unable to set 640x480 video: " << SDL_GetError() << std::endl;
+        return 1;
+    }
+
+    // [2.2] Préparation
+    SDL_Surface* originale = SDL_LoadBMP("cb.bmp");
+    SDL_Surface* copie = retournement(originale,78);//Appel de la fonction
+    SDL_Rect posOrig={20,20,0,0};
+    //SDL_Rect posCop={620-copie->w,20,0,0};
+    SDL_Surface* poubelle(0);
+
+    /// [3] Boucle principale
+    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_KEYDOWN:
+                if (event.key.keysym.sym == SDLK_ESCAPE)
+                    done = true;
+                break;
+            } // end switch event type
+        } // end of message processing
+
+        // [3.2] Calculs
+        poubelle = originale;
+        originale = retournement(originale,1);
+        SDL_FreeSurface(poubelle);
+        posOrig.x=320-originale->w/2;
+        posOrig.y=240-originale->h/2;
+
+        // [3.3] Dessin des composants
+        SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 255, 255));
+
+        SDL_BlitSurface(originale,0,screen,&posOrig);
+        //SDL_BlitSurface(copie,0,screen,&posCop);
+
+        SDL_Flip(screen);
+
+        // [3.4] 5 fois par seconde
+        SDL_Delay(200);
+
+    } //fin bcl principale
+
+    ///[4] Destruction des composants
+    SDL_FreeSurface(screen);
+    SDL_FreeSurface(originale);
+    SDL_FreeSurface(copie);
+
+    std::cout << "Aucune erreur détectée." << std::endl;
+    return 0;
+}

+ 43 - 0
sdl1/mirrorImage/retourner.cpp

@@ -0,0 +1,43 @@
+#include "retourner.h"
+
+SDL_Surface* retournement(SDL_Surface* originale, int nbQuarts)
+{
+    //Création de la seconde surface
+    SDL_Surface* pivot(0);
+    if (nbQuarts%2)
+        pivot=SDL_CreateRGBSurface(SDL_HWSURFACE,originale->h,originale->w,32,0,0,0,0);
+    else
+        pivot=SDL_CreateRGBSurface(SDL_HWSURFACE,originale->w,originale->h,32,0,0,0,0);
+
+    //Photocopie quart de tour
+    SDL_Rect fromPos={0,0,1,1};
+    SDL_Rect toPos={0,0,0,0};
+    for (fromPos.x = 0; fromPos.x < originale->w; fromPos.x++)
+        for (fromPos.y = 0; fromPos.y < originale->h; fromPos.y++)
+        {
+            if (nbQuarts%4 == 0)
+            {
+                toPos.x=fromPos.x;
+                toPos.y=fromPos.y;
+            }
+            if (nbQuarts%4 == 1)
+            {
+                toPos.x=originale->h-fromPos.y;
+                toPos.y=fromPos.x;
+            }
+            if (nbQuarts%4 == 2)
+            {
+                toPos.x=originale->w-fromPos.x;
+                toPos.y=originale->h-fromPos.y;
+            }
+            if (nbQuarts%4 == 3)
+            {
+                toPos.x=fromPos.y;
+                toPos.y=originale->w-fromPos.x;
+            }
+            SDL_BlitSurface(originale,&fromPos,pivot,&toPos);
+        }
+
+    return pivot;
+}
+

+ 17 - 0
sdl1/mirrorImage/retourner.h

@@ -0,0 +1,17 @@
+#ifndef RETOURNER_H_INCLUDED
+#define RETOURNER_H_INCLUDED
+
+#include <iostream>
+#include <SDL.h>
+
+SDL_Surface* retournement(SDL_Surface* originale, int nbQuarts);
+/** retournement() \brief Permet de créer une nouvelle image pivotée.
+ *
+ * SDL_Surface* originale \param Surface originale pour pivoter.
+ * int nbQuarts \param Indique le nombre de quarts de tour à réaliser, sens indirect.
+ * SDL_Surface* \return La surface pivotée. Attention ! Ne pas oublier freeSurface().
+ *
+ */
+
+
+#endif // RETOURNER_H_INCLUDED