From ca47770825317f50520510911eeca61d8d512e76 Mon Sep 17 00:00:00 2001 From: Ted John Date: Mon, 24 Oct 2016 13:05:44 +0100 Subject: [PATCH] Move rendering to lightfx.c and protect all areas with guard --- src/common.h | 4 +- src/drawing/engines/SoftwareDrawingEngine.cpp | 61 ++++--------------- src/drawing/lightfx.c | 58 ++++++++++++++++++ src/drawing/lightfx.h | 18 ++++-- 4 files changed, 86 insertions(+), 55 deletions(-) diff --git a/src/common.h b/src/common.h index 6909d5d92f..904e97d1a0 100644 --- a/src/common.h +++ b/src/common.h @@ -144,8 +144,6 @@ assert_struct_size(registers, 7 * 4); #define UNUSED(x) ((void)(x)) -#define STOUT_EXPANDED_RENDERING -#define STOUT_EXPANDED_RENDERING_MTT -#define STOUT_EXPANDED_RENDERING_LIGHT +// #define STOUT_EXPANDED_RENDERING_LIGHT #endif diff --git a/src/drawing/engines/SoftwareDrawingEngine.cpp b/src/drawing/engines/SoftwareDrawingEngine.cpp index b73106a1d3..158de95221 100644 --- a/src/drawing/engines/SoftwareDrawingEngine.cpp +++ b/src/drawing/engines/SoftwareDrawingEngine.cpp @@ -195,7 +195,9 @@ private: SDL_Texture * _screenTexture = nullptr; SDL_PixelFormat * _screenTextureFormat = nullptr; uint32 _paletteHWMapped[256] = { 0 }; +#ifdef STOUT_EXPANDED_RENDERING_LIGHT uint32 _lightPaletteHWMapped[256] = { 0 }; +#endif // Steam overlay checking uint32 _pixelBeforeOverlay = 0; @@ -308,11 +310,15 @@ public: { if (_screenTextureFormat != nullptr) { +#ifdef STOUT_EXPANDED_RENDERING_LIGHT const SDL_Color * lightPalette = lightfx_get_palette(); +#endif for (int i = 0; i < 256; i++) { _paletteHWMapped[i] = SDL_MapRGB(_screenTextureFormat, palette[i].r, palette[i].g, palette[i].b); +#ifdef STOUT_EXPANDED_RENDERING_LIGHT _lightPaletteHWMapped[i] = SDL_MapRGBA(_screenTextureFormat, lightPalette[i].r, lightPalette[i].g, lightPalette[i].b, lightPalette[i].a); +#endif } } } @@ -527,7 +533,9 @@ private: ConfigureDirtyGrid(); +#ifdef STOUT_EXPANDED_RENDERING_LIGHT lightfx_update_buffers(dpi); +#endif } void ConfigureDirtyGrid() @@ -685,56 +693,13 @@ private: } } - uint8 MixLight(uint32 a, uint32 b, uint32 intensity) - { - intensity = intensity * 6; - uint32 bMul = (b * intensity) >> 8; - uint32 ab = a + bMul; - uint8 result = Math::Min(255, ab); - return result; - } - void DisplayViaTexture() { - lightfx_update_viewport_settings(); - lightfx_swap_buffers(); - lightfx_prepare_light_list(); - lightfx_render_lights_to_frontbuffer(); - - uint8 * lightBits = (uint8 *)lightfx_get_front_buffer(); - - void * pixels; - int pitch; - if (SDL_LockTexture(_screenTexture, nullptr, &pixels, &pitch) == 0) - { - for (uint32 y = 0; y < _height; y++) - { - uintptr_t dstOffset = (uintptr_t)(y * pitch); - uint32 * dst = (uint32 *)((uintptr_t)pixels + dstOffset); - for (uint32 x = 0; x < _width; x++) - { - uint8 * src = &_bits[y * _width + x]; - uint32 darkColour = _paletteHWMapped[*src]; - uint32 lightColour = _lightPaletteHWMapped[*src]; - uint8 lightIntensity = lightBits[y * _width + x]; - - uint32 colour = 0; - if (lightIntensity == 0) - { - colour = darkColour; - } - else - { - colour |= MixLight((darkColour >> 0) & 0xFF, (lightColour >> 0) & 0xFF, lightIntensity); - colour |= MixLight((darkColour >> 8) & 0xFF, (lightColour >> 8) & 0xFF, lightIntensity) << 8; - colour |= MixLight((darkColour >> 16) & 0xFF, (lightColour >> 16) & 0xFF, lightIntensity) << 16; - colour |= MixLight((darkColour >> 24) & 0xFF, (lightColour >> 24) & 0xFF, lightIntensity) << 24; - } - *dst++ = colour; - } - } - SDL_UnlockTexture(_screenTexture); - } +#ifdef STOUT_EXPANDED_RENDERING_LIGHT + lightfx_render_to_texture(_screenTexture, _bits, _width, _height, _paletteHWMapped, _lightPaletteHWMapped); +#else + CopyBitsToTexture(_screenTexture, _bits, (sint32)_width, (sint32)_height, _paletteHWMapped); +#endif SDL_RenderCopy(_sdlRenderer, _screenTexture, nullptr, nullptr); if (gSteamOverlayActive && gConfigGeneral.steam_overlay_pause) diff --git a/src/drawing/lightfx.c b/src/drawing/lightfx.c index 38c080f4dc..9c99489705 100644 --- a/src/drawing/lightfx.c +++ b/src/drawing/lightfx.c @@ -14,6 +14,10 @@ *****************************************************************************/ #pragma endregion +#ifdef STOUT_EXPANDED_RENDERING_LIGHT + +#include "../common.h" +#include #include "../game.h" #include "../rct2.h" #include "../interface/viewport.h" @@ -969,3 +973,57 @@ static float flerp(float a, float b, float t) float amount = range * t; return a + amount; } + +static uint8 mix_light(uint32 a, uint32 b, uint32 intensity) +{ + intensity = intensity * 6; + uint32 bMul = (b * intensity) >> 8; + uint32 ab = a + bMul; + uint8 result = min(255, ab); + return result; +} + +void lightfx_render_to_texture( + SDL_Texture * texture, + uint8 * bits, + uint32 width, + uint32 height, + uint32 * palette, + uint32 * lightPalette) +{ + lightfx_update_viewport_settings(); + lightfx_swap_buffers(); + lightfx_prepare_light_list(); + lightfx_render_lights_to_frontbuffer(); + + uint8 * lightBits = (uint8 *)lightfx_get_front_buffer(); + + void * pixels; + int pitch; + if (SDL_LockTexture(texture, NULL, &pixels, &pitch) == 0) { + for (uint32 y = 0; y < height; y++) { + uintptr_t dstOffset = (uintptr_t)(y * pitch); + uint32 * dst = (uint32 *)((uintptr_t)pixels + dstOffset); + for (uint32 x = 0; x < width; x++) { + uint8 * src = &bits[y * width + x]; + uint32 darkColour = palette[*src]; + uint32 lightColour = lightPalette[*src]; + uint8 lightIntensity = lightBits[y * width + x]; + + uint32 colour = 0; + if (lightIntensity == 0) { + colour = darkColour; + } else { + colour |= mix_light((darkColour >> 0) & 0xFF, (lightColour >> 0) & 0xFF, lightIntensity); + colour |= mix_light((darkColour >> 8) & 0xFF, (lightColour >> 8) & 0xFF, lightIntensity) << 8; + colour |= mix_light((darkColour >> 16) & 0xFF, (lightColour >> 16) & 0xFF, lightIntensity) << 16; + colour |= mix_light((darkColour >> 24) & 0xFF, (lightColour >> 24) & 0xFF, lightIntensity) << 24; + } + *dst++ = colour; + } + } + SDL_UnlockTexture(texture); + } +} + +#endif // STOUT_EXPANDED_RENDERING_LIGHT diff --git a/src/drawing/lightfx.h b/src/drawing/lightfx.h index f6d2e49c47..a6c15e90f4 100644 --- a/src/drawing/lightfx.h +++ b/src/drawing/lightfx.h @@ -17,11 +17,13 @@ #ifndef _LIGHTFX_H #define _LIGHTFX_H -#include "drawing.h" -#include "../common.h" - #ifdef STOUT_EXPANDED_RENDERING_LIGHT +#include "../common.h" +#include "drawing.h" + +typedef struct SDL_Texture; + enum LIGHTFX_LIGHT_TYPE { LIGHTFX_LIGHT_TYPE_NONE = 0, LIGHTFX_LIGHT_TYPE_RESERVED_01 = 1, @@ -65,6 +67,14 @@ extern void lightfx_add_lights_magic_vehicles(); extern uint32 lightfx_get_light_polution(); void lightfx_apply_palette_filter(uint8 i, uint8 *r, uint8 *g, uint8 *b); +void lightfx_render_to_texture( + struct SDL_Texture * texture, + uint8 * bits, + uint32 width, + uint32 height, + uint32 * palette, + uint32 * lightPalette); + +#endif // STOUT_EXPANDED_RENDERING_LIGHT #endif -#endif \ No newline at end of file