浏览代码

Import launch circle project

DricomDragon 4 年之前
父节点
当前提交
98fda6e611
共有 3 个文件被更改,包括 221 次插入0 次删除
  1. 80 0
      sdl1/launchCircle/LaunchCircle.cpp
  2. 42 0
      sdl1/launchCircle/LaunchCircle.h
  3. 99 0
      sdl1/launchCircle/main.cpp

+ 80 - 0
sdl1/launchCircle/LaunchCircle.cpp

@@ -0,0 +1,80 @@
+#include "LaunchCircle.h"
+#include <cmath>
+
+LaunchCircle::LaunchCircle(int const rayon, Uint32 const couleur, int const stock)
+:m_rayon(rayon),m_epaisseur(40),m_stock(stock)
+{
+	m_pixel = SDL_CreateRGBSurface(SDL_HWSURFACE, 1, 1, 16,0,0,0,0);
+	SDL_FillRect(m_pixel,NULL,couleur);
+}
+
+LaunchCircle::LaunchCircle(int const rayon, Uint32 const couleur, int const stock, int const epaisseur)
+:m_rayon(rayon),m_epaisseur(epaisseur),m_stock(stock)
+{
+	m_pixel = SDL_CreateRGBSurface(SDL_HWSURFACE, 1, 1, 16,0,0,0,0);
+	SDL_FillRect(m_pixel,NULL,couleur);
+}
+
+LaunchCircle::~LaunchCircle()
+{
+    SDL_FreeSurface(m_pixel);
+}
+
+void LaunchCircle::afficher(int const x, int const y, int const valeur, SDL_Surface *screen)
+{
+	// [1] Création du % puis transformation en radians
+	double radian((double)valeur/m_stock*2*M_PI);//Entre 0 et 2PI
+	radian -= M_PI_2;//Entre -PI/2 et 3PI/2
+
+	// [2] Création de l'équation de droite
+	double m(0);
+	double p(0);
+
+    m = (sin(radian))/(cos(radian));//En gros c'est ça
+    p = -(m*x)+y;//ça c'est fait
+
+	// [3] Trace
+	SDL_Rect position;
+	for (int i(x-m_rayon); i<=x+m_rayon; i++)
+	{
+		for (int j(y-m_rayon); j<=y+m_rayon; j++)
+		{
+
+			if ((i-x)*(i-x)+(j-y)*(j-y)<m_rayon*m_rayon &&
+                (i-x)*(i-x)+(j-y)*(j-y)>m_rayon*m_rayon-m_epaisseur*m_epaisseur)
+			{
+				position.x=i;
+				position.y=j;
+				if (radian <=-M_PI_2)
+				{}
+				else if (radian >= 3*M_PI_2)//Cercle entier
+				{
+                    SDL_BlitSurface(m_pixel, 0, screen, &position);
+				}
+				else if (radian == M_PI_2)//Demi cercle
+				{
+                    if (i<=x)
+                        SDL_BlitSurface(m_pixel, 0, screen, &position);
+				}
+				else if (radian < M_PI_2)//Moins d'une moitié
+				{
+					if (i<=x && j>m*i+p)
+					{
+						SDL_BlitSurface(m_pixel, 0, screen, &position);
+					}
+				}
+				else if (radian > M_PI_2)//Plus d'une moitié
+				{
+					if (i<=x)
+					{
+						SDL_BlitSurface(m_pixel, 0, screen, &position);
+					}
+					else if (j<m*i+p)
+					{
+						SDL_BlitSurface(m_pixel, 0, screen, &position);
+                    }
+                }
+            }
+        }
+    }
+}

+ 42 - 0
sdl1/launchCircle/LaunchCircle.h

@@ -0,0 +1,42 @@
+#ifndef LAUNCHCIRCLE_DEF
+#define LAUNCHCIRCLE_DEF
+
+///Modifié le:  12/06/2013
+///Objet: création
+
+#include <SDL/SDL.h>
+#include <iostream>
+
+/*
+-Rôle de la classe LaunchCircle:
+	Cette classe permet de créer un arc cercle
+censé représenter une barre de chargement, ou une barre
+d'écoulement, etc...
+-Méthodes:
+	+Constructeur:
+		Arguments:Rayon, couleur, capacité maximale
+    +Constructeur amelioré:
+		Arguments:Rayon, couleur, capacité maximale, epaisseur
+    +afficher:
+        Pour coller le cercle sur une surface.
+*/
+
+class LaunchCircle
+{
+	public:
+	LaunchCircle(int const rayon, Uint32 const couleur, int const stock);
+	LaunchCircle(int const rayon, Uint32 const couleur, int const stock, int const epaisseur);
+	~LaunchCircle();
+	void afficher(int const x, int const y, int const valeur, SDL_Surface *screen);
+
+	private:
+	//Attributs standards
+	int const m_rayon;
+	int const m_epaisseur;
+	int const m_stock;
+
+	//Attributs SDL
+	SDL_Surface *m_pixel;
+};
+
+#endif

+ 99 - 0
sdl1/launchCircle/main.cpp

@@ -0,0 +1,99 @@
+#include <cstdlib>
+
+#include <SDL/SDL.h>
+#undef main
+
+#include "LaunchCircle.h"
+
+int main ( int argc, char** argv )
+{
+    // [1] Initialisation des SDL
+    if ( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_TIMER) < 0 )
+    {
+        printf( "Unable to init SDL: %s\n", SDL_GetError() );
+        return 1;
+    }
+
+    // make sure SDL cleans up before exit
+    atexit(SDL_Quit);
+
+    // create a new window
+    SDL_Surface* screen = SDL_SetVideoMode(640, 480, 16,
+                                           SDL_HWSURFACE|SDL_DOUBLEBUF);
+    if ( !screen )
+    {
+        printf("Unable to set 640x480 video: %s\n", SDL_GetError());
+        return 1;
+    }
+
+    // [2] création des composants
+    LaunchCircle cercle(200, SDL_MapRGB(screen->format,255,128,0), 360, 150);
+    int verrou(0);
+    bool up(false);
+    bool down(false);
+
+    // [3] program main loop
+    bool done = false;
+    while (!done)
+    {
+        // message processing loop
+        SDL_Event event;
+        // check for messages
+        while(SDL_PollEvent(&event))
+        {
+            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
+                    if (event.key.keysym.sym == SDLK_ESCAPE)
+                        done = true;
+                    if(event.key.keysym.sym == SDLK_UP)
+                        up=true;
+                    if(event.key.keysym.sym == SDLK_DOWN)
+                        down=true;
+
+                    break;
+                }
+            case SDL_KEYUP:
+                {
+                    if(event.key.keysym.sym == SDLK_UP)
+                        up=false;
+                    if(event.key.keysym.sym == SDLK_DOWN)
+                        down=false;
+
+                    break;
+                }
+            } // end switch
+        }//End message processing
+        // [4] Calculs
+        if (up)
+            verrou++;
+        if(down)
+            verrou--;
+
+        // [5] DRAWING STARTS HERE
+
+        // clear screen
+        SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 140, 128, 128));
+
+        cercle.afficher(320,240,verrou,screen);
+
+        // draw bitmap
+
+        // DRAWING ENDS HERE
+
+        // finally, update the screen :)
+        SDL_Flip(screen);
+    } // end main loop
+
+    // all is well ;)
+    printf("Exited cleanly\n");
+    return 0;
+}