Pokitto-Raycasting/helloRay.cpp

110 lines
3.4 KiB
C++
Raw Permalink Normal View History

2018-09-17 08:19:29 +02:00
/**
Raycasting hello world program for Pokitto, using raycastlib.
author: Miloslav "drummyfish" Ciz
license: CC0 1.0
*/
2018-09-17 17:55:33 +02:00
#define RCL_PIXEL_FUNCTION pixelFunc
2018-09-17 08:19:29 +02:00
/* ^ Before including raycastlib, this has to be set to the name of the
function that will render pixels. It allows super performance. */
2018-09-18 15:14:27 +02:00
#define RCL_COMPUTE_WALL_TEXCOORDS 0
2018-09-30 14:12:21 +02:00
#define RCL_COMPUTE_FLOOR_TEXCOORDS 0
#define RCL_COMPUTE_FLOOR_DEPTH 0
#define RCL_COMPUTE_FLOOR_DEPTH 0
#define RCL_COMPUTE_CEILING_DEPTH 0
/* ^ Turn off features we won't be using, so that the program runs faster.
2018-09-18 15:14:27 +02:00
2018-09-30 14:12:21 +02:00
There are many other options that can be defined here, check the library
for details. */
2018-09-17 08:19:29 +02:00
#include "raycastlib.h"
#include "Pokitto.h"
Pokitto::Core pokitto;
2018-09-30 14:12:21 +02:00
RCL_Camera camera; // Defines a view that will be rendered.
RCL_RayConstraints constraints; // Says the details of casting individual rays.
2018-09-17 08:19:29 +02:00
2018-09-18 15:01:01 +02:00
/* Function that for given square coordinates returns height of the floor
(in RCL_Units). */
2018-09-17 17:55:33 +02:00
RCL_Unit floorHeightAt(int16_t x, int16_t y)
2018-09-17 08:19:29 +02:00
{
return x < 0 || x >= 10 || y < 0 || y >= 10 ?
2018-09-17 17:55:33 +02:00
RCL_UNITS_PER_SQUARE * 2 : 0;
2018-09-18 15:01:01 +02:00
/* ^ RCL_UNITS_PER_SQUARE is the length of one side of the game square.
Since we'll place the camera at RCL_UNITS_PER_SQUARE height, let's
make the walls twice as high. */
/* You can either generate the level procedurally as above, or read it from
2018-09-30 14:12:21 +02:00
an array and return it here. You can also animate the level here, there
are no limits. Just keep in mind this function should be fast because
it will get called a lot. */
2018-09-17 08:19:29 +02:00
}
2018-09-18 15:01:01 +02:00
/* Function that will be called by the library in order to draw individual
pixels (similar to fragment shaders in OpenGL). */
2018-09-17 17:55:33 +02:00
void pixelFunc(RCL_PixelInfo *pixel)
2018-09-17 08:19:29 +02:00
{
uint8_t color;
2018-09-18 15:01:01 +02:00
/* The pixel variable holds all kind of info about the pixel to be rendered.
2018-09-30 14:12:21 +02:00
Check the RCL_PixelInfo struct for details. */
2018-09-17 08:19:29 +02:00
if (pixel->isWall)
2018-09-18 15:01:01 +02:00
color = pixel->hit.direction + 2; // give walls different colors
2018-09-17 08:19:29 +02:00
else
2018-09-18 15:01:01 +02:00
color = pixel->isFloor ? 10 : 11; // also make ceiling and floor differ
/* You can do all kinds of processing here and draw the pixel wherever you
want, or even discard it. Just remember this function has to be fast and
will usually be the bottleneck. */
2018-09-17 08:19:29 +02:00
pokitto.display.drawPixel(pixel->position.x,pixel->position.y,color);
}
void draw()
{
/* This triggers the rendering, which will keep calling pixelFunc to render
the camera view. */
2018-09-18 15:01:01 +02:00
2018-09-17 17:55:33 +02:00
RCL_renderSimple(camera,floorHeightAt,0,0,constraints);
2018-09-17 08:19:29 +02:00
}
int main()
{
pokitto.begin();
pokitto.setFrameRate(60);
2018-09-18 15:01:01 +02:00
RCL_initCamera(&camera); /* To initialize all parameters so that none
remains undefined. */
2018-09-17 08:19:29 +02:00
2018-09-30 14:12:21 +02:00
// Set the camera position to the middle of square [2;3].
camera.position.x = 2 * RCL_UNITS_PER_SQUARE + RCL_UNITS_PER_SQUARE / 2;
camera.position.y = 3 * RCL_UNITS_PER_SQUARE + RCL_UNITS_PER_SQUARE / 2;
2018-09-17 08:19:29 +02:00
2018-09-17 17:55:33 +02:00
camera.height = RCL_UNITS_PER_SQUARE;
2018-09-17 08:19:29 +02:00
// Set the camera resolution to Pokitto display resolution.
camera.resolution.x = 110;
camera.resolution.y = 88;
2018-09-17 17:55:33 +02:00
// This specifies the ray behavior.
2018-09-18 15:05:36 +02:00
RCL_initRayConstraints(&constraints);
2018-09-18 15:01:01 +02:00
constraints.maxHits = 1; // Stop at first intersection with a wall.
constraints.maxSteps = 20; // Trace maximum of 20 squares.
2018-09-17 17:55:33 +02:00
2018-09-17 08:19:29 +02:00
while (pokitto.isRunning())
{
if (pokitto.update())
{
draw();
2018-09-18 15:01:01 +02:00
camera.direction -= 10; // Rotate the camera for some animation.
2018-09-17 08:19:29 +02:00
}
}
return 0;
}