diff --git a/game.h b/game.h index 6525ddd..72360ab 100644 --- a/game.h +++ b/game.h @@ -7,9 +7,6 @@ #ifndef _LCR_GAME_H #define _LCR_GAME_H -#include "map.h" -#include "assets.h" - #define LCR_KEY_UP 0x00 #define LCR_KEY_RIGHT 0x01 #define LCR_KEY_DOWN 0x02 @@ -70,13 +67,24 @@ uint8_t LCR_gameStep(uint32_t timeMs); //------------------------------------------------------------------------------ +void LCR_drawPixelUnsafe(unsigned int x, unsigned int y, + uint_fast16_t color); + +/** + Internal pixel drawing function that checks for out-of-screen coordinates. Use + this if the pixel can potentially lie of screen (however if you know it won't, + use the normal unsafe function in sake of performance). +*/ +static inline void LCR_drawPixelSafe(unsigned int x, unsigned int y, + uint_fast16_t color); + +#include "map.h" +#include "assets.h" +#include "renderer.h" + uint8_t _LCR_keyStates[LCR_KEYS_TOTAL]; /**< Assures unchanging key states during a single frame. */ -/** - Internal pixel drawing function that takes into account things like subdivided - resolution etc. This function does not check for out-of-screen coordinates! -*/ void LCR_drawPixelUnsafe(unsigned int x, unsigned int y, uint_fast16_t color) { @@ -94,11 +102,6 @@ void LCR_drawPixelUnsafe(unsigned int x, unsigned int y, #endif } -/** - Internal pixel drawing function that checks for out-of-screen coordinates. Use - this if the pixel can potentially lie of screen (however if you know it won't, - use the normal unsafe function in sake of performance). -*/ static inline void LCR_drawPixelSafe(unsigned int x, unsigned int y, uint_fast16_t color) { @@ -116,51 +119,6 @@ void LCR_gameEnd(void) { } -void LCR_drawSkyStrip(int verticalOffset, uint8_t horizontalOffset) -{ -#if LCR_SETTING_SKY_SIZE != 0 - verticalOffset -= (LCR_SETTING_SKY_SIZE * LCR_SKY_IMAGE_SIZE) / 2; - - int finalY = verticalOffset + LCR_SETTING_SKY_SIZE * LCR_SKY_IMAGE_SIZE; - - finalY = (finalY / LCR_SETTING_SKY_SIZE) * LCR_SETTING_SKY_SIZE; - - if (finalY >= LCR_EFFECTIVE_RESOLUTION_Y) - finalY = LCR_EFFECTIVE_RESOLUTION_Y - 1; - - const uint16_t *skyLine = LCR_skyImages + 256; - - if (verticalOffset < 0) - { - skyLine += (-1 * verticalOffset / LCR_SETTING_SKY_SIZE) * (LCR_SKY_IMAGE_SIZE / 2); - verticalOffset = 0; - } - - while (verticalOffset < finalY) - { - for (int i = 0; i < LCR_EFFECTIVE_RESOLUTION_X; ++i) - { - // TODO: check z-buffer - - int offsetX = (i / LCR_SETTING_SKY_SIZE + horizontalOffset) - % LCR_SKY_IMAGE_SIZE; - - uint16_t pixel = *(skyLine + offsetX / 2); - - pixel = offsetX % 2 ? (pixel >> 8) : (pixel & 0x00ff); - - LCR_drawPixelUnsafe(i,verticalOffset,LCR_skyImages[pixel]); - } - - verticalOffset++; - - if (verticalOffset % LCR_SETTING_SKY_SIZE == 0) - skyLine += LCR_SKY_IMAGE_SIZE / 2; - } - -#endif -} - int aaa = 0; uint8_t LCR_gameStep(uint32_t time) @@ -173,7 +131,10 @@ for (int i = 0; i < LCR_EFFECTIVE_RESOLUTION_X; ++i) for (int i = 0; i < LCR_KEYS_TOTAL; ++i) _LCR_keyStates[i] = LCR_keyPressed(i); - LCR_drawSkyStrip(200 + aaa / 10,aaa / 30); + + +LCR_drawBackground(200 + aaa / 10); +LCR_drawSkyStrip(200 + aaa / 10,aaa / 30); aaa = (aaa + 1) % 10000; diff --git a/renderer.h b/renderer.h new file mode 100644 index 0000000..2c49ddf --- /dev/null +++ b/renderer.h @@ -0,0 +1,78 @@ +#ifndef _LCR_RENDERER_H +#define _LCR_RENDERER_H + +struct LCR_Renderer +{ + // TODO +}; + +void LCR_drawBackground(int verticalOffset) +{ + uint16_t color = LCR_skyImages[LCR_skyImages[256] & 0x00ff] ; // TODO + int limit = verticalOffset - LCR_SETTING_SKY_SIZE * LCR_SKY_IMAGE_SIZE / 2; + + if (limit >= LCR_EFFECTIVE_RESOLUTION_Y) + limit = LCR_EFFECTIVE_RESOLUTION_Y - 1; + + for (int y = 0; y <= limit; ++y) + for (int x = 0; x < LCR_EFFECTIVE_RESOLUTION_X; ++x) + LCR_drawPixelUnsafe(x,y,color); + + color = LCR_skyImages[LCR_skyImages[255 + (128 * 128) / 2] >> 8]; // TODO + limit = verticalOffset + LCR_SETTING_SKY_SIZE * LCR_SKY_IMAGE_SIZE / 2 - + LCR_SETTING_SKY_SIZE; + + if (limit < 0) + limit = 0; + + for (int y = LCR_EFFECTIVE_RESOLUTION_Y - 1; y >= limit; --y) + for (int x = 0; x < LCR_EFFECTIVE_RESOLUTION_X; ++x) + LCR_drawPixelUnsafe(x,y,color); +} + +void LCR_drawSkyStrip(int verticalOffset, uint8_t horizontalOffset) +{ +#if LCR_SETTING_SKY_SIZE != 0 + verticalOffset -= (LCR_SETTING_SKY_SIZE * LCR_SKY_IMAGE_SIZE) / 2; + + int finalY = verticalOffset + LCR_SETTING_SKY_SIZE * LCR_SKY_IMAGE_SIZE; + + finalY = (finalY / LCR_SETTING_SKY_SIZE) * LCR_SETTING_SKY_SIZE; + + if (finalY >= LCR_EFFECTIVE_RESOLUTION_Y) + finalY = LCR_EFFECTIVE_RESOLUTION_Y - 1; + + const uint16_t *skyLine = LCR_skyImages + 256; + + if (verticalOffset < 0) + { + skyLine += (-1 * verticalOffset / LCR_SETTING_SKY_SIZE) * (LCR_SKY_IMAGE_SIZE / 2); + verticalOffset = 0; + } + + while (verticalOffset < finalY) + { + for (int i = 0; i < LCR_EFFECTIVE_RESOLUTION_X; ++i) + { + // TODO: check z-buffer + + int offsetX = (i / LCR_SETTING_SKY_SIZE + horizontalOffset) + % LCR_SKY_IMAGE_SIZE; + + uint16_t pixel = *(skyLine + offsetX / 2); + + pixel = offsetX % 2 ? (pixel >> 8) : (pixel & 0x00ff); + + LCR_drawPixelUnsafe(i,verticalOffset,LCR_skyImages[pixel]); + } + + verticalOffset++; + + if (verticalOffset % LCR_SETTING_SKY_SIZE == 0) + skyLine += LCR_SKY_IMAGE_SIZE / 2; + } + +#endif +} + +#endif // guard