1 /*
2   Copyright (C) 2009 Facundo Domínguez
3 
4   This file is part of Spacejunk.
5 
6   Spacejunk is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10 
11   Foobar 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 Foobar.  If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #ifndef FONTRENDERER_H
21 #define FONTRENDERER_H
22 
23 #include <string>
24 #include <guichan/font.hpp>
25 #include "global.h"
26 #include "SFont.h"
27 #include "debugmsg.h"
28 
29 class FontRenderer : public gcn::Font {
30 public:
31     SDL_Surface *s;
32     int linespacing;
FontRenderer()33     FontRenderer() : s(NULL), linespacing(0) {};
34     typedef enum { LEFT, RIGHT, CENTER } Alingment;
35     /** Draws a (possibly multiline) string taking the position as logical coordinates. */
36     void drawL(int x, int y,const std::string & text,Alingment a=LEFT);
37     /** Draws a (possibly multiline) string taking the position as screen coordinates. */
38     void draw(int x, int y,const std::string & text,Alingment a=LEFT);
39     void drawString(gcn::Graphics * graphics,const std::string & text,int x, int y);
40     /** Draws a line taking the position in logical coordinates. */
41     void drawLineL(int x, int y,const std::string & text);
42     /** Draws a line taking the position as screen coordinates. */
43     virtual void drawLine(int x, int y,const std::string & text)=0;
~FontRenderer()44     virtual ~FontRenderer() {};
45 };
46 
47 
48 class SFontRenderer : public FontRenderer {
49 private:
50     SFont_Font * f;
drawLine(int x,int y,const std::string & text)51     void drawLine(int x, int y,const std::string & text) {
52         SFont_Write(s?s:SDL_GetVideoSurface(),&*f,x,y,text.c_str());
53     };
SFontRenderer(const SFontRenderer &)54     SFontRenderer(const SFontRenderer &): FontRenderer() {};
55     SFontRenderer & operator = (const SFontRenderer &) {
56         return *this;
57     };
58 public:
SFontRenderer(SFont_Font * f)59     SFontRenderer(SFont_Font * f) : f(f) {};
~SFontRenderer()60     ~SFontRenderer() {
61         SFont_FreeFont(f);
62     }
getHeight()63     int getHeight() const {
64         return SFont_TextHeight(f);
65     };
getWidth(const std::string & text)66     int getWidth(const std::string & text) const {
67         return SFont_TextWidth(f,text.c_str());
68     };
69 };
70 
71 #endif // FONTRENDERER_H
72