Add sprite drawing
This commit is contained in:
parent
e230cf96fb
commit
b398ecf241
1 changed files with 105 additions and 3 deletions
108
game.cpp
108
game.cpp
|
@ -15,6 +15,8 @@
|
||||||
|
|
||||||
#define SUBSAMPLE 2
|
#define SUBSAMPLE 2
|
||||||
|
|
||||||
|
Pokitto::Core p;
|
||||||
|
|
||||||
const unsigned char image[] =
|
const unsigned char image[] =
|
||||||
{ 32, 32 // width, height
|
{ 32, 32 // width, height
|
||||||
,0x12,0x12,0x12,0x12,0x12,0x12,0x00,0x09,0x00,0x12,0x12,0x12,0x12,0x12
|
,0x12,0x12,0x12,0x12,0x12,0x12,0x00,0x09,0x00,0x12,0x12,0x12,0x12,0x12
|
||||||
|
@ -249,7 +251,85 @@ const unsigned char image3[] =
|
||||||
,0x1d,0x25
|
,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(
|
int32_t index = clamp(
|
||||||
image[1] * ((image[1] * y) / UNITS_PER_SQUARE) +
|
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];
|
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
|
// r: 3 bits, g: 3 bits, b: 2 bits
|
||||||
inline uint8_t rgbToIndex(uint8_t r, uint8_t g, uint8_t b)
|
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;
|
intensity *= -1;
|
||||||
r = (intensity > r) ? 0 : r - intensity;
|
r = (intensity > r) ? 0 : r - intensity;
|
||||||
g = (intensity > g) ? 0 : g - 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);
|
return rgbToIndex(r,g,b);
|
||||||
|
@ -327,7 +428,6 @@ public:
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
Pokitto::Core p;
|
|
||||||
Character player;
|
Character player;
|
||||||
Level level;
|
Level level;
|
||||||
|
|
||||||
|
@ -385,6 +485,8 @@ void draw()
|
||||||
c.maxSteps = 16;
|
c.maxSteps = 16;
|
||||||
|
|
||||||
render(player.mCamera,heightFunc,pixelFunc,c);
|
render(player.mCamera,heightFunc,pixelFunc,c);
|
||||||
|
|
||||||
|
drawSprite(sprite,20,20,40);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
|
|
Loading…
Reference in a new issue