Dragonfly  4.3
A text-based game engine
DisplayManager.h
1 ///
2 /// The SFML display manager
3 ///
4 
5 #ifndef __DISPLAY_MANAGER_H__
6 #define __DISPLAY_MANAGER_H__
7 
8 // System includes.
9 #include <SFML/Graphics.hpp>
10 #include <SFML/Graphics/RectangleShape.hpp>
11 
12 // Engine includes.
13 #include "Frame.h"
14 #include "Manager.h"
15 #include "Vector.h"
16 
17 // Two-letter acronym for easier access to manager.
18 #define DM df::DisplayManager::getInstance()
19 
20 namespace df {
21 
22 /// String justifications.
24  LEFT_JUSTIFIED,
25  CENTER_JUSTIFIED,
26  RIGHT_JUSTIFIED,
27 };
28 
29 /// Defaults for SFML window.
31 const int WINDOW_VERTICAL_PIXELS_DEFAULT = 768;
32 const int WINDOW_HORIZONTAL_CHARS_DEFAULT = 80;
33 const int WINDOW_VERTICAL_CHARS_DEFAULT = 24;
34 const int WINDOW_STYLE_DEFAULT = sf::Style::Titlebar|sf::Style::Close;
35 const Color WINDOW_BACKGROUND_COLOR_DEFAULT = BLACK;
36 const std::string WINDOW_TITLE_DEFAULT = "Dragonfly";
37 const std::string FONT_FILE_DEFAULT = "df-font.ttf";
38 const float FONT_SCALE_DEFAULT = 2.0;
39 
40 /// Compute character height, based on window size and font.
41 float charHeight(void);
42 
43 /// Compute character width, based on window size and font.
44 float charWidth(void);
45 
46 /// Convert ASCII spaces (x,y) to window pixels (x,y).
48 
49 /// Convert window pixels (x,y) to ASCII spaces (x,y).
51 
52 class DisplayManager : public Manager {
53 
54  private:
55  DisplayManager(); ///< Private since a singleton.
56  DisplayManager(DisplayManager const&); ///< Don't allow copy.
57  void operator=(DisplayManager const&); ///< Don't allow assignment.
58  sf::Font m_font; ///< Font used for ASCII graphics.
59  sf::RenderWindow *m_p_window; ///< Pointer to SFML window.
60  int m_window_horizontal_pixels; ///< Horizontal pixels in window.
61  int m_window_vertical_pixels; ///< Vertical pixels in window.
62  int m_window_horizontal_chars; ///< Horizontal ASCII spaces in window.
63  int m_window_vertical_chars; ///< Vertical ASCII spaces in window.
64  sf::RectangleShape *m_p_rectangle; ///< Backing rectangle for under text.
65  sf::Color m_window_background_color; ///< Background color of window.
66  sf::Text *m_p_text; ///< ASCII character to draw.
67 
68  public:
69  /// Get the one and only instance of the DisplayManager.
70  static DisplayManager &getInstance();
71 
72  /// Open graphics window ready for text-based display.
73  /// Return 0 if ok, else -1.
74  int startUp();
75 
76  /// Close graphics window.
77  void shutDown();
78 
79  /// Draw character at window location (x,y) with color.
80  /// Return 0 if ok, else -1.
81  int drawCh(Vector world_pos, char ch, Color color) const;
82 
83  /// Draw character at window location (x,y) with custom color.
84  /// Return 0 if ok, else -1.
85  int drawCh(Vector world_pos, char ch, unsigned char r,
86  unsigned char g, unsigned char b) const;
87 
88  /// Draw single sprite frame at window location (x,y) with color.
89  /// If centered true, then center frame at (x,y).
90  /// Don't draw transparent characters (0 means none).
91  /// Return 0 if ok, else -1.
92  int drawFrame(Vector world_pos, Frame frame, bool centered,
93  Color color, char transparent=0) const;
94 
95  /// Draw string at window location (x,y) with color.
96  /// Justified left, center or right.
97  /// Return 0 if ok, else -1.
98  int drawString(Vector world_pos, std::string str, Justification just,
99  Color color) const;
100 
101  /// Return window's horizontal maximum (in characters).
102  int getHorizontal() const;
103 
104  /// Return window's vertical maximum (in characters).
105  int getVertical() const;
106 
107  /// Return window's horizontal maximum (in pixels).
108  int getHorizontalPixels() const;
109 
110  /// Return window's vertical maximum (in pixels).
111  int getVerticalPixels() const;
112 
113  /// Set window's background color.
114  /// Return true if ok, else false.
115  bool setBackgroundColor(Color new_color);
116 
117  /// Render current window buffer.
118  /// Return 0 if ok, else -1.
119  int swapBuffers();
120 
121  /// Return pointer to SFML drawing window.
122  sf::RenderWindow *getWindow() const;
123 };
124 
125 } // end of namespace df
126 #endif //__DISPLAY_MANAGER_H__
sf::Font m_font
Font used for ASCII graphics.
Definition: DisplayManager.h:58
int m_window_vertical_chars
Vertical ASCII spaces in window.
Definition: DisplayManager.h:63
sf::Color m_window_background_color
Background color of window.
Definition: DisplayManager.h:65
int m_window_vertical_pixels
Vertical pixels in window.
Definition: DisplayManager.h:61
int drawString(Vector world_pos, std::string str, Justification just, Color color) const
Draw string at window location (x,y) with color.
Justification
String justifications.
Definition: DisplayManager.h:23
int getVerticalPixels() const
Return window&#39;s vertical maximum (in pixels).
int m_window_horizontal_chars
Horizontal ASCII spaces in window.
Definition: DisplayManager.h:62
int swapBuffers()
Render current window buffer.
static DisplayManager & getInstance()
Get the one and only instance of the DisplayManager.
int drawFrame(Vector world_pos, Frame frame, bool centered, Color color, char transparent=0) const
Draw single sprite frame at window location (x,y) with color.
A 2-d bounding box.
Definition: Box.h:10
float charWidth(void)
Compute character width, based on window size and font.
void operator=(DisplayManager const &)
Don&#39;t allow assignment.
sf::RectangleShape * m_p_rectangle
Backing rectangle for under text.
Definition: DisplayManager.h:64
Vector pixelsToSpaces(Vector pixels)
Convert window pixels (x,y) to ASCII spaces (x,y).
Vector spacesToPixels(Vector spaces)
Convert ASCII spaces (x,y) to window pixels (x,y).
int getHorizontal() const
Return window&#39;s horizontal maximum (in characters).
sf::Text * m_p_text
ASCII character to draw.
Definition: DisplayManager.h:66
int getVertical() const
Return window&#39;s vertical maximum (in characters).
int getHorizontalPixels() const
Return window&#39;s horizontal maximum (in pixels).
sf::RenderWindow * getWindow() const
Return pointer to SFML drawing window.
Definition: Frame.h:12
void shutDown()
Close graphics window.
int m_window_horizontal_pixels
Horizontal pixels in window.
Definition: DisplayManager.h:60
Color
Colors Dragonfly recognizes.
Definition: Color.h:11
bool setBackgroundColor(Color new_color)
Set window&#39;s background color.
sf::RenderWindow * m_p_window
Pointer to SFML window.
Definition: DisplayManager.h:59
int drawCh(Vector world_pos, char ch, Color color) const
Draw character at window location (x,y) with color.
const int WINDOW_HORIZONTAL_PIXELS_DEFAULT
Defaults for SFML window.
Definition: DisplayManager.h:30
Definition: Manager.h:20
float charHeight(void)
Compute character height, based on window size and font.
int startUp()
Open graphics window ready for text-based display.
DisplayManager()
Private since a singleton.
Definition: Vector.h:10
Definition: DisplayManager.h:52