Add sprite drawing

This commit is contained in:
Miloslav Číž 2018-09-03 15:48:50 +02:00
parent e230cf96fb
commit b398ecf241

108
game.cpp
View file

@ -15,6 +15,8 @@
#define SUBSAMPLE 2
Pokitto::Core p;
const unsigned char image[] =
{ 32, 32 // width, height
,0x12,0x12,0x12,0x12,0x12,0x12,0x00,0x09,0x00,0x12,0x12,0x12,0x12,0x12
@ -249,7 +251,85 @@ const unsigned char image3[] =
,0x1d,0x25
};
uint8_t sampleImage(const unsigned char *image, Unit x, Unit y)
const unsigned char sprite[] =
{ 32, 32 // width, height
,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00,0x00,0x00,0x00,0x00,0x00
,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
,0x00,0x00,0x00,0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00,0x64,0x76
,0xff,0xff,0x76,0x76,0xff,0x76,0x76,0xff,0x76,0x76,0x76,0x76,0x76,0x64
,0x76,0x76,0x76,0x64,0x76,0x64,0x09,0x09,0x00,0xff,0xff,0xff,0xff,0xff
,0x00,0x52,0x76,0xff,0xff,0x76,0x76,0xff,0x76,0x76,0x76,0x76,0x76,0x76
,0x76,0x64,0x76,0x76,0x76,0x76,0x76,0x76,0x64,0x64,0x09,0x09,0x00,0x00
,0xff,0xff,0xff,0xff,0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
,0xff,0xff,0xff,0x76,0xff,0x76,0x76,0x76,0xff,0xff,0x76,0x64,0x76,0x52
,0x09,0x09,0x52,0x00,0xff,0xff,0xff,0xff,0xff,0x00,0x52,0x52,0x52,0x52
,0x64,0x64,0x52,0x52,0x64,0x64,0x52,0x64,0x52,0x52,0x52,0x52,0x52,0xff
,0x76,0x76,0x64,0x09,0x09,0x09,0x00,0xff,0xff,0xff,0xff,0xff,0xff,0x00
,0x64,0x64,0x52,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x52,0x64,0x52
,0x52,0x52,0x64,0xff,0x76,0x76,0x52,0x09,0x09,0x52,0x00,0xff,0xff,0xff
,0xff,0xff,0xff,0xff,0x00,0x52,0x64,0x64,0x64,0x64,0x64,0x52,0x64,0x52
,0x64,0x64,0x52,0x64,0x52,0x52,0xff,0x76,0x76,0x64,0x09,0x09,0x52,0x00
,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00,0x64,0x52,0x64,0x64,0x09
,0x09,0x09,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0x76,0x76,0x52
,0x09,0x09,0x52,0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00
,0x64,0x64,0x64,0x52,0x09,0x09,0x09,0x00,0xff,0xff,0xff,0xff,0x00,0x76
,0x76,0x76,0x64,0x09,0x09,0x52,0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff
,0xff,0xff,0xff,0x00,0x64,0x64,0x64,0x64,0x52,0x09,0x52,0x00,0xff,0xff
,0xff,0xff,0x00,0xff,0x76,0x76,0x52,0x52,0x09,0x52,0x00,0xff,0xff,0xff
,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00,0x64,0x64,0x64,0x52,0x52
,0x09,0x09,0x00,0xff,0xff,0x00,0x76,0x76,0x76,0x64,0x52,0x09,0x52,0x00
,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00,0x64
,0x64,0x76,0x64,0x09,0x52,0x09,0x00,0xff,0xff,0x00,0x76,0x64,0x76,0x52
,0x09,0x52,0x52,0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
,0xff,0xff,0xff,0x00,0x64,0x64,0x64,0x52,0x09,0x52,0x09,0x00,0x00,0x64
,0x64,0x76,0x64,0x52,0x52,0x09,0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff
,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00,0x64,0x64,0x64,0x76,0x52,0x52
,0x52,0x00,0x52,0x64,0x76,0x64,0x52,0x52,0x52,0x52,0x00,0xff,0xff,0xff
,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00,0x64
,0x52,0x64,0x52,0x09,0x52,0x52,0x64,0x64,0x64,0x64,0x52,0x09,0x52,0x00
,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
,0xff,0xff,0x00,0x64,0x64,0x52,0x76,0x52,0x52,0x52,0x64,0x64,0x64,0x52
,0x52,0x52,0x52,0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00,0x64,0x64,0x64,0x52,0x52,0x52
,0x52,0x64,0x64,0x52,0x52,0x52,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
,0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00,0x64,0x52
,0x64,0x76,0x52,0x52,0x52,0x64,0x52,0x52,0x52,0x52,0x00,0x00,0x00,0x00
,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
,0xff,0xff,0x00,0x64,0x52,0x76,0x52,0x52,0x52,0x52,0x52,0x52,0x52,0x00
,0x00,0xff,0xff,0xff,0xff,0x00,0x00,0x00,0x00,0x00,0xff,0xff,0xff,0xff
,0xff,0xff,0xff,0xff,0xff,0xff,0x00,0x52,0x52,0x52,0x76,0x52,0x52,0x52
,0x09,0x52,0x52,0x00,0x00,0xff,0xff,0xff,0x00,0x00,0x00,0x00,0xff,0xff
,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00,0x64,0x52
,0x64,0x52,0x52,0x52,0x52,0x52,0x00,0x00,0xff,0xff,0xff,0x00,0x00,0x00
,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00,0x00,0x00,0x00
,0x00,0x00,0x52,0x52,0x52,0x76,0x52,0x09,0x52,0x52,0x00,0x00,0x00,0x00
,0x00,0x00,0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00
,0x52,0x52,0x52,0x52,0x52,0x52,0x00,0x52,0x64,0x76,0x09,0x52,0x09,0x09
,0x00,0x00,0x00,0x09,0x00,0x09,0x00,0x00,0xff,0xff,0xff,0xff,0xff,0xff
,0xff,0xff,0x00,0x52,0x00,0x09,0x52,0x09,0x52,0x09,0x09,0x52,0x52,0x64
,0x64,0x52,0x52,0x09,0x00,0x00,0x00,0x00,0x09,0x00,0x00,0x00,0x00,0xff
,0xff,0xff,0xff,0xff,0xff,0x00,0x52,0x00,0x52,0x52,0x52,0x09,0x09,0x52
,0x09,0x09,0x64,0x64,0x76,0x52,0x09,0x00,0x09,0x09,0x00,0x09,0x00,0x00
,0x00,0x09,0x00,0x00,0xff,0xff,0xff,0xff,0x00,0x52,0x09,0x52,0x09,0x52
,0x09,0x52,0x09,0x09,0x09,0x09,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
,0x00,0x00,0x00,0x00,0x00,0x00,0x09,0x00,0x00,0xff,0xff,0xff,0x00,0x76
,0x76,0x64,0x76,0x76,0x64,0x76,0x64,0x64,0x76,0x76,0x76,0x64,0x64,0x76
,0x64,0x64,0x52,0x64,0x64,0x52,0x52,0x64,0x52,0x64,0x52,0x00,0x00,0xff
,0xff,0xff,0x00,0x76,0x52,0x09,0x52,0x09,0x52,0x09,0x09,0x52,0x52,0x52
,0x09,0x09,0x09,0x09,0x52,0x09,0x52,0x09,0x09,0x09,0x52,0x09,0x09,0x52
,0x52,0x00,0x00,0xff,0xff,0xff,0x00,0x64,0x64,0x64,0x64,0x64,0x52,0x52
,0x64,0x64,0x64,0x52,0x64,0x52,0x52,0x52,0x52,0x64,0x52,0x52,0x52,0x52
,0x52,0x52,0x52,0x52,0x52,0x00,0xff,0xff,0xff,0xff,0x00,0x00,0x00,0x00
,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0xff,0xff,0xff
,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
,0xff,0xff
};
inline uint8_t sampleImage(const unsigned char *image, Unit x, Unit y)
{
int32_t index = clamp(
image[1] * ((image[1] * y) / UNITS_PER_SQUARE) +
@ -259,6 +339,26 @@ uint8_t sampleImage(const unsigned char *image, Unit x, Unit y)
return image[2 + index];
}
void drawSprite(const unsigned char *sprite, int16_t x, int16_t y, int16_t size)
{
// TODO: optimize
Unit step = UNITS_PER_SQUARE / size;
uint8_t c;
for (Unit j = 0; j < size; ++j)
{
for (Unit i = 0; i < size; ++i)
{
c = sampleImage(sprite,(i * UNITS_PER_SQUARE) / size,(j * UNITS_PER_SQUARE) / size);
if (c != 0xff)
p.display.drawPixel(x + i,y + j,c);
}
}
}
// r: 3 bits, g: 3 bits, b: 2 bits
inline uint8_t rgbToIndex(uint8_t r, uint8_t g, uint8_t b)
{
@ -287,7 +387,8 @@ inline uint8_t addIntensity(uint8_t color, int intensity)
intensity *= -1;
r = (intensity > r) ? 0 : r - intensity;
g = (intensity > g) ? 0 : g - intensity;
b = (intensity / 2) > b ? 0 : b - (intensity / 2);
intensity /= 2;
b = intensity > b ? 0 : b - intensity;
}
return rgbToIndex(r,g,b);
@ -327,7 +428,6 @@ public:
}
};
Pokitto::Core p;
Character player;
Level level;
@ -385,6 +485,8 @@ void draw()
c.maxSteps = 16;
render(player.mCamera,heightFunc,pixelFunc,c);
drawSprite(sprite,20,20,40);
}
int main()