From c1225cfbbc2614bbca205b84fdc9978354729935 Mon Sep 17 00:00:00 2001 From: Miloslav Ciz Date: Sat, 16 Apr 2022 22:58:49 +0200 Subject: [PATCH] Optimize examples --- programs/city.c | 27 ++++++++++++--------------- programs/level.c | 30 +++++++++++++++--------------- programs/modelViewer.c | 34 ++++++++++++++-------------------- 3 files changed, 41 insertions(+), 50 deletions(-) diff --git a/programs/city.c b/programs/city.c index 448e607..6c3919d 100644 --- a/programs/city.c +++ b/programs/city.c @@ -81,16 +81,13 @@ void clearScreen() static inline void setPixel(int x, int y, uint8_t red, uint8_t green, uint8_t blue) { - uint32_t r = red & 0x000000FF; - r = r << 24; + uint8_t *p = ((uint8_t *) pixels) + (y * S3L_RESOLUTION_X + x) * 4 + 1; - uint32_t g = green & 0x000000FF; - g = g << 16; - - uint32_t b = blue & 0x000000FF; - b = b << 8; - - pixels[y * S3L_RESOLUTION_X + x] = r | g | b; + *p = blue; + ++p; + *p = green; + ++p; + *p = red; } void sampleTexture(int32_t u, int32_t v, uint8_t *r, uint8_t *g, uint8_t *b) @@ -98,13 +95,13 @@ void sampleTexture(int32_t u, int32_t v, uint8_t *r, uint8_t *g, uint8_t *b) u = S3L_clamp(u,0,CITY_TEXTURE_WIDTH - 1); v = S3L_clamp(v,0,CITY_TEXTURE_HEIGHT - 1); - int32_t index = (v * CITY_TEXTURE_WIDTH + u) * 3; + const uint8_t *t = cityTexture + (v * CITY_TEXTURE_WIDTH + u) * 3; - *r = cityTexture[index]; - index++; - *g = cityTexture[index]; - index++; - *b = cityTexture[index]; + *r = *t; + t++; + *g = *t; + t++; + *b = *t; } uint32_t previousTriangle = -1; diff --git a/programs/level.c b/programs/level.c index 4dcd35d..fa978a3 100644 --- a/programs/level.c +++ b/programs/level.c @@ -6,6 +6,7 @@ */ #define TEXTURES 1 // whether to use textures for the level +#define FOG 1 #include #include @@ -53,16 +54,13 @@ void clearScreen() static inline void setPixel(int x, int y, uint8_t red, uint8_t green, uint8_t blue) { - uint32_t r = red & 0x000000FF; - r = r << 24; + uint8_t *p = ((uint8_t *) pixels) + (y * S3L_resolutionX + x) * 4 + 1; - uint32_t g = green & 0x000000FF; - g = g << 16; - - uint32_t b = blue & 0x000000FF; - b = b << 8; - - pixels[y * S3L_resolutionX + x] = r | g | b; + *p = blue; + ++p; + *p = green; + ++p; + *p = red; } void sampleTexture(S3L_Unit u, S3L_Unit v, uint8_t *r, uint8_t *g, uint8_t *b) @@ -73,13 +71,13 @@ void sampleTexture(S3L_Unit u, S3L_Unit v, uint8_t *r, uint8_t *g, uint8_t *b) u = S3L_wrap(u,LEVEL_TEXTURE_WIDTH); v = S3L_wrap(v,LEVEL_TEXTURE_HEIGHT); - uint32_t index = (v * LEVEL_TEXTURE_WIDTH + u) * 3; + const uint8_t *t = texture + (v * LEVEL_TEXTURE_WIDTH + u) * 3; - *r = texture[index]; - index++; - *g = texture[index]; - index++; - *b = texture[index]; + *r = *t; + t++; + *g = *t; + t++; + *b = *t; } void drawTeleport(int16_t x, int16_t y, S3L_ScreenCoord size) @@ -161,6 +159,7 @@ void drawPixel(S3L_PixelInfo *p) } #endif +#if FOG S3L_Unit fog = (p->depth * #if TEXTURES 8 @@ -172,6 +171,7 @@ void drawPixel(S3L_PixelInfo *p) r = S3L_clamp(((S3L_Unit) r) - fog,0,255); g = S3L_clamp(((S3L_Unit) g) - fog,0,255); b = S3L_clamp(((S3L_Unit) b) - fog,0,255); +#endif setPixel(p->x,p->y,r,g,b); } diff --git a/programs/modelViewer.c b/programs/modelViewer.c index 8529ee9..8a9a1b5 100644 --- a/programs/modelViewer.c +++ b/programs/modelViewer.c @@ -98,19 +98,13 @@ void clearScreen() static inline void setPixel(int x, int y, uint8_t red, uint8_t green, uint8_t blue) { - if (x < 0 || x >= S3L_RESOLUTION_X || y < 0 || y >= S3L_RESOLUTION_Y) - return; + uint8_t *p = ((uint8_t *) pixels) + (y * S3L_RESOLUTION_X + x) * 4 + 1; - uint32_t r = red & 0x000000FF; - r = r << 24; - - uint32_t g = green & 0x000000FF; - g = g << 16; - - uint32_t b = blue & 0x000000FF; - b = b << 8; - - pixels[y * S3L_RESOLUTION_X + x] = r | g | b; + *p = blue; + ++p; + *p = green; + ++p; + *p = red; } void sampleTexture(int32_t u, int32_t v, uint8_t *r, uint8_t *g, uint8_t *b) @@ -118,13 +112,13 @@ void sampleTexture(int32_t u, int32_t v, uint8_t *r, uint8_t *g, uint8_t *b) u = S3L_clamp(u,0,TEXTURE_W - 1); v = S3L_clamp(v,0,TEXTURE_H - 1); - int32_t index = (v * TEXTURE_W + u) * 3; + const uint8_t *t = texture + (v * TEXTURE_W + u) * 3; - *r = texture[index]; - index++; - *g = texture[index]; - index++; - *b = texture[index]; + *r = *t; + t++; + *g = *t; + t++; + *b = *t; } void animate(double time) @@ -310,7 +304,7 @@ void drawPixel(S3L_PixelInfo *p) setPixel(p->x,p->y,r,g,b); } -void draw() +void draw(void) { S3L_newFrame(); clearScreen(); @@ -366,7 +360,7 @@ void setModel(uint8_t index) int16_t fps = 0; -int main() +int main(void) { printHelp();