Advertisement
Guest User

Untitled

a guest
Apr 5th, 2012
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.42 KB | None | 0 0
  1. #include "SDL/SDL.h"
  2. #include <math.h>
  3.  
  4. #ifndef PI
  5. #define PI  3.14159265358979323846
  6. #endif
  7.  
  8. #define VIDEOX 320
  9. #define VIDEOY 240
  10.  
  11. #define TABLEX VIDEOX*2
  12. #define TABLEY VIDEOY*2
  13. #define TONES 256
  14.  
  15.   double r[3];
  16.   double R[6];
  17.   unsigned char *t;
  18.  
  19.   int rands[512];
  20.   int lastRandI = 0;
  21.  
  22. static void do_plasma(SDL_Surface*surface,
  23.            double x1,
  24.            double y1,
  25.            double x2,
  26.            double y2,
  27.            double x3,
  28.            double y3,
  29.            unsigned char*t)
  30. {
  31.   unsigned int
  32.     X1=x1*(TABLEX/2),
  33.     Y1=y1*(TABLEY/2),
  34.     X2=x2*(TABLEX/2),
  35.     Y2=y2*(TABLEY/2),
  36.     X3=x3*(TABLEX/2),
  37.     Y3=y3*(TABLEY/2);
  38.   unsigned int y;
  39.   unsigned char *t1=t+X1+Y1*TABLEX, *t2=t+X2+Y2*TABLEX, *t3=t+X3+Y3*TABLEX;
  40.   for (y=0; y<VIDEOY; y++) {
  41.     unsigned x;
  42.     unsigned char*tmp= ((Uint8*)surface->pixels) + y * surface->pitch;
  43.     unsigned int t=y*TABLEX, tmax=t+VIDEOX;
  44.     for (x=0; t<tmax; t++, tmp++) {
  45.       tmp[0]=t1[t]+t2[t]+t3[t];
  46.     }
  47.   }
  48. }
  49.  
  50. int main ()
  51. {
  52.   SDL_Surface *screen;
  53.   SDL_Color colors[TONES*2];
  54.  
  55.   int starttime, endtime, realtime;
  56.   int i, state=0;
  57.  
  58.   /* randomized stuff */
  59.   startcrand(SDL_GetTicks(), 99999999);
  60.   {
  61.     int c;
  62.     for (c=0; c<3; c++)
  63.       r[c]=((double)(crand()%1000+1))/300000;
  64.     for (c=0; c<6; c++)
  65.       R[c]=((double)(crand()%1000+1))/5000;
  66.   }
  67.  
  68.  
  69.   /* Inizialize the SDL library */
  70.   if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
  71.     fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
  72.     return 1;
  73.   }
  74.  
  75.  
  76.   screen=SDL_SetVideoMode(VIDEOX, VIDEOY, 8, SDL_SWSURFACE);
  77.   if ( screen == NULL ) {
  78.     fprintf(stderr, "Couldn't initialize video mode: %s\n", SDL_GetError());
  79.     return 1;
  80.   }
  81.  
  82.   SDL_nFont *font = SDL_nLoadFont(NSP_FONT_THIN, SDL_MapRGB(screen->format, 255,0,60),NSP_FONT_DEFAULT);
  83.   SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 100));
  84.   SDL_nDrawString(screen, font, 120, 110, "Loading...");
  85.   SDL_Flip(screen);
  86.  
  87.   precalculateTable();
  88.  
  89.   SDL_EventState(SDL_ACTIVEEVENT, SDL_IGNORE);
  90.   SDL_EventState(SDL_MOUSEMOTION, SDL_IGNORE);
  91.  
  92.  
  93.   /* Main loop */
  94.   while (SDL_PollEvent(NULL)==0) {
  95.     starttime=SDL_GetTicks();
  96.     state++;
  97.     {
  98.       int c;
  99.       for (c=0 ; c<TONES; c++) {
  100.     colors[c].r=(sin(((double)c)/TONES*6*PI+r[0]*PI*state*PI)+1)*127;
  101.     colors[c].g=(sin(((double)c)/TONES*6*PI+r[1]*state*PI)+1)*127;
  102.     colors[c].b=(sin(((double)c)/TONES*6*PI+r[2]*state*PI)+1)*127;
  103.       }
  104.       SDL_SetColors(screen, colors, 0, TONES);
  105.     }
  106.     if (SDL_LockSurface(screen) <0 ) {
  107.       continue;
  108.     }
  109.     do_plasma(screen,
  110.     (sin(((double)state)*R[0])+1)/2,
  111.     (sin(((double)state)*R[1])+1)/2,
  112.     (sin(((double)state)*R[2])+1)/2,
  113.     (sin(((double)state)*R[3])+1)/2,
  114.     (sin(((double)state)*R[4])+1)/2,
  115.     (sin(((double)state)*R[5])+1)/2, t);
  116.     SDL_UnlockSurface(screen);
  117.     SDL_Flip(screen);
  118.     endtime = SDL_GetTicks();
  119.     //limit frame draw time to a minimum of 40ms (25fps)
  120.     if((endtime-starttime)<40){
  121.         sleep(40-(endtime-starttime));
  122.         realtime = SDL_GetTicks()-starttime;
  123.     }
  124.   }
  125.   printf("Time to draw last frame was %d ms (%d fps)\n",endtime-starttime,1000/(endtime-starttime));
  126.   if((endtime-starttime)<40)
  127.     printf("But was forced to %d ms (%d fps)\n",realtime,1000/realtime);
  128.   SDL_Quit();
  129.   return 0;
  130. }
  131.  
  132. void precalculateTable()
  133. {
  134.   printf("Precalculating table...\n");
  135.   t = (unsigned char *)malloc(TABLEY*TABLEX);
  136.   if ( t == NULL ) {
  137.     fprintf(stderr, "Out of memory\n");
  138.     exit(1);
  139.   }
  140.   {
  141.     int y;
  142.     for (y=0 ; y<TABLEY ; y++) {
  143.       int x;
  144.       for (x=0 ; x<TABLEX ; x++) {
  145.     double tmp=(((double)((x-(TABLEX/2))*(x-(TABLEX/2))+(y-(TABLEX/2))*(y-(TABLEX/2))))
  146.             *(PI/(TABLEX*TABLEX+TABLEY*TABLEY)));
  147.     t[y*TABLEX+x]=(sin(sqrt(tmp)*12)+1)*TONES/6;
  148.       }
  149.     }
  150.   }
  151. }
  152.  
  153.  
  154. //RAND functions
  155.  
  156.  
  157. void startcrand(int seed, int max){
  158.  
  159.     printf("Starting pseudorandom number generator...\n");
  160.    
  161.    
  162.     if(seed>max){
  163.         printf("Oops: seed is bigger than max\n");
  164.         exit(-1);
  165.         }
  166.    
  167.     if(seed == 0){
  168.         printf("Oops: seed is 0 - setting it to 1337\n");
  169.         seed = 1337;
  170.     }
  171.        
  172.     printf("Seed is %d\n",seed);
  173.  
  174.  
  175.  
  176.     int a = 5; //multiplier
  177.     int c = 3; //increment
  178.  
  179.     int i; //iterator
  180.    
  181.     printf("Generating random numbers.. \n");
  182.     for(i=0; i<500; i++)
  183.     {
  184.         seed = (a * seed + c) % max;
  185.        
  186.         rands[i] = seed;
  187.        
  188.  
  189.     }
  190.     printf("Done!\n");
  191. }
  192.  
  193.  
  194.  
  195. int crand(){
  196.  
  197.     if(lastRandI > 490)
  198.         lastRandI = 2;
  199.    
  200.     lastRandI++;
  201.    
  202.    
  203.     return rands[lastRandI];
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement