toaruos/game/julia.c

152 lines
2.6 KiB
C
Raw Normal View History

/*
* Julia Fractal Generator
*/
#include <stdio.h>
#include <stdint.h>
#include <syscall.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
DEFN_SYSCALL0(getgraphicsaddress, 11);
DEFN_SYSCALL1(kbd_mode, 12, int);
DEFN_SYSCALL0(kbd_get, 13);
DEFN_SYSCALL1(setgraphicsoffset, 16, int);
#define GFX_W 1024
#define GFX_H 768
#define GFX_B 4
#define GFX(x,y) gfx_mem[GFX_W * (y) + (x)]
#define SPRITE(sprite,x,y) sprite->bitmap[sprite->width * (y) + (x)]
uint32_t * gfx_mem;
uint32_t gfx_size = GFX_B * GFX_H * GFX_W;
float conx = -0.74;
float cony = 0.1;
float Maxx = 2;
float Minx = -2;
float Maxy = 1;
float Miny = -1;
float initer = 1000;
float pixcorx;
float pixcory;
int newcolor;
int lastcolor;
int colors[] = {
0xeec73e,
0xf0a513,
0xfb8b00,
0xf44800,
0xffff99,
0xffff00,
0xfdca01,
0x986601,
0xf44800,
0xfd3301,
0xd40000,
0x980101,
};
void julia(int xpt, int ypt) {
long double x = xpt * pixcorx + Minx;
long double y = Maxy - ypt * pixcory;
long double xnew = 0;
long double ynew = 0;
int k = 0;
for (k = 0; k <= initer; k++) {
xnew = x * x - y * y + conx;
ynew = 2 * x * y + cony;
x = xnew;
y = ynew;
if ((x * x + y * y) > 4)
break;
}
int color = k;
if (color > 11) color = color % 12;
if (k >= initer)
GFX(xpt, ypt) = 0;
else
GFX(xpt, ypt) = colors[color];
newcolor = color;
}
int main(int argc, char ** argv) {
gfx_mem = (void *)syscall_getgraphicsaddress();
/* Read some arguments */
if (argc > 1) {
optind = 0;
}
int index, c;
while ((c = getopt(argc, argv, "i:x:X:y:Y:c:C:")) != -1) {
switch (c) {
case 'i':
initer = atof(optarg);
break;
case 'x':
Minx = atof(optarg);
break;
case 'X':
Maxx = atof(optarg);
break;
case 'y':
Miny = atof(optarg);
break;
case 'Y':
Maxy = atof(optarg);
break;
case 'c':
conx = atof(optarg);
break;
case 'C':
cony = atof(optarg);
break;
default:
break;
}
}
printf("initer: %f\n", initer);
printf("X: %f %f\n", Minx, Maxx);
printf("Y: %f %f\n", Miny, Maxy);
printf("conx: %f cony: %f\n", conx, cony);
printf("\033[J\n");
syscall_kbd_mode(1);
pixcorx = (Maxx - Minx) / GFX_W;
pixcory = (Maxy - Miny) / GFX_H;
int j = 0;
do {
int i = 1;
do {
julia(i,j);
if (lastcolor != newcolor) julia(i-1,j);
else if (i > 0) GFX(i-1,j) = colors[lastcolor];
newcolor = lastcolor;
i+= 2;
} while ( i < GFX_W );
++j;
} while ( j < GFX_H );
while (1) {
char ch = 0;
if ((ch = syscall_kbd_get())) {
if (ch == 16) {
break;
}
}
}
syscall_kbd_mode(0);
return 0;
}