2014-06-30 09:10:35 +00:00
|
|
|
/*****************************************************************************\
|
|
|
|
** mandelbrot sample program for cc65. **
|
|
|
|
** **
|
|
|
|
** (w) 2002 by groepaz/hitmen, TGI support by Stefan Haubenthal **
|
|
|
|
\*****************************************************************************/
|
2004-10-08 20:10:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <conio.h>
|
|
|
|
#include <tgi.h>
|
2016-06-18 21:35:57 +00:00
|
|
|
#include <cc65.h>
|
2004-10-08 20:10:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Graphics definitions */
|
2013-05-09 11:56:54 +00:00
|
|
|
#define SCREEN_X (tgi_getxres())
|
|
|
|
#define SCREEN_Y (tgi_getyres())
|
|
|
|
#define MAXCOL (tgi_getcolorcount())
|
2004-10-08 20:10:58 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
#define maxiterations 32
|
|
|
|
#define fpshift (10)
|
|
|
|
#define tofp(_x) ((_x)<<fpshift)
|
|
|
|
#define fromfp(_x) ((_x)>>fpshift)
|
|
|
|
#define fpabs(_x) (abs(_x))
|
2004-10-08 20:10:58 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
#define mulfp(_a,_b) ((((signed long)_a)*(_b))>>fpshift)
|
|
|
|
#define divfp(_a,_b) ((((signed long)_a)<<fpshift)/(_b))
|
2004-10-08 20:10:58 +00:00
|
|
|
|
2005-11-20 17:49:44 +00:00
|
|
|
/* Workaround missing clock stuff */
|
2013-06-17 19:34:08 +00:00
|
|
|
#ifdef __APPLE2__
|
2013-05-09 11:56:54 +00:00
|
|
|
# define clock() 0
|
2020-10-25 20:33:08 +00:00
|
|
|
# undef CLK_TCK
|
2013-05-09 11:56:54 +00:00
|
|
|
# define CLK_TCK 1
|
2005-11-20 17:49:44 +00:00
|
|
|
#endif
|
|
|
|
|
2012-11-04 22:01:51 +00:00
|
|
|
/* Use dynamically loaded driver by default */
|
|
|
|
#ifndef DYN_DRV
|
2013-05-09 11:56:54 +00:00
|
|
|
# define DYN_DRV 1
|
2012-11-04 22:01:51 +00:00
|
|
|
#endif
|
|
|
|
|
2004-10-08 20:10:58 +00:00
|
|
|
/* Use static local variables for speed */
|
2009-10-14 12:55:15 +00:00
|
|
|
#pragma static-locals (1);
|
2004-10-08 20:10:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void mandelbrot (signed short x1, signed short y1, signed short x2,
|
2012-11-04 22:01:51 +00:00
|
|
|
signed short y2)
|
2004-10-08 20:10:58 +00:00
|
|
|
{
|
|
|
|
register unsigned char count;
|
|
|
|
register signed short r, r1, i;
|
|
|
|
register signed short xs, ys, xx, yy;
|
|
|
|
register signed short x, y;
|
2020-07-15 21:11:38 +00:00
|
|
|
register unsigned char maxcol = MAXCOL;
|
2004-10-08 20:10:58 +00:00
|
|
|
|
2016-06-18 21:35:57 +00:00
|
|
|
/* Calc stepwidth */
|
2004-10-08 20:10:58 +00:00
|
|
|
xs = ((x2 - x1) / (SCREEN_X));
|
|
|
|
ys = ((y2 - y1) / (SCREEN_Y));
|
|
|
|
|
|
|
|
yy = y1;
|
|
|
|
for (y = 0; y < (SCREEN_Y); y++) {
|
2012-11-04 22:01:51 +00:00
|
|
|
yy += ys;
|
|
|
|
xx = x1;
|
|
|
|
for (x = 0; x < (SCREEN_X); x++) {
|
|
|
|
xx += xs;
|
2016-06-18 21:35:57 +00:00
|
|
|
/* Do iterations */
|
2012-11-04 22:01:51 +00:00
|
|
|
r = 0;
|
|
|
|
i = 0;
|
|
|
|
for (count = 0; (count < maxiterations) &&
|
|
|
|
(fpabs (r) < tofp (2)) && (fpabs (i) < tofp (2));
|
|
|
|
++count) {
|
|
|
|
r1 = (mulfp (r, r) - mulfp (i, i)) + xx;
|
|
|
|
/* i = (mulfp(mulfp(r,i),tofp(2)))+yy; */
|
|
|
|
i = (((signed long) r * i) >> (fpshift - 1)) + yy;
|
|
|
|
r = r1;
|
|
|
|
}
|
|
|
|
if (count == maxiterations) {
|
|
|
|
tgi_setcolor (0);
|
|
|
|
} else {
|
2020-07-15 21:11:38 +00:00
|
|
|
switch (maxcol) {
|
|
|
|
case 2:
|
2012-11-04 22:01:51 +00:00
|
|
|
tgi_setcolor (1);
|
2020-07-15 21:11:38 +00:00
|
|
|
break;
|
|
|
|
case 0: /* 256 colors */
|
|
|
|
tgi_setcolor (count);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
tgi_setcolor (count % maxcol);
|
2016-06-18 21:35:57 +00:00
|
|
|
}
|
2012-11-04 22:01:51 +00:00
|
|
|
}
|
2016-06-18 21:35:57 +00:00
|
|
|
/* Set pixel */
|
2012-11-04 22:01:51 +00:00
|
|
|
tgi_setpixel (x, y);
|
|
|
|
}
|
2004-10-08 20:10:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main (void)
|
|
|
|
{
|
|
|
|
clock_t t;
|
|
|
|
unsigned long sec;
|
|
|
|
unsigned sec10;
|
|
|
|
unsigned char err;
|
|
|
|
|
|
|
|
clrscr ();
|
|
|
|
|
2012-11-04 22:01:51 +00:00
|
|
|
#if DYN_DRV
|
2009-10-14 12:55:15 +00:00
|
|
|
/* Load the graphics driver */
|
2004-10-08 20:10:58 +00:00
|
|
|
cprintf ("initializing... mompls\r\n");
|
2009-10-03 11:10:26 +00:00
|
|
|
tgi_load_driver (tgi_stddrv);
|
2012-11-04 22:01:51 +00:00
|
|
|
#else
|
|
|
|
/* Install the graphics driver */
|
|
|
|
tgi_install (tgi_static_stddrv);
|
|
|
|
#endif
|
2004-10-08 20:10:58 +00:00
|
|
|
err = tgi_geterror ();
|
|
|
|
if (err != TGI_ERR_OK) {
|
2012-11-04 22:01:51 +00:00
|
|
|
cprintf ("Error #%d initializing graphics.\r\n%s\r\n",
|
|
|
|
err, tgi_geterrormsg (err));
|
2016-06-18 21:35:57 +00:00
|
|
|
if (doesclrscrafterexit ()) {
|
|
|
|
cgetc ();
|
|
|
|
}
|
2012-11-04 22:01:51 +00:00
|
|
|
exit (EXIT_FAILURE);
|
2004-10-08 20:10:58 +00:00
|
|
|
};
|
|
|
|
cprintf ("ok.\n\r");
|
|
|
|
|
|
|
|
/* Initialize graphics */
|
|
|
|
tgi_init ();
|
|
|
|
tgi_clear ();
|
|
|
|
|
|
|
|
t = clock ();
|
|
|
|
|
2016-06-18 21:35:57 +00:00
|
|
|
/* Calc mandelbrot set */
|
2004-10-08 20:10:58 +00:00
|
|
|
mandelbrot (tofp (-2), tofp (-2), tofp (2), tofp (2));
|
|
|
|
|
|
|
|
t = clock () - t;
|
|
|
|
|
|
|
|
/* Fetch the character from the keyboard buffer and discard it */
|
2016-06-18 21:35:57 +00:00
|
|
|
cgetc ();
|
2004-10-08 20:10:58 +00:00
|
|
|
|
2016-06-18 21:35:57 +00:00
|
|
|
/* Shut down gfx mode and return to textmode */
|
2004-10-08 20:10:58 +00:00
|
|
|
tgi_done ();
|
|
|
|
|
|
|
|
/* Calculate stats */
|
|
|
|
sec = (t * 10) / CLK_TCK;
|
|
|
|
sec10 = sec % 10;
|
|
|
|
sec /= 10;
|
|
|
|
|
|
|
|
/* Output stats */
|
|
|
|
cprintf ("time : %lu.%us\n\r", sec, sec10);
|
|
|
|
|
2016-06-18 21:35:57 +00:00
|
|
|
if (doesclrscrafterexit ()) {
|
|
|
|
/* Wait for a key, then end */
|
|
|
|
cputs ("Press any key when done...\n\r");
|
|
|
|
cgetc ();
|
|
|
|
}
|
2004-10-08 20:10:58 +00:00
|
|
|
|
|
|
|
/* Done */
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|