NickG

cpos.cpp

Apr 30th, 2012
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.69 KB | None | 0 0
  1. #include "cpos.hpp"
  2.  
  3. #ifdef __WIN32__
  4. #include <windows.h>
  5. coord_t getpos() {               //We're going to get the current position of the cursor!
  6. CONSOLE_SCREEN_BUFFER_INFO csbi; //Create screen info structure
  7. coord_t coord;                   //Create our coordinate structure
  8.  
  9. if (GetConsoleScreenBufferInfo (               //And assuming nothing breaks (thus the if statement)
  10.     GetStdHandle(STD_OUTPUT_HANDLE), &csbi)) { //Pass the info into the screen info structure
  11.    coord.x = csbi.dwCursorPosition.X;          //Set our coordinate x equal to the screen info X
  12.    coord.y = csbi.dwCursorPosition.Y;          //Set our coordinate y equal to the screen info Y
  13.    return coord;                               //Return our structure and we're done!
  14.    }
  15.  
  16. else {           //If it does break, just set everything 0 and return the structure
  17.    coord.x = 0;  //This shouldn't happen...
  18.    coord.y = 0;
  19.    return coord; //...but if it does, this should stop a crash while failing loudly
  20.    }
  21. }
  22.  
  23. void setpos(int x, int y) {
  24. CONSOLE_SCREEN_BUFFER_INFO csbi;                //Create screen info structure
  25. HANDLE hStdout=GetStdHandle(STD_OUTPUT_HANDLE); //Get the handle for Std Out
  26. GetConsoleScreenBufferInfo(hStdout, &csbi);     //Pass the info into the screen info structure
  27. csbi.dwCursorPosition.X=(x-1);                      //Set the screen info X equal to our x
  28. csbi.dwCursorPosition.Y=(y-1);                      //Set the screen info Y equal to our y
  29. SetConsoleCursorPosition(hStdout, csbi.dwCursorPosition); //And ship it out!
  30. }
  31.  
  32. #else
  33. #include <iostream>
  34. #include <sstream>
  35. #include <termios.h>
  36.  
  37. using namespace std;
  38.  
  39. coord_t getpos() {
  40.  
  41. //Create the variables and what not that we're going to use
  42. coord_t coord;
  43. string buffer;
  44. stringstream ss;
  45. int fd=0, i=0;
  46. termios oldio, newio;
  47.  
  48. //Get the current terminal attributes, one to backup, one to alter
  49. tcgetattr(fd, &oldio);
  50. tcgetattr(fd, &newio);
  51. //Turn off echo and line controls
  52. newio.c_lflag &= ~ECHO;
  53. newio.c_lflag &= ~ICANON;
  54. //Flush the terminal and give it the new parameters
  55. tcflush(fd, TCIFLUSH);
  56. tcsetattr(fd,TCSANOW,&newio);
  57.  
  58. //Ask the terminal for the coordinates
  59. cout << "\E[6n";
  60. getline(cin, buffer, 'R');
  61.  
  62. //Restore the old parameters
  63. tcflush(fd, TCIFLUSH);
  64. tcsetattr(fd,TCSANOW,&oldio);
  65.  
  66. //Cut off the escape character
  67. buffer.erase(0,3);
  68. //Extract the coordinates with a couple loops
  69. while (isdigit(buffer[i])) {
  70.     ss << buffer[i];
  71.     i++;
  72.     }
  73. ss << " ";
  74. ss >> coord.y;
  75. i++;
  76. while (isdigit(buffer[i])) {
  77.     ss << buffer[i];
  78.     i++;
  79.     }
  80. ss << " ";
  81. ss >> coord.x;
  82. return coord;
  83. }
  84.  
  85. void setpos(int x, int y) {
  86. //Yep, one escape code, just pass the coordinates
  87. cout << "\E["<<y<<";"<<x<<"H";
  88. }
  89. #endif
Advertisement
Add Comment
Please, Sign In to add comment