[Super-tux-commit] supertux/lib/video drawing_context.cpp,1.13,1.14 drawing_context.h,1.12,1.13 font
Brought to you by:
wkendrick
From: Matze B. <mat...@us...> - 2005-03-30 12:01:33
|
Update of /cvsroot/super-tux/supertux/lib/video In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv1392/lib/video Modified Files: drawing_context.cpp drawing_context.h font.cpp font.h Log Message: -Some cleanups in text scrolling code -Added the (temporary) bell graphics which I forgot yesterday -Implemented an infoblock object (the textbox isn't finished yet) and it also needs new graphics Index: font.cpp =================================================================== RCS file: /cvsroot/super-tux/supertux/lib/video/font.cpp,v retrieving revision 1.18 retrieving revision 1.19 diff -u -d -r1.18 -r1.19 --- font.cpp 5 Dec 2004 16:57:12 -0000 1.18 +++ font.cpp 30 Mar 2005 12:00:53 -0000 1.19 @@ -119,10 +119,11 @@ } void -Font::draw(const std::string& text, const Vector& pos_, int allignment, Uint32 drawing_effect, int alpha) +Font::draw(const std::string& text, const Vector& pos_, FontAlignment alignment, + uint32_t drawing_effect, uint8_t alpha) const { /* Cut lines changes into seperate strings, needed to support center/right text - allignments with break lines. + alignments with break lines. Feel free to replace this hack with a more elegant solution */ char temp[1024]; @@ -141,11 +142,11 @@ temp[text.copy(temp, l - i, i)] = '\0'; - // calculate X positions based on the allignment type + // calculate X positions based on the alignment type Vector pos = Vector(pos_); - if(allignment == CENTER_ALLIGN) + if(alignment == CENTER_ALLIGN) pos.x -= get_text_width(temp) / 2; - else if(allignment == RIGHT_ALLIGN) + else if(alignment == RIGHT_ALLIGN) pos.x -= get_text_width(temp); draw_text(temp, pos + Vector(0,y), drawing_effect, alpha); @@ -156,7 +157,8 @@ } void -Font::draw_text(const std::string& text, const Vector& pos, Uint32 drawing_effect, int alpha) +Font::draw_text(const std::string& text, const Vector& pos, + uint32_t drawing_effect, uint8_t alpha) const { if(shadowsize > 0) draw_chars(shadow_chars, text, pos + Vector(shadowsize, shadowsize), @@ -167,7 +169,7 @@ void Font::draw_chars(Surface* pchars, const std::string& text, const Vector& pos, - Uint32 drawing_effect, int alpha) + uint32_t drawing_effect, uint8_t alpha) const { SurfaceImpl* impl = pchars->impl; @@ -197,170 +199,3 @@ } } -/* --- SCROLL TEXT FUNCTION --- */ - -#define MAX_VEL 10 -#define SPEED_INC 0.01 -#define SCROLL 60 -#define ITEMS_SPACE 4 - -void SuperTux::display_text_file(const std::string& file, float scroll_speed, - Font* heading_font, Font* normal_font, Font* small_font, - Font* reference_font) -{ - std::string text; - std::string background_file; - std::vector<std::string> names; - - std::string filename = datadir + "/" + file; - lisp::Parser parser; - try { - std::auto_ptr<lisp::Lisp> root (parser.parse(filename)); - - const lisp::Lisp* text_lisp = root->get_lisp("supertux-text"); - if(!text_lisp) - throw std::runtime_error("File isn't a supertux-text file"); - - if(!text_lisp->get("text", text)) - throw std::runtime_error("file doesn't contain a text field"); - if(!text_lisp->get("background", background_file)) - throw std::runtime_error("file doesn't contain a background file"); - } catch(std::exception& e) { - std::cerr << "Couldn't load file '" << filename << "': " << e.what() << - "\n"; - return; - } - - // Split text string lines into a vector - names.clear(); - std::string::size_type i, l; - i = 0; - while(true) - { - l = text.find("\n", i); - - if(l == std::string::npos) - { - char temp[1024]; - temp[text.copy(temp, text.size() - i, i)] = '\0'; - names.push_back(temp); - break; - } - - char temp[1024]; - temp[text.copy(temp, l-i, i)] = '\0'; - names.push_back(temp); - - i = l+1; - } - - // load background image - Surface* background = new Surface(datadir + "/images/background/" + background_file, false); - - int done = 0; - float scroll = 0; - float speed = scroll_speed / 50; - float left_border = 50; - - DrawingContext context; - SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL); - - Uint32 lastticks = SDL_GetTicks(); - while(!done) - { - /* in case of input, exit */ - SDL_Event event; - while(SDL_PollEvent(&event)) - switch(event.type) - { - case SDL_KEYDOWN: - switch(event.key.keysym.sym) - { - case SDLK_UP: - speed -= SPEED_INC; - break; - case SDLK_DOWN: - speed += SPEED_INC; - break; - case SDLK_SPACE: - case SDLK_RETURN: - if(speed >= 0) - scroll += SCROLL; - break; - case SDLK_ESCAPE: - done = 1; - break; - default: - break; - } - break; - case SDL_QUIT: - done = 1; - break; - default: - break; - } - - if(speed > MAX_VEL) - speed = MAX_VEL; - else if(speed < -MAX_VEL) - speed = -MAX_VEL; - - /* draw the credits */ - context.draw_surface(background, Vector(0,0), 0); - - float y = 0; - for(size_t i = 0; i < names.size(); i++) { - if(names[i].size() == 0) { - y += normal_font->get_height() + ITEMS_SPACE; - continue; - } - - Font* font = 0; - bool center = true; - switch(names[i][0]) - { - case ' ': font = small_font; break; - case '\t': font = normal_font; break; - case '-': font = heading_font; break; - case '*': font = reference_font; break; - case '#': font = normal_font; center = false; break; - default: - break; - } - - if(font) { - if(center) { - context.draw_text(font, - names[i].substr(1, names[i].size()-1), - Vector(screen->w/2, screen->h + y - scroll), - CENTER_ALLIGN, LAYER_FOREGROUND1); - } else { - context.draw_text(font, - names[i].substr(1, names[i].size()-1), - Vector(left_border, screen->h + y - scroll), - LEFT_ALLIGN, LAYER_FOREGROUND1); - } - } - - y += font->get_height() + ITEMS_SPACE; - } - - context.do_drawing(); - - if(screen->h+y-scroll < 0 && 20+screen->h+y-scroll < 0) - done = 1; - - Uint32 ticks = SDL_GetTicks(); - scroll += speed * (ticks - lastticks); - lastticks = ticks; - if(scroll < 0) - scroll = 0; - - SDL_Delay(10); - } - - SDL_EnableKeyRepeat(0, 0); // disables key repeating - delete background; -} - Index: drawing_context.cpp =================================================================== RCS file: /cvsroot/super-tux/supertux/lib/video/drawing_context.cpp,v retrieving revision 1.13 retrieving revision 1.14 diff -u -d -r1.13 -r1.14 --- drawing_context.cpp 24 Nov 2004 14:10:24 -0000 1.13 +++ drawing_context.cpp 30 Mar 2005 12:00:52 -0000 1.14 @@ -39,7 +39,7 @@ void DrawingContext::draw_surface(const Surface* surface, const Vector& position, - int layer, Uint32 drawing_effect) + int layer, uint32_t drawing_effect) { assert(surface != 0); @@ -63,7 +63,7 @@ void DrawingContext::draw_surface_part(const Surface* surface, const Vector& source, - const Vector& size, const Vector& dest, int layer, Uint32 drawing_effect) + const Vector& size, const Vector& dest, int layer, uint32_t drawing_effect) { assert(surface != 0); @@ -101,9 +101,9 @@ } void -DrawingContext::draw_text(Font* font, const std::string& text, - const Vector& position, int allignment, int layer, - Uint32 drawing_effect) +DrawingContext::draw_text(const Font* font, const std::string& text, + const Vector& position, FontAlignment alignment, int layer, + uint32_t drawing_effect) { DrawingRequest request; @@ -117,18 +117,18 @@ TextRequest* textrequest = new TextRequest; textrequest->font = font; textrequest->text = text; - textrequest->allignment = allignment; + textrequest->alignment = alignment; request.request_data = textrequest; drawingrequests.push_back(request); } void -DrawingContext::draw_center_text(Font* font, const std::string& text, - const Vector& position, int layer, Uint32 drawing_effect) +DrawingContext::draw_center_text(const Font* font, const std::string& text, + const Vector& position, int layer, uint32_t drawing_effect) { -draw_text(font, text, Vector(position.x + screen->w/2, position.y), - CENTER_ALLIGN, layer, drawing_effect); + draw_text(font, text, Vector(position.x + screen->w/2, position.y), + CENTER_ALLIGN, layer, drawing_effect); } void @@ -240,7 +240,8 @@ { TextRequest* textrequest = (TextRequest*) request.request_data; - textrequest->font->draw(textrequest->text, request.pos, textrequest->allignment, request.drawing_effect, request.alpha); + textrequest->font->draw(textrequest->text, request.pos, + textrequest->alignment, request.drawing_effect, request.alpha); delete textrequest; } Index: drawing_context.h =================================================================== RCS file: /cvsroot/super-tux/supertux/lib/video/drawing_context.h,v retrieving revision 1.12 retrieving revision 1.13 diff -u -d -r1.12 -r1.13 --- drawing_context.h 24 Nov 2004 14:10:24 -0000 1.12 +++ drawing_context.h 30 Mar 2005 12:00:53 -0000 1.13 @@ -21,18 +21,19 @@ #include <vector> #include <string> +#include <stdint.h> #include "SDL.h" #include "math/vector.h" #include "video/screen.h" #include "video/surface.h" +#include "video/font.h" namespace SuperTux { class Surface; - class Font; // some constants for predefined layer values enum { @@ -59,23 +60,23 @@ ~DrawingContext(); /// Adds a drawing request for a surface into the request list. - void draw_surface(const Surface* surface, const Vector& position, int layer, - Uint32 drawing_effect = NONE_EFFECT); + void draw_surface(const Surface* surface, const Vector& position, + int layer, uint32_t drawing_effect = NONE_EFFECT); /// Adds a drawing request for part of a surface. void draw_surface_part(const Surface* surface, const Vector& source, const Vector& size, const Vector& dest, int layer, - Uint32 drawing_effect = NONE_EFFECT); + uint32_t drawing_effect = NONE_EFFECT); /// Draws a text. - void draw_text(Font* font, const std::string& text, const Vector& position, - int allignment, int layer, - Uint32 drawing_effect = NONE_EFFECT); + void draw_text(const Font* font, const std::string& text, + const Vector& position, FontAlignment alignment, int layer, + uint32_t drawing_effect = NONE_EFFECT); /// Draws text on screen center (feed Vector.x with a 0). /// This is the same as draw_text() with a screen->w/2 position and - /// allignment set to LEFT_ALLIGN - void draw_center_text(Font* font, const std::string& text, + /// alignment set to LEFT_ALLIGN + void draw_center_text(const Font* font, const std::string& text, const Vector& position, int layer, - Uint32 drawing_effect = NONE_EFFECT); + uint32_t drawing_effect = NONE_EFFECT); /// Draws a color gradient onto the whole screen */ void draw_gradient(Color from, Color to, int layer); /// Fills a rectangle. @@ -87,7 +88,7 @@ const Vector& get_translation() const { return transform.translation; } - Uint32 get_drawing_effect() const + uint32_t get_drawing_effect() const { return transform.drawing_effect; } void set_translation(const Vector& newtranslation) @@ -108,7 +109,7 @@ { public: Vector translation; - Uint32 drawing_effect; + uint32_t drawing_effect; float zoom; int alpha; @@ -140,9 +141,9 @@ struct TextRequest { - Font* font; + const Font* font; std::string text; - int allignment; + FontAlignment alignment; }; struct GradientRequest @@ -163,7 +164,7 @@ Vector pos; int layer; - Uint32 drawing_effect; + uint32_t drawing_effect; float zoom; int alpha; Index: font.h =================================================================== RCS file: /cvsroot/super-tux/supertux/lib/video/font.h,v retrieving revision 1.10 retrieving revision 1.11 diff -u -d -r1.10 -r1.11 --- font.h 24 Nov 2004 14:10:24 -0000 1.10 +++ font.h 30 Mar 2005 12:00:53 -0000 1.11 @@ -22,6 +22,7 @@ #define SUPERTUX_FONT_H #include <string> +#include <stdint.h> #include "video/surface.h" #include "math/vector.h" @@ -29,7 +30,7 @@ namespace SuperTux { - enum { + enum FontAlignment { LEFT_ALLIGN, CENTER_ALLIGN, RIGHT_ALLIGN @@ -45,7 +46,8 @@ NUM // only images for numbers }; - Font(const std::string& file, FontType type, int w, int h, int shadowsize=2); + Font(const std::string& file, FontType type, int w, int h, + int shadowsize=2); ~Font(); /** returns the width of a given text. (Note that I won't add a normal @@ -69,17 +71,17 @@ /** 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); + FontAlignment allignment = LEFT_ALLIGN, + uint32_t drawing_effect = NONE_EFFECT, uint8_t alpha = 255) const; private: friend class DrawingContext; void draw_text(const std::string& text, const Vector& pos, - Uint32 drawing_effect = NONE_EFFECT, int alpha = 255); + uint32_t drawing_effect = NONE_EFFECT, uint8_t alpha = 255) const; void draw_chars(Surface* pchars, const std::string& text, - const Vector& position, Uint32 drawing_effect, int alpha); + const Vector& position, uint32_t drawing_effect, uint8_t alpha) const; Surface* chars; Surface* shadow_chars; @@ -93,12 +95,6 @@ /// the number of the last character that is represented in the font int last_char; }; - - - /** Reads a text file (using LispReader, so it as to be in its formatting) - and displays it in a StarTrek fashion */ - void display_text_file(const std::string& file, float scroll_speed, Font* heading_font, Font* normal_font, Font* small_font, Font* reference_font ); - } //namespace SuperTux #endif /*SUPERTUX_FONT_H*/ |