1
0
Fork 0
mirror of https://git.coom.tech/drummyfish/raycastlib.git synced 2024-11-23 20:49:57 +01:00
raycastlib/testTerminal.c

139 lines
2.9 KiB
C
Raw Normal View History

2018-09-15 14:48:01 +02:00
#include <stdio.h>
#include "raycastlib.h"
#include <unistd.h>
2018-09-15 15:50:40 +02:00
#include <stdlib.h>
2018-09-15 14:48:01 +02:00
#define LEVEL_W 20
#define LEVEL_H 15
2018-09-15 15:50:40 +02:00
#define SCREEN_W 80
#define SCREEN_H 40
2018-09-15 14:48:01 +02:00
char pixels[SCREEN_W * SCREEN_H];
Camera camera;
2018-09-15 15:50:40 +02:00
const int8_t level[LEVEL_W * LEVEL_H] =
2018-09-15 14:48:01 +02:00
{
/* 11 13 15 17 19
0 1 2 3 4 5 6 7 8 9 10 12 14 16 18 */
2018-09-15 15:50:40 +02:00
0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0, // 0
0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0, // 1
0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,1,0,0, // 2
1,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0, // 3
0,0,0,0,0,0,0,0,0,1,0,0,1,1,1,0,0,1,0,0, // 4
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0, // 5
1,1,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0, // 6
0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0, // 7
0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,1,1,1,1, // 8
0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0, // 9
0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,1, // 10
0,0,0,0,0,1,0,0,0,1,0,0,0,0,1,1,1,0,0,1, // 11
2018-09-15 14:48:01 +02:00
0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0, // 12
2018-09-15 15:50:40 +02:00
0,0,0,0,0,1,0,0,0,1,1,1,0,0,1,0,0,0,0,0, // 13
0,0,0,0,0,1,0,0,0,1,0,0,0,0,1,0,0,0,0,0 // 14
2018-09-15 14:48:01 +02:00
};
Unit heightAt(int16_t x, int16_t y)
{
2018-09-15 15:50:40 +02:00
int32_t index = y * LEVEL_W + x;
if (index < 0 || (index >= LEVEL_W * LEVEL_H))
2018-09-15 14:48:01 +02:00
return UNITS_PER_SQUARE * 2;
2018-09-15 15:50:40 +02:00
return level[y * LEVEL_W + x] * UNITS_PER_SQUARE * 2;
2018-09-15 14:48:01 +02:00
}
void pixelFunc(PixelInfo *p)
{
2018-09-15 15:50:40 +02:00
char c = ' ';
2018-09-15 14:48:01 +02:00
if (p->isWall)
{
switch (p->hit.direction)
{
case 0: c = 'X'; break;
case 1: c = '#'; break;
2018-09-15 15:50:40 +02:00
case 2: c = 'o'; break;
2018-09-15 14:48:01 +02:00
case 3:
2018-09-15 15:50:40 +02:00
default: c = '.'; break;
2018-09-15 14:48:01 +02:00
}
}
pixels[p->position.y * SCREEN_W + p->position.x] = c;
}
void draw()
{
for (int i = 0; i < 10; ++i)
printf("\n");
RayConstraints c;
initRayConstraints(&c);
c.maxHits = 1;
c.maxSteps = 40;
c.computeTextureCoords = 0;
2018-09-15 15:50:40 +02:00
//renderSimple(camera,heightAt,0,pixelFunc,0,c);
render(camera,heightAt,0,0,pixelFunc,c);
2018-09-15 14:48:01 +02:00
for (int j = 0; j < SCREEN_H; ++j)
{
for (int i = 0; i < SCREEN_W; ++i)
printf("%c",pixels[j * SCREEN_W + i]);
printf("\n");
}
}
2018-09-15 15:50:40 +02:00
int dx = 1;
int dy = 0;
int dr = 1;
int frame = 0;
2018-09-15 14:48:01 +02:00
int main()
{
initCamera(&camera);
camera.position.x = 2 * UNITS_PER_SQUARE;
camera.position.y = 2 * UNITS_PER_SQUARE;
2018-09-15 15:50:40 +02:00
camera.direction = 0;
2018-09-15 14:48:01 +02:00
camera.resolution.x = SCREEN_W;
camera.resolution.y = SCREEN_H;
2018-09-15 15:50:40 +02:00
for (int i = 0; i < 10000; ++i)
2018-09-15 14:48:01 +02:00
{
draw();
2018-09-15 15:50:40 +02:00
int squareX = divRoundDown(camera.position.x,UNITS_PER_SQUARE);
int squareY = divRoundDown(camera.position.y,UNITS_PER_SQUARE);
if (rand() % 100 == 0)
{
dx = 1 - rand() % 3;
dy = 1 - rand() % 3;
dr = 1 - rand() % 3;
}
while (heightAt(squareX + dx,squareY + dy) > 0)
{
dx = 1 - rand() % 3;
dy = 1 - rand() % 3;
dr = 1 - rand() % 3;
}
camera.position.x += dx * 200;
camera.position.y += dy * 200;
camera.direction += dr * 10;
camera.height = UNITS_PER_SQUARE + sinInt(frame * 16) / 2;
2018-09-15 14:48:01 +02:00
usleep(100000);
2018-09-15 15:50:40 +02:00
frame++;
2018-09-15 14:48:01 +02:00
}
return 0;
}