From 5a69e137b5dd05607a5a359d0cc56f51aeb9ed0f Mon Sep 17 00:00:00 2001 From: Ricardo Cruz Date: Wed, 28 Jul 2004 14:57:24 +0000 Subject: [PATCH] Added support for break lines in centered text. 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 --- lib/video/drawing_context.cpp | 12 ++++++++---- lib/video/drawing_context.h | 2 ++ lib/video/font.cpp | 29 +++++++++++++++++++++++++++++ lib/video/font.h | 2 ++ 4 files changed, 41 insertions(+), 4 deletions(-) diff --git a/lib/video/drawing_context.cpp b/lib/video/drawing_context.cpp index f6e9af10d..ea1020fec 100644 --- a/lib/video/drawing_context.cpp +++ b/lib/video/drawing_context.cpp @@ -90,6 +90,7 @@ DrawingContext::draw_text(Font* font, const std::string& text, TextRequest* textrequest = new TextRequest; textrequest->font = font; textrequest->text = text; + textrequest->center = false; request.request_data = textrequest; drawingrequests.push_back(request); @@ -103,13 +104,13 @@ DrawingContext::draw_text_center(Font* font, const std::string& text, 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); @@ -215,8 +216,11 @@ void 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; } diff --git a/lib/video/drawing_context.h b/lib/video/drawing_context.h index 46742344b..3c2b6eaff 100644 --- a/lib/video/drawing_context.h +++ b/lib/video/drawing_context.h @@ -129,6 +129,7 @@ namespace SuperTux { Font* font; std::string text; + bool center; }; struct GradientRequest @@ -161,6 +162,7 @@ namespace SuperTux 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); diff --git a/lib/video/font.cpp b/lib/video/font.cpp index a0f7d73e5..f0bdaffc6 100644 --- a/lib/video/font.cpp +++ b/lib/video/font.cpp @@ -93,6 +93,35 @@ Font::draw(const std::string& text, const Vector& pos, Uint32 drawing_effect) } 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) { diff --git a/lib/video/font.h b/lib/video/font.h index 9304cab72..dd6de7771 100644 --- a/lib/video/font.h +++ b/lib/video/font.h @@ -55,6 +55,8 @@ namespace SuperTux 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); -- 2.11.0