The code is pretty hacky, but seems to work fine, and such a feature was really needed to avoid hacks (for instance, the one found in display_text_file()).
In the future, it would be a good idea to add break lines support for get_text_width() and get_text_height().
SVN-Revision: 1652
TextRequest* textrequest = new TextRequest;
textrequest->font = font;
textrequest->text = text;
+ textrequest->center = false;
request.request_data = textrequest;
drawingrequests.push_back(request);
request.type = TEXT;
request.layer = layer;
- request.pos = transform.apply(position) + Vector(screen->w/2 -
- font->get_text_width(text)/2, 0);
+ request.pos = transform.apply(position);
request.drawing_effect = drawing_effect;
TextRequest* textrequest = new TextRequest;
textrequest->font = font;
textrequest->text = text;
+ textrequest->center = true;
request.request_data = textrequest;
drawingrequests.push_back(request);
DrawingContext::draw_text(DrawingRequest& request)
{
TextRequest* textrequest = (TextRequest*) request.request_data;
-
- textrequest->font->draw(textrequest->text, request.pos, request.drawing_effect);
+
+ if(textrequest->center)
+ textrequest->font->draw_center(textrequest->text, request.pos, request.drawing_effect);
+ else
+ textrequest->font->draw(textrequest->text, request.pos, request.drawing_effect);
delete textrequest;
}
{
Font* font;
std::string text;
+ bool center;
};
struct GradientRequest
void draw_surface_part(DrawingRequest& request);
void draw_text(DrawingRequest& request);
+ void draw_text_center(DrawingRequest& request);
void draw_gradient(DrawingRequest& request);
void draw_filled_rect(DrawingRequest& request);
}
void
+Font::draw_center(const std::string& text, const Vector& pos, Uint32 drawing_effect)
+{
+ /* Cut lines changes into seperate strings, needed to support centering text
+ with break lines.
+ Feel free to replace this hack with a more elegant solution
+ */
+ char temp[1024];
+ unsigned int i, l, y;
+ i = y = 0;
+ while(true)
+ {
+ l = text.find("\n", i);
+ if(l == std::string::npos)
+ {
+ temp[text.copy(temp, text.size() - i, i)] = '\0';
+ draw(temp, Vector(screen->w/2 - get_text_width(temp)/2 + pos.x, pos.y + y),
+ drawing_effect);
+ break;
+ }
+ temp[text.copy(temp, l - i, i)] = '\0';
+ draw(temp, Vector(screen->w/2 - get_text_width(temp)/2 + pos.x, pos.y + y),
+ drawing_effect);
+
+ i = l+1;
+ y += h + 2;
+ }
+}
+
+void
Font::draw_chars(Surface* pchars, const std::string& text, const Vector& pos,
Uint32 drawing_effect)
{
void draw(const std::string& text, const Vector& pos,
Uint32 drawing_effect = NONE_EFFECT);
+ void draw_center(const std::string& text, const Vector& pos,
+ Uint32 drawing_effect = NONE_EFFECT);
void draw_chars(Surface* pchars, const std::string& text,
const Vector& position, Uint32 drawing_effect);