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.

91 lines
1.6 KiB

  1. #include "Graphics.h"
  2. #include <chrono>
  3. #include <thread>
  4. #include <time.h>
  5. #include <cstdlib>
  6. void Graphics::createBox() {
  7. box_ = newwin(LINES-2, COLS, 2, 0);
  8. box(box_, 0, 0);
  9. wrefresh(box_);
  10. }
  11. void Graphics::destroyBox() {
  12. wborder(box_, ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ');
  13. wrefresh(box_);
  14. delwin(box_);
  15. }
  16. void Graphics::init(const std::string& gameName) {
  17. srand((unsigned int) time(NULL));
  18. initscr();
  19. cbreak();
  20. noecho();
  21. keypad(stdscr, TRUE);
  22. nodelay(stdscr, true);
  23. curs_set(0);
  24. clear();
  25. mvprintw(0, (COLS/2) - 12, gameName.c_str());
  26. refresh();
  27. createBox();
  28. }
  29. void Graphics::finalize() {
  30. curs_set(1);
  31. destroyBox();
  32. endwin();
  33. }
  34. void Graphics::refreshScreen() {
  35. using namespace std::this_thread;
  36. using namespace std::chrono;
  37. sleep_for(nanoseconds(sleep_time_));
  38. refresh();
  39. wrefresh(box_);
  40. }
  41. void Graphics::printChar(int y, int x, int img) {
  42. mvwaddch(box_, y, x, img);
  43. refresh();
  44. wrefresh(box_);
  45. }
  46. void Graphics::printMsg(int y, int x, const std::string& str) {
  47. if(y > 0 && x > 0) {
  48. mvwaddstr(box_, y, x, str.c_str());
  49. }
  50. else {
  51. if(y < 0) y += 2;
  52. if(x < 0) x = 0;
  53. mvaddstr(y, x, str.c_str());
  54. }
  55. refresh();
  56. wrefresh(box_);
  57. }
  58. char Graphics::readChar(int y, int x) {
  59. refresh();
  60. wrefresh(box_);
  61. return mvwgetch(box_, y, x);
  62. }
  63. int Graphics::readInpt() {
  64. return getch();
  65. }
  66. void Graphics::advanceDifficulty() {
  67. if(sleep_time_ > 28000000) {
  68. sleep_time_ -= 1000000;
  69. }
  70. }
  71. Graphics& Graphics::get() {
  72. static Graphics graphics;
  73. return graphics;
  74. }