MiniGame-Madness
screenBuffer.h
1 
2 #ifndef SCREENBUFFER_H
3 #define SCREENBUFFER_H
4 
11 #include <string>
12 #include <utility>
13 #include <vector>
14 #include <stdexcept>
15 #include <windows.h>
16 #include <iostream>
17 #include <conio.h>
18 
27 {
28 private:
29  HANDLE screenHandle = CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE, 0, NULL, CONSOLE_TEXTMODE_BUFFER, NULL);
30  const WORD NO_COLOUR = -1;
31 
36  void throwError(BOOL result, const std::string& message) const;
37 
45  BOOL writeToScreenBuffer(int x, int y, const std::wstring& text);
46 
52  BOOL writeToScreenBuffer(const std::wstring& text);
53 
60  std::wstring setTextColours(const std::wstring& text, WORD textColour, WORD backgroundColour);
61 
67  WORD convertColour(WORD colour) const;
68 
69 
70 
71 public:
72  // Constants for text and background colours
73  static const WORD FOREGROUND_NORMAL = -3;
74  static const WORD BACKGROUND_NORMAL = -13;
75  static const WORD BLACK = 0;
76  static const WORD RED = 1;
77  static const WORD GREEN = 2;
78  static const WORD YELLOW = 3;
79  static const WORD BLUE = 4;
80  static const WORD MAGENTA = 5;
81  static const WORD CYAN = 6;
82  static const WORD WHITE = 7;
83 
87  ScreenBuffer();
88 
94  ScreenBuffer(int width, int height);
95 
99  ~ScreenBuffer();
100 
105  HANDLE getScreenHandle() const;
106 
111  bool isActive() const;
112 
116  void setActive();
117 
122  CONSOLE_SCREEN_BUFFER_INFO getScreenBufferInfo() const;
123 
127  void clearScreen();
128 
133  int getScreenWidth() const;
134 
139  int getScreenHeight() const;
140 
146  void setScreenSize(int width, int height);
147 
155  std::pair<WORD, WORD> getScreenColours(int x, int y, int length) const;
156 
161  void setCursorVisibility(bool isVisible);
162 
168  void setCursorPosition(int x, int y);
169 
174  std::pair<int, int> getCursorPosition() const;
175 
183  std::wstring readScreenText(int x, int y, int length) const;
184 
189  std::wstring readAllScreenText() const;
190 
197  void writeToScreen(int x, int y, const std::wstring& text);
198 
207  void writeToScreen(int x, int y, const std::wstring& text, WORD textColour, WORD backgroundColour);
208 
213  std::string getBlockingInput();
214 
219  std::string getNonBlockingInput();
220 };
221 
222 #endif // SCREENBUFFER_H
~ScreenBuffer()
Destructor for the screenBuffer class.
Definition: screenBuffer.cpp:104
A class that represents the screen buffer.
Definition: screenBuffer.h:26
std::pair< int, int > getCursorPosition() const
Get the current location of the cursor.
Definition: screenBuffer.cpp:332
std::pair< WORD, WORD > getScreenColours(int x, int y, int length) const
Get the screen text and background colors.
Definition: screenBuffer.cpp:213
std::string getBlockingInput()
get blocking input from the user
Definition: screenBuffer.cpp:397
void setCursorVisibility(bool isVisible)
Set cursor visibility.
Definition: screenBuffer.cpp:300
void clearScreen()
Clears the screen buffer.
Definition: screenBuffer.cpp:150
void setScreenSize(int width, int height)
Set the size of the screen buffer and window.
Definition: screenBuffer.cpp:183
int getScreenWidth() const
Get the screen buffer width.
Definition: screenBuffer.cpp:164
HANDLE getScreenHandle() const
Get the screen buffer handle.
Definition: screenBuffer.cpp:112
void setCursorPosition(int x, int y)
Move the cursor to the specified location.
Definition: screenBuffer.cpp:319
std::string getNonBlockingInput()
get non-blocking input from the user
CONSOLE_SCREEN_BUFFER_INFO getScreenBufferInfo() const
Get screen buffer info.
Definition: screenBuffer.cpp:137
ScreenBuffer()
Constructor for the screenBuffer class.
Definition: screenBuffer.cpp:51
int getScreenHeight() const
Get the screen buffer height.
Definition: screenBuffer.cpp:174
std::wstring readScreenText(int x, int y, int length) const
Get part of text in the screen buffer.
Definition: screenBuffer.cpp:342
bool isActive() const
Check if the screen buffer is active.
Definition: screenBuffer.cpp:119
std::wstring readAllScreenText() const
Get all of the text in the screen buffer.
Definition: screenBuffer.cpp:370
void setActive()
Set the screen buffer to active.
Definition: screenBuffer.cpp:126
void writeToScreen(int x, int y, const std::wstring &text)
Write text to the screen at a specific location.
Definition: screenBuffer.cpp:378