Space Walk board game
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

223 lines
4.7 KiB

  1. #include <cstdio>
  2. #include "gamestate.h"
  3. #include "window.h"
  4. Window::Window(int rows, int cols, int y, int x) :
  5. rows(rows),
  6. cols(cols),
  7. y(0),
  8. x(0),
  9. window(newwin(rows, cols, y, x)) {
  10. }
  11. Window::~Window() {
  12. delwin(this->window);
  13. }
  14. void Window::initialize() {
  15. // Start curses mode
  16. initscr();
  17. // Disable line buffering
  18. cbreak();
  19. // Enable reading of function keys
  20. keypad(stdscr, TRUE);
  21. // Dont echo while reading input
  22. noecho();
  23. // Disable cursor visibility
  24. curs_set(0);
  25. // If terminal supports colors
  26. if(!has_colors()) {
  27. printw("Your terminal does not support color\n");
  28. }
  29. else {
  30. start_color();
  31. use_default_colors();
  32. init_pair(PAIR_WHITE_BLACK, COLOR_WHITE, COLOR_BLACK);
  33. init_pair(PAIR_WHITE_BRIGHT_BLACK, COLOR_WHITE_BRIGHT, COLOR_BLACK);
  34. init_pair(PAIR_RED_BLACK, COLOR_RED, COLOR_BLACK);
  35. init_pair(PAIR_BLUE_BLACK, COLOR_BLUE, COLOR_BLACK);
  36. init_pair(PAIR_GRAY_BLACK_BRIGHT, COLOR_GRAY, COLOR_BLACK_BRIGHT);
  37. }
  38. // Add bright colors, intensity range: 0-1000
  39. if(!can_change_color()) {
  40. printw("Your terminal does not support changing colors\n");
  41. }
  42. else {
  43. init_color(COLOR_BLACK, 0, 0, 0);
  44. init_color(COLOR_WHITE, 680, 680, 680);
  45. init_color(COLOR_GRAY, 380, 380, 380);
  46. init_color(COLOR_BLACK_BRIGHT, 110, 110, 110); // 0, 0, 0
  47. init_color(COLOR_RED_BRIGHT, 900, 0, 0); // 680, 0, 0
  48. init_color(COLOR_GREEN_BRIGHT, 0, 900, 0); // 0, 680, 0
  49. init_color(COLOR_YELLOW_BRIGHT, 900, 900, 0); // 680, 680, 0
  50. init_color(COLOR_BLUE_BRIGHT, 0, 0, 900); // 0, 0, 680
  51. init_color(COLOR_MAGENTA_BRIGHT, 900, 0, 900); // 680, 0, 680
  52. init_color(COLOR_CYAN_BRIGHT, 0, 900, 900); // 0, 680, 680
  53. init_color(COLOR_WHITE_BRIGHT, 900, 900, 900); // 680, 680, 680
  54. }
  55. }
  56. void Window::update() {
  57. Window::getMaXYZ(stdscr, this->maxRows, this->maxCols);
  58. }
  59. void Window::render() {
  60. box(this->window, 0, 0);
  61. wrefresh(this->window);
  62. }
  63. void Window::destroy() {
  64. // Restore line buffering
  65. nocbreak();
  66. // Restore cursor
  67. curs_set(1);
  68. // End curses mode
  69. endwin();
  70. }
  71. void Window::clear() {
  72. this->setYX(0, 0);
  73. // Set background color
  74. // wbkgdset(this->window, COLOR_PAIR(3));
  75. // Copy blanks to every position in the window, clearing the screen
  76. werase(this->window);
  77. }
  78. void Window::resize() {
  79. wresize(this->window, this->rows, this->cols);
  80. mvwin(this->window, this->y, this->x);
  81. }
  82. void Window::print(std::string str, char newLine) {
  83. this->print(str, this->y, this->x);
  84. this->x += str.length();
  85. if (newLine == 1) {
  86. this->x = 0;
  87. this->y++;
  88. }
  89. }
  90. void Window::print(std::string str, int y, int x) {
  91. this->y = y;
  92. this->x = x;
  93. mvwprintw(this->window, y + 1, x + 1, str.c_str());
  94. }
  95. void Window::print(std::string str, int y, int x, int attribute) {
  96. wattron(this->window, attribute);
  97. this->print(str, y, x);
  98. wattroff(this->window, attribute);
  99. }
  100. void Window::printCenter(std::string str) {
  101. int halfBoard = this->cols / 2;
  102. int halfStr = str.length() / 2;
  103. this->print(str, this->y, halfBoard - halfStr - 1);
  104. }
  105. void Window::printCenter(std::string str, int attribute) {
  106. wattron(this->window, attribute);
  107. this->printCenter(str);
  108. wattroff(this->window, attribute);
  109. }
  110. void Window::printSide(std::string left, std::string right) {
  111. this->print(left, this->y, 1);
  112. this->print(right, this->y, this->cols - right.length() - 3);
  113. }
  114. void Window::getYX(int &y, int &x) {
  115. getyx(this->window, y, x);
  116. }
  117. void Window::setYX(int y, int x) {
  118. this->y = y;
  119. this->x = x;
  120. wmove(this->window, y, x);
  121. }
  122. void Window::getMaXYZ(int &rows, int &cols) {
  123. getmaxyx(this->window, rows, cols);
  124. }
  125. void Window::getMaXYZ(WINDOW *window, int &rows, int &cols) {
  126. getmaxyx(window, rows, cols);
  127. }
  128. std::string Window::getStr() {
  129. curs_set(1);
  130. echo();
  131. char *tmp = new char();
  132. wgetstr(this->window, tmp);
  133. noecho();
  134. curs_set(0);
  135. return std::string(tmp);
  136. }
  137. int Window::getMaxRows() {
  138. return this->maxRows;
  139. }
  140. int Window::getMaxCols() {
  141. return this->maxCols;
  142. }
  143. int Window::getRows() {
  144. return this->rows;
  145. }
  146. void Window::setRows(int rows) {
  147. this->rows = rows;
  148. }
  149. int Window::getCols() {
  150. return this->cols;
  151. }
  152. void Window::setCols(int cols) {
  153. this->cols = cols;
  154. }
  155. int Window::getY() {
  156. return this->y;
  157. }
  158. void Window::setY(int y) {
  159. this->y = y;
  160. }
  161. int Window::getX() {
  162. return this->x;
  163. }
  164. void Window::setX(int x) {
  165. this->x = x;
  166. }
  167. void Window::setWindow(int rows, int cols, int y, int x) {
  168. this->setRows(rows);
  169. this->setCols(cols);
  170. this->setY(y);
  171. this->setX(x);
  172. if (this->window == nullptr) {
  173. this->window = newwin(rows, cols, y, x);
  174. }
  175. }
  176. //----------------------------------------------------------------------------//
  177. Window::Window() :
  178. rows(0),
  179. cols(0),
  180. y(0),
  181. x(0),
  182. window(nullptr) {
  183. }