Guest User

Untitled

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