Share Pastebin
Guest
Public paste!

Untitled

By: a guest | Mar 22nd, 2010 | Syntax: C | Size: 3.54 KB | Hits: 24 | Expires: Never
Copy text to clipboard
  1. //////////////////////////////////////////////////////////////
  2. // lite-C Mandelbrot test program
  3. //////////////////////////////////////////////////////////////
  4. // There are two versions of this program:
  5. // one for the conventional Windows API (mandelbrot_legacy)
  6. // and one for the engine API (mandelbrot_pure)
  7. //////////////////////////////////////////////////////////////
  8. #include <acknex.h> // pure mode
  9. #include <default.c>
  10.  
  11. /////////////////////////////////////////////////////////////////////
  12. // Define an image panel, a click sound, a font, and a message
  13. PANEL* pMandel = { layer = 0; }
  14. SOUND* sClick = "tap.wav";
  15. FONT* fArial = "Arial #20bi";
  16. TEXT* tCalculate = {
  17.   pos_x = 5; pos_y = 5; layer = 2;
  18.   font = fArial; red = 255; green = 0; blue = 0;
  19.   flags = SHADOW;
  20.   string = "Calculate...";
  21. }
  22.  
  23. /////////////////////////////////////////////////////////////////////
  24. // Draw a Mandelbrot fractal.
  25. long JetColor(double v)
  26. {
  27.     double d = 25.0;
  28.     v = v/d + 0.5;
  29.     int i = (int)v;
  30.     int f = (int)(255*(v-i));
  31.     int r=0, g=0, b=0;
  32.  
  33.     switch(i){
  34.         case 0: r=0;    g=0;    b=f;    break;
  35.         case 1: r=0;    g=f;    b=255;  break;
  36.         case 2: r=f;    g=255;  b=255-f;break;
  37.         case 3: r=255;  g=255-f;b=0;    break;
  38.         case 4: r=255-f;g=0;    b=0;    break;
  39.     }
  40.     return (255<<24)|(r<<16)|(g<<8)|b;
  41. }
  42.  
  43. double m_x = 0.344142,
  44.     m_y = 0.075094,
  45.     m_width = 0.017813;
  46.  
  47.  
  48. void Draw(BMAP* bmap)
  49. {
  50.     long w = (long)bmap_width(bmap);    // convert var to long
  51.     long h = (long)bmap_height(bmap);
  52.     long i,j;
  53.     long times,inset;
  54.     double x,y,zx,zy,zxs,zys;
  55.     long detail=100;
  56.  
  57.     set(tCalculate,SHOW);
  58.     wait(2);    // 2 frames until text is flipped to the foreground
  59.     bmap_lock(bmap,0);
  60.     for(i=0;i<w;i++)
  61.     {
  62.         for(j=0;j<h;j++)
  63.         {
  64.             x = m_x+((double)i)*m_width/w;
  65.             y = m_y+((double)(h-j))*m_width/w;
  66.             zx = 0;
  67.             zy = 0;
  68.             inset = 1;
  69.             times = 0;
  70.             while(inset && times<detail)
  71.             {
  72.                 times++;
  73.                 zxs = zx*zx;
  74.                 zys = zy*zy;
  75.                 zy = 2*zx*zy+y;
  76.                 zx = zxs-zys+x;
  77.                 if (zxs+zys >= 4.0) inset=0;
  78.             }
  79.             if(inset)
  80.                 pixel_to_bmap(bmap,i,j,(void*)(255<<24));
  81.             else
  82.                 pixel_to_bmap(bmap,i,j,(void*)JetColor(times));  // prevent a long/var conversion by casting it to a pointer
  83.         }
  84.     }
  85.     bmap_unlock(bmap);
  86.     reset(tCalculate,SHOW);
  87. }
  88.  
  89. ////////////////////////////////////////////////////////////////
  90. // Left mouse button clicked somewhere on the image.
  91. // Zoom in by 0.5, calculate a new image position, and draw.
  92. void mandel_click(void)
  93. {
  94.     double a;
  95.     a = mouse_cursor.x; a /= screen_size.x; m_x += m_width*(a-0.5);
  96.     a = mouse_cursor.y; a /= screen_size.y; m_y += m_width*(0.5-a);
  97.     m_width *= 0.5;
  98.     snd_play(sClick,100,0);
  99.     Draw(pMandel.bmap);
  100. }
  101.  
  102. void main()
  103. {
  104. // don't draw before the D3D device is created in the first frame
  105.     wait(1);    
  106. // Create a black 32 bit bitmap, screen sized, for the panel background.
  107. // That will be the bitmap we're drawing on.
  108. // Note that lite-C allows both '.' and '->' for pointer elements
  109.     pMandel.bmap = bmap_createblack(screen_size.x,screen_size.y,8888);
  110. // make the panel and text visible on screen
  111.     set(pMandel,SHOW);
  112. // Now we can draw    
  113.     Draw(pMandel.bmap);
  114. // Set a mouse event for redrawing the image
  115.     on_mouse_left = mandel_click;
  116. }