Update of /cvsroot/super-tux/supertux/lib/video
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv25734/lib/video
Modified Files:
drawing_context.cpp drawing_context.h font.cpp font.h
Log Message:
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().
Index: font.cpp
===================================================================
RCS file: /cvsroot/super-tux/supertux/lib/video/font.cpp,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -d -r1.4 -r1.5
--- font.cpp 26 Jul 2004 15:48:37 -0000 1.4
+++ font.cpp 28 Jul 2004 14:57:24 -0000 1.5
@@ -93,6 +93,35 @@
}
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)
{
Index: drawing_context.cpp
===================================================================
RCS file: /cvsroot/super-tux/supertux/lib/video/drawing_context.cpp,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- drawing_context.cpp 22 Jul 2004 19:07:51 -0000 1.3
+++ drawing_context.cpp 28 Jul 2004 14:57:24 -0000 1.4
@@ -90,6 +90,7 @@
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 @@
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 @@
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;
}
Index: drawing_context.h
===================================================================
RCS file: /cvsroot/super-tux/supertux/lib/video/drawing_context.h,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -d -r1.4 -r1.5
--- drawing_context.h 25 Jul 2004 19:03:35 -0000 1.4
+++ drawing_context.h 28 Jul 2004 14:57:24 -0000 1.5
@@ -129,6 +129,7 @@
{
Font* font;
std::string text;
+ bool center;
};
struct GradientRequest
@@ -161,6 +162,7 @@
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);
Index: font.h
===================================================================
RCS file: /cvsroot/super-tux/supertux/lib/video/font.h,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -d -r1.5 -r1.6
--- font.h 26 Jul 2004 15:48:38 -0000 1.5
+++ font.h 28 Jul 2004 14:57:24 -0000 1.6
@@ -55,6 +55,8 @@
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);
|