tfv: more work on fixed point conversion

struggling with speed a bit
This commit is contained in:
Vince Weaver 2017-08-24 13:30:19 -04:00
parent 3bbe719824
commit 468b94f24e
2 changed files with 143 additions and 55 deletions

View File

@ -10,17 +10,15 @@ struct fixed_type {
void double_to_fixed(double d, struct fixed_type *f) {
double temp;
int temp;
f->i=(int)d;
temp=d*256;
temp=d-(f->i);
f->i=(temp>>8)&0xff;
temp*=256;
f->f=temp&0xff;
f->f=temp;
printf("%lf=%02x.%02x (%d/0x%x)\n",d,f->i,f->f,(int)temp,(int)temp);
printf("%lf=%02x.%02x\n",d,f->i,f->f);
}
void fixed_to_double(struct fixed_type *f, double *d) {
@ -46,6 +44,23 @@ void fixed_add(struct fixed_type *x, struct fixed_type *y, struct fixed_type *z)
z->i=x->i+y->i+carry;
}
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) {
struct fixed_type f,fa,fb,fc;
@ -74,6 +89,26 @@ int main(int argc, char **argv) {
fixed_add(&fa,&fb,&fc);
fixed_to_double(&fc,&c);
double_to_fixed(2.5,&fa);
double_to_fixed(2.5,&fb);
fixed_mul(&fa,&fb,&fc);
fixed_to_double(&fc,&c);
double_to_fixed(-1.1,&fa);
double_to_fixed(-1.1,&fb);
fixed_mul(&fa,&fb,&fc);
fixed_to_double(&fc,&c);
double_to_fixed(-2.0,&fa);
double_to_fixed(5.0,&fb);
fixed_mul(&fa,&fb,&fc);
fixed_to_double(&fc,&c);
double_to_fixed(13.2,&fa);
double_to_fixed(-0.5,&fb);
fixed_mul(&fa,&fb,&fc);
fixed_to_double(&fc,&c);
return 0;
}

View File

@ -66,16 +66,72 @@ static int over_water;
static int screen_x, screen_y;
static char angle=0;
// map coordinates
double cx=0.0,cy=0.0;
#if 1
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
struct fixed_type {
char i;
unsigned char f;
};
#if 0
// map coordinates
struct fixed_type cx = {0,0};
struct fixed_type cy = {0,0};
struct fixed_type dx = {0,0};
struct fixed_type dy = {0,0};
struct fixed_type speed = {0,0};
// the distance and horizontal scale of the line we are drawing
struct fixed_type distance = {0,0};
struct fixed_type horizontal_scale = {0,0};
// step for points in space between two pixels on a horizontal line
struct fixed_type line_dx = {0,0};
struct fixed_type line_dy = {0,0};
// current space position
struct fixed_type space_x;
struct fixed_type space_y;
// height of the camera above the plane
struct fixed_type space_z= {0x04,0x80}; // 4.5;
struct fixed_type BETA = {0xff,0x80}; // -0.5;
#else
// map coordinates
double cx=0;
double cy=0;
double dx=0;
double dy=0;
#define SPEED_STOPPED 0
unsigned char speed=SPEED_STOPPED; // 0..4, with 0=stopped
// the distance and horizontal scale of the line we are drawing
double distance=0;
double horizontal_scale=0;
// step for points in space between two pixels on a horizontal line
double line_dx=0;
double line_dy=0;
// current space position
double space_x,space_y;
// height of the camera above the plane
double space_z=4.5;
double BETA=-0.5;
#endif
static int horizon=-2; // number of pixels line 0 is below the horizon
#define SCALE_X 20.0
#define SCALE_Y 20.0
@ -115,21 +171,9 @@ double our_cos(unsigned char angle) {
//
//
struct fixed_type {unsigned char i; unsigned char f;};
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;
@ -145,7 +189,7 @@ void draw_background_mode7(void) {
color_equals(COLOR_GREY);
hlin_double(ram[DRAW_PAGE], 0, 40, 6);
for (screen_y = 8; screen_y < LOWRES_H; screen_y++) {
for (screen_y = 8; screen_y < LOWRES_H; screen_y+=2) {
// first calculate the distance of the line we are drawing
distance = (space_z * SCALE_Y) / (screen_y + horizon);
@ -158,18 +202,19 @@ void draw_background_mode7(void) {
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);
// space_x+=factor*our_cos(angle);
// space_y+=factor*our_sin(angle);
// calculate the starting position
space_x = cx + ((distance+factor) * our_cos(angle)) - LOWRES_W/2 * line_dx;
space_y = cy + ((distance+factor) * our_sin(angle)) - LOWRES_W/2 * line_dy;
// go through all points in this screen line
@ -181,7 +226,6 @@ void draw_background_mode7(void) {
ram[COLOR]=map_color;
ram[COLOR]|=map_color<<4;
if (screen_x==20) {
if (map_color==COLOR_DARKBLUE) over_water=1;
else over_water=0;
@ -204,7 +248,6 @@ int flying(void) {
unsigned char ch;
int shipy;
int turning=0;
double dy,dx,speed=0;
int draw_splash=0;
/************************************************/
@ -262,31 +305,39 @@ int flying(void) {
}
// increase speed
if (ch=='z') {
if (speed>0.5) speed=0.5;
speed+=0.05;
if (speed<3) speed++;
}
// decrease speed
if (ch=='x') {
if (speed<-0.5) speed=-0.5;
speed-=0.05;
if (speed>0) speed--;
}
// emergency break
if (ch==' ') {
speed=0;
speed=SPEED_STOPPED;
}
if (speed!=SPEED_STOPPED) {
dx = speed * our_cos (angle);
dy = speed * our_sin (angle);
int ii;
cx += dx;
cy += dy;
dx = our_cos(angle)/8.0;
dy = our_sin(angle)/8.0;
for(ii=0;ii<speed;ii++) {
cx += dx;
cy += dy;
}
}
draw_background_mode7();//our_angle, flyx, flyy);
if (turning==0) {
if ((speed>0.0) && (over_water)&&(draw_splash)) {
if ((speed!=SPEED_STOPPED) && (over_water)&&(draw_splash)) {
grsim_put_sprite(splash_forward,
SHIPX+1,shipy+9);
}
@ -295,7 +346,7 @@ int flying(void) {
}
if (turning<0) {
if ((shipy>25) && (speed>0.0)) draw_splash=1;
if ((shipy>25) && (speed!=SPEED_STOPPED)) draw_splash=1;
if (over_water&&draw_splash) {
grsim_put_sprite(splash_left,
@ -308,7 +359,7 @@ int flying(void) {
if (turning>0) {
if ((shipy>25) && (speed>0.0)) draw_splash=1;
if ((shipy>25) && (speed!=SPEED_STOPPED)) draw_splash=1;
if (over_water&&draw_splash) {
grsim_put_sprite(splash_right,
@ -344,6 +395,12 @@ int flying(void) {
@ -353,6 +410,8 @@ int flying(void) {
#else
// map coordinates
double cx=0.0,cy=0.0;
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;
@ -425,19 +484,13 @@ void draw_background_mode7(void) {
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;
// Move camera back a bit
factor=space_z*BETA;
space_x+=factor*our_cos(angle);
space_y+=factor*our_sin(angle);
// calculate the starting position
space_x = cx + ((distance+factor) * our_cos(angle)) - LOWRES_W/2 * line_dx;
space_y = cy + ((distance+factor) * our_sin(angle)) - LOWRES_W/2 * line_dy;
// go through all points in this screen line
for (screen_x = 0; screen_x < LOWRES_W-1; screen_x++) {