From 3457570fb677f84f48b3ad6cbd963b45df9baabf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20=C4=8C=C3=AD=C5=BE?= Date: Sat, 8 Sep 2018 14:55:14 +0200 Subject: [PATCH] Move more stuff to general --- demo1.cpp | 67 +++++++++------------------------------------ general.hpp | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+), 54 deletions(-) diff --git a/demo1.cpp b/demo1.cpp index 8af5fdd..6a2ecd8 100644 --- a/demo1.cpp +++ b/demo1.cpp @@ -9,15 +9,18 @@ #include "general.hpp" -#define SPRITES 3 -#define SPRITE_MAX_DISTANCE 5 * UNITS_PER_SQUARE - #define SUBSAMPLE 2 #define SUBSAMPLED_WIDTH (SCREEN_WIDTH / SUBSAMPLE) #define LEVEL_X_RES 29 #define LEVEL_Y_RES 21 +Player player; + +#define SPRITES 3 +#define SPRITE_MAX_DISTANCE 5 * UNITS_PER_SQUARE +Sprite sprites[SPRITES]; + const unsigned char levelTexture[] = { 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 3, 0, 3, 3, 2, 2, 2, 2, @@ -563,36 +566,6 @@ const unsigned char sprite2[] = const unsigned char *textures[] = {texture1, texture2, texture3, texture4}; -class Sprite -{ -public: - const unsigned char *mImage; - Vector2D mPosition; - Unit mHeight; - Unit mPixelSize; -}; - -class Character -{ -public: - Camera mCamera; - Unit mVericalSpeed; - - Character() - { - mCamera.position.x = 23295;//UNITS_PER_SQUARE * 1; - mCamera.position.y = 14593;//UNITS_PER_SQUARE * 5; - mCamera.direction = -3350;//200; - mCamera.height = UNITS_PER_SQUARE * 3; - mCamera.resolution.x = 110 / SUBSAMPLE; - mCamera.resolution.y = 88; - mCamera.shear = 0; - mVericalSpeed = 0; - } -}; - -Character player; -Sprite sprites[SPRITES]; Unit textureAt(int16_t x, int16_t y) { @@ -663,8 +636,6 @@ inline void pixelFunc(PixelInfo pixel) *buf = c; } -unsigned short pal[256]; - void draw() { RayConstraints c; @@ -712,29 +683,17 @@ pokitto.display.print(player.mCamera.direction); int main() { - #define placeSprite(S,I,X,Y,Z,P)\ - sprites[I].mImage = S;\ - sprites[I].mPosition.x = X * UNITS_PER_SQUARE + UNITS_PER_SQUARE / 2;\ - sprites[I].mPosition.y = Y * UNITS_PER_SQUARE + UNITS_PER_SQUARE / 2;\ - sprites[I].mHeight = Z * UNITS_PER_SQUARE + UNITS_PER_SQUARE / 2;\ - sprites[I].mPixelSize = P; - - placeSprite(sprite1,0,10,5,1,100) - placeSprite(sprite1,1,14,5,1,100) - placeSprite(sprite2,2,15,19,1,200) - - #undef placeSprite - pokitto.begin(); + initPalette(); - for (uint8_t r = 0; r < 8; ++r) - for (uint8_t g = 0; g < 8; ++g) - for (uint8_t b = 0; b < 4; ++b) - pal[rgbToIndex(r,g,b)] = pokitto.display.RGBto565(36 * r, 36 * g, 85 * b); + player.setPositionSquare(4,5); + + sprites[0] = Sprite(sprite1,10,5,1,100); + sprites[1] = Sprite(sprite1,14,5,1,100); + sprites[2] = Sprite(sprite1,15,19,1,100); - pokitto.display.load565Palette(&pal[0]); // load a palette the same way as any other palette in any other screen mode pokitto.display.persistence = 1; - pokitto.setFrameRate(30); + pokitto.setFrameRate(FPS); pokitto.display.setFont(fontTiny); while (pokitto.isRunning()) diff --git a/general.hpp b/general.hpp index ae77c2f..48ddfaf 100644 --- a/general.hpp +++ b/general.hpp @@ -17,6 +17,10 @@ Pokitto::Core pokitto; +#ifndef FPS +#define FPS 30 +#endif + #define SCREEN_WIDTH 110 #define SCREEN_HEIGHT 88 @@ -33,6 +37,8 @@ Pokitto::Core pokitto; Unit zBuffer[SUBSAMPLED_WIDTH]; ///< 1D z-buffer for visibility determination. +unsigned short palette[256]; + /** Gets (the index of) color by specified RGB components. @@ -46,6 +52,20 @@ inline uint8_t rgbToIndex(uint8_t r, uint8_t g, uint8_t b) return (r & 0b00000111) | ((g & 0b00000111) << 3) | ((b & 0b00000011) << 6); } +/** + Inits and loads a general 256 color palette. +*/ +void initPalette() +{ + for (uint8_t r = 0; r < 8; ++r) + for (uint8_t g = 0; g < 8; ++g) + for (uint8_t b = 0; b < 4; ++b) + palette[rgbToIndex(r,g,b)] = + pokitto.display.RGBto565(36 * r, 36 * g, 85 * b); + + pokitto.display.load565Palette(palette); +} + /** Adds given intensity to a color. @@ -131,4 +151,62 @@ void inline drawSprite(const unsigned char *sprite, int16_t x, int16_t y, Unit d } } +class Player +{ +public: + Camera mCamera; + Unit mVericalSpeed; + + Player() + { + mCamera.position.x = 0; + mCamera.position.y = 0; + mCamera.direction = 0; + mCamera.height = UNITS_PER_SQUARE * 3; + mCamera.resolution.x = SCREEN_WIDTH / SUBSAMPLE; + mCamera.resolution.y = SCREEN_HEIGHT; + mCamera.shear = 0; + mVericalSpeed = 0; + } + + void setPosition(Unit x, Unit y) + { + mCamera.position.x = x; + mCamera.position.y = y; + } + + void setPositionSquare(int16_t squareX, int16_t squareY) + { + setPosition( + squareX * UNITS_PER_SQUARE + UNITS_PER_SQUARE / 2, + squareY * UNITS_PER_SQUARE + UNITS_PER_SQUARE / 2); + } +}; + +class Sprite +{ +public: + Sprite(const unsigned char *image, int16_t squareX, int16_t squareY, Unit z, + Unit pixelSize): + mImage(image), + mPixelSize(pixelSize) + { + mPosition.x = squareX * UNITS_PER_SQUARE + UNITS_PER_SQUARE / 2; + mPosition.y = squareY * UNITS_PER_SQUARE + UNITS_PER_SQUARE / 2; + mHeight = z * UNITS_PER_SQUARE + UNITS_PER_SQUARE / 2; + } + + Sprite(): + mImage(0), mHeight(0), mPixelSize(1) + { + mPosition.x = 0; + mPosition.y = 0; + } + + const unsigned char *mImage; + Vector2D mPosition; + Unit mHeight; + Unit mPixelSize; +}; + #endif