From 86fc9d3ddadb1512fddf34dc74e0f2fefd510e25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20=C4=8C=C3=AD=C5=BE?= Date: Wed, 12 Sep 2018 12:01:16 +0200 Subject: [PATCH] Optimize sprites even more --- demo1.cpp | 8 +++++--- general.hpp | 15 +++++++++++---- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/demo1.cpp b/demo1.cpp index 7206ae9..a2a1fea 100644 --- a/demo1.cpp +++ b/demo1.cpp @@ -756,9 +756,12 @@ void draw() PixelInfo pos = mapToScreen(sprites[i].mPosition,sprites[i].mHeight,player.mCamera); if (pos.depth > 0) - drawSprite(sprites[i].mImage,pos.position.x * SUBSAMPLE,pos.position.y, - pos.depth,perspectiveScale(sprites[i].mPixelSize,pos.depth)); + drawSpriteSquare(sprites[i].mImage,pos.position.x * SUBSAMPLE, + pos.position.y, pos.depth, + perspectiveScale(sprites[i].mPixelSize,pos.depth)); + /* trick: sort the sprites by distance with bubble sort as we draw - the + order will be correct in a few frames */ if (i != 0 && pos.depth > previousDepth) { Sprite tmp = sprites[i]; @@ -791,7 +794,6 @@ int main() initGeneral(); player.setPositionSquare(6,4); -player.setPosition(7119,14343,5120,566); sprites[0] = Sprite(sprite1,10,5,1,100); sprites[1] = Sprite(sprite1,14,5,1,100); diff --git a/general.hpp b/general.hpp index fac169a..4d6c7c9 100644 --- a/general.hpp +++ b/general.hpp @@ -177,9 +177,14 @@ inline uint8_t sampleImage(const unsigned char *image, Unit x, Unit y) return image[2 + index]; } -void inline drawSprite(const unsigned char *sprite, int16_t x, int16_t y, Unit depth, int16_t size) +/** + Draws a scaled sprite on screen in an optimized way. The sprite has to be + square in resolution. +*/ +void inline drawSpriteSquare(const unsigned char *sprite, int16_t x, int16_t y, Unit depth, int16_t size) { - if (size < 0 || size > 200) // let's not mess up with the incoming array + if (size < 0 || size > 200 || // let's not mess up with the incoming array + sprite[0] != sprite[1]) // only draw square sprites return; int16_t samplingIndices[size]; @@ -187,7 +192,7 @@ void inline drawSprite(const unsigned char *sprite, int16_t x, int16_t y, Unit d // optimization: precompute the indices for (Unit i = 0; i < size; ++i) - samplingIndices[i] = (i * UNITS_PER_SQUARE) / size; + samplingIndices[i] = (i * sprite[0]) / size; x -= size / 2; y -= size / 2; @@ -206,9 +211,11 @@ void inline drawSprite(const unsigned char *sprite, int16_t x, int16_t y, Unit d if (zBuffer[xPos / SUBSAMPLE] <= depth) continue; + int16_t columnLocation = 2 + samplingIndices[i] * sprite[0]; + for (Unit j = max(-1 * y,0); j < jTo; ++j) { - c = sampleImage(sprite,samplingIndices[i],samplingIndices[j]); + c = sprite[columnLocation + samplingIndices[j]]; if (c != TRANSPARENT_COLOR) pokitto.display.drawPixel(xPos,y + j,c);