File: font.h

package info (click to toggle)
warmux 1%3A11.04.1%2Brepack2-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, sid
  • size: 126,388 kB
  • sloc: cpp: 186,040; xml: 8,909; sh: 3,358; makefile: 1,052; ansic: 713
file content (111 lines) | stat: -rw-r--r-- 3,657 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/******************************************************************************
 *  Warmux is a convivial mass murder game.
 *  Copyright (C) 2001-2011 Warmux Team.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 ******************************************************************************/

#ifndef FONT_H
#define FONT_H
#include <SDL.h>
#include <SDL_ttf.h>
#include <map>
#include <WARMUX_base.h>
#include "graphic/colors.h"
#include "graphic/surface.h"

class Font
{
  typedef std::map<std::string, Surface>::value_type txt_sample;
  typedef std::map<std::string, Surface>::iterator txt_iterator;

  static bool  LIB_INIT;

  static std::map<int, Font *> fontMapNormal;
  static std::map<int, Font *> fontMapBold;
  static std::map<int, Font *> fontMapItalic;

  std::map<std::string, Surface> surface_text_table;
  TTF_Font *m_font;
  void Write(const Point2i &pos, const Surface &surface) const;

  Font(int size);

public:
  // Size
  typedef enum {
    FONT_HUGE = 40,
    FONT_LARGE = 32,
    FONT_BIG = 24,
    FONT_MEDIUM = 16,
    FONT_SMALL = 12,
    FONT_TINY = 10
  } font_size_t;

  // Style
  typedef enum {
    FONT_NORMAL = 0,
    FONT_BOLD = 1,
    FONT_ITALIC = 2,
  } font_style_t;

  // type: defined as static consts above
  static Font * GetInstance(font_size_t size, 
                            font_style_t style = FONT_BOLD);
  static void ReleaseInstances(void);

  ~Font();

  static Surface GenerateSurface(const std::string &txt, const Color &color,
                                 font_size_t size = FONT_MEDIUM, font_style_t style = FONT_BOLD);

  void WriteLeft(const Point2i &pos, const std::string &txt, const Color &color)
  {
    Surface surface(Render(txt, color, true));
    Write(pos, surface);
  }
  void WriteLeftBottom(const Point2i &pos, const std::string &txt, const Color &color)
  {
    Surface surface(Render(txt, color, true));
    Write(pos - Point2i(0, surface.GetHeight()), surface);
  }
  void WriteRight(const Point2i &pos, const std::string &txt, const Color &color)
  {
    Surface surface(Render(txt, color, true));
    Write(pos - Point2i(surface.GetWidth(), 0), surface);
  }
  void WriteCenterTop(const Point2i &pos, const std::string &txt, const Color &color)
  {
    Surface surface(Render(txt, color, true));
    Write(pos - Point2i(surface.GetWidth()>>1, 0), surface);
  }
  void WriteCenter(const Point2i &pos, const std::string &txt, const Color &color)
  {
    Surface surface(Render(txt, color, true));
    Write(pos - Point2i(surface.GetWidth()>>1, surface.GetHeight()), surface);
  }

  int GetWidth(const std::string &txt) const;
  int GetHeight() const;
  int GetHeight(const std::string &txt) const;
  Point2i GetSize(const std::string &txt) const { return Point2i(GetWidth(txt), GetHeight(txt)); }

  Surface Render(const std::string &txt, const Color &color, bool cache=false);
  Surface CreateSurface(const std::string &txt, const Color &color);
  void SetBold();
  void SetItalic();
};

#endif