diff --git a/desktop_version/src/GraphicsUtil.cpp b/desktop_version/src/GraphicsUtil.cpp index 8e1b5509..81ea4599 100644 --- a/desktop_version/src/GraphicsUtil.cpp +++ b/desktop_version/src/GraphicsUtil.cpp @@ -312,6 +312,76 @@ void BlitSurfaceColoured( SDL_FreeSurface(tempsurface); } +void BlitSurfaceTinted( + SDL_Surface* _src, + SDL_Rect* _srcRect, + SDL_Surface* _dest, + SDL_Rect* _destRect, + colourTransform& ct +) { + SDL_Rect *tempRect = _destRect; + + const SDL_PixelFormat& fmt = *(_src->format); + + SDL_Surface* tempsurface = SDL_CreateRGBSurface( + SDL_SWSURFACE, + _src->w, + _src->h, + fmt.BitsPerPixel, + fmt.Rmask, + fmt.Gmask, + fmt.Bmask, + fmt.Amask + ); + + for (int x = 0; x < tempsurface->w; x++) { + for (int y = 0; y < tempsurface->h; y++) { + Uint32 pixel = ReadPixel(_src, x, y); + + Uint8 pixred = (pixel & _src->format->Rmask) >> 16; + Uint8 pixgreen = (pixel & _src->format->Gmask) >> 8; + Uint8 pixblue = (pixel & _src->format->Bmask) >> 0; + + double temp_pixred = pixred * 0.299; + double temp_pixgreen = pixgreen * 0.587; + double temp_pixblue = pixblue * 0.114; + + double gray = floor((temp_pixred + temp_pixgreen + temp_pixblue + 0.5)); + + Uint8 ctred = (ct.colour & graphics.backBuffer->format->Rmask) >> 16; + Uint8 ctgreen = (ct.colour & graphics.backBuffer->format->Gmask) >> 8; + Uint8 ctblue = (ct.colour & graphics.backBuffer->format->Bmask) >> 0; + + temp_pixred = gray * ctred / 255.0; + temp_pixgreen = gray * ctgreen / 255.0; + temp_pixblue = gray * ctblue / 255.0; + + if (temp_pixred > 255) + temp_pixred = 255; + if (temp_pixgreen > 255) + temp_pixgreen = 255; + if (temp_pixblue > 255) + temp_pixblue = 255; + + pixred = temp_pixred; + pixgreen = temp_pixgreen; + pixblue = temp_pixblue; + + Uint32 Alpha = pixel & fmt.Amask; + Uint32 result = (pixred << 16) + (pixgreen << 8) + (pixblue << 0); + Uint32 CTAlpha = ct.colour & fmt.Amask; + float div1 = ((Alpha >> 24) / 255.0f); + float div2 = ((CTAlpha >> 24) / 255.0f); + Uint32 UseAlpha = (div1 * div2) * 255.0f; + + DrawPixel(tempsurface, x, y, result | (UseAlpha << 24)); + } + } + + SDL_BlitSurface(tempsurface, _srcRect, _dest, tempRect); + SDL_FreeSurface(tempsurface); +} + int scrollamount = 0; bool isscrolling = 0; diff --git a/desktop_version/src/GraphicsUtil.h b/desktop_version/src/GraphicsUtil.h index 78f264d7..9fa62695 100644 --- a/desktop_version/src/GraphicsUtil.h +++ b/desktop_version/src/GraphicsUtil.h @@ -25,6 +25,8 @@ void BlitSurfaceStandard( SDL_Surface* _src, SDL_Rect* _srcRect, SDL_Surface* _d void BlitSurfaceColoured( SDL_Surface* _src, SDL_Rect* _srcRect, SDL_Surface* _dest, SDL_Rect* _destRect, colourTransform& ct ); +void BlitSurfaceTinted( SDL_Surface* _src, SDL_Rect* _srcRect, SDL_Surface* _dest, SDL_Rect* _destRect, colourTransform& ct ); + void FillRect( SDL_Surface* surface, const int x, const int y, const int w, const int h, const int r, int g, int b ); void FillRect( SDL_Surface* surface, const int r, int g, int b );