Fix floor really
[raycastlib.git] / testTerminal.c
blob885cda785d471e11c5f5ae1f08df0c7d0989e6bd
1 #include <stdio.h>
2 #include "raycastlib.h"
3 #include <unistd.h>
4 #include <stdlib.h>
6 #define LEVEL_W 20
7 #define LEVEL_H 15
9 #define SCREEN_W 80
10 #define SCREEN_H 40
12 char pixels[SCREEN_W * SCREEN_H];
13 Camera camera;
15 const int8_t level[LEVEL_W * LEVEL_H] =
17 /* 11 13 15 17 19
18 0 1 2 3 4 5 6 7 8 9 10 12 14 16 18 */
19 0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0, // 0
20 0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0, // 1
21 0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,1,0,0, // 2
22 1,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0, // 3
23 0,0,0,0,0,0,0,0,0,1,0,0,1,1,1,0,0,1,0,0, // 4
24 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0, // 5
25 1,1,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0, // 6
26 0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0, // 7
27 0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,1,1,1,1, // 8
28 0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0, // 9
29 0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,1, // 10
30 0,0,0,0,0,1,0,0,0,1,0,0,0,0,1,1,1,0,0,1, // 11
31 0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0, // 12
32 0,0,0,0,0,1,0,0,0,1,1,1,0,0,1,0,0,0,0,0, // 13
33 0,0,0,0,0,1,0,0,0,1,0,0,0,0,1,0,0,0,0,0 // 14
36 Unit heightAt(int16_t x, int16_t y)
38 int32_t index = y * LEVEL_W + x;
40 if (index < 0 || (index >= LEVEL_W * LEVEL_H))
41 return UNITS_PER_SQUARE * 2;
43 return level[y * LEVEL_W + x] * UNITS_PER_SQUARE * 2;
46 void pixelFunc(PixelInfo *p)
48 char c = ' ';
50 if (p->isWall)
52 switch (p->hit.direction)
54 case 0: c = 'X'; break;
55 case 1: c = '#'; break;
56 case 2: c = 'o'; break;
57 case 3:
58 default: c = '.'; break;
62 pixels[p->position.y * SCREEN_W + p->position.x] = c;
65 void draw()
67 for (int i = 0; i < 10; ++i)
68 printf("\n");
70 RayConstraints c;
72 initRayConstraints(&c);
74 c.maxHits = 1;
75 c.maxSteps = 40;
76 c.computeTextureCoords = 0;
79 //renderSimple(camera,heightAt,0,pixelFunc,0,c);
80 render(camera,heightAt,0,0,pixelFunc,c);
82 for (int j = 0; j < SCREEN_H; ++j)
84 for (int i = 0; i < SCREEN_W; ++i)
85 printf("%c",pixels[j * SCREEN_W + i]);
87 printf("\n");
91 int dx = 1;
92 int dy = 0;
93 int dr = 1;
94 int frame = 0;
96 int main()
98 initCamera(&camera);
99 camera.position.x = 2 * UNITS_PER_SQUARE;
100 camera.position.y = 2 * UNITS_PER_SQUARE;
101 camera.direction = 0;
102 camera.resolution.x = SCREEN_W;
103 camera.resolution.y = SCREEN_H;
105 for (int i = 0; i < 10000; ++i)
107 draw();
109 int squareX = divRoundDown(camera.position.x,UNITS_PER_SQUARE);
110 int squareY = divRoundDown(camera.position.y,UNITS_PER_SQUARE);
112 if (rand() % 100 == 0)
114 dx = 1 - rand() % 3;
115 dy = 1 - rand() % 3;
116 dr = 1 - rand() % 3;
119 while (heightAt(squareX + dx,squareY + dy) > 0)
121 dx = 1 - rand() % 3;
122 dy = 1 - rand() % 3;
123 dr = 1 - rand() % 3;
126 camera.position.x += dx * 200;
127 camera.position.y += dy * 200;
128 camera.direction += dr * 10;
130 camera.height = UNITS_PER_SQUARE + sinInt(frame * 16) / 2;
132 usleep(100000);
134 frame++;
137 return 0;