1
0
mirror of https://github.com/sehugg/8bitworkshop.git synced 2024-11-29 14:51:17 +00:00
8bitworkshop/presets/c64/mandel.c

96 lines
2.5 KiB
C
Raw Normal View History

2020-04-10 02:35:38 +00:00
/*****************************************************************************\
** mandelbrot sample program for cc65. **
** **
** (w) 2002 by groepaz/hitmen, TGI support by Stefan Haubenthal **
\*****************************************************************************/
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#include <c64.h>
#include <cc65.h>
#include "common.h"
2022-08-11 20:27:20 +00:00
//#link "mcbitmap.c"
#include "mcbitmap.h"
2020-04-10 02:35:38 +00:00
/* Graphics definitions */
#define SCREEN_X 160
#define SCREEN_Y 192
#define MAXCOL 16
2022-08-11 20:27:20 +00:00
#define maxiterations 64
2020-04-10 02:35:38 +00:00
#define fpshift (10)
2022-08-11 20:27:20 +00:00
#define fpone (1<<fpshift)
2020-04-10 02:35:38 +00:00
#define tofp(_x) ((_x)<<fpshift)
#define fromfp(_x) ((_x)>>fpshift)
#define fpabs(_x) (abs(_x))
#define mulfp(_a,_b) ((((signed long)_a)*(_b))>>fpshift)
#define divfp(_a,_b) ((((signed long)_a)<<fpshift)/(_b))
const byte COLORS[MAXCOL] = {
0, 11, 12, 15, 1,
2, 9, 4, 8, 10, 7,
6, 14, 3,
5, 13,
};
void mandelbrot (signed short x1, signed short y1, signed short x2,
signed short y2)
{
unsigned char count;
signed short r, r1, i;
signed short xs, ys, xx, yy;
signed short x, y;
byte color;
/* Calc stepwidth */
xs = ((x2 - x1) / (SCREEN_X));
ys = ((y2 - y1) / (SCREEN_Y));
yy = y1;
for (y = 3; y < (SCREEN_Y); y++) {
yy += ys;
xx = x1;
for (x = 0; x < (SCREEN_X); x++) {
xx += xs;
/* Do iterations */
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) {
color = (0);
} else {
2022-08-11 20:27:20 +00:00
color = count < MAXCOL
? COLORS[count]
: COLORS[MAXCOL-1];
set_pixel(x, y, color);
2020-04-10 02:35:38 +00:00
}
}
}
}
int main (void)
{
setup_bitmap_multi();
VIC.bgcolor0 = 0x00;
/* Calc mandelbrot set */
2022-08-11 20:27:20 +00:00
mandelbrot (-fpone/2, -fpone, 0, -fpone/2);
2020-04-10 02:35:38 +00:00
/* Fetch the character from the keyboard buffer and discard it */
cgetc ();
2022-08-11 20:27:20 +00:00
2020-04-10 02:35:38 +00:00
/* Done */
return EXIT_SUCCESS;
}