mode7: update fixed-point starfield

This commit is contained in:
Vince Weaver 2017-12-28 23:27:31 -05:00
parent 96cd7ac131
commit 3b53300ca2
3 changed files with 318 additions and 19 deletions

View File

@ -5,7 +5,7 @@ LFLAGS = -lm
SDL_LIBS= `sdl-config --libs`
SDL_INCLUDE= `sdl-config --cflags`
all: fade fixed_point rainbow sparkle starfield kaleido \
all: fade fixed_point rainbow sparkle starfield starfield_fixed kaleido \
tfv mode7_demo text tfv_multiply
@ -163,12 +163,18 @@ starfield: starfield.o gr-sim.o
starfield.o: starfield.c
$(CC) $(CFLAGS) -c starfield.c
starfield_fixed: starfield_fixed.o gr-sim.o
$(CC) $(LFLAGS) $(SDL_LIBS) -o starfield_fixed starfield_fixed.o gr-sim.o
starfield_fixed.o: starfield_fixed.c
$(CC) $(CFLAGS) -c starfield_fixed.c
gr-sim.o: gr-sim.c gr-sim.h apple2_font.h
$(CC) $(CFLAGS) $(SDL_INCLUDE) -c gr-sim.c
clean:
rm -f *~ *.o gr-sim rainbow sparkle starfield kaleido \
rm -f *~ *.o gr-sim rainbow sparkle starfield starfield_fixed kaleido \
tfv text mode7_demo fade fixed_point tfv_multiply

View File

