From c3f588031f51c57d8b8a8a4e967767a5f1a48043 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20=C4=8C=C3=AD=C5=BE?= Date: Tue, 11 Sep 2018 14:37:54 +0200 Subject: [PATCH] Optimize --- demo1.cpp | 26 +++++++++++++------------- demo2.cpp | 28 ++++++++++++++-------------- demo3.cpp | 31 +++++++++++++++---------------- 3 files changed, 42 insertions(+), 43 deletions(-) diff --git a/demo1.cpp b/demo1.cpp index ec6a58b..e9d16ea 100644 --- a/demo1.cpp +++ b/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,9 +785,8 @@ int main() { initGeneral(); -// player.setPositionSquare(4,5); -player.setPositionSquare(6,4); -player.mCamera.direction = 256; + player.setPositionSquare(6,4); + player.mCamera.direction = 256; sprites[0] = Sprite(sprite1,10,5,1,100); sprites[1] = Sprite(sprite1,14,5,1,100); diff --git a/demo2.cpp b/demo2.cpp index 4bb93cf..1cb6573 100644 --- a/demo2.cpp +++ b/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) diff --git a/demo3.cpp b/demo3.cpp index 3af0e04..ae304a9 100644 --- a/demo3.cpp +++ b/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); }