Navigation Menu

Skip to content

Commit

Permalink
Sharing colors was indeed causing problems. Now we hash the textures …
Browse files Browse the repository at this point in the history
…by color as well.
  • Loading branch information
slouken committed Oct 25, 2011
1 parent 2c66aa0 commit da30b19
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 19 deletions.
10 changes: 7 additions & 3 deletions UIElementKeyButton.cpp
Expand Up @@ -8,13 +8,17 @@ UIElementKeyButton::UIElementKeyButton(UIPanel *panel, const char *name) :
UIElementButton(panel, name)
{
m_text = NULL;
m_textShadow = NULL;
}

UIElementKeyButton::~UIElementKeyButton()
{
if (m_text) {
fontserv->FreeText(m_text);
}
if (m_textShadow) {
fontserv->FreeText(m_textShadow);
}
}

bool
Expand All @@ -29,6 +33,8 @@ UIElementKeyButton::Load(rapidxml::xml_node<> *node)

if (m_hotkey != SDLK_UNKNOWN) {
m_text = fontserv->TextImage(SDL_GetKeyName(m_hotkey),
fonts[GENEVA_9], STYLE_BOLD, 0x00, 0x00, 0x00);
m_textShadow = fontserv->TextImage(SDL_GetKeyName(m_hotkey),
fonts[GENEVA_9], STYLE_BOLD, 0xFF, 0xFF, 0xFF);
}
return true;
Expand All @@ -42,8 +48,6 @@ UIElementKeyButton::Draw()
printf("KeyButton: %s at %d,%d\n", SDL_GetKeyName(m_hotkey), m_rect.x+14, m_rect.y+10);
printf("KeyButton: %s at %d,%d\n", SDL_GetKeyName(m_hotkey), m_rect.x+13, m_rect.y+9);
#endif
SDL_SetTextureColorMod(m_text, 0xFF, 0xFF, 0xFF);
m_screen->QueueBlit(m_rect.x+14, m_rect.y+10, m_text, NOCLIP);
SDL_SetTextureColorMod(m_text, 0x00, 0x00, 0x00);
m_screen->QueueBlit(m_rect.x+14, m_rect.y+10, m_textShadow, NOCLIP);
m_screen->QueueBlit(m_rect.x+13, m_rect.y+9, m_text, NOCLIP);
}
1 change: 1 addition & 0 deletions UIElementKeyButton.h
Expand Up @@ -20,6 +20,7 @@ class UIElementKeyButton : public UIElementButton

protected:
SDL_Texture *m_text;
SDL_Texture *m_textShadow;

protected:
static UIElementType s_elementType;
Expand Down
37 changes: 27 additions & 10 deletions UIElementLabel.cpp
Expand Up @@ -11,14 +11,15 @@ UIElementLabel::UIElementLabel(UIPanel *panel, const char *name) :
m_font = NULL;
m_style = STYLE_NORM;
m_color = m_screen->MapRGB(0xFF, 0xFF, 0xFF);
m_text = NULL;
m_texture = NULL;
#ifdef UI_DEBUG
m_text = NULL;
#endif
}

UIElementLabel::~UIElementLabel()
{
if (m_text) {
delete[] m_text;
}
if (m_texture) {
fontserv->FreeText(m_texture);
}
Expand Down Expand Up @@ -94,14 +95,20 @@ printf("Label: '%s' %d,%d\n", m_text, m_rect.x, m_rect.y);
void
UIElementLabel::SetText(const char *text)
{
if (m_text && strcmp(text, m_text) == 0) {
return;
}

if (m_text) {
delete[] m_text;
}
if (m_texture) {
fontserv->FreeText(m_texture);
}

#ifdef UI_DEBUG
m_text = strdup(text);
#endif
m_texture = fontserv->TextImage(text, m_font, m_style, m_color);
m_text = new char[strlen(text)+1];
strcpy(m_text, text);
m_texture = fontserv->TextImage(m_text, m_font, m_style, m_color);
m_rect.w = m_screen->GetImageWidth(m_texture);
m_rect.h = m_screen->GetImageHeight(m_texture);
CalculateAnchor();
Expand All @@ -114,10 +121,20 @@ printf("Label: '%s' %d,%d\n", m_text, m_rect.x, m_rect.y);
void
UIElementLabel::SetTextColor(Uint8 R, Uint8 G, Uint8 B)
{
if (m_texture) {
SDL_SetTextureColorMod(m_texture, R, G, B);
Uint32 color;

color = m_screen->MapRGB(R, G, B);
if (color == m_color) {
return;
}
m_color = color;

if (m_text) {
if (m_texture) {
fontserv->FreeText(m_texture);
}
m_texture = fontserv->TextImage(m_text, m_font, m_style, m_color);
}
m_color = m_screen->MapRGB(R, G, B);
}

void
Expand Down
2 changes: 1 addition & 1 deletion UIElementLabel.h
Expand Up @@ -26,8 +26,8 @@ class UIElementLabel : public UIElement
MFont *m_font;
Uint8 m_style;
Uint32 m_color;
char *m_text;
SDL_Texture *m_texture;
char *m_text;

protected:
static UIElementType s_elementType;
Expand Down
8 changes: 3 additions & 5 deletions maclib/Mac_FontServ.cpp
Expand Up @@ -295,11 +295,10 @@ FontServ:: TextImage(const char *text, MFont *font, Uint8 style, SDL_Color fg)
int bit;

/* First see if we can find it in our cache */
keysize = strlen(font->name)+1+2+1+1+1+strlen(text)+1;
keysize = strlen(font->name)+1+2+1+1+1+6+1+strlen(text)+1;
key = SDL_stack_alloc(char, keysize);
sprintf(key, "%s:%d:%c:%s", font->name, font->ptsize, '0'+style, text);
sprintf(key, "%s:%d:%c:%2.2x%2.2x%2.2x:%s", font->name, font->ptsize, '0'+style, fg.r, fg.g, fg.b, text);
if (hash_find(strings, key, (const void**)&image)) {
SDL_SetTextureColorMod(image, fg.r, fg.g, fg.b);
return image;
}

Expand Down Expand Up @@ -361,7 +360,7 @@ FontServ:: TextImage(const char *text, MFont *font, Uint8 style, SDL_Color fg)
/* Allocate the text pixels */
bitmap = new Uint32[width*height];
memset(bitmap, 0, width*height*sizeof(Uint32));
color = screen->MapRGB(0xFF, 0xFF, 0xFF);
color = screen->MapRGB(fg.r, fg.g, fg.b);

/* Print the individual characters */
/* Note: this could probably be optimized.. eh, who cares. :) */
Expand Down Expand Up @@ -412,7 +411,6 @@ FontServ:: TextImage(const char *text, MFont *font, Uint8 style, SDL_Color fg)
/* Create the image */
image = screen->LoadImage(width, height, bitmap);
delete[] bitmap;
SDL_SetTextureColorMod(image, fg.r, fg.g, fg.b);
SDL_SetTextureBlendMode(image, SDL_BLENDMODE_BLEND);

/* Add it to our cache */
Expand Down

0 comments on commit da30b19

Please sign in to comment.