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

166 lines
3.8 KiB
C
Raw Normal View History

2019-06-04 19:27:00 +02:00
/*
2020-06-21 12:02:37 +02:00
Raycasting terminal test. Renders a raycasted animation in the terminal as
ASCII.
2019-06-04 19:27:00 +02:00
author: Miloslav Ciz
2020-06-20 22:34:10 +02:00
license: CC0 1.0, public domain
2019-06-04 19:27:00 +02:00
*/
2020-06-20 22:34:10 +02:00
#define RCL_PIXEL_FUNCTION pixelFunc // set our pixel functio
#define RCL_COMPUTE_FLOOR_DEPTH 0 // turn off what we don't need
2019-06-04 19:27:00 +02:00
#define RCL_COMPUTE_CEILING_DEPTH 0
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
2020-06-20 22:34:10 +02:00
#include <string.h>
#include "../raycastlib.h"
2019-06-04 19:27:00 +02:00
#define LEVEL_W 20
#define LEVEL_H 15
#define SCREEN_W 80
#define SCREEN_H 40
2020-06-20 22:34:10 +02:00
#define FRAME_OFFSET 20 // number of newlines printed before each frame
#define PIXELS_TOTAL (FRAME_OFFSET+ (SCREEN_W + 1) * SCREEN_H + 1)
char pixels[FRAME_OFFSET + (SCREEN_W + 1) * SCREEN_H + 1];
2019-06-04 19:27:00 +02:00
RCL_Camera camera;
2020-06-20 22:34:10 +02:00
const int8_t level[LEVEL_W * LEVEL_H] = // here 1 means wall, 0 floor
2019-06-04 19:27:00 +02:00
{
/* 11 13 15 17 19
0 1 2 3 4 5 6 7 8 9 10 12 14 16 18 */
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
0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0, // 12
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
};
2020-06-20 22:34:10 +02:00
/*
Function that says the floor height at each square. We do it by reading the
anove level array.
*/
2019-06-04 19:27:00 +02:00
RCL_Unit heightAt(int16_t x, int16_t y)
{
int32_t index = y * LEVEL_W + x;
if (index < 0 || (index >= LEVEL_W * LEVEL_H))
return RCL_UNITS_PER_SQUARE * 2;
return level[y * LEVEL_W + x] * RCL_UNITS_PER_SQUARE * 2;
}
2020-06-20 22:34:10 +02:00
static const char asciiShades[] = "HXi/;,. ";
2019-06-04 19:27:00 +02:00
void pixelFunc(RCL_PixelInfo *p)
{
char c = ' ';
2020-06-20 22:34:10 +02:00
uint8_t shade = 3;
shade -= RCL_min(3,p->depth / RCL_UNITS_PER_SQUARE);
2019-06-04 19:27:00 +02:00
if (p->isWall)
{
switch (p->hit.direction)
{
2020-06-20 22:34:10 +02:00
case 0: shade += 2;
2020-06-21 12:02:37 +02:00
case 1: c = asciiShades[shade];
2020-06-20 22:34:10 +02:00
break;
2019-06-04 19:27:00 +02:00
case 2: c = 'o'; break;
case 3:
default: c = '.'; break;
}
}
2020-06-20 22:34:10 +02:00
pixels[FRAME_OFFSET + p->position.y * (SCREEN_W + 1) + p->position.x] = c;
2019-06-04 19:27:00 +02:00
}
void draw()
{
2020-06-20 22:34:10 +02:00
memset(pixels,'\n',PIXELS_TOTAL);
pixels[PIXELS_TOTAL - 1] = 0; // terminate string
2019-06-04 19:27:00 +02:00
RCL_RayConstraints c;
RCL_initRayConstraints(&c);
c.maxHits = 1;
c.maxSteps = 40;
2020-06-20 22:34:10 +02:00
#if 1
RCL_renderSimple(camera,heightAt,0,0,c);
#else
/* Here you can try using the complex rendering function. The result should
be practically the same. */
2019-06-04 19:27:00 +02:00
2020-06-20 22:34:10 +02:00
RCL_renderComplex(camera,heightAt,0,0,c);
#endif
2019-06-04 19:27:00 +02:00
2020-06-20 22:34:10 +02:00
puts(pixels);
2019-06-04 19:27:00 +02:00
}
int dx = 1;
int dy = 0;
int dr = 1;
int frame = 0;
int main()
{
RCL_initCamera(&camera);
camera.position.x = 2 * RCL_UNITS_PER_SQUARE;
camera.position.y = 2 * RCL_UNITS_PER_SQUARE;
camera.direction = 0;
camera.resolution.x = SCREEN_W;
camera.resolution.y = SCREEN_H;
for (int i = 0; i < 10000; ++i)
{
draw();
int squareX = RCL_divRoundDown(camera.position.x,RCL_UNITS_PER_SQUARE);
int squareY = RCL_divRoundDown(camera.position.y,RCL_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;
2020-06-21 11:39:52 +02:00
camera.height = RCL_UNITS_PER_SQUARE + RCL_sin(frame * 16) / 2;
2019-06-04 19:27:00 +02:00
usleep(100000);
frame++;
}
return 0;
}