Optimize
This commit is contained in:
parent
2363c2b099
commit
c3f588031f
3 changed files with 42 additions and 43 deletions
22
demo1.cpp
22
demo1.cpp
|
@ -11,6 +11,7 @@
|
|||
*/
|
||||
|
||||
// redefine some parameters
|
||||
#define FPS 40
|
||||
#define GRAVITY_ACCELERATION (UNITS_PER_SQUARE * 3)
|
||||
#define PLAYER_JUMP_SPEED 700
|
||||
#define CAMERA_COLL_HEIGHT_BELOW ((3 * UNITS_PER_SQUARE) / 2)
|
||||
|
@ -695,14 +696,14 @@ Unit ceilingHeightAt(int16_t x, int16_t y)
|
|||
Function for drawing a single pixel (like fragment shader). Bottleneck =>
|
||||
should be as fast as possible.
|
||||
*/
|
||||
inline void pixelFunc(PixelInfo pixel)
|
||||
inline void pixelFunc(PixelInfo *pixel)
|
||||
{
|
||||
if (pixel.position.y == MIDDLE_ROW)
|
||||
zBuffer[pixel.position.x] = pixel.depth;
|
||||
if (pixel->position.y == MIDDLE_ROW)
|
||||
zBuffer[pixel->position.x] = pixel->depth;
|
||||
|
||||
uint8_t c;
|
||||
|
||||
Unit depth = pixel.depth - UNITS_PER_SQUARE * 3;
|
||||
Unit depth = pixel->depth - UNITS_PER_SQUARE * 3;
|
||||
depth = depth > 0 ? depth : 1;
|
||||
|
||||
int intensity = 7 - (depth * 7) / (UNITS_PER_SQUARE * 5);
|
||||
|
@ -710,26 +711,26 @@ inline void pixelFunc(PixelInfo pixel)
|
|||
if (intensity < 0)
|
||||
intensity = 0;
|
||||
|
||||
if (pixel.isWall)
|
||||
if (pixel->isWall)
|
||||
{
|
||||
if ((pixel.hit.direction == 0 || pixel.hit.direction == 2))
|
||||
if ((pixel->hit.direction == 0 || pixel->hit.direction == 2))
|
||||
intensity -= 2;
|
||||
|
||||
if (intensity < 0)
|
||||
intensity = 0;
|
||||
|
||||
c = sampleImage(textures[pixel.hit.type],pixel.hit.textureCoord,pixel.textureCoordY);
|
||||
c = sampleImage(textures[pixel->hit.type],pixel->hit.textureCoord,pixel->textureCoordY);
|
||||
c = addIntensity(c,intensity - 3);
|
||||
}
|
||||
else
|
||||
c = pixel.isFloor ?
|
||||
c = pixel->isFloor ?
|
||||
rgbToIndex(intensity/2,intensity,intensity/3) :
|
||||
rgbToIndex(intensity,intensity/2,0);
|
||||
|
||||
uint8_t *buf = pokitto.display.screenbuffer;
|
||||
|
||||
buf += pixel.position.x * SUBSAMPLE;
|
||||
buf += pixel.position.y * SCREEN_WIDTH;
|
||||
buf += pixel->position.x * SUBSAMPLE;
|
||||
buf += pixel->position.y * SCREEN_WIDTH;
|
||||
|
||||
#pragma unroll
|
||||
for (uint8_t i = 0; i < SUBSAMPLE - 1; ++i)
|
||||
|
@ -784,7 +785,6 @@ int main()
|
|||
{
|
||||
initGeneral();
|
||||
|
||||
// player.setPositionSquare(4,5);
|
||||
player.setPositionSquare(6,4);
|
||||
player.mCamera.direction = 256;
|
||||
|
||||
|
|
28
demo2.cpp
28
demo2.cpp
|
@ -766,42 +766,42 @@ int8_t pixelIntensity = 0; ///< Precomputed column intensity addition.
|
|||
/**
|
||||
Function for drawing a single pixel (like fragment shader).
|
||||
*/
|
||||
inline void pixelFunc(PixelInfo pixel)
|
||||
inline void pixelFunc(PixelInfo *pixel)
|
||||
{
|
||||
if (pixel.position.y == MIDDLE_ROW)
|
||||
zBuffer[pixel.position.x] = pixel.depth;
|
||||
if (pixel->position.y == MIDDLE_ROW)
|
||||
zBuffer[pixel->position.x] = pixel->depth;
|
||||
|
||||
uint8_t c = 0;
|
||||
|
||||
if (!pixel.isWall)
|
||||
if (!pixel->isWall)
|
||||
{
|
||||
c = pixel.isFloor ? cFloor: cCeiling;
|
||||
c = pixel->isFloor ? cFloor: cCeiling;
|
||||
}
|
||||
else
|
||||
{
|
||||
Unit textureScroll = pixel.hit.type != 4 ? 0 : 16 * pokitto.frameCount;
|
||||
Unit textureScroll = pixel->hit.type != 4 ? 0 : 16 * pokitto.frameCount;
|
||||
|
||||
c = pixel.depth < TEXTURE_MAX_DISTANCE ?
|
||||
sampleImage(textures[pixel.hit.type],pixel.hit.textureCoord + textureScroll,pixel.textureCoordY) :
|
||||
textureAverageColors[pixel.hit.type];
|
||||
c = pixel->depth < TEXTURE_MAX_DISTANCE ?
|
||||
sampleImage(textures[pixel->hit.type],pixel->hit.textureCoord + textureScroll,pixel->textureCoordY) :
|
||||
textureAverageColors[pixel->hit.type];
|
||||
|
||||
if (previousColumn == pixel.position.x)
|
||||
if (previousColumn == pixel->position.x)
|
||||
{
|
||||
c = addIntensity(c,pixelIntensity);
|
||||
}
|
||||
else
|
||||
{
|
||||
// optimization: precompute intensity for the whole column
|
||||
pixelIntensity = 1 - pixel.depth / (UNITS_PER_SQUARE * 2) + (pixel.hit.direction % 2 == 0 ? 2 : 0);
|
||||
previousColumn = pixel.position.x;
|
||||
pixelIntensity = 1 - pixel->depth / (UNITS_PER_SQUARE * 2) + (pixel->hit.direction % 2 == 0 ? 2 : 0);
|
||||
previousColumn = pixel->position.x;
|
||||
c = addIntensity(c,pixelIntensity);
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t *buf = pokitto.display.screenbuffer;
|
||||
|
||||
buf += pixel.position.x * SUBSAMPLE;
|
||||
buf += pixel.position.y * pokitto.display.width;
|
||||
buf += pixel->position.x * SUBSAMPLE;
|
||||
buf += pixel->position.y * pokitto.display.width;
|
||||
|
||||
#pragma unroll
|
||||
for (uint8_t i = 0; i < SUBSAMPLE - 1; ++i)
|
||||
|
|
31
demo3.cpp
31
demo3.cpp
|
@ -194,36 +194,36 @@ uint16_t backgroundColumn = 0; ///< Precomputed background column.
|
|||
/**
|
||||
Function for drawing a single pixel (like fragment shader).
|
||||
*/
|
||||
inline void pixelFunc(PixelInfo pixel)
|
||||
inline void pixelFunc(PixelInfo *pixel)
|
||||
{
|
||||
uint8_t c = 0;
|
||||
int16_t intensity = 0;
|
||||
|
||||
if (pixel.isWall)
|
||||
if (pixel->isWall)
|
||||
{
|
||||
c = pixel.hit.square.x != selectedSquare.x || pixel.hit.square.y != selectedSquare.y || (editing && pokitto.frameCount % 2) == 0 ?
|
||||
squareColors[pixel.hit.type] : 30;
|
||||
c = pixel->hit.square.x != selectedSquare.x || pixel->hit.square.y != selectedSquare.y || (editing && pokitto.frameCount % 2) == 0 ?
|
||||
squareColors[pixel->hit.type] : 30;
|
||||
|
||||
intensity = pixel.depth / (UNITS_PER_SQUARE * 3);
|
||||
intensity += pixel.hit.direction % 2 == 0 ? 2 : 0;
|
||||
intensity = pixel->depth / (UNITS_PER_SQUARE * 3);
|
||||
intensity += pixel->hit.direction % 2 == 0 ? 2 : 0;
|
||||
}
|
||||
else if (pixel.isFloor)
|
||||
else if (pixel->isFloor)
|
||||
{
|
||||
c = floorColor;
|
||||
|
||||
if (!pixel.isHorizon)
|
||||
intensity = pixel.depth / (UNITS_PER_SQUARE * 3);
|
||||
if (!pixel->isHorizon)
|
||||
intensity = pixel->depth / (UNITS_PER_SQUARE * 3);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (previousColumn == pixel.position.x)
|
||||
if (previousColumn == pixel->position.x)
|
||||
{
|
||||
c = imageBackground[2 + backgroundColumn * 22 + min(pixel.position.y,43) / 2];
|
||||
c = imageBackground[2 + backgroundColumn * 22 + min(pixel->position.y,43) / 2];
|
||||
}
|
||||
else
|
||||
{
|
||||
backgroundColumn = absVal(pixel.position.x / 2 + (110 * player.mCamera.direction) / UNITS_PER_SQUARE) % 55;
|
||||
previousColumn = pixel.position.x;
|
||||
backgroundColumn = absVal(pixel->position.x / 2 + (110 * player.mCamera.direction) / UNITS_PER_SQUARE) % 55;
|
||||
previousColumn = pixel->position.x;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -232,8 +232,8 @@ inline void pixelFunc(PixelInfo pixel)
|
|||
|
||||
uint8_t *buf = pokitto.display.screenbuffer;
|
||||
|
||||
buf += pixel.position.x * SUBSAMPLE;
|
||||
buf += pixel.position.y * SCREEN_WIDTH;
|
||||
buf += pixel->position.x * SUBSAMPLE;
|
||||
buf += pixel->position.y * SCREEN_WIDTH;
|
||||
|
||||
#pragma unroll
|
||||
for (uint8_t i = 0; i < SUBSAMPLE - 1; ++i)
|
||||
|
@ -291,7 +291,6 @@ void cameraFlyBy(uint32_t dt)
|
|||
|
||||
player.mCamera.position.x += step * 30;
|
||||
player.mCamera.position.y += step * 15;
|
||||
|
||||
player.mCamera.direction = sinInt(pokitto.frameCount / 8);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue