SegaRaycast/src/main.c

275 lines
6.6 KiB
C
Raw Normal View History

2024-08-29 17:51:49 -06:00
#include <genesis.h>
2024-08-30 15:32:58 -06:00
#define SPEED 5
2024-08-29 18:49:38 -06:00
2024-08-29 18:17:52 -06:00
Line l;
2024-08-30 09:09:31 -06:00
s16 angle = 0;
2024-08-30 15:54:02 -06:00
s16 x, y;
2024-08-29 18:17:52 -06:00
2024-08-29 19:39:58 -06:00
const u8 map[10][10] = {{1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,1},
{1,1,1,1,0,0,1,1,1,1},
{1,0,0,1,0,0,1,0,0,1},
{1,0,0,0,0,0,1,0,0,1},
{1,0,0,1,0,0,1,1,0,1},
{1,1,1,1,0,0,1,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,1,0,0,1},
{1,1,1,1,1,1,1,1,1,1}};
2024-08-30 12:12:20 -06:00
int dist(int ax, int ay, int bx, int by){
return (ax-bx)*(ax-bx) + (ay-by)*(ay-by);
}
2024-08-29 21:04:02 -06:00
void castRay(s16 angle){
2024-08-30 15:32:58 -06:00
if(angle >= 360) angle = angle - 360;
if(angle < 0) angle = 360 + angle;
2024-08-30 12:12:20 -06:00
u16 r,mx,my,mp,dof;
2024-08-30 09:09:31 -06:00
fix16 rx,ry,ra,xo,yo;
2024-08-29 21:04:02 -06:00
s16 ind = (int)((float)angle/360.0f*1024.0f);
2024-08-30 09:09:31 -06:00
fix16 dy = sinFix16(ind);
fix16 dx = cosFix16(ind);
2024-08-30 14:04:02 -06:00
fix16 tan = FIX16(0);
2024-08-30 12:12:20 -06:00
if(dx != 0) tan = fix16Div(dy, dx);
2024-08-30 20:33:43 -06:00
char str[9];
sprintf(str, "Ang: %4d", angle);
BMP_drawText(str, 20, 0);
char str2[13];
bool neg = false;
fix16 nsin = dy;
if (dy < 0){
neg = true;
nsin = 0 - nsin;
}
sprintf(str2, "Sin: %s%3d.%03d", neg ? "-" : " ", nsin >> FIX16_FRAC_BITS, 1000*fix16Frac(nsin)/(1<<FIX16_FRAC_BITS));
BMP_drawText(str2, 20, 1);
neg = false;
fix16 ncos = dx;
if (dx < 0){
neg = true;
ncos = 0 - ncos;
}
sprintf(str2, "Cos: %s%3d.%03d", neg ? "-" : " ", ncos >> FIX16_FRAC_BITS, 1000*fix16Frac(ncos)/(1<<FIX16_FRAC_BITS));
BMP_drawText(str2, 20, 2);
neg = false;
fix16 ntan = tan;
if (tan < 0){
neg = true;
ntan = 0 - ntan;
}
sprintf(str2, "Tan: %s%3d.%03d", neg ? "-" : " ", ntan >> FIX16_FRAC_BITS, 1000*fix16Frac(ntan)/(1<<FIX16_FRAC_BITS));
BMP_drawText(str2, 20, 3);
2024-08-30 12:12:20 -06:00
fix16 aTan = FIX16(0);
if(tan != 0) aTan = fix16Div(FIX16(-1), tan);
2024-08-30 09:09:31 -06:00
s16 tanInt = fix16ToInt(fix16Mul(aTan, FIX16(100)));
dof = 0;
2024-08-30 12:12:20 -06:00
fix16 distH = FIX16(500);
fix16 hx = FIX16(l.pt1.x);
fix16 hy = FIX16(l.pt1.y);
2024-08-30 14:04:02 -06:00
if(dy == 0){
2024-08-30 12:12:20 -06:00
rx = FIX16(l.pt1.x);
ry = FIX16(l.pt1.y);
dof = 8;
}else {
if(angle > 180) {
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 = 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);
}
2024-08-30 09:09:31 -06:00
}
2024-08-30 12:12:20 -06:00
while(dof<8){
mx = fix16ToInt(rx) / 10;
my = fix16ToInt(ry) / 10;
2024-08-30 14:04:02 -06:00
if(rx < 0 || ry < 0){
dof = 8;
continue;
}
if(mx < 10 && my < 10 && map[my][mx] == 1){
2024-08-30 12:12:20 -06:00
dof = 8;
hx = rx;
hy = ry;
distH = dist(l.pt1.x, l.pt1.y, fix16ToInt(rx), fix16ToInt(ry));
}else{
rx = rx + xo;
ry = ry + yo;
dof += 1;
}
2024-08-30 09:09:31 -06:00
}
2024-08-30 12:12:20 -06:00
dof = 0;
fix16 distV = FIX16(500);
fix16 vx = FIX16(l.pt1.x);
fix16 vy = FIX16(l.pt1.y);
float nTan = 0-tan;
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) {
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);
}
2024-08-30 14:04:02 -06:00
if(dx == 0){
2024-08-30 09:09:31 -06:00
rx = FIX16(l.pt1.x);
ry = FIX16(l.pt1.y);
2024-08-30 14:04:02 -06:00
xo = FIX16(0);
yo = FIX16(0);
2024-08-30 09:09:31 -06:00
dof = 8;
}
while(dof<8){
2024-08-30 12:12:20 -06:00
mx = fix16ToInt(rx) / 10;
my = fix16ToInt(ry) / 10;
2024-08-30 16:58:36 -06:00
if(mx < 10 && my < 10 && map[my][mx] == 1){
2024-08-30 12:12:20 -06:00
dof = 8;
vx = rx;
vy = ry;
distV = dist(l.pt1.x, l.pt1.y, fix16ToInt(rx), fix16ToInt(ry));
}else{
rx = rx + xo;
ry = ry + yo;
dof += 1;
}
}
if(distV < distH){
l.pt2.x = fix16ToInt(vx);
l.pt2.y = fix16ToInt(vy);
}else {
l.pt2.x = fix16ToInt(hx);
l.pt2.y = fix16ToInt(hy);
2024-08-30 09:09:31 -06:00
}
2024-08-29 21:04:02 -06:00
}
2024-08-29 19:39:58 -06:00
void mapscan(){
2024-08-29 21:04:02 -06:00
u8 mapcolor = 12;
2024-08-29 19:39:58 -06:00
mapcolor |= mapcolor << 4;
int mapscale = 10;
for(u8 x = 0; x < 10; x++){
for(u8 y = 0; y < 10; y++){
if(map[y][x] == 1){
Vect2D_s16 mapverts[4] = {{x*mapscale,y*mapscale},{(x+1)*mapscale,y*mapscale},{(x+1)*mapscale,(y+1)*mapscale},{x*mapscale,(y+1)*mapscale}};
BMP_drawPolygon(mapverts, 4, mapcolor);
}
}
}
}
2024-08-29 18:49:38 -06:00
2024-08-29 18:17:52 -06:00
void render(){
2024-08-30 15:54:02 -06:00
l.pt1.x = x / 10;
l.pt1.y = y / 10;
2024-08-29 18:17:52 -06:00
//Clear the bitmap
BMP_clear();
//Draw the line defined above (in the background, hidden)
2024-08-29 19:39:58 -06:00
mapscan();
2024-08-30 14:04:02 -06:00
if(angle >= 360) angle = angle - 360;
if(angle < 0) angle = 360 + angle;
2024-08-30 20:33:43 -06:00
//castRay(angle-20);
//BMP_drawLine(&l);
//castRay(angle-10);
//BMP_drawLine(&l);
2024-08-30 12:12:20 -06:00
castRay(angle);
2024-08-30 20:33:43 -06:00
//BMP_drawLine(&l);
//castRay(angle+10);
//BMP_drawLine(&l);
//castRay(angle+20);
2024-08-30 12:12:20 -06:00
BMP_drawLine(&l);
2024-08-30 14:04:02 -06:00
BMP_showFPS(0);
2024-08-29 19:39:58 -06:00
2024-08-29 18:17:52 -06:00
//Flip the data to the screen - i.e. actually draw the complete image on screen
BMP_flip(1);
//Increment the destination y coordinate
2024-08-30 15:54:02 -06:00
//l.pt2.y = l.pt2.y + 2;
2024-08-29 18:17:52 -06:00
//Reset the destination y coordinate if it hits 160
2024-08-30 15:54:02 -06:00
//if (l.pt2.y == 160)
//{
//l.pt2.y = 0;
//}
2024-08-29 18:17:52 -06:00
2024-08-29 18:49:38 -06:00
SYS_doVBlankProcess();
2024-08-29 18:17:52 -06:00
}
void update(){
2024-08-29 18:49:38 -06:00
u16 joy = JOY_readJoypad(JOY_1);
if(joy & BUTTON_LEFT) {
2024-08-30 12:12:20 -06:00
//l.pt1.x -= SPEED;
2024-08-30 14:04:02 -06:00
angle -= 1;
2024-08-29 18:49:38 -06:00
}
if(joy & BUTTON_RIGHT) {
2024-08-30 12:12:20 -06:00
//l.pt1.x += SPEED;
2024-08-30 14:04:02 -06:00
angle += 1;
2024-08-29 18:49:38 -06:00
}
if(joy & BUTTON_UP) {
2024-08-30 15:32:58 -06:00
s16 ind = (int)((float)angle/360.0f*1024.0f);
fix16 dy = sinFix16(ind);
fix16 dx = cosFix16(ind);
2024-08-30 15:54:02 -06:00
y += fix16ToInt(fix16Mul(dy, FIX16(SPEED)));
x += fix16ToInt(fix16Mul(dx, FIX16(SPEED)));
2024-08-29 18:49:38 -06:00
}
if(joy & BUTTON_DOWN) {
2024-08-30 15:32:58 -06:00
s16 ind = (int)((float)angle/360.0f*1024.0f);
fix16 dy = sinFix16(ind);
fix16 dx = cosFix16(ind);
2024-08-30 15:54:02 -06:00
y -= fix16ToInt(fix16Mul(dy, FIX16(SPEED)));
x -= fix16ToInt(fix16Mul(dx, FIX16(SPEED)));
2024-08-29 18:49:38 -06:00
}
}
2024-08-29 18:17:52 -06:00
2024-08-29 18:49:38 -06:00
void initVDP(){
SYS_disableInts();
VDP_setPlaneSize(64,32,TRUE);
SYS_enableInts();
2024-08-29 18:17:52 -06:00
}
2024-08-29 17:51:49 -06:00
int main()
{
2024-08-30 15:54:02 -06:00
x = 150;
y = 150;
2024-08-29 18:49:38 -06:00
initVDP();
2024-08-29 17:51:49 -06:00
//Initialise the bitmap engine
2024-08-29 18:49:38 -06:00
BMP_init(FALSE, BG_A, PAL0, FALSE);
2024-08-29 17:51:49 -06:00
//Set the colour of the line. We are using pallete 0 for the bitmap, so we have 0->15. 15 is used for white text, so we set an unused pallet colour, 14 to blue - RGB 0000FF.
u16 colour_blue = RGB24_TO_VDPCOLOR(0x0000ff);
2024-08-29 19:39:58 -06:00
u16 colour_red = RGB24_TO_VDPCOLOR(0x756a4a);
2024-08-29 18:08:02 -06:00
PAL_setColor(14, colour_blue);
2024-08-29 18:13:30 -06:00
PAL_setColor(13, colour_red);
2024-08-29 21:04:02 -06:00
PAL_setColor(12, RGB24_TO_VDPCOLOR(0x00ff00));
PAL_setColor(15, RGB24_TO_VDPCOLOR(0xff0000));
2024-08-29 18:13:30 -06:00
VDP_setBackgroundColor(13);
2024-08-29 17:51:49 -06:00
//A line needs a source coordinate x,y and a destination coordinate x,y along with a pallete colour.
2024-08-29 21:04:02 -06:00
l.pt1.x = 15;
l.pt1.y = 15;
2024-08-29 17:51:49 -06:00
l.pt2.x = 255;
l.pt2.y = 0;
2024-08-29 18:08:02 -06:00
l.col = 14;
2024-08-29 17:51:49 -06:00
l.col |= l.col << 4; // if we do not left shift the colour, we get gaps in the line
while(TRUE)
{
2024-08-29 18:17:52 -06:00
render();
2024-08-29 18:49:38 -06:00
update();
2024-08-29 17:51:49 -06:00
}
return 0;
}