From e39104d8ea66ccc7a2919eb65bf5b46d187297f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20=C4=8C=C3=AD=C5=BE?= Date: Tue, 11 Sep 2018 10:33:47 +0200 Subject: [PATCH] Optimize sprites --- demo3.cpp | 5 +++-- general.hpp | 12 ++++++++++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/demo3.cpp b/demo3.cpp index 4469723..94b0609 100644 --- a/demo3.cpp +++ b/demo3.cpp @@ -11,6 +11,7 @@ */ // redefine player's height +#define PLAYER_SPEED (UNITS_PER_SQUARE * 7) #define CAMERA_COLL_HEIGHT_BELOW ((3 * UNITS_PER_SQUARE) / 2) #define FPS 40 #define HEAD_BOB_HEIGHT 150 @@ -270,7 +271,7 @@ void cameraFlyBy(uint32_t dt) Unit heightDiff = player.mCamera.height - height; - Unit step = (100 * dt) / 1000; + Unit step = (200 * dt) / 1000; if (heightDiff > UNITS_PER_SQUARE * 2) { @@ -289,7 +290,7 @@ void cameraFlyBy(uint32_t dt) player.mCamera.position.x += step * 30; player.mCamera.position.y += step * 15; - player.mCamera.direction = sinInt(pokitto.frameCount / 4); + player.mCamera.direction = sinInt(pokitto.frameCount / 8); } int main() diff --git a/general.hpp b/general.hpp index b470ff7..f07a0d5 100644 --- a/general.hpp +++ b/general.hpp @@ -163,7 +163,15 @@ inline uint8_t sampleImage(const unsigned char *image, Unit x, Unit y) void inline drawSprite(const unsigned char *sprite, int16_t x, int16_t y, Unit depth, int16_t size) { - // TODO: optimize + if (size < 0 || size > 200) // let's not mess up with the incoming array + return; + + int16_t samplingIndices[size]; + + // optimization: precompute the indices + + for (Unit i = 0; i < size; ++i) + samplingIndices[i] = (i * UNITS_PER_SQUARE) / size; x -= size / 2; y -= size / 2; @@ -184,7 +192,7 @@ void inline drawSprite(const unsigned char *sprite, int16_t x, int16_t y, Unit d for (Unit j = max(-1 * y,0); j < jTo; ++j) { - c = sampleImage(sprite,(i * UNITS_PER_SQUARE) / size,(j * UNITS_PER_SQUARE) / size); + c = sampleImage(sprite,samplingIndices[i],samplingIndices[j]); if (c != TRANSPARENT_COLOR) pokitto.display.drawPixel(xPos,y + j,c);