Advertisement
Guest User

Untitled

a guest
Sep 7th, 2013
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.99 KB | None | 0 0
  1. /*
  2.  * apt-get install libsdl1.2-dev (ubuntu)
  3.  * gcc plasma.c -o plasma -Wall -lSDL
  4.  *
  5.  * n'importe quelle touche ou la croix pour quitter
  6.  */
  7.  
  8. #include <SDL/SDL.h>
  9. #include <malloc.h>
  10. #include <math.h>
  11.  
  12. #define LARGEUR 500
  13. #define HAUTEUR 500
  14. #define PI 3.14159265359
  15. #define STEP 1000
  16.  
  17. double distance (unsigned int x1, unsigned int y1, unsigned int x2, unsigned int y2) {
  18.     // trigonometrie de ~15/16 ans : calcul de l'hypothenuse
  19.     unsigned int i = x1 - x2, j = y1 - y2;
  20.     return sqrt(i*i+j*j);
  21. }
  22.  
  23. int main (void) {
  24.     SDL_Surface *screen;
  25.     SDL_Event event;
  26.     unsigned int x,y,t=0;
  27.     unsigned long c, hlarg=LARGEUR/2, hhaut=HAUTEUR/2;
  28.     unsigned long pal[1024];
  29.     double f1, f2, f3, deg = PI/180;
  30.     double *_sin = calloc(360*STEP,sizeof(double)), *_cos = calloc(360*STEP,sizeof(double));
  31.     if (!_sin || !_cos) return -1; // au cas ou malloc se chie dessus, pas la peine d'aller plus loin
  32.  
  33.     // on initialise la sdl et le mode gfx
  34.     SDL_Init(SDL_INIT_VIDEO);
  35.     screen = SDL_SetVideoMode(LARGEUR, HAUTEUR, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
  36.  
  37.     // sine/cosine tables pre-computation
  38.     for (x=0; x<360*STEP; x++) {
  39.         _sin[x] = floor(sin(x*PI/180/STEP)*STEP)/STEP;
  40.         _cos[x] = floor(cos(x*PI/180/STEP)*STEP)/STEP;
  41.     }
  42.     // colors palette
  43.     for (x=0; x<1024; x++) {
  44.         pal[x] = (unsigned long)(
  45.             ((unsigned char)(96+95*sin(x*0.6*deg))) << 16 |
  46.             ((unsigned char)(128+127*sin(x*0.5*deg))) << 8 |
  47.             ((unsigned char)(192+63*cos(x*0.1*deg)))
  48.         );
  49.     }
  50.  
  51.     // la boucle principale
  52.     while (1) {
  53.         if (SDL_PollEvent(&event)) // on check le clavier, si on presse une touche on sort
  54.             if ((event.type == SDL_QUIT) || (event.type == SDL_KEYDOWN))
  55.                 break;
  56.  
  57.         if (SDL_MUSTLOCK(screen)) // on lock la surface si necessaire
  58.             SDL_LockSurface(screen);
  59.  
  60.         for (y=0;y<HAUTEUR;y++) { // la routine principale, on balaye l'ecran, pour chaque pixel on calcule son index dans la palette
  61.             for (x=0;x<LARGEUR;x++) {
  62.                 // nos fonctions trigo horribles et consomatrices de cycles
  63.                 f1 = sin(distance(x, y, LARGEUR + LARGEUR * _sin[(t*2)%(360*STEP)], HAUTEUR + (hhaut * _sin[(t)%(360*STEP)])) * deg * 0.3);
  64.                 f2 = cos(distance(x, y, LARGEUR + hlarg * _sin[(t*3)%(360*STEP)], HAUTEUR + (HAUTEUR * _cos[(t*4)%(360*STEP)])) * deg * 0.7);
  65.                 f3 = sin(distance(x, y, LARGEUR + hlarg * _cos[(t)%(360*STEP)], HAUTEUR + (hhaut * _cos[(t*2)%(360*STEP)])) * deg * 1.1);
  66.                 // on somme et fait la moyenne des trois fonctions point a point, et on "anime" en ajoutant t, le tout donne l'indice dans la palette de couleurs
  67.                 c = pal[((unsigned char)(512 + 512 * ((f1+f2+f3)/3))+t)%1024];
  68.                 // putpixel
  69.                 *(unsigned long *)(screen->pixels + y * screen->pitch + x * screen->format->BytesPerPixel) = c;
  70.             }
  71.         }
  72.  
  73.         if (SDL_MUSTLOCK(screen)) // si c'etait necessaire de lock on unlock la surface prete pour affichage maintenant
  74.             SDL_UnlockSurface(screen);
  75.  
  76.         t += STEP;
  77.  
  78.         SDL_UpdateRect(screen,0,0,0,0); // on update l'affichage
  79.     }
  80.  
  81.     // on quitte proprement
  82.     SDL_Quit();
  83.     free (_cos);
  84.     free (_sin);
  85.     return 0;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement