[ADD] Multi-line text rendering.

This commit is contained in:
Tamir Atias 2012-01-05 07:19:29 +02:00
parent 0f9c39ec1b
commit 6fe0ea2b24
4 changed files with 193 additions and 50 deletions

View File

@ -269,6 +269,14 @@
RelativePath="..\..\..\src\libUnuk\MapTile.h" RelativePath="..\..\..\src\libUnuk\MapTile.h"
> >
</File> </File>
<File
RelativePath="..\..\..\src\libUnuk\MemManager.cpp"
>
</File>
<File
RelativePath="..\..\..\src\libUnuk\MemManager.h"
>
</File>
<File <File
RelativePath="..\..\..\src\libUnuk\NPC.cpp" RelativePath="..\..\..\src\libUnuk\NPC.cpp"
> >

View File

@ -57,7 +57,7 @@ void Player::HandleInput(void) {
void Player::Update(void) { void Player::Update(void) {
Move(); Move();
AddSpeachBubble("Woot, My name is Allanis, welcome to my home"); AddSpeachBubble("Woot, My name is Allanis\n, welcome to my home");
// For now The camera will be static. // For now The camera will be static.
SetCamera(); SetCamera();

View File

@ -6,14 +6,21 @@ TTF_Font* Text::mediumFont = NULL;
TTF_Font* Text::largeFont = NULL; TTF_Font* Text::largeFont = NULL;
TTF_Font* Text::vLargeFont = NULL; TTF_Font* Text::vLargeFont = NULL;
const static int lineSpacing = 3;
Text::Text(void) { Text::Text(void) {
_text = NULL; x=0;
y=0;
w=0;
h=0;
} }
Text::~Text(void) { Text::~Text(void) {
if (_text) { if(!_lines.empty()) {
SDL_FreeSurface(_text); for(std::list<SDL_Surface*>::iterator it = _lines.begin(); it != _lines.end(); ++it) {
_text = NULL; SDL_FreeSurface(*it);
}
_lines.clear();
} }
} }
@ -55,29 +62,83 @@ void Text::SetXY(int xArg, int yArg) {
int Text::SetTextBlended(string textArg, textSizes_t size, SDL_Color colour) { int Text::SetTextBlended(string textArg, textSizes_t size, SDL_Color colour) {
_textContents = textArg; _textContents = textArg;
if(_text != NULL) { if(!_lines.empty()) {
SDL_FreeSurface(_text); for(std::list<SDL_Surface*>::iterator it = _lines.begin(); it != _lines.end(); ++it) {
SDL_FreeSurface(*it);
}
_lines.clear();
}
std::list<std::string> lines;
std::string line;
for(int i = 0; i < (int)textArg.size(); i++) {
char c = textArg.at(i);
if(c=='\n') {
lines.push_back(line);
line.clear();
} else {
line += c;
}
}
if (!line.empty()) {
lines.push_back(line);
} }
if(size == vsmall) { for(std::list<std::string>::iterator it = lines.begin(); it != lines.end(); ++it) {
_text = TTF_RenderText_Blended(vSmallFont, textArg.c_str(), colour); SDL_Surface* lineSurf = NULL;
return 1; if(size == vsmall) {
} lineSurf = TTF_RenderText_Blended(vSmallFont, textArg.c_str(), colour);
else if(size == small) { int linePixelWidth;
_text = TTF_RenderText_Blended(smallFont, textArg.c_str(), colour); int linePixelHeight;
return 1; TTF_SizeText(vSmallFont, textArg.c_str(), &linePixelWidth, &linePixelHeight);
} if(linePixelWidth > w) {
else if(size == medium) { w = linePixelWidth;
_text = TTF_RenderText_Blended(mediumFont, textArg.c_str(), colour); }
return 1; h += linePixelHeight + lineSpacing;
} }
else if(size == large) { else if(size == small) {
_text = TTF_RenderText_Blended(largeFont, textArg.c_str(), colour); lineSurf = TTF_RenderText_Blended(smallFont, it->c_str(), colour);
return 1; int linePixelWidth;
} else { int linePixelHeight;
_text = TTF_RenderText_Blended(vLargeFont, textArg.c_str(), colour); TTF_SizeText(smallFont, it->c_str(), &linePixelWidth, &linePixelHeight);
return 1; 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;
}
_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) {
@ -88,29 +149,82 @@ int Text::SetTextBlended(string textArg, textSizes_t size, Uint8 r, Uint8 g, Uin
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) {
_textContents = textArg; _textContents = textArg;
if(_text != NULL) { if(!_lines.empty()) {
SDL_FreeSurface(_text); for(std::list<SDL_Surface*>::iterator it = _lines.begin(); it != _lines.end(); ++it) {
SDL_FreeSurface(*it);
}
_lines.clear();
} }
if(size == vsmall) { std::list<std::string> lines;
_text = TTF_RenderText_Shaded(vSmallFont, textArg.c_str(), colour, bgColour); std::string line;
return 1; for(int i = 0; i < (int)textArg.size(); i++) {
char c = textArg.at(i);
if(c=='\n') {
lines.push_back(line);
line.clear();
} else {
line += c;
}
} }
else if(size == small) { if (!line.empty()) {
_text = TTF_RenderText_Shaded(smallFont, textArg.c_str(), colour, bgColour); lines.push_back(line);
return 1;
} }
else if(size == medium) {
_text = TTF_RenderText_Shaded(mediumFont, textArg.c_str(), colour, bgColour); for(std::list<std::string>::iterator it = lines.begin(); it != lines.end(); ++it) {
return 1; SDL_Surface* lineSurf;
} if(size == vsmall) {
else if(size == large) { lineSurf = TTF_RenderText_Shaded(vSmallFont, it->c_str(), colour, bgColour);
_text = TTF_RenderText_Shaded(largeFont, textArg.c_str(), colour, bgColour); int linePixelWidth;
return 1; int linePixelHeight;
} else { TTF_SizeText(vSmallFont, it->c_str(), &linePixelWidth, &linePixelHeight);
_text = TTF_RenderText_Shaded(vLargeFont, textArg.c_str(), colour, bgColour); if(linePixelWidth > w) {
return 1; 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;
}
_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) {
@ -120,17 +234,37 @@ int Text::SetTextShaded(string textArg, textSizes_t size, Uint8 rF, Uint8 gF, Ui
} }
void Text::Render(void) { void Text::Render(void) {
ApplySurface(x, y, _text, screen); int yOffset = 0;
for(std::list<SDL_Surface*>::iterator it = _lines.begin(); it != _lines.end(); ++it) {
SDL_Surface* lineSurf = *it;
ApplySurface(x, y + yOffset, lineSurf, screen);
yOffset += lineSurf->h + lineSpacing;
}
} }
void Text::Render(int xArg, int yArg) { void Text::Render(int xArg, int yArg) {
ApplySurface(xArg, yArg, _text, screen); int yOffset = 0;
for(std::list<SDL_Surface*>::iterator it = _lines.begin(); it != _lines.end(); ++it) {
SDL_Surface* lineSurf = *it;
ApplySurface(x + xArg, y + yArg + yOffset, lineSurf, screen);
yOffset += lineSurf->h + lineSpacing;
}
} }
void Text::RenderLiteral(void) { void Text::RenderLiteral(void) {
ApplySurfaceLiteral(x, y, _text, screen); int yOffset = 0;
for(std::list<SDL_Surface*>::iterator it = _lines.begin(); it != _lines.end(); ++it) {
SDL_Surface* lineSurf = *it;
ApplySurfaceLiteral(x, y + yOffset, lineSurf, screen);
yOffset += lineSurf->h + lineSpacing;
}
} }
void Text::RenderLiteral(int xArg, int yArg) { void Text::RenderLiteral(int xArg, int yArg) {
ApplySurfaceLiteral(xArg, yArg, _text, screen); int yOffset = 0;
for(std::list<SDL_Surface*>::iterator it = _lines.begin(); it != _lines.end(); ++it) {
SDL_Surface* lineSurf = *it;
ApplySurfaceLiteral(x + xArg, y + yArg + yOffset, lineSurf, screen);
yOffset += lineSurf->h + lineSpacing;
}
} }

View File

@ -3,6 +3,7 @@
#include <SDL/SDL.h> #include <SDL/SDL.h>
#include <SDL/SDL_ttf.h> #include <SDL/SDL_ttf.h>
#include <string> #include <string>
#include <list>
#include "../Unuk/Globals.h" #include "../Unuk/Globals.h"
#include "ApplySurface.h" #include "ApplySurface.h"
@ -20,8 +21,8 @@ public:
static void LoadFonts(void); static void LoadFonts(void);
static void FreeFonts(void); static void FreeFonts(void);
int GetWidth(void) { return _text->w; } int GetWidth(void) { return w; }
int GetHeight(void) { return _text->h; } int GetHeight(void) { return h; }
int GetX(void) { return x; } int GetX(void) { return x; }
int GetY(void) { return y; } int GetY(void) { return y; }
@ -46,7 +47,7 @@ private:
string _textContents; string _textContents;
SDL_Color _textColour; SDL_Color _textColour;
SDL_Surface* _text; std::list<SDL_Surface*> _lines;
static TTF_Font* vSmallFont; static TTF_Font* vSmallFont;
static TTF_Font* smallFont; static TTF_Font* smallFont;