[Super-tux-commit] supertux/lib/video drawing_context.cpp,1.5,1.6 drawing_context.h,1.6,1.7 font.cpp
Brought to you by:
wkendrick
From: Ricardo C. <rm...@us...> - 2004-09-16 15:04:27
|
Update of /cvsroot/super-tux/supertux/lib/video In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv18913/lib/video Modified Files: drawing_context.cpp drawing_context.h font.cpp font.h Log Message: Changed behavior of centered text drawing and added right allignment, as well. Added a new allignment argument for draw_text(). draw_text_center() is draw_text() with CENTER_ALLIGN in point Vector(screen->w/2, ...). Index: font.cpp =================================================================== RCS file: /cvsroot/super-tux/supertux/lib/video/font.cpp,v retrieving revision 1.7 retrieving revision 1.8 diff -u -d -r1.7 -r1.8 --- font.cpp 15 Sep 2004 18:47:45 -0000 1.7 +++ font.cpp 16 Sep 2004 15:04:16 -0000 1.8 @@ -114,38 +114,34 @@ } void -Font::draw(const std::string& text, const Vector& pos, Uint32 drawing_effect, int alpha) +Font::draw(const std::string& text, const Vector& pos_, int allignment, Uint32 drawing_effect, int alpha) { - if(shadowsize > 0) - draw_chars(shadow_chars, text, pos + Vector(shadowsize, shadowsize), - drawing_effect, alpha); - - draw_chars(chars, text, pos, drawing_effect, alpha); -} + // calculate X positions based on the allignment type + Vector pos = Vector(pos_); + if(allignment == CENTER_ALLIGN) + pos.x -= get_text_width(text) / 2; + else if(allignment == RIGHT_ALLIGN) + pos.x -= get_text_width(text); -void -Font::draw_center(const std::string& text, const Vector& pos, Uint32 drawing_effect, int alpha) -{ - /* Cut lines changes into seperate strings, needed to support centering text - with break lines. + /* Cut lines changes into seperate strings, needed to support center/right text + allignments 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, alpha); + draw_text(temp, pos + Vector(0,y), drawing_effect, alpha); 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, alpha); + draw_text(temp, pos + Vector(0,y), drawing_effect, alpha); i = l+1; y += h + 2; @@ -153,6 +149,16 @@ } void +Font::draw_text(const std::string& text, const Vector& pos, Uint32 drawing_effect, int alpha) +{ + if(shadowsize > 0) + draw_chars(shadow_chars, text, pos + Vector(shadowsize, shadowsize), + drawing_effect, alpha); + + draw_chars(chars, text, pos, drawing_effect, alpha); +} + +void Font::draw_chars(Surface* pchars, const std::string& text, const Vector& pos, Uint32 drawing_effect, int alpha) { @@ -303,9 +309,9 @@ default: font = reference_font; break; } - context.draw_text_center(font, + context.draw_text(font, names[i].substr(1, names[i].size()-1), - Vector(0, screen->h + y - scroll), LAYER_FOREGROUND1); + Vector(screen->w/2, screen->h + y - scroll), CENTER_ALLIGN, LAYER_FOREGROUND1); y += font->get_height() + ITEMS_SPACE; } Index: drawing_context.cpp =================================================================== RCS file: /cvsroot/super-tux/supertux/lib/video/drawing_context.cpp,v retrieving revision 1.5 retrieving revision 1.6 diff -u -d -r1.5 -r1.6 --- drawing_context.cpp 15 Sep 2004 18:47:45 -0000 1.5 +++ drawing_context.cpp 16 Sep 2004 15:04:16 -0000 1.6 @@ -78,28 +78,8 @@ void DrawingContext::draw_text(Font* font, const std::string& text, - const Vector& position, int layer, Uint32 drawing_effect, int alpha) -{ - DrawingRequest request; - - request.type = TEXT; - request.layer = layer; - request.pos = transform.apply(position); - request.drawing_effect = drawing_effect; - - TextRequest* textrequest = new TextRequest; - textrequest->font = font; - textrequest->text = text; - textrequest->center = false; - textrequest->alpha = alpha; - request.request_data = textrequest; - - drawingrequests.push_back(request); -} - -void -DrawingContext::draw_text_center(Font* font, const std::string& text, - const Vector& position, int layer, Uint32 drawing_effect, int alpha) + const Vector& position, int allignment, int layer, + Uint32 drawing_effect, int alpha) { DrawingRequest request; @@ -111,7 +91,7 @@ TextRequest* textrequest = new TextRequest; textrequest->font = font; textrequest->text = text; - textrequest->center = true; + textrequest->allignment = allignment; textrequest->alpha = alpha; request.request_data = textrequest; @@ -219,10 +199,7 @@ { TextRequest* textrequest = (TextRequest*) request.request_data; - if(textrequest->center) - textrequest->font->draw_center(textrequest->text, request.pos, request.drawing_effect, textrequest->alpha); - else - textrequest->font->draw(textrequest->text, request.pos, request.drawing_effect, textrequest->alpha); + textrequest->font->draw(textrequest->text, request.pos, textrequest->allignment, request.drawing_effect, textrequest->alpha); delete textrequest; } Index: drawing_context.h =================================================================== RCS file: /cvsroot/super-tux/supertux/lib/video/drawing_context.h,v retrieving revision 1.6 retrieving revision 1.7 diff -u -d -r1.6 -r1.7 --- drawing_context.h 15 Sep 2004 18:47:45 -0000 1.6 +++ drawing_context.h 16 Sep 2004 15:04:16 -0000 1.7 @@ -67,10 +67,8 @@ Uint32 drawing_effect = NONE_EFFECT); /// Draws a text. void draw_text(Font* font, const std::string& text, const Vector& position, - int layer, Uint32 drawing_effect = NONE_EFFECT, int alpha = 255); - /// Draws aligned text. - void draw_text_center(Font* font, const std::string& text, - const Vector& position, int layer, Uint32 drawing_effect = NONE_EFFECT, int alpha = 255); + int allignment, int layer, + Uint32 drawing_effect = NONE_EFFECT, int alpha = 255); /// Draws a color gradient onto the whole screen */ void draw_gradient(Color from, Color to, int layer); /// Fills a rectangle. @@ -129,7 +127,7 @@ { Font* font; std::string text; - bool center; + int allignment; int alpha; }; Index: font.h =================================================================== RCS file: /cvsroot/super-tux/supertux/lib/video/font.h,v retrieving revision 1.8 retrieving revision 1.9 diff -u -d -r1.8 -r1.9 --- font.h 15 Sep 2004 18:47:45 -0000 1.8 +++ font.h 16 Sep 2004 15:04:16 -0000 1.9 @@ -29,6 +29,12 @@ namespace SuperTux { + enum { + LEFT_ALLIGN, + CENTER_ALLIGN, + RIGHT_ALLIGN + }; + /// Font class Font { @@ -60,13 +66,18 @@ /// returns the height of the font. float get_height() const; + /** Draws the given text to the screen. Also needs the position. + * Type of alignment, drawing effect and alpha are optional. */ + void draw(const std::string& text, const Vector& pos, + int allignment = LEFT_ALLIGN, + Uint32 drawing_effect = NONE_EFFECT, int alpha = 255); + private: friend class DrawingContext; - void draw(const std::string& text, const Vector& pos, - Uint32 drawing_effect = NONE_EFFECT, int alpha = 255); - void draw_center(const std::string& text, const Vector& pos, + void draw_text(const std::string& text, const Vector& pos, Uint32 drawing_effect = NONE_EFFECT, int alpha = 255); + void draw_chars(Surface* pchars, const std::string& text, const Vector& position, Uint32 drawing_effect, int alpha); |