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.

88 lines
2.0 KiB

  1. #include "Graphics.h"
  2. #include <chrono>
  3. #include <thread>
  4. #include <time.h>
  5. #include <cstdlib>
  6. ///Utillities for the graphics
  7. unsigned int sleepTime = 40000000;
  8. static WINDOW *_box = NULL;
  9. static void createBox(void){
  10. _box = newwin(LINES-2, COLS, 2, 0);
  11. box(_box, 0, 0);
  12. wrefresh(_box);
  13. }
  14. static void destroyBox(void){
  15. wborder(_box, ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ');
  16. wrefresh(_box);
  17. delwin(_box);
  18. }
  19. //Initialization function
  20. void initializeGraphics(char* gameName){
  21. srand((unsigned int) time(NULL));
  22. initscr(); //initialize curses
  23. cbreak(); //set line buffering to false
  24. noecho(); //set input echo to false
  25. keypad(stdscr, TRUE); //this step enables the use of arrow keys and other function keys
  26. nodelay(stdscr, true);
  27. curs_set(0); //to hide the cursor
  28. //We must clear the screen from unecessary garbage
  29. clear();
  30. //Print the title
  31. mvprintw(0, (COLS/2) - 12, gameName);
  32. refresh();
  33. //create the game box
  34. createBox();
  35. }
  36. //Exit function
  37. void endGraphics(void){
  38. curs_set(1);
  39. destroyBox();
  40. endwin();
  41. }
  42. //Typical refresh function (ease the eye with a custom function instead of the actual one)
  43. void refreshScreen(void){
  44. using namespace std::this_thread; // sleep_for, sleep_until
  45. using namespace std::chrono; // nanoseconds, system_clock, seconds
  46. sleep_for(nanoseconds(sleepTime));
  47. refresh(); // just use the curses version ;p
  48. wrefresh(_box);
  49. }
  50. void printChar(int y, int x, graphics_input img){
  51. mvwaddch(_box, y, x, img);
  52. refresh();
  53. wrefresh(_box);
  54. }
  55. void printMsg(int y, int x, char* str){
  56. if(y>0 && x>0)
  57. mvwaddstr(_box, y, x, str);
  58. else{
  59. if(y < 0) y = 2 + y;
  60. if(x < 0) x = 0;
  61. mvaddstr(y, x, str);
  62. }
  63. refresh();
  64. wrefresh(_box);
  65. }
  66. char readChar(int y, int x){
  67. refresh();
  68. wrefresh(_box);
  69. return mvwgetch(_box, y, x);
  70. }
  71. int readInpt(){
  72. return getch();
  73. }
  74. void advanceDifficulty(void){
  75. if(sleepTime > 28000000) // we set 28000000 as teh min sleep time
  76. sleepTime -= 1000000;
  77. }