Optimize sprites

This commit is contained in:
Miloslav Číž 2018-09-11 10:33:47 +02:00
parent bb8be2a13e
commit e39104d8ea
2 changed files with 13 additions and 4 deletions

View file

@ -11,6 +11,7 @@
*/ */
// redefine player's height // redefine player's height
#define PLAYER_SPEED (UNITS_PER_SQUARE * 7)
#define CAMERA_COLL_HEIGHT_BELOW ((3 * UNITS_PER_SQUARE) / 2) #define CAMERA_COLL_HEIGHT_BELOW ((3 * UNITS_PER_SQUARE) / 2)
#define FPS 40 #define FPS 40
#define HEAD_BOB_HEIGHT 150 #define HEAD_BOB_HEIGHT 150
@ -270,7 +271,7 @@ void cameraFlyBy(uint32_t dt)
Unit heightDiff = player.mCamera.height - height; Unit heightDiff = player.mCamera.height - height;
Unit step = (100 * dt) / 1000; Unit step = (200 * dt) / 1000;
if (heightDiff > UNITS_PER_SQUARE * 2) if (heightDiff > UNITS_PER_SQUARE * 2)
{ {
@ -289,7 +290,7 @@ void cameraFlyBy(uint32_t dt)
player.mCamera.position.x += step * 30; player.mCamera.position.x += step * 30;
player.mCamera.position.y += step * 15; player.mCamera.position.y += step * 15;
player.mCamera.direction = sinInt(pokitto.frameCount / 4); player.mCamera.direction = sinInt(pokitto.frameCount / 8);
} }
int main() int main()

View file

@ -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) 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; x -= size / 2;
y -= 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) 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) if (c != TRANSPARENT_COLOR)
pokitto.display.drawPixel(xPos,y + j,c); pokitto.display.drawPixel(xPos,y + j,c);