429cf97e113004431e607b9fc856cd110db07871
[supertux.git] / lib / video / font.h
1 //  $Id$
2 // 
3 //  SuperTux
4 //  Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.de>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 // 
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 //  02111-1307, USA.
20
21 #ifndef SUPERTUX_FONT_H
22 #define SUPERTUX_FONT_H
23
24 #include <string>
25
26 #include "video/surface.h"
27 #include "math/vector.h"
28
29 namespace SuperTux
30   {
31
32   enum {
33     LEFT_ALLIGN,
34     CENTER_ALLIGN,
35     RIGHT_ALLIGN
36     };
37
38   /// Font
39   class Font
40     {
41     public:
42       /// Kinds of texts.
43       enum FontType {
44         TEXT, // images for all characters
45         NUM   // only images for numbers
46       };
47
48       Font(const std::string& file, FontType type, int w, int h, int shadowsize=2);
49       ~Font();
50
51       /** returns the width of a given text. (Note that I won't add a normal
52        * get_width function here, as we might switch to variable width fonts in the
53        * future.)
54        * Supports breaklines.
55        */
56       float get_text_width(const std::string& text) const;
57
58       /** returns the height of a given text. (Note that I won't add a normal
59        * get_width function here, as we might switch to variable width fonts in the
60        * future.)
61        * Supports breaklines.
62        * In case, you are positive that your text doesn't use break lines, you can
63        * just use get_height().
64        */
65       float get_text_height(const std::string& text) const;
66       /// returns the height of the font.
67       float get_height() const;
68
69       /** Draws the given text to the screen. Also needs the position.
70        * Type of alignment, drawing effect and alpha are optional. */
71       void draw(const std::string& text, const Vector& pos,
72                 int allignment = LEFT_ALLIGN,
73                 Uint32 drawing_effect = NONE_EFFECT, int alpha = 255);
74
75     private:
76       friend class DrawingContext;
77
78       void draw_text(const std::string& text, const Vector& pos,
79                 Uint32 drawing_effect = NONE_EFFECT, int alpha = 255);
80
81       void draw_chars(Surface* pchars, const std::string& text,
82                       const Vector& position, Uint32 drawing_effect, int alpha);
83
84       Surface* chars;
85       Surface* shadow_chars;
86       FontType type;
87       int w;
88       int h;
89       int shadowsize;
90
91       /// the number of the first character that is represented in the font
92       int first_char;
93       /// the number of the last character that is represented in the font
94       int last_char;
95     };
96
97
98   /** Reads a text file (using LispReader, so it as to be in its formatting)
99       and displays it in a StarTrek fashion */
100   void display_text_file(const std::string& file, float scroll_speed, Font* heading_font, Font* normal_font, Font* small_font, Font* reference_font );
101
102 } //namespace SuperTux
103
104 #endif /*SUPERTUX_FONT_H*/
105