Advertisement
Guest User

Untitled

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