//////////////////////////////////////////////////////////////
// lite-C Mandelbrot test program
//////////////////////////////////////////////////////////////
// There are two versions of this program:
// one for the conventional Windows API (mandelbrot_legacy)
// and one for the engine API (mandelbrot_pure)
//////////////////////////////////////////////////////////////
#include <acknex.h> // pure mode
#include <default.c>
/////////////////////////////////////////////////////////////////////
// Define an image panel, a click sound, a font, and a message
PANEL* pMandel = { layer = 0; }
SOUND* sClick = "tap.wav";
FONT* fArial = "Arial #20bi";
TEXT* tCalculate = {
pos_x = 5; pos_y = 5; layer = 2;
font = fArial; red = 255; green = 0; blue = 0;
flags = SHADOW;
string = "Calculate...";
}
/////////////////////////////////////////////////////////////////////
// Draw a Mandelbrot fractal.
long JetColor(double v)
{
double d = 25.0;
v = v/d + 0.5;
int i = (int)v;
int f = (int)(255*(v-i));
int r=0, g=0, b=0;
switch(i){
case 0: r=0; g=0; b=f; break;
case 1: r=0; g=f; b=255; break;
case 2: r=f; g=255; b=255-f;break;
case 3: r=255; g=255-f;b=0; break;
case 4: r=255-f;g=0; b=0; break;
}
return (255<<24)|(r<<16)|(g<<8)|b;
}
double m_x = 0.344142,
m_y = 0.075094,
m_width = 0.017813;
void Draw(BMAP* bmap)
{
long w = (long)bmap_width(bmap); // convert var to long
long h = (long)bmap_height(bmap);
long i,j;
long times,inset;
double x,y,zx,zy,zxs,zys;
long detail=100;
set(tCalculate,SHOW);
wait(2); // 2 frames until text is flipped to the foreground
bmap_lock(bmap,0);
for(i=0;i<w;i++)
{
for(j=0;j<h;j++)
{
x = m_x+((double)i)*m_width/w;
y = m_y+((double)(h-j))*m_width/w;
zx = 0;
zy = 0;
inset = 1;
times = 0;
while(inset && times<detail)
{
times++;
zxs = zx*zx;
zys = zy*zy;
zy = 2*zx*zy+y;
zx = zxs-zys+x;
if (zxs+zys >= 4.0) inset=0;
}
if(inset)
pixel_to_bmap(bmap,i,j,(void*)(255<<24));
else
pixel_to_bmap(bmap,i,j,(void*)JetColor(times)); // prevent a long/var conversion by casting it to a pointer
}
}
bmap_unlock(bmap);
reset(tCalculate,SHOW);
}
////////////////////////////////////////////////////////////////
// Left mouse button clicked somewhere on the image.
// Zoom in by 0.5, calculate a new image position, and draw.
void mandel_click(void)
{
double a;
a = mouse_cursor.x; a /= screen_size.x; m_x += m_width*(a-0.5);
a = mouse_cursor.y; a /= screen_size.y; m_y += m_width*(0.5-a);
m_width *= 0.5;
snd_play(sClick,100,0);
Draw(pMandel.bmap);
}
void main()
{
// don't draw before the D3D device is created in the first frame
wait(1);
// Create a black 32 bit bitmap, screen sized, for the panel background.
// That will be the bitmap we're drawing on.
// Note that lite-C allows both '.' and '->' for pointer elements
pMandel.bmap = bmap_createblack(screen_size.x,screen_size.y,8888);
// make the panel and text visible on screen
set(pMandel,SHOW);
// Now we can draw
Draw(pMandel.bmap);
// Set a mouse event for redrawing the image
on_mouse_left = mandel_click;
}