diff --git a/src/Unuk/Player.cpp b/src/Unuk/Player.cpp index af84aa6..93f5d73 100644 --- a/src/Unuk/Player.cpp +++ b/src/Unuk/Player.cpp @@ -57,7 +57,7 @@ void Player::HandleInput(void) { void Player::Update(void) { Move(); - AddSpeachBubble("Woot, My name is Allanis\n, welcome to my home"); + AddSpeachBubble("Woot, My name is Allanis, welcome to my home. Just testing some more text to see if this works.."); // For now The camera will be static. SetCamera(); diff --git a/src/libUnuk/Character.cpp b/src/libUnuk/Character.cpp index f8bb9c2..d335772 100644 --- a/src/libUnuk/Character.cpp +++ b/src/libUnuk/Character.cpp @@ -55,7 +55,8 @@ void Character::LoadSprites(string filename, int wArg, int hArg) { void Character::AddSpeachBubble(string text) { _speachBubble.push_back(text); - _speachBubbleText.SetTextBlended(text, small, 0, 0, 0); + _speachBubbleText.SetLineWidth(200); + _speachBubbleText.SetTextBlended(text, small, 0, 0, 0, true); if(_speachBubbleTimer.IsStarted() == false) _speachBubbleTimer.Start(); diff --git a/src/libUnuk/Text.cpp b/src/libUnuk/Text.cpp index 5341714..08dc92b 100644 --- a/src/libUnuk/Text.cpp +++ b/src/libUnuk/Text.cpp @@ -13,6 +13,7 @@ Text::Text(void) { y=0; w=0; h=0; + lineWidth=50; } Text::~Text(void) { @@ -59,7 +60,7 @@ void Text::SetXY(int xArg, int yArg) { y = yArg; } -int Text::SetTextBlended(string textArg, textSizes_t size, SDL_Color colour) { +int Text::SetTextBlended(string textArg, textSizes_t size, SDL_Color colour,bool wordWrap) { _textContents = textArg; if(!_lines.empty()) { @@ -68,11 +69,30 @@ int Text::SetTextBlended(string textArg, textSizes_t size, SDL_Color colour) { } _lines.clear(); } + + TTF_Font* font = NULL; + if(size == vsmall) { + font = vSmallFont; + } else if(size == small) { + font = smallFont; + } else if(size == medium) { + font = mediumFont; + } else if(size == large) { + font = largeFont; + } else { + font = vLargeFont; + } + + std::string finalTextContents = textArg; + + if(wordWrap) { + finalTextContents = DoWordWrap(font, finalTextContents); + } std::list lines; std::string line; - for(int i = 0; i < (int)textArg.size(); i++) { - char c = textArg.at(i); + for(int i = 0; i < (int)finalTextContents.size(); i++) { + char c = finalTextContents.at(i); if(c=='\n') { lines.push_back(line); line.clear(); @@ -85,68 +105,30 @@ int Text::SetTextBlended(string textArg, textSizes_t size, SDL_Color colour) { } for(std::list::iterator it = lines.begin(); it != lines.end(); ++it) { - SDL_Surface* lineSurf = NULL; - if(size == vsmall) { - lineSurf = TTF_RenderText_Blended(vSmallFont, textArg.c_str(), colour); - int linePixelWidth; - int linePixelHeight; - TTF_SizeText(vSmallFont, textArg.c_str(), &linePixelWidth, &linePixelHeight); - if(linePixelWidth > w) { - w = linePixelWidth; - } - h += linePixelHeight + lineSpacing; - } - else if(size == small) { - lineSurf = TTF_RenderText_Blended(smallFont, it->c_str(), colour); - int linePixelWidth; - int linePixelHeight; - TTF_SizeText(smallFont, it->c_str(), &linePixelWidth, &linePixelHeight); - if(linePixelWidth > w) { - w = linePixelWidth; - } - h += linePixelHeight + lineSpacing; - } - else if(size == medium) { - lineSurf = TTF_RenderText_Blended(mediumFont, it->c_str(), colour); - int linePixelWidth; - int linePixelHeight; - TTF_SizeText(mediumFont, it->c_str(), &linePixelWidth, &linePixelHeight); - if(linePixelWidth > w) { - w = linePixelWidth; - } - h += linePixelHeight + lineSpacing; - } - else if(size == large) { - lineSurf = TTF_RenderText_Blended(largeFont, it->c_str(), colour); - int linePixelWidth; - int linePixelHeight; - TTF_SizeText(largeFont, it->c_str(), &linePixelWidth, &linePixelHeight); - if(linePixelWidth > w) { - w = linePixelWidth; - } - h += linePixelHeight + lineSpacing; - } else { - lineSurf = TTF_RenderText_Blended(vLargeFont, it->c_str(), colour); - int linePixelWidth; - int linePixelHeight; - TTF_SizeText(vLargeFont, it->c_str(), &linePixelWidth, &linePixelHeight); - if(linePixelWidth > w) { - w = linePixelWidth; - } - h += linePixelHeight + lineSpacing; + SDL_Surface* lineSurf = TTF_RenderText_Blended(font, it->c_str(), colour); + + int linePixelWidth; + int linePixelHeight; + TTF_SizeText(font, it->c_str(), &linePixelWidth, &linePixelHeight); + + if(linePixelWidth > w) { + w = linePixelWidth; } + + h += linePixelHeight + lineSpacing; + _lines.push_back(lineSurf); - } + return 1; } -int Text::SetTextBlended(string textArg, textSizes_t size, Uint8 r, Uint8 g, Uint8 b) { +int Text::SetTextBlended(string textArg, textSizes_t size, Uint8 r, Uint8 g, Uint8 b, bool wordWrap) { SDL_Color f = { r, g, b }; - return SetTextBlended(textArg, size, f); + return SetTextBlended(textArg, size, f, wordWrap); } -int Text::SetTextShaded(string textArg, textSizes_t size, SDL_Color colour, SDL_Color bgColour) { +int Text::SetTextShaded(string textArg, textSizes_t size, SDL_Color colour, SDL_Color bgColour, bool wordWrap) { _textContents = textArg; if(!_lines.empty()) { @@ -156,10 +138,29 @@ int Text::SetTextShaded(string textArg, textSizes_t size, SDL_Color colour, SDL_ _lines.clear(); } + TTF_Font* font = NULL; + if(size == vsmall) { + font = vSmallFont; + } else if(size == small) { + font = smallFont; + } else if(size == medium) { + font = mediumFont; + } else if(size == large) { + font = largeFont; + } else { + font = vLargeFont; + } + + std::string finalTextContents = textArg; + + if(wordWrap) { + finalTextContents = DoWordWrap(font, finalTextContents); + } + std::list lines; std::string line; - for(int i = 0; i < (int)textArg.size(); i++) { - char c = textArg.at(i); + for(int i = 0; i < (int)finalTextContents.size(); i++) { + char c = finalTextContents.at(i); if(c=='\n') { lines.push_back(line); line.clear(); @@ -172,65 +173,28 @@ int Text::SetTextShaded(string textArg, textSizes_t size, SDL_Color colour, SDL_ } for(std::list::iterator it = lines.begin(); it != lines.end(); ++it) { - SDL_Surface* lineSurf; - if(size == vsmall) { - lineSurf = TTF_RenderText_Shaded(vSmallFont, it->c_str(), colour, bgColour); - int linePixelWidth; - int linePixelHeight; - TTF_SizeText(vSmallFont, it->c_str(), &linePixelWidth, &linePixelHeight); - if(linePixelWidth > w) { - w = linePixelWidth; - } - h += linePixelHeight + lineSpacing; - } - else if(size == small) { - lineSurf = TTF_RenderText_Shaded(smallFont, it->c_str(), colour, bgColour); - int linePixelWidth; - int linePixelHeight; - TTF_SizeText(smallFont, it->c_str(), &linePixelWidth, &linePixelHeight); - if(linePixelWidth > w) { - w = linePixelWidth; - } - h += linePixelHeight + lineSpacing; - } - else if(size == medium) { - lineSurf = TTF_RenderText_Shaded(mediumFont, it->c_str(), colour, bgColour); - int linePixelWidth; - int linePixelHeight; - TTF_SizeText(mediumFont, it->c_str(), &linePixelWidth, &linePixelHeight); - if(linePixelWidth > w) { - w = linePixelWidth; - } - h += linePixelHeight + lineSpacing; - } - else if(size == large) { - lineSurf = TTF_RenderText_Shaded(largeFont, it->c_str(), colour, bgColour); - int linePixelWidth; - int linePixelHeight; - TTF_SizeText(largeFont, it->c_str(), &linePixelWidth, &linePixelHeight); - if(linePixelWidth > w) { - w = linePixelWidth; - } - h += linePixelHeight + lineSpacing; - } else { - lineSurf = TTF_RenderText_Shaded(vLargeFont, it->c_str(), colour, bgColour); - int linePixelWidth; - int linePixelHeight; - TTF_SizeText(vLargeFont, it->c_str(), &linePixelWidth, &linePixelHeight); - if(linePixelWidth > w) { - w = linePixelWidth; - } - h += linePixelHeight + lineSpacing; + SDL_Surface* lineSurf = TTF_RenderText_Shaded(font, it->c_str(), colour, bgColour); + + int linePixelWidth; + int linePixelHeight; + TTF_SizeText(font, it->c_str(), &linePixelWidth, &linePixelHeight); + + if(linePixelWidth > w) { + w = linePixelWidth; } + + h += linePixelHeight + lineSpacing; + _lines.push_back(lineSurf); } + return 1; } -int Text::SetTextShaded(string textArg, textSizes_t size, Uint8 rF, Uint8 gF, Uint8 bF, Uint8 rB, Uint8 gB, Uint8 bB) { +int Text::SetTextShaded(string textArg, textSizes_t size, Uint8 rF, Uint8 gF, Uint8 bF, Uint8 rB, Uint8 gB, Uint8 bB, bool wordWrap) { SDL_Color f = { rF, gF, bF }; SDL_Color b = { rB, gB, bB }; - return SetTextShaded(textArg, size, f, b); + return SetTextShaded(textArg, size, f, b, wordWrap); } void Text::Render(void) { @@ -268,3 +232,38 @@ void Text::RenderLiteral(int xArg, int yArg) { yOffset += lineSurf->h + lineSpacing; } } + +std::string Text::DoWordWrap(TTF_Font* fontArg, const std::string& textArg) { + int leftSpace = lineWidth; + + char* tokenizedText = strdup(textArg.c_str()); + char* tokenizedTextOrigin = tokenizedText; + + std::string wrappedText(textArg); + int offsetInWrappedText = 0; + + int spaceWidth; + TTF_SizeText(fontArg, " ", &spaceWidth, NULL); + + char* word = strtok(tokenizedText, " "); + + while (word) { + int wordWidth; + TTF_SizeText(fontArg, word, &wordWidth, NULL); + + if ((wordWidth + spaceWidth) > leftSpace) { + wrappedText.insert((word - tokenizedTextOrigin) + offsetInWrappedText, "\n"); + offsetInWrappedText++; + + leftSpace = lineWidth - wordWidth; + } else { + leftSpace -= wordWidth + spaceWidth; + } + + word = strtok(NULL, " "); + } + + // delete[] tokenizedText; + + return wrappedText; +} diff --git a/src/libUnuk/Text.h b/src/libUnuk/Text.h index fa6c4bb..7ed5744 100644 --- a/src/libUnuk/Text.h +++ b/src/libUnuk/Text.h @@ -30,10 +30,13 @@ public: void SetXY(int xArg, int yArg); - int SetTextBlended(string textArg, textSizes_t size, SDL_Color); - int SetTextBlended(string textArg, textSizes_t size, Uint8 r, Uint8 g, Uint8 b); - int SetTextShaded(string textArg, textSizes_t size, SDL_Color, SDL_Color); - int SetTextShaded(string textArg, textSizes_t size, Uint8 rF, Uint8 gF, Uint8 bF, Uint8 rB, Uint8 gB, Uint8 bB); + int GetLineWidth() { return lineWidth; } + void SetLineWidth(int lineWidthArg) { lineWidth = lineWidthArg; } + + int SetTextBlended(string textArg, textSizes_t size, SDL_Color, bool wordWrap=false); + int SetTextBlended(string textArg, textSizes_t size, Uint8 r, Uint8 g, Uint8 b, bool wordWrap=false); + int SetTextShaded(string textArg, textSizes_t size, SDL_Color, SDL_Color, bool wordWrap=false); + int SetTextShaded(string textArg, textSizes_t size, Uint8 rF, Uint8 gF, Uint8 bF, Uint8 rB, Uint8 gB, Uint8 bB, bool wordWrap=false); string GetText(void) { return _textContents; } @@ -44,11 +47,14 @@ public: private: int x, y, w, h; + int lineWidth; string _textContents; SDL_Color _textColour; std::list _lines; + std::string DoWordWrap(TTF_Font* fontArg, const std::string& textArg); + static TTF_Font* vSmallFont; static TTF_Font* smallFont; static TTF_Font* mediumFont;