From e8653d2b713c1b7793f423af7cfaa33c6e2ac19a Mon Sep 17 00:00:00 2001 From: emb <> Date: Mon, 12 Jan 2015 18:49:22 -0600 Subject: [PATCH] Re-fixed colors for text images. --- RetroFE/Source/Graphics/Font.cpp | 47 ++++++++++++++++++++----- RetroFE/Source/Graphics/Font.h | 2 +- RetroFE/Source/Graphics/FontCache.cpp | 4 +-- RetroFE/Source/Graphics/FontCache.h | 2 +- RetroFE/Source/Graphics/PageBuilder.cpp | 6 ++-- 5 files changed, 45 insertions(+), 16 deletions(-) diff --git a/RetroFE/Source/Graphics/Font.cpp b/RetroFE/Source/Graphics/Font.cpp index eaf5b97..bb233e8 100644 --- a/RetroFE/Source/Graphics/Font.cpp +++ b/RetroFE/Source/Graphics/Font.cpp @@ -50,8 +50,44 @@ bool Font::GetRect(unsigned int charCode, GlyphInfo &glyph) return false; } +void SetSurfaceAlpha (SDL_Surface *surface, Uint8 alpha) +{ + SDL_PixelFormat* fmt = surface->format; -bool Font::Initialize(std::string fontPath) + // If surface has no alpha channel, just set the surface alpha. + if( fmt->Amask == 0 ) { + SDL_SetSurfaceAlphaMod( surface, alpha ); + } + // Else change the alpha of each pixel. + else { + unsigned bpp = fmt->BytesPerPixel; + // Scaling factor to clamp alpha to [0, alpha]. + float scale = alpha / 255.0f; + + SDL_LockSurface(surface); + + for (int y = 0; y < surface->h; ++y) + for (int x = 0; x < surface->w; ++x) { + // Get a pointer to the current pixel. + Uint32* pixel_ptr = (Uint32 *)( + (Uint8 *)surface->pixels + + y * surface->pitch + + x * bpp + ); + + // Get the old pixel components. + Uint8 r, g, b, a; + SDL_GetRGBA( *pixel_ptr, fmt, &r, &g, &b, &a ); + + // Set the pixel with the new alpha. + *pixel_ptr = SDL_MapRGBA( fmt, r, g, b, (Uint8)(scale * a) ); + } + + SDL_UnlockSurface(surface); + } +} + +bool Font::Initialize(std::string fontPath, SDL_Color color) { TTF_Font *font = TTF_OpenFont(fontPath.c_str(), 128); @@ -71,10 +107,6 @@ bool Font::Initialize(std::string fontPath) GlyphInfoBuild *info = new GlyphInfoBuild; memset(info, sizeof(GlyphInfoBuild), 0); - SDL_Color color; - color.r = 255; - color.g = 255; - color.b = 255; color.a = 255; info->Surface = TTF_RenderGlyph_Blended(font, i, color); TTF_GlyphMetrics(font, i, &info->Glyph.MinX, &info->Glyph.MaxX, &info->Glyph.MinY, &info->Glyph.MaxY, &info->Glyph.Advance); @@ -121,8 +153,7 @@ bool Font::Initialize(std::string fontPath) amask = 0xff000000; #endif - SDL_Surface *atlasSurface = SDL_CreateRGBSurface(0, atlasWidth, atlasHeight, 24, rmask, gmask, bmask, amask); - + SDL_Surface *atlasSurface = SDL_CreateRGBSurface(0, atlasWidth, atlasHeight, 32, rmask, gmask, bmask, amask); std::map::iterator it; for(it = Atlas.begin(); it != Atlas.end(); it++) { @@ -131,12 +162,10 @@ bool Font::Initialize(std::string fontPath) SDL_FreeSurface(info->Surface); info->Surface = NULL; } - SDL_LockMutex(SDL::GetMutex()); Texture = SDL_CreateTextureFromSurface(SDL::GetRenderer(), atlasSurface); SDL_FreeSurface(atlasSurface); - SDL_SetTextureBlendMode(Texture, SDL_BLENDMODE_ADD); SDL_UnlockMutex(SDL::GetMutex()); TTF_CloseFont(font); diff --git a/RetroFE/Source/Graphics/Font.h b/RetroFE/Source/Graphics/Font.h index 6c11350..83cad32 100644 --- a/RetroFE/Source/Graphics/Font.h +++ b/RetroFE/Source/Graphics/Font.h @@ -22,7 +22,7 @@ public: Font(); virtual ~Font(); - bool Initialize(std::string fontPath); + bool Initialize(std::string fontPath, SDL_Color color); void DeInitialize(); SDL_Texture *GetTexture(); bool GetRect(unsigned int charCode, GlyphInfo &glyph); diff --git a/RetroFE/Source/Graphics/FontCache.cpp b/RetroFE/Source/Graphics/FontCache.cpp index e627542..ee5b715 100644 --- a/RetroFE/Source/Graphics/FontCache.cpp +++ b/RetroFE/Source/Graphics/FontCache.cpp @@ -66,14 +66,14 @@ Font *FontCache::GetFont(std::string fontPath) return t; } -bool FontCache::LoadFont(std::string fontPath) +bool FontCache::LoadFont(std::string fontPath, SDL_Color color) { std::map::iterator it = FontFaceMap.find(fontPath); if(it == FontFaceMap.end()) { Font *f = new Font(); - f->Initialize(fontPath); + f->Initialize(fontPath, color); FontFaceMap[fontPath] = f; } diff --git a/RetroFE/Source/Graphics/FontCache.h b/RetroFE/Source/Graphics/FontCache.h index 51a0d3c..c2fe4db 100644 --- a/RetroFE/Source/Graphics/FontCache.h +++ b/RetroFE/Source/Graphics/FontCache.h @@ -13,7 +13,7 @@ public: void Initialize(); void DeInitialize(); FontCache(); - bool LoadFont(std::string font); + bool LoadFont(std::string font, SDL_Color color); Font *GetFont(std::string font); virtual ~FontCache(); diff --git a/RetroFE/Source/Graphics/PageBuilder.cpp b/RetroFE/Source/Graphics/PageBuilder.cpp index eb10907..5ae4c83 100644 --- a/RetroFE/Source/Graphics/PageBuilder.cpp +++ b/RetroFE/Source/Graphics/PageBuilder.cpp @@ -363,7 +363,7 @@ bool PageBuilder::BuildComponents(xml_node<> *layout, Page *page) } else { - FC->LoadFont(Font); + FC->LoadFont(Font, FontColor); Text *c = new Text(value->value(), FC->GetFont(Font), FontColor, ScaleX, ScaleY); ViewInfo *v = c->GetBaseViewInfo(); @@ -427,7 +427,7 @@ void PageBuilder::LoadReloadableImages(xml_node<> *layout, std::string tagName, { if(type) { - FC->LoadFont(Font); + FC->LoadFont(Font, FontColor); c = new ReloadableText(type->value(), FC->GetFont(Font), FontColor, LayoutKey, Collection, ScaleX, ScaleY); } } @@ -500,7 +500,7 @@ ScrollingList * PageBuilder::BuildMenu(xml_node<> *menuXml) } // on default, text will be rendered to the menu. Preload it into cache. - FC->LoadFont(Font); + FC->LoadFont(Font, FontColor); menu = new ScrollingList(Config, ScaleX, ScaleY, FC->GetFont(Font), FontColor, LayoutKey, Collection, imageType);