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 public:
70  // Constants for text and background colours
71  static const WORD FOREGROUND_NORMAL = -3;
72  static const WORD BACKGROUND_NORMAL = -13;
73  static const WORD BLACK = 0;
74  static const WORD RED = 1;
75  static const WORD GREEN = 2;
76  static const WORD YELLOW = 3;
77  static const WORD BLUE = 4;
78  static const WORD MAGENTA = 5;
79  static const WORD CYAN = 6;
80  static const WORD WHITE = 7;
81 
85  ScreenBuffer();
86 
92  ScreenBuffer(int width, int height);
93 
97  ~ScreenBuffer();
98 
103  HANDLE getScreenHandle() const;
104 
109  bool isActive() const;
110 
114  void setActive();
115 
120  CONSOLE_SCREEN_BUFFER_INFO getScreenBufferInfo() const;
121 
125  void clearScreen();
126 
131  int getScreenWidth() const;
132 
137  int getScreenHeight() const;
138 
144  void setScreenSize(int width, int height);
145 
153  std::pair<WORD, WORD> getScreenColours(int x, int y, int length) const;
154 
159  void setCursorVisibility(bool isVisible);
160 
166  void setCursorPosition(int x, int y);
167 
172  std::pair<int, int> getCursorPosition() const;
173 
181  std::wstring readScreenText(int x, int y, int length) const;
182 
187  std::wstring readAllScreenText() const;
188 
195  void writeToScreen(int x, int y, const std::wstring& text);
196 
205  void writeToScreen(int x, int y, const std::wstring& text, WORD textColour, WORD backgroundColour);
206 
211  std::string getBlockingInput();
212 
217  std::string getNonBlockingInput();
218 };
219 
220 #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