Skip to content

Instantly share code, notes, and snippets.

@kdrnic
Created December 14, 2022 17:21
Show Gist options
  • Save kdrnic/be5ba73a2cbfb1568a8982c5ef5d7b26 to your computer and use it in GitHub Desktop.
Save kdrnic/be5ba73a2cbfb1568a8982c5ef5d7b26 to your computer and use it in GitHub Desktop.
#include <sys/nearptr.h>
#include <sys/farptr.h>
#include <go32.h>
#include <pc.h>
#include <math.h>
#include <conio.h>
int main(int argc, char **argv)
{
asm(
"movb $0,%ah \n\t"
"movb $0x13,%al \n\t"
"int $0x10 \n\t"
);
unsigned long seg = 0xA000;
_farsetsel(_go32_info_block.selector_for_linear_memory);
//Generate palette
outportb(0x3c8, 0);
for(int z = 0; z < 64; z++){
outportb(0x3c9, z);
outportb(0x3c9, z);
outportb(0x3c9, z / 2);
}
for(int z = 0; z < 64; z++){
outportb(0x3c9, z / 2);
outportb(0x3c9, z);
outportb(0x3c9, z);
}
#define POKE(arg1, arg2) _farnspokeb(seg*16 + arg1, arg2)
short hei[4096], col[4096];
//Generate interesting height & color maps
int p = 0;
for(int y = 0; y < 64; y++){
for(int x = 0; x < 64; x++){
int d = 15 * 15 - pow(((x & 31) - 16), 2) - pow(((y & 31) - 16), 2);
if(d > 0 && ((x ^ y) & 32)){
hei[p] = 64 - sqrt(d);
col[p] = (x + y) * 0.5;
}
else{
hei[p] = 64;
col[p] = (cos(x * 0.2) + sin(y * 0.3)) * 3 + 88;
}
p++;
}
}
//Initialize starting position
int posx = 0, posy = 0, posz = 40 * 65536, horiz = -50;
double ang = 0;
const double r160 = 1.0 / 160.0;
int dd = 65536 * r160; //Increment size
int de = dd * 128; //Scan out 128 units
int sdz = (100 - horiz) * 65536 * r160;
do{
int cosang = cos(ang) * 65536, sinang = sin(ang) * 65536;
int dx = sinang + cosang, dxi = -sinang * r160;
int dy = sinang - cosang, dyi = cosang * r160;
//For each column in 320*200 mode...
for(int sx = 0; sx < 320; sx++){
//Fast ray trace! No *'s or /'s in here (\ 65536 is a shift)
int x = posx;
int y = posy;
int z = posz;
int dz = sdz;
int p = sx + 199 * 320;
for(int d = 0; d < de; d += dd){
x += dx;
y += dy;
z += dz;
int i = (((x * 64) & (63 * 64 * 0x10000)) + (y & (63 * 0x10000))) / 65536;
int h = hei[i] * 65536;
while(h < z){
POKE(p, col[i]);
p -= 320;
z -= d;
dz -= dd;
}
}
dx += dxi;
dy += dyi;
//Finish off rest of line
while(p >= 0){
POKE(p, 0);
p -= 320;
}
}
//Move position & angle
posx = posx + cosang * 4;
posy = posy + sinang * 4;
ang = ang + 0.02;
} while(!_conio_kbhit());
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment