tfv: starting to convert floating point to lookup tables

This commit is contained in:
Vince Weaver 2017-08-23 21:16:17 -04:00
parent 57ec050aae
commit ef64d2c4ba
2 changed files with 353 additions and 160 deletions

26
gr-sim/lookup_tables.c Normal file
View File

@ -0,0 +1,26 @@
#include <stdio.h>
#include <math.h>
#define ANGLE_STEPS 16
int main(int argc, char **argv) {
double angle;
int i;
printf("Sin\n");
for(i=0;i<ANGLE_STEPS;i++) {
angle=3.1415926535897932384*2.0*((double)i/(double)ANGLE_STEPS);
printf("%d %lf\n",i,sin(angle));
}
printf("Cos\n");
for(i=0;i<ANGLE_STEPS;i++) {
angle=3.1415926535897932384*2.0*((double)i/(double)ANGLE_STEPS);
printf("%d %lf\n",i,cos(angle));
}
return 0;
}

View File

@ -11,6 +11,8 @@
#include "tfv_sprites.h" #include "tfv_sprites.h"
/* Mode7 code based on code from: */
/* http://www.helixsoft.nl/articles/circle/sincos.htm */
static unsigned char flying_map[64]= { static unsigned char flying_map[64]= {
2,15,15,15, 15,15,15, 2, 2,15,15,15, 15,15,15, 2,
@ -30,12 +32,7 @@ static unsigned char flying_map[64]= {
#define LOWRES_W 40 #define LOWRES_W 40
#define LOWRES_H 40 #define LOWRES_H 40
#if 1
//
// Detailed version
//
//
static int lookup_map(int x, int y) { static int lookup_map(int x, int y) {
@ -54,176 +51,77 @@ static int lookup_map(int x, int y) {
color=flying_map[(y*8)+x]; color=flying_map[(y*8)+x];
} }
/* 2 2 2 2 2 2 2 2 */ /* 2 2 2 2 2 2 2 2 */
/* 14 14 2 2 2 2 2 2 */ /* 14 14 2 2 2 2 2 2 */
/* 2 2 2 2 14 14 2 2 */ /* 2 2 2 2 14 14 2 2 */
/* 2 2 2 2 2 2 2 2 */ /* 2 2 2 2 2 2 2 2 */
return color; return color;
} }
/* http://www.helixsoft.nl/articles/circle/sincos.htm */
static double space_z=4.5; // height of the camera above the plane static double space_z=4.5; // height of the camera above the plane
static int horizon=-2; // number of pixels line 0 is below the horizon static int horizon=-2; // number of pixels line 0 is below the horizon
static double scale_x=20, scale_y=20; static double scale_x=20, scale_y=20;
//void mode_7 (BITMAP *bmp, BITMAP *tile, fixed angle, fixed cx, fixed cy, MODE_7_PARAMS params)
double BETA=-0.5; double BETA=-0.5;
static int over_water; static int over_water;
void draw_background_mode7(double angle, double cx, double cy) {
// current screen position // current screen position
int screen_x, screen_y; static int screen_x, screen_y;
static char angle=0;
// the distance and horizontal scale of the line we are drawing // map coordinates
double distance, horizontal_scale; double cx=0.0,cy=0.0;
// step for points in space between two pixels on a horizontal line
double line_dx, line_dy;
// current space position
double space_x, space_y;
int map_color;
over_water=0;
/* Draw Sky */
/* Originally wanted to be fancy and have sun too, but no */
color_equals(COLOR_MEDIUMBLUE);
for(screen_y=0;screen_y<6;screen_y+=2) {
hlin_double(ram[DRAW_PAGE], 0, 40, screen_y);
}
/* Draw hazy horizon */
color_equals(COLOR_GREY);
hlin_double(ram[DRAW_PAGE], 0, 40, 6);
for (screen_y = 8; screen_y < LOWRES_H; screen_y++) {
// first calculate the distance of the line we are drawing
distance = (space_z * scale_y) / (screen_y + horizon);
// then calculate the horizontal scale, or the distance between
// space points on this horizontal line
horizontal_scale = (distance / scale_x);
// calculate the dx and dy of points in space when we step
// through all points on this line
line_dx = -sin(angle) * horizontal_scale;
line_dy = cos(angle) * horizontal_scale;
// calculate the starting position
space_x = cx + (distance * cos(angle)) - LOWRES_W/2 * line_dx;
space_y = cy + (distance * sin(angle)) - LOWRES_W/2 * line_dy;
// Move camera back a bit
double factor;
factor=space_z*BETA;
// factor=2.0*BETA;
space_x+=factor*cos(angle);
space_y+=factor*sin(angle);
// go through all points in this screen line
for (screen_x = 0; screen_x < LOWRES_W-1; screen_x++) {
// get a pixel from the tile and put it on the screen
map_color=lookup_map((int)space_x,(int)space_y); #if 1
//ram[COLOR]=map_color;
//ram[COLOR]|=map_color<<4;
color_equals(map_color); #define ANGLE_STEPS 16
if (screen_x==20) { double sin_table[ANGLE_STEPS]={
if (map_color==COLOR_DARKBLUE) over_water=1; 0.000000,
else over_water=0; 0.382683,
} 0.707107,
0.923880,
1.000000,
0.923880,
0.707107,
0.382683,
0.000000,
-0.382683,
-0.707107,
-0.923880,
-1.000000,
-0.923880,
-0.707107,
-0.382683,
};
//hlin_double(ram[DRAW_PAGE], screen_x, screen_x+1, double our_sin(unsigned char angle) {
// screen_y); return sin_table[angle&0xf];
plot(screen_x,screen_y);
// advance to the next position in space
space_x += line_dx;
space_y += line_dy;
}
}
} }
#else double our_cos(unsigned char angle) {
return sin_table[(angle+4)&0xf];
}
// //
// Non-detailed version // Non-detailed version
// //
// //
//
static int lookup_map(int x, int y) {
int color;
color=2;
x=x&MASK_X;
y=y&MASK_Y;
if ( ((y&0x3)==1) && ((x&7)==0) ) color=14;
if ( ((y&0x3)==3) && ((x&7)==4) ) color=14;
if ((y<8) && (x<8)) { struct fixed_type {unsigned char i; unsigned char f;};
color=flying_map[(y*8)+x];
}
/* 2 2 2 2 2 2 2 2 */
/* 14 14 2 2 2 2 2 2 */
/* 2 2 2 2 14 14 2 2 */
/* 2 2 2 2 2 2 2 2 */
return color;
}
void draw_background_mode7(void) {
/* http://www.helixsoft.nl/articles/circle/sincos.htm */
static double space_z=4.5; // height of the camera above the plane
static int horizon=-2; // number of pixels line 0 is below the horizon
static double scale_x=20, scale_y=20;
//void mode_7 (BITMAP *bmp, BITMAP *tile, fixed angle, fixed cx, fixed cy, MODE_7_PARAMS params)
double BETA=-0.5;
static int over_water;
void draw_background_mode7(double angle, double cx, double cy) {
// current screen position
int screen_x, screen_y;
// the distance and horizontal scale of the line we are drawing // the distance and horizontal scale of the line we are drawing
double distance, horizontal_scale; double distance, horizontal_scale;
@ -259,12 +157,12 @@ void draw_background_mode7(double angle, double cx, double cy) {
// calculate the dx and dy of points in space when we step // calculate the dx and dy of points in space when we step
// through all points on this line // through all points on this line
line_dx = -sin(angle) * horizontal_scale; line_dx = -our_sin(angle) * horizontal_scale;
line_dy = cos(angle) * horizontal_scale; line_dy = our_cos(angle) * horizontal_scale;
// calculate the starting position // calculate the starting position
space_x = cx + (distance * cos(angle)) - LOWRES_W/2 * line_dx; space_x = cx + (distance * our_cos(angle)) - LOWRES_W/2 * line_dx;
space_y = cy + (distance * sin(angle)) - LOWRES_W/2 * line_dy; space_y = cy + (distance * our_sin(angle)) - LOWRES_W/2 * line_dy;
// Move camera back a bit // Move camera back a bit
@ -274,8 +172,8 @@ void draw_background_mode7(double angle, double cx, double cy) {
// factor=2.0*BETA; // factor=2.0*BETA;
space_x+=factor*cos(angle); space_x+=factor*our_cos(angle);
space_y+=factor*sin(angle); space_y+=factor*our_sin(angle);
// go through all points in this screen line // go through all points in this screen line
@ -296,8 +194,6 @@ void draw_background_mode7(double angle, double cx, double cy) {
hlin_double(ram[DRAW_PAGE], screen_x, screen_x+1, hlin_double(ram[DRAW_PAGE], screen_x, screen_x+1,
screen_y); screen_y);
//basic_plot(screen_x,screen_y);
// advance to the next position in space // advance to the next position in space
space_x += line_dx; space_x += line_dx;
space_y += line_dy; space_y += line_dy;
@ -305,9 +201,6 @@ void draw_background_mode7(double angle, double cx, double cy) {
} }
} }
#endif
#define SHIPX 15 #define SHIPX 15
int flying(void) { int flying(void) {
@ -315,8 +208,6 @@ int flying(void) {
unsigned char ch; unsigned char ch;
int shipy; int shipy;
int turning=0; int turning=0;
double flyx=0,flyy=0;
double our_angle=0.0;
double dy,dx,speed=0; double dy,dx,speed=0;
int draw_splash=0; int draw_splash=0;
@ -376,11 +267,9 @@ int flying(void) {
else { else {
turning=-20; turning=-20;
our_angle-=(6.28/32.0); angle-=1;
if (our_angle<0.0) our_angle+=6.28; if (angle<0) angle+=ANGLE_STEPS;
} }
// printf("Angle %lf\n",our_angle);
} }
if ((ch=='d') || (ch==APPLE_RIGHT)) { if ((ch=='d') || (ch==APPLE_RIGHT)) {
if (turning<0) { if (turning<0) {
@ -388,8 +277,8 @@ int flying(void) {
} }
else { else {
turning=20; turning=20;
our_angle+=(6.28/32.0); angle+=1;
if (our_angle>6.28) our_angle-=6.28; if (angle>=ANGLE_STEPS) angle-=ANGLE_STEPS;
} }
} }
@ -409,13 +298,13 @@ int flying(void) {
} }
dx = speed * cos (our_angle); dx = speed * our_cos (angle);
dy = speed * sin (our_angle); dy = speed * our_sin (angle);
flyx += dx; cx += dx;
flyy += dy; cy += dy;
draw_background_mode7(our_angle, flyx, flyy); draw_background_mode7();//our_angle, flyx, flyy);
if (turning==0) { if (turning==0) {
if ((speed>0.0) && (over_water)&&(draw_splash)) { if ((speed>0.0) && (over_water)&&(draw_splash)) {
@ -459,3 +348,281 @@ int flying(void) {
return 0; return 0;
} }
#else
#define ANGLE_STEPS 32
double our_sin(unsigned char angle) {
double r;
r=3.14159265358979*2.0*(double)angle/(double)ANGLE_STEPS;
return sin(r);
}
double our_cos(unsigned char angle) {
double r;
r=3.14159265358979*2.0*(double)angle/(double)ANGLE_STEPS;
return cos(r);
}
//
// Detailed version
//
//
void draw_background_mode7(void) {
// the distance and horizontal scale of the line we are drawing
double distance, horizontal_scale;
// step for points in space between two pixels on a horizontal line
double line_dx, line_dy;
// current space position
double space_x, space_y;
int map_color;
over_water=0;
/* Draw Sky */
/* Originally wanted to be fancy and have sun too, but no */
color_equals(COLOR_MEDIUMBLUE);
for(screen_y=0;screen_y<6;screen_y+=2) {
hlin_double(ram[DRAW_PAGE], 0, 40, screen_y);
}
/* Draw hazy horizon */
color_equals(COLOR_GREY);
hlin_double(ram[DRAW_PAGE], 0, 40, 6);
for (screen_y = 8; screen_y < LOWRES_H; screen_y++) {
// first calculate the distance of the line we are drawing
distance = (space_z * scale_y) / (screen_y + horizon);
// then calculate the horizontal scale, or the distance between
// space points on this horizontal line
horizontal_scale = (distance / scale_x);
// calculate the dx and dy of points in space when we step
// through all points on this line
line_dx = -our_sin(angle) * horizontal_scale;
line_dy = our_cos(angle) * horizontal_scale;
// calculate the starting position
space_x = cx + (distance * our_cos(angle)) - LOWRES_W/2 * line_dx;
space_y = cy + (distance * our_sin(angle)) - LOWRES_W/2 * line_dy;
// Move camera back a bit
double factor;
factor=space_z*BETA;
space_x+=factor*our_cos(angle);
space_y+=factor*our_sin(angle);
// go through all points in this screen line
for (screen_x = 0; screen_x < LOWRES_W-1; screen_x++) {
// get a pixel from the tile and put it on the screen
map_color=lookup_map((int)space_x,(int)space_y);
color_equals(map_color);
if (screen_x==20) {
if (map_color==COLOR_DARKBLUE) over_water=1;
else over_water=0;
}
plot(screen_x,screen_y);
// advance to the next position in space
space_x += line_dx;
space_y += line_dy;
}
}
}
#define SHIPX 15
int flying(void) {
unsigned char ch;
int shipy;
int turning=0;
double dy,dx,speed=0;
int draw_splash=0;
/************************************************/
/* Flying */
/************************************************/
gr();
shipy=20;
while(1) {
if (draw_splash>0) draw_splash--;
ch=grsim_input();
if ((ch=='q') || (ch==27)) break;
#if 0
if (ch=='g') {
BETA+=0.1;
printf("Horizon=%lf\n",BETA);
}
if (ch=='h') {
BETA-=0.1;
printf("Horizon=%lf\n",BETA);
}
if (ch=='s') {
scale_x++;
scale_y++;
printf("Scale=%lf\n",scale_x);
}
#endif
if ((ch=='w') || (ch==APPLE_UP)) {
if (shipy>16) {
shipy-=2;
space_z+=1;
}
printf("Z=%lf\n",space_z);
}
if ((ch=='s') || (ch==APPLE_DOWN)) {
if (shipy<28) {
shipy+=2;
space_z-=1;
}
else {
draw_splash=10;
}
printf("Z=%lf\n",space_z);
}
if ((ch=='a') || (ch==APPLE_LEFT)) {
if (turning>0) {
turning=0;
}
else {
turning=-20;
angle-=1;
if (angle<0) angle+=ANGLE_STEPS;
}
}
if ((ch=='d') || (ch==APPLE_RIGHT)) {
if (turning<0) {
turning=0;
}
else {
turning=20;
angle+=1;
if (angle>=ANGLE_STEPS) angle-=ANGLE_STEPS;
}
}
if (ch=='z') {
if (speed>0.5) speed=0.5;
speed+=0.05;
}
if (ch=='x') {
if (speed<-0.5) speed=-0.5;
speed-=0.05;
}
if (ch==' ') {
speed=0;
}
dx = speed * our_cos (angle);
dy = speed * our_sin (angle);
cx += dx;
cy += dy;
draw_background_mode7();//our_angle, flyx, flyy);
if (turning==0) {
if ((speed>0.0) && (over_water)&&(draw_splash)) {
grsim_put_sprite(splash_forward,
SHIPX+1,shipy+9);
}
grsim_put_sprite(shadow_forward,SHIPX+3,31+space_z);
grsim_put_sprite(ship_forward,SHIPX,shipy);
}
if (turning<0) {
if ((shipy>25) && (speed>0.0)) draw_splash=1;
if (over_water&&draw_splash) {
grsim_put_sprite(splash_left,
SHIPX+1,36);
}
grsim_put_sprite(shadow_left,SHIPX+3,31+space_z);
grsim_put_sprite(ship_left,SHIPX,shipy);
turning++;
}
if (turning>0) {
if ((shipy>25) && (speed>0.0)) draw_splash=1;
if (over_water&&draw_splash) {
grsim_put_sprite(splash_right,
SHIPX+1,36);
}
grsim_put_sprite(shadow_right,SHIPX+3,31+space_z);
grsim_put_sprite(ship_right,SHIPX,shipy);
turning--;
}
page_flip();
usleep(20000);
}
return 0;
}
#endif