Optimize sprites even more

This commit is contained in:
Miloslav Číž 2018-09-12 12:01:16 +02:00
parent d54399f7a3
commit 86fc9d3dda
2 changed files with 16 additions and 7 deletions

View file

@ -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);

View file

@ -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);