Dragonfly 4.20
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 "Config.h" // For DF_DEPRECATED.
14#include "Frame.h"
15#include "Manager.h"
16#include "Vector.h"
17
18// Two-letter acronym for easier access to manager.
19#define DM df::DisplayManager::getInstance()
20
21namespace df {
22
23/// String justifications.
25 LEFT_JUSTIFIED,
26 CENTER_JUSTIFIED,
27 RIGHT_JUSTIFIED,
28};
29
30/// Defaults for SFML window.
32const int WINDOW_VERTICAL_PIXELS_DEFAULT = 768;
33const int WINDOW_HORIZONTAL_CHARS_DEFAULT = 80;
34const int WINDOW_VERTICAL_CHARS_DEFAULT = 24;
35const int WINDOW_STYLE_DEFAULT = sf::Style::Titlebar|sf::Style::Close;
36const Color WINDOW_BACKGROUND_COLOR_DEFAULT = BLACK;
37const std::string WINDOW_TITLE_DEFAULT = "Dragonfly";
38const std::string FONT_FILE_DEFAULT = "df-font.ttf";
39const float FONT_SCALE_DEFAULT = 2.0;
40
41/// Compute character height in pixels, based on window size.
42float charHeight(void);
43
44/// Compute character width in pixels, based on window size.
45float charWidth(void);
46
47/// Convert ASCII spaces (x,y) to window pixels (x,y).
49
50/// Convert window pixels (x,y) to ASCII spaces (x,y).
52
53class DisplayManager : public Manager {
54
55 private:
56 DisplayManager(); ///< Private since a singleton.
57 DisplayManager(DisplayManager const&); ///< Don't allow copy.
58 void operator=(DisplayManager const&); ///< Don't allow assignment.
59 sf::Font m_font; ///< Font used for ASCII graphics.
60 sf::RenderWindow *m_p_window; ///< Pointer to SFML window.
61 int m_window_horizontal_pixels; ///< Horizontal pixels in window.
62 int m_window_vertical_pixels; ///< Vertical pixels in window.
63 int m_window_horizontal_chars; ///< Horizontal ASCII spaces in window.
64 int m_window_vertical_chars; ///< Vertical ASCII spaces in window.
65 sf::RectangleShape *m_p_rectangle; ///< Backing rectangle for under text.
66 sf::Color m_window_background_color; ///< Background color of window.
67 sf::Text *m_p_text; ///< ASCII character to draw.
68 int m_shake_duration; ///< Time left to shake (frames).
69 int m_shake_scale_x, m_shake_scale_y; ///< Severity of shake (pixels).
70 int m_shake_x, m_shake_y; ///< Shake (this frame).
71
72 public:
73 /// Get the one and only instance of the DisplayManager.
75
76 /// Open graphics window, ready for text-based display.
77 /// Return 0 if ok, else -1.
78 int startUp() override;
79
80 /// Close graphics window.
81 void shutDown() override;
82
83 /// Draw character at window location (x,y) with color
84 /// and rotation (degrees).
85 /// Return 0 if ok, else -1.
86 int drawCh(Vector world_pos, char ch, Color color,
87 float rotation=0.0f) const;
88
89 /// Draw character at window location (x,y) with custom color,
90 /// and rotation (degrees).
91 /// Return 0 if ok, else -1.
92 int drawCh(Vector world_pos, char ch, unsigned char r,
93 unsigned char g, unsigned char b, float rotation=0.0f) 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 /// Set window's horizontal maximum (in characters).
102 void setHorizontal(int new_horizontal);
103
104 /// Return window's horizontal maximum (in characters).
105 int getHorizontal() const;
106
107 /// Set window's vertical maximum (in characters).
108 void setVertical(int new_vertical);
109
110 /// Return window's vertical maximum (in characters).
111 int getVertical() const;
112
113 /// Return window's horizontal maximum (in pixels).
115
116 /// Return window's vertical maximum (in pixels).
117 int getVerticalPixels() const;
118
119 /// Set window's background color.
120 /// Return true if ok, else false.
121 bool setBackgroundColor(Color new_color);
122
123 /// "Shake" window with severity scale (pixels) and duration (frames).
124 /// Delta true then add else replace.
125 void shake(int scale_x, int scale_y, int duration, bool delta=false);
126
127 /// Render current window buffer.
128 /// Return 0 if ok, else -1.
130
131 /// Return pointer to SFML graphics window.
132 sf::RenderWindow *getWindow() const;
133};
134
135} // end of namespace df
136#endif //__DISPLAY_MANAGER_H__
Definition: DisplayManager.h:53
DisplayManager(DisplayManager const &)
Don't allow copy.
int m_window_horizontal_pixels
Horizontal pixels in window.
Definition: DisplayManager.h:61
int m_window_vertical_pixels
Vertical pixels in window.
Definition: DisplayManager.h:62
int m_window_horizontal_chars
Horizontal ASCII spaces in window.
Definition: DisplayManager.h:63
void shake(int scale_x, int scale_y, int duration, bool delta=false)
"Shake" window with severity scale (pixels) and duration (frames).
int m_shake_scale_y
Severity of shake (pixels).
Definition: DisplayManager.h:69
sf::RectangleShape * m_p_rectangle
Backing rectangle for under text.
Definition: DisplayManager.h:65
sf::Text * m_p_text
ASCII character to draw.
Definition: DisplayManager.h:67
bool setBackgroundColor(Color new_color)
Set window's background color.
sf::Font m_font
Font used for ASCII graphics.
Definition: DisplayManager.h:59
int getVertical() const
Return window's vertical maximum (in characters).
sf::RenderWindow * getWindow() const
Return pointer to SFML graphics window.
int m_shake_duration
Time left to shake (frames).
Definition: DisplayManager.h:68
void operator=(DisplayManager const &)
Don't allow assignment.
DisplayManager()
Private since a singleton.
sf::Color m_window_background_color
Background color of window.
Definition: DisplayManager.h:66
sf::RenderWindow * m_p_window
Pointer to SFML window.
Definition: DisplayManager.h:60
int getHorizontalPixels() const
Return window's horizontal maximum (in pixels).
int m_shake_y
Shake (this frame).
Definition: DisplayManager.h:70
int swapBuffers()
Render current window buffer.
static DisplayManager & getInstance()
Get the one and only instance of the DisplayManager.
void shutDown() override
Close graphics window.
void setVertical(int new_vertical)
Set window's vertical maximum (in characters).
int drawCh(Vector world_pos, char ch, Color color, float rotation=0.0f) const
Draw character at window location (x,y) with color and rotation (degrees).
int drawString(Vector world_pos, std::string str, Justification just, Color color) const
Draw string at window location (x,y) with color.
int getHorizontal() const
Return window's horizontal maximum (in characters).
int startUp() override
Open graphics window, ready for text-based display.
void setHorizontal(int new_horizontal)
Set window's horizontal maximum (in characters).
int getVerticalPixels() const
Return window's vertical maximum (in pixels).
int m_window_vertical_chars
Vertical ASCII spaces in window.
Definition: DisplayManager.h:64
int drawCh(Vector world_pos, char ch, unsigned char r, unsigned char g, unsigned char b, float rotation=0.0f) const
Draw character at window location (x,y) with custom color, and rotation (degrees).
Definition: Manager.h:24
Definition: Vector.h:12
An animation for a sprite.
Definition: Animation.h:15
float charHeight(void)
Compute character height in pixels, based on window size.
Vector pixelsToSpaces(Vector pixels)
Convert window pixels (x,y) to ASCII spaces (x,y).
const int WINDOW_HORIZONTAL_PIXELS_DEFAULT
Defaults for SFML window.
Definition: DisplayManager.h:31
Color
Colors Dragonfly recognizes.
Definition: Color.h:11
float charWidth(void)
Compute character width in pixels, based on window size.
Vector spacesToPixels(Vector spaces)
Convert ASCII spaces (x,y) to window pixels (x,y).
Justification
String justifications.
Definition: DisplayManager.h:24