PDA

View Full Version : Quick and Dirty MandelBrot Generator


Mike Halliday
10-02-2007, 10:49 PM
Having put my DB tests to one side for the moment, I thought I would have a go at some graphics.

NOTE: This is very quick and extremely dirty code but illustrates the process for creating a mandelbrot.

Please feel free to change it and re-post the code.

// Code start //
#include "gameapi.ppl"

func mainproc(hWnd$, Msg$, wParam$, lParam$)
ok$ = true;

case (Msg$)
WM_CLOSE:
// Shutdown game api.
ShutGameAPI(hWnd$);

WM_KEYUP:

if (wParam$ == keys.vkB$)
if (KeysPressed$[keys.vkB$])
FirstTime$ = not FirstTime$;
end;
end;
KeysPressed$[wParam$] = 0;
end;
return (ok$);
end;

func GameProc(hWnd$, Msg$, wParam$, lParam$)

case (Msg$)
WM_PAINT:
// Clear screen with black.
G_Clear(0);

// Each Pixel Down the screen
for (Y$,0,239)

Ic$ = Y$ * (1 / YSize$) + YAxis$;

// Each Pixel Across the screen
for (X$,0,319)

Rc$ = X$ * (1 / XSize$) + XAxis$;
Rz$ = 0;
Iz$ = 0;

Count$ = 0;

Repeat

//Calculate the Pixel position
Tempz$ = (Rz$ * Rz$) - (Iz$ * Iz$) + Rc$;
Iz$ = 2 * (Rz$ * Iz$) + Ic$;
Rz$ = Tempz$;
Count$ = Count$ + 1;

// Find the square root
Size$ = SQRT((Rz$ * Rz$) + (Iz$ * Iz$));

Until (Count$ > Ac$ or Size$ > 2);

// Plot the pixels on the display

// This line shows the Mandelbrot set in static - to show how it changes shape
// g_setPixel(X$, Y$, Count$ * Random(255));

g_SETPIXEL(X$, Y$, Count$ MOD (Colours$ - 1) + 1);

end;

end;

WM_TIMER:

// If A key is pressed, close the active window.
if (KeysPressed$[keys.vkA$])
PostMessage(hWnd$, WM_CLOSE, 0, 0);
end;

end;
return (true);
end;



func WinMain

global(FirstTime$);
global(Ac$,XAxis$,YAxis$,XSize$,YSize$);
global(Colours$);

local (h$);
FirstTime$ = true;

// Iterations/resolution
//
// Higher number - better calulations - much longer time!
Ac$ = 100;

// Number of colours to use
// Lower the number the darker the mandelbrot
Colours$ = 100;

// Size of the set
XAxis$ = -2.5;
YAxis$ = -1.5;

// Zoom in the screen
XSize$ = 320/4;
YSize$ = 240/4;

// Create form for the gameapi display.
h$ = newform({GAPI}, {GAPIClass}, &mainproc);
ShowWindow(h$, SW_SHOW);

// Initialize game api.
InitGameAPIEx(h$, &GameProc, 240, 320, false, 5, 0);

return (true);
end;
//Code End//

I think a ZOOM would be nice and maybe some more colour?

Cheers

Mike

(I am working on other graphics stuff at the moment as I get to grips with PPL and the GAMEAPI - so watch this space)

kornalius
10-03-2007, 12:39 PM
Thank you very much for your contribution. Would you mind putting this code file into a .zip file and attach it to your thread. It would certainly be easier for the users to run the file directly than trying to copy the text from the forums.