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