@ -32,10 +32,12 @@ static void double_to_fixed(double d, struct fixed_type *f) {
}
#if 0
static void print_fixed(struct fixed_type *f) {
printf("%02X.%02X",f->i,f->f);
}
#endif
static void fixed_add(struct fixed_type *x, struct fixed_type *y, struct fixed_type *z) {
int carry;
@ -66,29 +68,73 @@ static double fixed_to_double(struct fixed_type *f) {
static struct star_type stars[NUMSTARS];
static int random_table[256];
static int random_pointer=0;
static void random_star(int i) {
/* -128 to 128 */
/* Should we xor? */
stars[i].x.i=random_table[random_pointer++];
if (random_pointer>255) random_pointer=0;
stars[i].x.f=random_table[random_pointer++];
if (random_pointer>255) random_pointer=0;
stars[i].y.i=random_table[random_pointer++];
if (random_pointer>255) random_pointer=0;
stars[i].y.f=random_table[random_pointer++];
if (random_pointer>255) random_pointer=0;
// double_to_fixed( (drand48()-0.5)*spreadx,&stars[i].x);
// double_to_fixed( (drand48()-0.5)*spready,&stars[i].y);
/* 0.1 to 16 */
stars[i].z.i=random_table[random_pointer++]/16;
if (random_pointer>255) random_pointer=0;
stars[i].z.f=0x1;
// double_to_fixed( ((drand48())*spreadz)+0.1,&stars[i].z);
// print_fixed(&stars[i].x);
// printf(",");
// print_fixed(&stars[i].y);
// printf(",");
// print_fixed(&stars[i].z);
// printf("\n");
// double_to_fixed((drand48()-0.5)*spreadx,&stars[i].x);
// double_to_fixed((drand48()-0.5)*spready,&stars[i].y);
// stars[i].z.i=spreadz;
}
int main(int argc, char **argv) {
int ch,i;
int spreadx=256;
int spready=256;
// int spreadx=256;
// int spready=256;
int spreadz=16;
struct fixed_type speedz;
for(i=0;i<256;i++) {
random_table[i]=rand()%256;
printf("%d\n",random_table[i]);
}
double_to_fixed(-0.25,&speedz);
grsim_init();
/* Should NUMSTARS be prime to help with randomness */
for(i=0;i<NUMSTARS;i++) {
double_to_fixed( (drand48()-0.5)*spreadx,&stars[i].x);
double_to_fixed( (drand48()-0.5)*spready,&stars[i].y);
double_to_fixed( ((drand48())*spreadz)+0.1,&stars[i].z);
print_fixed(&stars[i].x);
printf(",");
print_fixed(&stars[i].y);
printf(",");
print_fixed(&stars[i].z);
printf("\n");
random_star(i);
}
gr();
@ -121,12 +167,7 @@ int main(int argc, char **argv) {
if ((tempx<0) || (tempy<0) || (tempx>=40) ||
(tempy>=40)) {
double_to_fixed((drand48()-0.5)*spreadx,
&stars[i].x);
double_to_fixed((drand48()-0.5)*spready,
&stars[i].y);
stars[i].z.i=spreadz;
random_star(i);
}
else {
color_equals(stars[i].color);

252
gr-sim/starfield_fixed.c Normal file
View File

@ -0,0 +1,252 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "gr-sim.h"
#define NUMSTARS 16
struct fixed_type {
char i;
unsigned char f;
};
struct star_type {
struct fixed_type x;
struct fixed_type y;
int z;
int color;
};
static void double_to_fixed(double d, struct fixed_type *f) {
int temp;
temp=d*256;
f->i=(temp>>8)&0xff;
f->f=temp&0xff;
}
#if 0
static void print_fixed(struct fixed_type *f) {
printf("%02X.%02X",f->i,f->f);
}
#endif
#if 0
static void fixed_add(struct fixed_type *x, struct fixed_type *y, struct fixed_type *z) {
int carry;
short sum;
sum=(short)(x->f)+(short)(y->f);
if (sum>=256) carry=1;
else carry=0;
z->f=sum&0xff;
z->i=x->i+y->i+carry;
}
#endif
#if 0
static double fixed_to_double(struct fixed_type *f) {
double d;
d=f->i;
d+=((double)(f->f))/256.0;
// printf("(%02x.%02x)=%lf\n",f->i,f->f,*d);
return d;
}
#endif
static struct star_type stars[NUMSTARS];
static int random_table[256];
static int random_pointer=0;
static int z_table[256];
static void random_star(int i) {
/* -128 to 128 */
/* Should we xor? */
stars[i].x.i=random_table[random_pointer++];
if (random_pointer>255) random_pointer=0;
stars[i].x.f=random_table[random_pointer++];
if (random_pointer>255) random_pointer=0;
stars[i].y.i=random_table[random_pointer++];
if (random_pointer>255) random_pointer=0;
stars[i].y.f=random_table[random_pointer++];
if (random_pointer>255) random_pointer=0;
// double_to_fixed( (drand48()-0.5)*spreadx,&stars[i].x);
// double_to_fixed( (drand48()-0.5)*spready,&stars[i].y);
/* 0 to 63 corresponding to */
stars[i].z=random_table[random_pointer++]&0x3f;
if (random_pointer>255) random_pointer=0;
if (stars[i].z>58) stars[i].z=0;
// double_to_fixed( ((drand48())*spreadz)+0.1,&stars[i].z);
// print_fixed(&stars[i].x);
// printf(",");
// print_fixed(&stars[i].y);
// printf(",");
// print_fixed(&stars[i].z);
// printf("\n");
// double_to_fixed((drand48()-0.5)*spreadx,&stars[i].x);
// double_to_fixed((drand48()-0.5)*spready,&stars[i].y);
// stars[i].z.i=spreadz;
}
static void fixed_mul(struct fixed_type *x, struct fixed_type *y, struct fixed_type *z) {
int a,b,c;
a=((x->i)<<8)+(x->f);
b=((y->i)<<8)+(y->f);
c=a*b;
// printf("%x %x %x\n",a,b,c);
c>>=8;
z->i=(c>>8);
z->f=(c&0xff);
}
int main(int argc, char **argv) {
int ch,i;
// int spreadx=256;
// int spready=256;
// int spreadz=16;
// struct fixed_type speedz;
for(i=0;i<256;i++) {
random_table[i]=rand()%256;
printf("%d,",random_table[i]);
}
printf("\n");
double g;
struct fixed_type gf;
i=0;
for(g=16.0;g>0;g-=0.25) {
double_to_fixed(1.0/g,&gf);
printf("%d %.2f: %.2f %2X %2X\n",i,g,1.0/g,
gf.i,gf.f);
z_table[i]=gf.f;
i++;
}
printf("\n");
// double_to_fixed(-0.25,&speedz);
grsim_init();
/* Should NUMSTARS be prime to help with randomness */
for(i=0;i<NUMSTARS;i++) {
random_star(i);
}
gr();
while(1) {
gr();
/* Set color */
for(i=0;i<NUMSTARS;i++) {
if (stars[i].z<16) stars[i].color=5;
else if (stars[i].z<32) stars[i].color=13;
else stars[i].color=15;
}
/* draw stars */
for(i=0;i<NUMSTARS;i++) {
// int tempx,tempy;
struct fixed_type dx,dy,temp;
temp.i=0;//z_table[stars[i].z];
if ((stars[i].z==60) || (stars[i].z==61)) {
temp.i=1;
}
if (stars[i].z==62) {
temp.i=2;
}
if (stars[i].z==63) {
temp.i=4;
}
temp.f=z_table[stars[i].z];
fixed_mul(&stars[i].x,&temp,&dx);
// dx=fixed_to_double(&stars[i].x)/
// fixed_to_double(&stars[i].z);
// tempx=dx+20;
dx.i+=20;
fixed_mul(&stars[i].y,&temp,&dy);
// dy=fixed_to_double(&stars[i].y)/
// fixed_to_double(&stars[i].z);
// tempy=dy+20;
dy.i+=20;
if ((dx.i<0) || (dy.i<0) || (dx.i>=40) ||
(dy.i>=40)) {
random_star(i);
}
else {
// if (stars[i].z>58) {
// printf("EEK! %2X.%2X\n",
// stars[i].z,stars[i].z);
// }
color_equals(stars[i].color);
basic_plot(dx.i,dy.i);
}
}
/* Move stars */
for(i=0;i<NUMSTARS;i++) {
// fixed_add(&stars[i].z,&speedz,&stars[i].z);
stars[i].z++;
if (stars[i].z>=64) random_star(i);
}
grsim_update();
ch=grsim_input();
if (ch=='q') exit(0);
usleep(10000);
}
return 0;
}