Update hello ray
This commit is contained in:
parent
d1083dbd93
commit
24165c9544
1 changed files with 27 additions and 12 deletions
39
helloRay.cpp
39
helloRay.cpp
|
@ -10,7 +10,8 @@
|
||||||
function that will render pixels. It allows super performance. */
|
function that will render pixels. It allows super performance. */
|
||||||
|
|
||||||
/* There are many other options that can be defined here, check the library
|
/* There are many other options that can be defined here, check the library
|
||||||
for details. */
|
for details. Don't forget to disable the features you won't be using - it
|
||||||
|
will speed up the rendering. */
|
||||||
|
|
||||||
#include "raycastlib.h"
|
#include "raycastlib.h"
|
||||||
#include "Pokitto.h"
|
#include "Pokitto.h"
|
||||||
|
@ -20,26 +21,38 @@ Pokitto::Core pokitto;
|
||||||
RCL_Camera camera; // Defines a view that will be rendered.
|
RCL_Camera camera; // Defines a view that will be rendered.
|
||||||
RCL_RayConstraints constraints;
|
RCL_RayConstraints constraints;
|
||||||
|
|
||||||
// Function that for given square coordinates returns height of the floor.
|
/* Function that for given square coordinates returns height of the floor
|
||||||
|
(in RCL_Units). */
|
||||||
RCL_Unit floorHeightAt(int16_t x, int16_t y)
|
RCL_Unit floorHeightAt(int16_t x, int16_t y)
|
||||||
{
|
{
|
||||||
return x < 0 || x >= 10 || y < 0 || y >= 10 ?
|
return x < 0 || x >= 10 || y < 0 || y >= 10 ?
|
||||||
RCL_UNITS_PER_SQUARE * 2 : 0;
|
RCL_UNITS_PER_SQUARE * 2 : 0;
|
||||||
// ^ RCL_UNITS_PER_SQUARE is the length of one side of the game world square.
|
/* ^ 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
|
||||||
|
an array and return it here. */
|
||||||
}
|
}
|
||||||
|
|
||||||
// Function which the library will call to draw indivifual pixels.
|
/* Function that will be called by the library in order to draw individual
|
||||||
|
pixels (similar to fragment shaders in OpenGL). */
|
||||||
|
|
||||||
void pixelFunc(RCL_PixelInfo *pixel)
|
void pixelFunc(RCL_PixelInfo *pixel)
|
||||||
{
|
{
|
||||||
uint8_t color;
|
uint8_t color;
|
||||||
|
|
||||||
/* Pixel holds all kind of info about the pixel to be rendered. Check
|
/* The pixel variable holds all kind of info about the pixel to be rendered.
|
||||||
the PixelInfo struct for details. */
|
Check the PixelInfo struct for details. */
|
||||||
|
|
||||||
if (pixel->isWall)
|
if (pixel->isWall)
|
||||||
color = pixel->hit.direction + 2;
|
color = pixel->hit.direction + 2; // give walls different colors
|
||||||
else
|
else
|
||||||
color = pixel->isFloor ? 10 : 11;
|
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. */
|
||||||
|
|
||||||
pokitto.display.drawPixel(pixel->position.x,pixel->position.y,color);
|
pokitto.display.drawPixel(pixel->position.x,pixel->position.y,color);
|
||||||
}
|
}
|
||||||
|
@ -48,6 +61,7 @@ void draw()
|
||||||
{
|
{
|
||||||
/* This triggers the rendering, which will keep calling pixelFunc to render
|
/* This triggers the rendering, which will keep calling pixelFunc to render
|
||||||
the camera view. */
|
the camera view. */
|
||||||
|
|
||||||
RCL_renderSimple(camera,floorHeightAt,0,0,constraints);
|
RCL_renderSimple(camera,floorHeightAt,0,0,constraints);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -56,7 +70,8 @@ int main()
|
||||||
pokitto.begin();
|
pokitto.begin();
|
||||||
pokitto.setFrameRate(60);
|
pokitto.setFrameRate(60);
|
||||||
|
|
||||||
RCL_initCamera(&camera);
|
RCL_initCamera(&camera); /* To initialize all parameters so that none
|
||||||
|
remains undefined. */
|
||||||
|
|
||||||
// Set the camera position to square [4;6].
|
// Set the camera position to square [4;6].
|
||||||
camera.position.x = 4 * RCL_UNITS_PER_SQUARE;
|
camera.position.x = 4 * RCL_UNITS_PER_SQUARE;
|
||||||
|
@ -69,15 +84,15 @@ int main()
|
||||||
camera.resolution.y = 88;
|
camera.resolution.y = 88;
|
||||||
|
|
||||||
// This specifies the ray behavior.
|
// This specifies the ray behavior.
|
||||||
constraints.maxHits = 1;
|
constraints.maxHits = 1; // Stop at first intersection with a wall.
|
||||||
constraints.maxSteps = 20;
|
constraints.maxSteps = 20; // Trace maximum of 20 squares.
|
||||||
|
|
||||||
while (pokitto.isRunning())
|
while (pokitto.isRunning())
|
||||||
{
|
{
|
||||||
if (pokitto.update())
|
if (pokitto.update())
|
||||||
{
|
{
|
||||||
draw();
|
draw();
|
||||||
camera.direction -= 10; // Rotate camera for some animation.
|
camera.direction -= 10; // Rotate the camera for some animation.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue