Guest User

Untitled

a guest
Jan 16th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.43 KB | None | 0 0
  1. #include<SDL/SDL.h>
  2. #include<iostream>
  3. #include<math.h>
  4.  
  5. void putpixel(SDL_Surface* surface, int x, int y, Uint32 pixel);
  6. void draw_line(SDL_Surface* scr, int x, int y, int x2, int y2, Uint32 color);
  7. bool user_quit(SDL_Event &event);
  8. double mandelbrot_percent(double x, double y, double offset_x, double offset_y,
  9.               int iterations, double zoom);
  10.  
  11. int main (int argc, char** argv)
  12. {
  13.     // grab commandline args for height and width
  14.     if(argc < 3)
  15.     {
  16.     std::cout << "You did not specify width then height" << std::endl;
  17.     return 1;
  18.     }
  19.  
  20.     int SCREEN_WIDTH = atoi(argv[1]);
  21.     int SCREEN_HEIGHT = atoi(argv[2]);
  22.  
  23.     // initialize SDL video
  24.     if ( SDL_Init( SDL_INIT_VIDEO ) < 0 )
  25.     {
  26.         std::cout << "Unable to init SDL" <<  SDL_GetError() << "\n" << std::endl;
  27.         return 1;
  28.     }
  29.  
  30.     // make sure SDL cleans up before exit
  31.     atexit(SDL_Quit);
  32.  
  33.     // create a new window
  34.     SDL_Surface* screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 16,
  35.                                            SDL_HWSURFACE|SDL_DOUBLEBUF);
  36.    
  37.     if(!screen)
  38.     {
  39.         printf("Unable to set video mode.\n", SDL_GetError());
  40.         return 1;
  41.     }
  42.  
  43.     // generate the 'brot
  44.      SDL_LockSurface(screen);
  45.      for(int y = 0; y < SCREEN_HEIGHT; ++y)
  46.      {
  47.      for(int x = 0; x < SCREEN_WIDTH; ++x)
  48.      {
  49.          double p = mandelbrot_percent(x, y,  SCREEN_WIDTH/2.0,
  50.                        SCREEN_HEIGHT/2.0, 100, SCREEN_HEIGHT/3.14);
  51.          if( p == -1) // -1 means within the set
  52.          {
  53.          putpixel(screen, x, y,0);
  54.          }
  55.          else
  56.          {
  57.  
  58.          // TODO ensure that p min and p max are accounted for with gradient
  59.          // sometimes the lowerbound of p is never 0 while the upperbound is
  60.          // never the full iterations, this should be fixed later.
  61.          // Ideally a grid should be populated with P values, and the upper
  62.          // and lower should be noted. The gradient band currently assumes
  63.          // that the lowerbound is 0 and upper is iteration max.
  64.          
  65.  
  66.          // use sin interpolation to make the bands stretch a bit more
  67.          p = sin(p/100 * 3.1416); // this is set for 100 iterations per pixel
  68.                      // match it with the iterations passed into mandelbrot_percent
  69.          
  70.  
  71.          // define two mixing colors for area outside
  72.          Uint8 r1,r2,g1,g2,b1,b2;
  73.          r1 = 64;
  74.          g1 = 0;
  75.          b1 = 64;
  76.          r2 = 0;
  77.          g2 = 255;
  78.          b2 = 0;
  79.  
  80.          // TODO make the gradient fit the upper and lower bounds for the values
  81.          r1 += (r2 - r1) * p;
  82.          g1 += (g2 - g1) * p;
  83.          b1 += (b2 - b1) * p;
  84.          putpixel(screen, x, y,SDL_MapRGB(screen->format,r1, g1, b1));
  85.          
  86.          }
  87.      }
  88.      }
  89.      SDL_UnlockSurface(screen);
  90.        
  91.     SDL_Event event;
  92.     while(user_quit(event) == false)
  93.     {
  94.         SDL_Flip(screen);
  95.     }
  96.     return 0;
  97. }
  98.  
  99. bool user_quit(SDL_Event &event)
  100. {
  101.     SDL_PollEvent(&event);
  102.     switch (event.type)
  103.     {
  104.     case SDL_QUIT:
  105.         return  true;
  106.         break;
  107.  
  108.     case SDL_KEYDOWN:
  109.     {
  110.         if (event.key.keysym.sym == SDLK_ESCAPE)
  111.             return  true;
  112.         break;
  113.     }
  114.     default:
  115.     return false;
  116.     }
  117.    
  118. }
  119.  
  120. void putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel)
  121. {
  122.     int bpp = surface->format->BytesPerPixel;
  123.     /* Here p is the address to the pixel we want to set */
  124.     Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
  125.  
  126.     switch(bpp)
  127.     {
  128.     case 1:
  129.         *p = pixel;
  130.         break;
  131.  
  132.     case 2:
  133.         *(Uint16 *)p = pixel;
  134.         break;
  135.  
  136.     case 3:
  137.         if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
  138.         {
  139.             p[0] = (pixel >> 16) & 0xff;
  140.             p[1] = (pixel >> 8) & 0xff;
  141.             p[2] = pixel & 0xff;
  142.         }
  143.         else
  144.         {
  145.             p[0] = pixel & 0xff;
  146.             p[1] = (pixel >> 8) & 0xff;
  147.             p[2] = (pixel >> 16) & 0xff;
  148.         }
  149.         break;
  150.  
  151.     case 4:
  152.         *(Uint32 *)p = pixel;
  153.         break;
  154.     }
  155. }
  156.  
  157. double mandelbrot_percent(double x, double y, double offset_x,
  158.               double offset_y, int iterations, double zoom)
  159. {
  160.     // this function returns -1 if the value is inside the set
  161.     // otherwise returns a fraction if outside
  162.     x -= offset_x;
  163.     y -= offset_y;
  164.     x /= zoom;
  165.     y /= zoom;
  166.     double r = x;
  167.     double s = y;
  168.     double old_r = r;
  169.     double old_s = s;
  170.     // If the position survives this iteration loop it is "inside"
  171.     for(int i = 0; i < iterations + 1; ++i)
  172.     {
  173.     if(r*r + s*s > 4)
  174.         return i; // escaped the set, color it!
  175.     else
  176.     {
  177.         r = (old_r * old_r - old_s * old_s) + x;
  178.         s = 2 * old_r * old_s + y;
  179.         old_r = r;
  180.         old_s = s;
  181.     }
  182.     }
  183.     return -1; // in the set, return -1
  184. }
Add Comment
Please, Sign In to add comment