Miloslav Číž / raycasting

Dependencies:   PokittoLib

Fork of HelloWorld by Pokitto Community Team

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /**
00002   WIP raycasting demo for Pokitto.
00003 
00004   author: Miloslav "drummyfish" Ciz
00005   license: CC0
00006  */
00007 
00008 #include <stdio.h>
00009 #include "raycastlib.h"
00010 #include "Pokitto.h"
00011 #include <stdlib.h>
00012 
00013 class Level
00014 {
00015 public:
00016   int16_t getHeight(int16_t x, int16_t y)
00017   {
00018     if (x > 12 || y > 12)
00019       return max(x,y) - 10;
00020 
00021     if (y < 5 && y > 2)
00022       return x;
00023 
00024     return (x < 0 || y < 0 || x > 9 || y > 9) ? 4 : 0;
00025   }
00026 };
00027 
00028 class Character
00029 {
00030 public:
00031   Camera mCamera;
00032 
00033   Character()
00034   {
00035     mCamera.position.x = 400;
00036     mCamera.position.y = 6811;
00037     mCamera.direction = 660;
00038     mCamera.fovAngle = UNITS_PER_SQUARE / 4;
00039     mCamera.height = 0;
00040     mCamera.resolution.x = 36;
00041     mCamera.resolution.y = 88;
00042   } 
00043 };
00044 
00045 Pokitto::Core p;
00046 Character player;
00047 Level level;
00048 
00049 Unit heightFunc(int16_t x, int16_t y)
00050 {
00051   return level.getHeight(x,y) * UNITS_PER_SQUARE / 4;
00052 }
00053 
00054 bool dither(uint8_t intensity, uint32_t x, uint32_t y)
00055 {
00056   switch (intensity)
00057   {
00058     case 0: return false; break;
00059     case 1: return x % 2 == 0 && y % 2 == 0; break;
00060     case 2: return x % 2 == y % 2; break;
00061     case 3: return x % 2 != 0 || y % 2 != 0; break;
00062     default: return true; break;
00063   }
00064 }
00065 
00066 void pixelFunc(PixelInfo pixel)
00067 {
00068   uint8_t c = pixel.isWall ? pixel.hit.direction + 4 : 3;
00069 
00070   uint16_t x = pixel.position.x * 3;
00071   uint16_t y = pixel.position.y;
00072 
00073   uint8_t d = pixel.depth / (UNITS_PER_SQUARE * 2);
00074 
00075   p.display.color = dither(d,x,y) ? 0 : c;
00076   p.display.drawPixel(x,pixel.position.y);
00077 
00078   x++;
00079 
00080   p.display.color = dither(d,x,y) ? 0 : c;
00081   p.display.drawPixel(x,pixel.position.y);
00082 
00083   x++;
00084 
00085   p.display.color = dither(d,x,y) ? 0 : c;
00086   p.display.drawPixel(x,pixel.position.y);
00087 }
00088 
00089 void draw()
00090 {
00091 
00092   RayConstraints c;
00093 
00094   c.maxHits = 3;
00095   c.maxSteps = 10;
00096 
00097   render(player.mCamera,heightFunc,pixelFunc,c);
00098 
00099 }
00100 
00101 int main()
00102 {
00103   p.begin();
00104 
00105   p.setFrameRate(30);
00106 
00107   p.display.setFont(fontTiny);
00108 
00109   while (p.isRunning())
00110   {
00111     if (p.update())
00112     {
00113       draw();
00114 
00115       const int16_t step = 50;
00116       const int16_t step2 = 10;
00117 
00118       Vector2D d = angleToDirection(player.mCamera.direction);
00119 
00120       d.x = (d.x * step) / UNITS_PER_SQUARE;
00121       d.y = (d.y * step) / UNITS_PER_SQUARE;
00122 
00123       if (p.upBtn())
00124       {
00125         player.mCamera.position.x += d.x;
00126         player.mCamera.position.y += d.y;
00127       }
00128       else if (p.downBtn())
00129       {
00130         player.mCamera.position.x -= d.x;
00131         player.mCamera.position.y -= d.y;
00132       }
00133 
00134       if (p.rightBtn())
00135         player.mCamera.direction += step2;
00136       else if (p.leftBtn())
00137         player.mCamera.direction -= step2;
00138 
00139       if (p.bBtn())
00140         player.mCamera.height += step;
00141       else if (p.cBtn())
00142         player.mCamera.height -= step;
00143     }
00144   }
00145 
00146   return 0;
00147 }