reimplemented fixed arithmetic

This commit is contained in:
Cuptain 2024-09-01 14:11:01 +10:00
parent 8e26b01b05
commit 9ea6ce4023

View File

@ -42,45 +42,40 @@ void castRay(s16 angle){
if(angle >= 360) angle = angle - 360;
if(angle < 0) angle = 360 + angle;
u16 r,mx,my,mp,dof;
s16 rx,ry,ra,xo,yo;
s16 dy = sinTable[angle << 1];
s16 dx = cosTable[angle << 1];
s16 tan = 0;
if(dx != 0) tan = (dy << FRAC_BITS)/dx;
s16 aTan = 0;
if(tan != 0) aTan = -(1 << (FRAC_BITS*2))/tan;
s16 threshold = (20 << FRAC_BITS);
if(aTan > threshold){
aTan = threshold;
}
if(aTan < -threshold){
aTan = -threshold;
}
fix16 rx,ry,ra,xo,yo;
s16 ind = (int)((float)angle/360.0f*1024.0f);
fix16 dy = sinFix16(ind);
fix16 dx = cosFix16(ind);
fix16 tan = FIX16(0);
if(dx != 0) tan = fix16Div(dy, dx);
fix16 aTan = FIX16(0);
if(tan != 0) aTan = fix16Div(FIX16(-1), tan);
s16 tanInt = fix16ToInt(fix16Mul(aTan, FIX16(100)));
dof = 0;
s16 distH = 30000;
s16 hx = l.pt1.x << FRAC_BITS;
s16 hy = l.pt1.y << FRAC_BITS;
fix16 distH = FIX16(500);
fix16 hx = FIX16(l.pt1.x);
fix16 hy = FIX16(l.pt1.y);
if(dy == 0){
rx = l.pt1.x << FRAC_BITS;
ry = l.pt1.y << FRAC_BITS;
rx = FIX16(l.pt1.x);
ry = FIX16(l.pt1.y);
dof = 8;
}else {
if(angle > 180) {
ry = ((l.pt1.y / 10 * 10)-1) << FRAC_BITS;
rx = (((l.pt1.y - (ry >> FRAC_BITS))*aTan)) + (l.pt1.x << FRAC_BITS);
yo = -(10 << FRAC_BITS);
xo = ((-yo >> FRAC_BITS)*aTan);
ry = FIX16(l.pt1.y / 10 * 10)-FIX16(1);
rx = fix16Mul(FIX16(l.pt1.y) - ry,aTan) + FIX16(l.pt1.x);
yo = FIX16(-10);
xo = fix16Mul(0-yo,aTan);
}
if(angle < 180) {
ry = ((l.pt1.y / 10 * 10)+10) << FRAC_BITS;
rx = (((l.pt1.y - (ry >> FRAC_BITS))*aTan)) + (l.pt1.x << FRAC_BITS);
yo = 10 << FRAC_BITS;
xo = (-(yo >> FRAC_BITS)*aTan);
ry = FIX16((l.pt1.y / 10 * 10)+10);
rx = fix16Mul(FIX16(l.pt1.y) - ry,aTan) + FIX16(l.pt1.x);
yo = FIX16(10);
xo = fix16Mul(0-yo,aTan);
}
}
while(dof<8){
mx = (rx >> FRAC_BITS) / 10;
my = (ry >> FRAC_BITS) / 10;
mx = fix16ToInt(rx) / 10;
my = fix16ToInt(ry) / 10;
if(rx < 0 || ry < 0){
dof = 8;
continue;
@ -89,7 +84,7 @@ void castRay(s16 angle){
dof = 8;
hx = rx;
hy = ry;
distH = dist(l.pt1.x, l.pt1.y, rx >> FRAC_BITS, ry >> FRAC_BITS);
distH = dist(l.pt1.x, l.pt1.y, fix16ToInt(rx), fix16ToInt(ry));
}else{
rx = rx + xo;
ry = ry + yo;
@ -98,39 +93,38 @@ void castRay(s16 angle){
}
dof = 0;
s16 distV = 30000;
s16 vx = l.pt1.x << FRAC_BITS;
s16 vy = l.pt1.y << FRAC_BITS;
s16 nTan = -tan;
threshold = (20 << FRAC_BITS);
if(nTan > threshold){
nTan = threshold;
fix16 distV = FIX16(500);
fix16 vx = FIX16(l.pt1.x);
fix16 vy = FIX16(l.pt1.y);
fix16 nTan = 0-tan;
if(fix16ToInt(nTan) > 51){
nTan = FIX16(51);
}
if(nTan < -threshold){
nTan = -threshold;
if(fix16ToInt(nTan) < -51){
nTan = FIX16(-51);
}
if(angle > 90 && angle < 270) { // Facing Left
rx = ((l.pt1.x / 10 * 10)-1) << FRAC_BITS;
ry = (((l.pt1.x) - (rx >> FRAC_BITS))*nTan) + (l.pt1.y << FRAC_BITS);
xo = -(10 << FRAC_BITS);
yo = ((-xo >> FRAC_BITS)*nTan);
if(angle > 90 && r < 270) {
rx = FIX16(l.pt1.x / 10 * 10)-FIX16(1);
ry = fix16Mul(FIX16(l.pt1.x) - rx,nTan) + FIX16(l.pt1.y);
xo = FIX16(-10);
yo = fix16Mul(0-xo,nTan);
}
if(angle < 90 || angle > 270) { // Facing Right
rx = ((l.pt1.x / 10 * 10)+10) << FRAC_BITS;
ry = (((l.pt1.x) - (rx >> FRAC_BITS))*nTan) + (l.pt1.y << FRAC_BITS);
xo = 10 << FRAC_BITS;
yo = -(xo >> FRAC_BITS)*nTan;
if(angle < 90 || angle > 270) {
rx = FIX16((l.pt1.x / 10 * 10)+10);
ry = fix16Mul(FIX16(l.pt1.x) - rx,nTan) + FIX16(l.pt1.y);
xo = FIX16(10);
yo = fix16Mul(0-xo,nTan);
}
if(dx == 0){
rx = l.pt1.x << FRAC_BITS;
ry = l.pt1.y << FRAC_BITS;
xo = 0;
yo = 0;
rx = FIX16(l.pt1.x);
ry = FIX16(l.pt1.y);
xo = FIX16(0);
yo = FIX16(0);
dof = 8;
}
while(dof<8){
mx = (rx >> FRAC_BITS) / 10;
my = (ry >> FRAC_BITS) / 10;
mx = fix16ToInt(rx) / 10;
my = fix16ToInt(ry) / 10;
if(rx < 0 || ry < 0){
dof = 8;
continue;
@ -139,7 +133,7 @@ void castRay(s16 angle){
dof = 8;
vx = rx;
vy = ry;
distV = dist(l.pt1.x, l.pt1.y, rx >> FRAC_BITS, ry >> FRAC_BITS);
distV = dist(l.pt1.x, l.pt1.y, fix16ToInt(rx), fix16ToInt(ry));
}else{
rx = rx + xo;
ry = ry + yo;
@ -147,11 +141,11 @@ void castRay(s16 angle){
}
}
if(distV < distH){
l.pt2.x = vx >> FRAC_BITS;
l.pt2.y = vy >> FRAC_BITS;
l.pt2.x = fix16ToInt(vx);
l.pt2.y = fix16ToInt(vy);
}else {
l.pt2.x = hx >> FRAC_BITS;
l.pt2.y = hy >> FRAC_BITS;
l.pt2.x = fix16ToInt(hx);
l.pt2.y = fix16ToInt(hy);
}
@ -170,7 +164,7 @@ void uploadTileDataToVRAM(u16 vramTileIndex) {
}
void mapscan() {
u8 mapcolor = 12; // map color
u8 mapcolor = 15; // map color
mapcolor |= mapcolor << 4;
for (u8 x = 0; x < 10; x++) {
@ -184,7 +178,7 @@ void mapscan() {
}
// Upload the filled tile to VRAM
uploadTileDataToVRAM(tileIndex);
VDP_setTileMapXY(VDP_BG_A, tileIndex, x, y);
VDP_setTileMapXY(BG_A, tileIndex, x, y);
}
}
}
@ -210,8 +204,9 @@ void render() {
VDP_setTextPlane(BG_B);
VDP_setTextPalette(2);
VDP_showFPS(1);
for(int i = 0; i < 50; i++){
castRay(10);
s16 castStart = angle - 20;
for(int i = 0; i < 40; i++){
castRay(castStart + i);
}
SYS_doVBlankProcess();
}
@ -250,12 +245,12 @@ int main() {
SYS_disableInts();
VDP_setPlaneSize(64, 32, TRUE);
SYS_enableInts();
PAL_setColor(14, RGB24_TO_VDPCOLOR(0x0000ff));
PAL_setColor(15, RGB24_TO_VDPCOLOR(0xff0000));
PAL_setColor(14, RGB24_TO_VDPCOLOR(0x1c1c14));
PAL_setColor(15, RGB24_TO_VDPCOLOR(0x17ffe8));
VDP_setBackgroundColor(14);
// Initialize and upload tile data for TILE_EMPTY and TILE_FILLED
initTileBuffer(12); // Color for TILE_FILLED
initTileBuffer(15); // Color for TILE_FILLED
VDP_loadTileData(tileBuffer, TILE_FILLED, 1, DMA);
initTileBuffer(0); // Color for TILE_EMPTY