Guest User

Untitled

a guest
Jul 20th, 2018
971
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.07 KB | None | 0 0
  1. /*
  2.   Written by Bini Michele <mibin@tin.it>, 1998
  3.   */
  4.  
  5.  
  6. #include "os.h"
  7.  
  8. #include "SDL/SDL.h"
  9. #include "math.h"
  10.  
  11.  
  12. #ifndef PI
  13. #define PI  3.14159265358979323846
  14. #endif
  15.  
  16. #define VIDEOX SCREEN_WIDTH
  17. #define VIDEOY SCREEN_HEIGHT
  18.  
  19. #define TABLEX VIDEOX*2
  20. #define TABLEY VIDEOY*2
  21. #define TONES 256
  22.  
  23. int rands[512];
  24. int lastRandI = 0;
  25.  
  26. static void do_plasma(SDL_Surface*surface,
  27.            double x1,
  28.            double y1,
  29.            double x2,
  30.            double y2,
  31.            double x3,
  32.            double y3,
  33.            unsigned char*t)
  34. {
  35.   unsigned int
  36.     X1=x1*(TABLEX/2),
  37.     Y1=y1*(TABLEY/2),
  38.     X2=x2*(TABLEX/2),
  39.     Y2=y2*(TABLEY/2),
  40.     X3=x3*(TABLEX/2),
  41.     Y3=y3*(TABLEY/2);
  42.   unsigned int y;
  43.   unsigned char *t1=t+X1+Y1*TABLEX, *t2=t+X2+Y2*TABLEX, *t3=t+X3+Y3*TABLEX;
  44.   for (y=0; y<VIDEOY; y++) {
  45.     unsigned x;
  46.     unsigned char*tmp= ((Uint8*)surface->pixels) + y * surface->pitch;
  47.     unsigned int t=y*TABLEX, tmax=t+VIDEOX;
  48.     for (x=0; t<tmax; t++, tmp++) {
  49.       tmp[0]=t1[t]+t2[t]+t3[t];
  50.     }
  51.   }
  52. }
  53.  
  54. int main ()
  55. {
  56.  
  57.  
  58.  
  59.   Uint32 video_flags;
  60.   double r[3];
  61.   double R[6];
  62.   SDL_Surface *screen;
  63.   SDL_Color colors[TONES*2];
  64.   unsigned char *t;
  65.   time_t starttime;
  66.   int i, state=0;
  67.  
  68.  
  69.  
  70.  
  71.  
  72.   t = (unsigned char *)malloc(TABLEY*TABLEX);
  73.   if ( t == NULL ) {
  74.     printf("Out of memory\n");
  75.     exit(1);
  76.   }
  77.  
  78.     startcrand(SDL_GetTicks(),9999999);
  79.     printf("Precalculating table...\n");
  80.       {
  81.     int c;
  82.     for (c=0; c<3; c++)
  83.       r[c]=((double)(crand()%1000+1))/300000;
  84.     for (c=0; c<6; c++)
  85.       R[c]=((double)(crand()%1000+1))/5000;
  86.   }
  87.  
  88.  
  89.   {
  90.     int y;
  91.     for (y=0 ; y<TABLEY ; y++) {
  92.       int x;
  93.       for (x=0 ; x<TABLEX ; x++) {
  94.     double tmp=(((double)((x-(TABLEX/2))*(x-(TABLEX/2))+(y-(TABLEX/2))*(y-(TABLEX/2))))
  95.             *(PI/(TABLEX*TABLEX+TABLEY*TABLEY)));
  96.     t[y*TABLEX+x]=(sin(sqrt(tmp)*12)+1)*TONES/6;
  97.       }
  98.     }
  99.   }
  100.  
  101.   /* Inizialize the SDL library */
  102.   if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
  103.     fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
  104.     return 1;
  105.   }
  106.  
  107.   //atexit(SDL_Quit);   // <---- This doesn't work
  108.  
  109.  
  110.   screen=SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, NSP_BPP, SDL_SWSURFACE);
  111.   if ( screen == NULL ) {
  112.     fprintf(stderr, "Couldn't initialize video mode: %s\n", SDL_GetError());
  113.     return 1;
  114.   }
  115.  
  116.  
  117.   /* Main loop */
  118.   while (SDL_PollEvent(NULL)==0) {
  119.  
  120.     state++;
  121.     {
  122.       int c;
  123.      
  124.       int re,ge,be;
  125.      
  126.      /* for (c=0 ; c<TONES; c++) {
  127.         re=(sin(((double)c)/TONES*6*PI+r[0]*PI*state*PI)+1)*127;
  128.         ge=(sin(((double)c)/TONES*6*PI+r[1]*state*PI)+1)*127;
  129.         be=(sin(((double)c)/TONES*6*PI+r[2]*state*PI)+1)*127;
  130.         //colors[c] = SDL_MapRGB(screen->format, (Uint8)re, (Uint8)ge, (Uint8)be);
  131.       }
  132.       SDL_SetColors(screen, colors, 0, TONES); */ //    <---- doensn't work on 16-bit
  133.     }
  134.     if (SDL_LockSurface(screen) <0 ) {
  135.       continue;
  136.     }
  137.    
  138.     do_plasma(screen,
  139.     (sin(((double)state)*R[0])+1)/2,
  140.     (sin(((double)state)*R[1])+1)/2,
  141.     (sin(((double)state)*R[2])+1)/2,
  142.     (sin(((double)state)*R[3])+1)/2,
  143.     (sin(((double)state)*R[4])+1)/2,
  144.     (sin(((double)state)*R[5])+1)/2, t);
  145.     SDL_UnlockSurface(screen);
  146.     SDL_Flip(screen);
  147.   }
  148.  
  149.   return 0;
  150. }
  151.  
  152.  
  153. //RAND functions
  154.  
  155.  
  156. void startcrand(int seed, int max){
  157.  
  158.     printf("Starting pseudorandom number generator...\n");
  159.    
  160.    
  161.     if(seed>max){
  162.         printf("Oops: seed is bigger than max\n");
  163.         exit(-1);
  164.         }
  165.    
  166.     if(seed == 0){
  167.         printf("Oops: seed is 0 - setting it to 1337\n");
  168.         seed = 1337;
  169.     }
  170.        
  171.     printf("Seed is ");
  172.     printInt(seed);
  173.     printf("\n");
  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.  
  196. int crand(){
  197.  
  198.     if(lastRandI > 490)
  199.         lastRandI = 2;
  200.    
  201.     lastRandI++;
  202.    
  203.    
  204.     return rands[lastRandI];
  205. }
  206.  
  207. void printInt(int n){
  208.  
  209.     char c[128];
  210.     sprintf(c, "%d", n);
  211.  
  212.     const char* ccharAst;
  213.     char* charAst;
  214.    
  215.     strcpy(charAst, c);
  216.     ccharAst = charAst;
  217.  
  218.     printf(ccharAst);
  219. }
Add Comment
Please, Sign In to add comment