Optimize floor precomputation
[raycastlib.git] / testTerminal.c
blobda1748ba74ffd6c651d7bb72d08da8c8c4b66cad
1 /*
2 Raycasting terminal test.
4 author: Miloslav Ciz
5 license: CC0
6 */
8 #define RCL_PIXEL_FUNCTION pixelFunc
9 #define RCL_COMPUTE_WALL_TEXCOORDS 0
10 #define RCL_COMPUTE_FLOOR_DEPTH 0
11 #define RCL_COMPUTE_CEILING_DEPTH 0
13 #include <stdio.h>
14 #include "raycastlib.h"
15 #include <unistd.h>
16 #include <stdlib.h>
18 #define LEVEL_W 20
19 #define LEVEL_H 15
21 #define SCREEN_W 80
22 #define SCREEN_H 40
24 char pixels[SCREEN_W * SCREEN_H];
25 RCL_Camera camera;
27 const int8_t level[LEVEL_W * LEVEL_H] =
29 /* 11 13 15 17 19
30 0 1 2 3 4 5 6 7 8 9 10 12 14 16 18 */
31 0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0, // 0
32 0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0, // 1
33 0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,1,0,0, // 2
34 1,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0, // 3
35 0,0,0,0,0,0,0,0,0,1,0,0,1,1,1,0,0,1,0,0, // 4
36 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0, // 5
37 1,1,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0, // 6
38 0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0, // 7
39 0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,1,1,1,1, // 8
40 0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0, // 9
41 0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,1, // 10
42 0,0,0,0,0,1,0,0,0,1,0,0,0,0,1,1,1,0,0,1, // 11
43 0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0, // 12
44 0,0,0,0,0,1,0,0,0,1,1,1,0,0,1,0,0,0,0,0, // 13
45 0,0,0,0,0,1,0,0,0,1,0,0,0,0,1,0,0,0,0,0 // 14
48 RCL_Unit heightAt(int16_t x, int16_t y)
50 int32_t index = y * LEVEL_W + x;
52 if (index < 0 || (index >= LEVEL_W * LEVEL_H))
53 return RCL_UNITS_PER_SQUARE * 2;
55 return level[y * LEVEL_W + x] * RCL_UNITS_PER_SQUARE * 2;
58 void pixelFunc(RCL_PixelInfo *p)
60 char c = ' ';
62 if (p->isWall)
64 switch (p->hit.direction)
66 case 0: c = 'X'; break;
67 case 1: c = '#'; break;
68 case 2: c = 'o'; break;
69 case 3:
70 default: c = '.'; break;
74 pixels[p->position.y * SCREEN_W + p->position.x] = c;
77 void draw()
79 for (int i = 0; i < 10; ++i)
80 printf("\n");
82 RCL_RayConstraints c;
84 RCL_initRayConstraints(&c);
86 c.maxHits = 1;
87 c.maxSteps = 40;
89 RCL_renderSimple(camera,heightAt,0,0,c);
90 //RCL_renderComplex(camera,heightAt,0,0,c);
92 for (int j = 0; j < SCREEN_H; ++j)
94 for (int i = 0; i < SCREEN_W; ++i)
95 printf("%c",pixels[j * SCREEN_W + i]);
97 printf("\n");
101 int dx = 1;
102 int dy = 0;
103 int dr = 1;
104 int frame = 0;
106 int main()
108 RCL_initCamera(&camera);
109 camera.position.x = 2 * RCL_UNITS_PER_SQUARE;
110 camera.position.y = 2 * RCL_UNITS_PER_SQUARE;
111 camera.direction = 0;
112 camera.resolution.x = SCREEN_W;
113 camera.resolution.y = SCREEN_H;
115 for (int i = 0; i < 10000; ++i)
117 draw();
119 int squareX = RCL_divRoundDown(camera.position.x,RCL_UNITS_PER_SQUARE);
120 int squareY = RCL_divRoundDown(camera.position.y,RCL_UNITS_PER_SQUARE);
122 if (rand() % 100 == 0)
124 dx = 1 - rand() % 3;
125 dy = 1 - rand() % 3;
126 dr = 1 - rand() % 3;
129 while (heightAt(squareX + dx,squareY + dy) > 0)
131 dx = 1 - rand() % 3;
132 dy = 1 - rand() % 3;
133 dr = 1 - rand() % 3;
136 camera.position.x += dx * 200;
137 camera.position.y += dy * 200;
138 camera.direction += dr * 10;
140 camera.height = RCL_UNITS_PER_SQUARE + RCL_sinInt(frame * 16) / 2;
142 usleep(100000);
144 frame++;
147 return 0;