From 759c525962836d47fef87cbccdaf244b7ed44ec0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20=C4=8C=C3=AD=C5=BE?= Date: Sat, 15 Sep 2018 14:48:01 +0200 Subject: [PATCH] Add terminal test --- raycastlib.h | 7 +--- testTerminal.c | 104 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+), 5 deletions(-) create mode 100644 testTerminal.c diff --git a/raycastlib.h b/raycastlib.h index d6b1096..bccfaec 100644 --- a/raycastlib.h +++ b/raycastlib.h @@ -1071,14 +1071,11 @@ void _columnFunctionSimple(HitResult *hits, uint16_t hitCount, uint16_t x, int16_t wallHeightWorld = _floorFunction(hit.square.x,hit.square.y); - wallHeightWorld = wallHeightWorld != 0 ? wallHeightWorld : 1; - // ^ prevent division by zero - wallHeightScreen = perspectiveScale((wallHeightWorld * _camera.resolution.y) / UNITS_PER_SQUARE,dist); - int16_t normalizedWallHeight = - (UNITS_PER_SQUARE * wallHeightScreen) / wallHeightWorld; + int16_t normalizedWallHeight = wallHeightWorld != 0 ? + ((UNITS_PER_SQUARE * wallHeightScreen) / wallHeightWorld) : 0; heightOffset = perspectiveScale(_cameraHeightScreen,dist); diff --git a/testTerminal.c b/testTerminal.c new file mode 100644 index 0000000..f9cf9f6 --- /dev/null +++ b/testTerminal.c @@ -0,0 +1,104 @@ +#include +#include "raycastlib.h" +#include + +#define LEVEL_W 20 +#define LEVEL_H 15 + +#define SCREEN_W 60 +#define SCREEN_H 20 + +char pixels[SCREEN_W * SCREEN_H]; +Camera camera; + +int8_t level[LEVEL_W * LEVEL_H] = +{ +/* 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,0,0,0, // 0 + 0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0, // 1 + 0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0, // 2 + 0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0, // 3 + 0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0, // 4 + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 5 + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 6 + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 7 + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 8 + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 9 + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 10 + 0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0, // 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,0,0,0,0,0,1,0,0,0,0,0, // 13 + 0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0 // 14 +}; + +Unit heightAt(int16_t x, int16_t y) +{ + if (x < 0 || x >= SCREEN_W || y < 0 || y >= SCREEN_H) + return UNITS_PER_SQUARE * 2; + +return 0; +// return level[y * LEVEL_W + x] * UNITS_PER_SQUARE * 2; +} + +void pixelFunc(PixelInfo *p) +{ + char c = '.'; + + if (p->isWall) + { + switch (p->hit.direction) + { + case 0: c = 'X'; break; + case 1: c = '#'; break; + case 2: c = 'p'; break; + case 3: + default: c = 'o'; break; + } + } + + 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; + + renderSimple(camera,heightAt,0,pixelFunc,0,c); + + 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"); + } +} + +int main() +{ + initCamera(&camera); + camera.position.x = 2 * UNITS_PER_SQUARE; + camera.position.y = 2 * UNITS_PER_SQUARE; + camera.direction = -UNITS_PER_SQUARE / 3; //0;//(3 * UNITS_PER_SQUARE) / 4; + camera.resolution.x = SCREEN_W; + camera.resolution.y = SCREEN_H; + + for (int i = 0; i < 100; ++i) + { + draw(); + camera.position.x += 100; + usleep(100000); + } + + return 0; +}