Dragonfly 4.19
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 /// Return 0 if ok, else -1.
85 int drawCh(Vector world_pos, char ch, Color color) const;
86
87 /// Draw character at window location (x,y) with custom color.
88 /// Return 0 if ok, else -1.
89 int drawCh(Vector world_pos, char ch, unsigned char r,
90 unsigned char g, unsigned char b) const;
91
92 /// Deprecated. Just uses Frame::draw().
93 DF_DEPRECATED int drawFrame(Vector world_pos, Frame frame, bool centered,
94 Color color, char transparent=0) const;
95
96 /// Draw string at window location (x,y) with color.
97 /// Justified left, center or right.
98 /// Return 0 if ok, else -1.
99 int drawString(Vector world_pos, std::string str, Justification just,
100 Color color) const;
101
102 /// Set window's horizontal maximum (in characters).
103 void setHorizontal(int new_horizontal);
104
105 /// Return window's horizontal maximum (in characters).
106 int getHorizontal() const;
107
108 /// Set window's vertical maximum (in characters).
109 void setVertical(int new_vertical);
110
111 /// Return window's vertical maximum (in characters).
112 int getVertical() const;
113
114 /// Return window's horizontal maximum (in pixels).
116
117 /// Return window's vertical maximum (in pixels).
118 int getVerticalPixels() const;
119
120 /// Set window's background color.
121 /// Return true if ok, else false.
122 bool setBackgroundColor(Color new_color);
123
124 /// "Shake" window with severity scale (pixels) and duration (frames).
125 /// Delta true then add else replace.
126 void shake(int scale_x, int scale_y, int duration, bool delta=false);
127
128 /// Render current window buffer.
129 /// Return 0 if ok, else -1.
131
132 /// Return pointer to SFML graphics window.
133 sf::RenderWindow *getWindow() const;
134};
135
136} // end of namespace df
137#endif //__DISPLAY_MANAGER_H__
Definition: DisplayManager.h:53
int drawCh(Vector world_pos, char ch, unsigned char r, unsigned char g, unsigned char b) const
Draw character at window location (x,y) with custom color.
int drawCh(Vector world_pos, char ch, Color color) const
Draw character at window location (x,y) with color.
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 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
DF_DEPRECATED int drawFrame(Vector world_pos, Frame frame, bool centered, Color color, char transparent=0) const
Deprecated. Just uses Frame::draw().
Definition: Frame.h:17
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