diff --git a/helloRay.cpp b/helloRay.cpp new file mode 100644 index 0000000..eb50f09 --- /dev/null +++ b/helloRay.cpp @@ -0,0 +1,85 @@ +/** + Raycasting hello world program for Pokitto, using raycastlib. + + author: Miloslav "drummyfish" Ciz + license: CC0 1.0 +*/ + +#define PIXEL_FUNCTION pixelFunc +/* ^ Before including raycastlib, this has to be set to the name of the + function that will render pixels. It allows super performance. */ + +/* There are many other options that can be defined here, check the library + for details. */ + +#include "raycastlib.h" +#include "Pokitto.h" + +Pokitto::Core pokitto; + +Camera camera; // Defines a view that will be rendered. + +// Function that for given square coordinates returns height of the floor. +Unit floorHeightAt(int16_t x, int16_t y) +{ + return x < 0 || x >= 10 || y < 0 || y >= 10 ? + UNITS_PER_SQUARE * 2 : 0; + // ^ UNITS_PER_SQUARE is the length of one side of the game world square. +} + +// Function which the library will call to draw indivifual pixels. +void pixelFunc(PixelInfo *pixel) +{ + uint8_t color; + + /* Pixel holds all kind of info about the pixel to be rendered. Check + the PixelInfo struct for details. */ + + if (pixel->isWall) + color = pixel->hit.direction + 2; + else + color = pixel->isFloor ? 10 : 11; + + pokitto.display.drawPixel(pixel->position.x,pixel->position.y,color); +} + +void draw() +{ + RayConstraints c; + + c.maxHits = 1; + c.maxSteps = 20; + + /* This triggers the rendering, which will keep calling pixelFunc to render + the camera view. */ + renderSimple(camera,floorHeightAt,0,0,c); +} + +int main() +{ + pokitto.begin(); + pokitto.setFrameRate(60); + + initCamera(&camera); + + // Set the camera position to square [4;6]. + camera.position.x = 4 * UNITS_PER_SQUARE; + camera.position.y = 6 * UNITS_PER_SQUARE; + + camera.height = UNITS_PER_SQUARE; + + // Set the camera resolution to Pokitto display resolution. + camera.resolution.x = 110; + camera.resolution.y = 88; + + while (pokitto.isRunning()) + { + if (pokitto.update()) + { + draw(); + camera.direction -= 10; // Rotate camera for some animation. + } + } + + return 0; +}