Chinaunix首页 | 论坛 | 博客
  • 博客访问: 895377
  • 博文数量: 299
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2493
  • 用 户 组: 普通用户
  • 注册时间: 2014-03-21 10:07
个人简介

Linux后台服务器编程。

文章分类

全部博文(299)

文章存档

2015年(2)

2014年(297)

分类: C/C++

2014-06-09 17:04:44

翻看1年前的帖子,感慨呀。。。这个代码。。还是稚嫩了一点。
现在编程风格也改了很多。而且逐渐的抛弃了LINUX,转为WIN程序员。
谢谢大家的支持,补上个图把
复制下面地址,在浏览器访问:
els.JPG 





学C++和linux有1.5年了,但是来论坛才没几天。昨天发个帖子,大家回复都很热情

,谢谢各位帮助!(昨天帖子地址是  )

这个方块游戏是用linux终端的光标控制、颜色设置做的
用 A S D W 控制移动、转向,空格键下坠到底;

最初是2个线程,1个用来显示画面,1个用来接收用户输入;
后来改用单线程,linux的异步aio函数解决了很多麻烦;

用了个简单的模板单例模式,继承它就可以;
对POSIX线程简单封装成java线程接口;

bug不少,大家不吝指教和建议。


///////////main.cpp
  1. #include
  2. #include "Tetris.h"
  3. #include "TtyAuto.h"

  4. int main(int ac, char *av[])
  5. {
  6.         std::auto_ptr autoAdjustTty(TtyAuto::getInstance());

  7.         Tetris game;
  8.         game.start();
  9.         game.join();

  10.         printf("\t\t\t\t\t\t\t\t\r");
  11.         printf("GAME OVER!\n");

  12.         return 0;
  13. }
复制代码
/////////   Thread.h
  1. #ifndef BERT_THREAD_H
  2. #define BERT_THREAD_H

  3. #include

  4. /**
  5. * 线程封装,接口模仿JAVA
  6. */
  7. class Runnable
  8. {
  9. public:
  10.         virtual ~Runnable() { }

  11.         /**
  12.          * 线程逻辑在此实现
  13.          */
  14.         virtual void run() = 0;
  15. };

  16. class Thread: public Runnable
  17. {
  18.         /**
  19.          * 本线程ID
  20.          */
  21.         pthread_t m_tid;

  22.         /**
  23.          * 是否在运行的标志
  24.          */
  25.         bool running;

  26.         /**
  27.          * 线程函数,内部调用run
  28.          */
  29.         static void * threadFunc(void * arg);

  30. public:
  31.         Thread();
  32.         ~Thread();

  33.         /**
  34.          * 睡眠 秒
  35.          */
  36.         static unsigned int sleep( unsigned int seconds);

  37.         /**
  38.          * 睡眠毫秒
  39.          */
  40.         static unsigned int msleep( unsigned int mSeconds);

  41.         /**
  42.          * 启动线程
  43.          */
  44.         bool start() ;

  45.         /**
  46.          * 等待本线程运行完
  47.          */
  48.         void join() const;

  49.         /**
  50.          * 是否在运行
  51.          */
  52.         bool isAlive() const
  53.         {
  54.                 return running;
  55.         }

  56.         /**
  57.          * 终止线程运行
  58.          */
  59.         void stop()
  60.         {
  61.                 running = false;
  62.         }
  63. };

  64. #endif
复制代码
////////    Thread.cc
  1. #include
  2. #include
  3. #include
  4. #include "Thread.h"

  5. Thread::Thread( ) : running(false)
  6. {
  7. }

  8. Thread::~Thread()
  9. {
  10. }

  11. void * Thread::threadFunc(void * arg)
  12. {
  13.         Thread * r = (Thread *) arg;
  14.         r->running = true;
  15.         srand(time(NULL));
  16.         r->run();
  17.         ::pthread_exit(NULL);
  18.         return NULL;
  19. }

  20. unsigned int Thread::sleep( unsigned int seconds)
  21. {
  22.         return ::sleep(seconds);
  23. }

  24. unsigned int Thread::msleep( unsigned int mSeconds)
  25. {
  26.         return ::usleep(mSeconds * 1000U);
  27. }

  28. bool Thread::start()
  29. {
  30.         if ( !running )
  31.         {
  32.                 if ( ::pthread_create(&m_tid, NULL, threadFunc, this ) )
  33.                         return false;
  34.         }
  35.         return true;
  36. }

  37. void Thread::join() const
  38. {
  39.         ::pthread_join(m_tid, NULL);
  40. }
复制代码
///  俄罗斯方块头文件
  1. #ifndef BERT_TETRIS_H
  2. #define BERT_TETRIS_H

  3. #include
  4. #include

  5. #include "Thread.h"

  6. /**
  7. * 一个用printf做的linux俄罗斯方块
  8. */

  9. class Tetris : public Thread
  10. {
  11. public:
  12.         Tetris();
  13.         ~Tetris();
  14. private:
  15.         enum BOARD_T
  16.         {
  17.                 TYPE_BLANK = 0, //空白
  18.                 TYPE_BLOCK,        //阻挡
  19.                 TYPE_BORDER,        //边界

  20.                 TYPE_MAX = 3,
  21.         };
  22.         
  23.         /**
  24.          * 游戏背景,尺寸
  25.          */
  26.         static const int HEIGHT = 22;
  27.         static const int WIDTH  = 14;
  28.         BOARD_T board[HEIGHT][WIDTH] ;
  29.         
  30.         /**??是否用volatile
  31.          * 当前活动方块的坐标
  32.          */
  33.         int curx, cury;
  34.         
  35.         /**
  36.          * 当前活动方块的样式及翻转形状
  37.          */
  38.         int curstyle;
  39.         int curpos;
  40.         
  41.         /**
  42.          * 当前活动方块的颜色
  43.          */
  44.         int curcolor ;

  45.         /**
  46.          * 玩家当前得分和等级
  47.          */
  48.         unsigned int score;
  49.         unsigned short level;
  50.         static const unsigned short MAXLEVEL = 9;

  51.         /**
  52.          * 一个方块的定义
  53.          */
  54.         typedef std::bitset<16> Block;
  55.         
  56.         /**
  57.          * 方块的样式种类
  58.          */
  59.         static const unsigned short STYLE_MAX = 7;
  60.         
  61.         /**
  62.          * 当前活动方块与下一个方块
  63.          */
  64.         Block activeBlock, nextBlock;

  65.         /**
  66.          * 现在是否有活动方块?
  67.          */
  68.         bool active;

  69.         /**
  70.          * 方块样式预定义
  71.          */
  72.         static const unsigned short _styles_[STYLE_MAX][4] ;
  73.         Block styles[STYLE_MAX][4];

  74.         /**
  75.          *  初始化方块样式
  76.          */
  77.         void initStyles();
  78.         
  79.         /**
  80.          *  显示分数
  81.          */
  82.         void drawScore();
  83.         
  84.         /**
  85.          *  显示等级
  86.          */
  87.         void drawLevel();

  88.         /**
  89.          *  显示下一个方块
  90.          */
  91.         void drawNext();

  92.         /**
  93.          * 异步IO,使得单线程能完成显示和响应键盘的功能
  94.          */
  95.         struct aiocb cb;
  96.         static const int SIZE = 16;
  97.         unsigned char inputBuf[SIZE];
  98.         friend void handler(int sig, siginfo_t *info, void * context);

  99.         /**
  100.          * 初始化背景数据
  101.          */
  102.         void initBackground();

  103.         /**
  104.          * 绘制游戏画面
  105.          */
  106.         void paint(int srcx = 0, int dstx = HEIGHT);

  107.         /**
  108.          * 判断方块是否与阻挡或边界冲突
  109.          */
  110.         bool isCollide();

  111.         /**
  112.          * 消行,并返回削去的行数(0-4)
  113.          */
  114.         int removeLines();

  115.         /**
  116.          * 判断line行是否为空
  117.          */
  118.         bool isEmptyLine(int line);

  119.         /**
  120.          * 根据消行,返回应得分数
  121.          */
  122.         static const unsigned short bonus[5];

  123.         /**
  124.          * 达到相应等级所需要的分数;10-levels
  125.          */
  126.         static const unsigned int levels[10];

  127.         /**
  128.          *  根据得分,计算等级
  129.          */
  130.         static unsigned short getLevel(unsigned int score);

  131.         /**
  132.          * 线程函数,游戏逻辑
  133.          */
  134.         virtual void run();
  135. };

  136. #endif
复制代码
//////////// 俄罗斯方块 实现
  1. #include
  2. #include
  3. #include
  4. #include
  5. #include
  6. #include
  7. #include
  8. #include

  9. #include "Tetris.h"

  10. // 清除屏幕
  11. #define CLEAR()  printf("\033[2J")

  12. // 上移光标
  13. #define MOVEUP(x) printf("\033[%dA", (x))
  14. // 下移光标
  15. #define MOVEDOWN(x) printf("\033[%dB", (x))
  16. // 左移光标
  17. #define MOVELEFT(y) printf("\033[%dD", (y))
  18. // 右移光标
  19. #define MOVERIGHT(y) printf("\033[%dC",(y))

  20. // 定位光标
  21. #define MOVETO(x,y) printf("\033[%d;%dH", (x), (y))
  22. // 光标复位
  23. #define RESET_CURSOR() printf("\033[H")

  24. // 隐藏光标
  25. #define        HIDE_CURSOR() printf("\033[?25l")
  26. // 显示光标
  27. #define        SHOW_CURSOR() printf("\033[?25h")

  28. // 画背景
  29. #define DRAW_BLANK()  printf("\033[1;31;40m%s\033[0m",". " )
  30. // 画阻挡
  31. #define DRAW_BLOCK() printf("\033[7m\033[1;37;40m%s\033[0m", "[]" )
  32. // 画活动方块
  33. #define DRAW_ACTIVE_BLOCK() printf("\033[7m\033[1;%d;40m%s\033[0m",curcolor, "[]" )
  34. // 画边界
  35. #define DRAW_BORDER()  printf("\033[1;33;43m%s\033[0m", "||" )
  36. // 随机获得一种颜色
  37. #define GET_COLOR() (rand()%6 + 31)

  38. const int Tetris::HEIGHT;
  39. const int Tetris::WIDTH;
  40. const int Tetris::SIZE;
  41. const unsigned short Tetris::STYLE_MAX;
  42. const unsigned short Tetris::MAXLEVEL;
  43. const unsigned short Tetris::bonus[5] = {0, 100, 300, 600, 1000};
  44. const unsigned int Tetris::levels[MAXLEVEL+1] = {0, 10000, 30000, 60000, 100000, 150000, 210000, 280000, 360000, 450000};

  45. const unsigned short Tetris::_styles_[STYLE_MAX][4] = {
  46.                 {0x000f, 0x4444, 0x000f, 0x4444},
  47.                 {0x004e, 0x0464, 0x00e4, 0x04c4},
  48.                 {0x0462, 0x006c, 0x0462, 0x006c},
  49.                 {0x0264, 0x00c6, 0x0264, 0x00c6},
  50.                 {0x0622, 0x0017, 0x0223, 0x0074},
  51.                 {0x0644, 0x00e2, 0x044c, 0x008e},
  52.                 {0x0066, 0x0066, 0x0066, 0x0066},
  53. };

  54. unsigned short Tetris::getLevel(unsigned int score )
  55. {
  56.         int low = 0, high = MAXLEVEL;
  57.         int mid;

  58.         while ( true )
  59.         {
  60.                 mid = (low + high ) / 2;

  61.                 if ( score >= levels[mid])
  62.                                 {
  63.                                         if ( mid==MAXLEVEL || score < levels[mid+1])
  64.                                                 return mid;
  65.                                         else
  66.                                                 low = mid + 1;
  67.                                 }
  68.                                 else//不可能是mid等级 
  69.                                 {
  70.                                         assert(mid>0);
  71.                                         high = mid - 1;
  72.                                 }
  73.         }
  74. }

  75. Tetris::Tetris() : score(0), level(0), active(false)
  76. {
  77.         bzero(&cb, sizeof(cb));
  78.         initStyles();
  79.         initBackground();
  80.         HIDE_CURSOR();
  81. }

  82. Tetris::~Tetris()
  83. {
  84.         SHOW_CURSOR();
  85. }

  86. void Tetris::initStyles()
  87. {
  88.         for ( int i=0; i
  89.         {
  90.                 for ( int j=0; j<4; ++j)
  91.                 {
  92.                         styles[i][j] = _styles_[i][j];
  93.                 }
  94.         }
  95. }

  96. void Tetris::initBackground()
  97. {
  98.         for ( int i=0; i
  99.                 for( int j=0; j
  100.                 {
  101.                         if ( i==0 || j==0 || i==HEIGHT-1 || j==WIDTH-1)
  102.                                 board[i][j] = TYPE_BORDER;
  103.                         else
  104.                                 board[i][j] = TYPE_BLANK;
  105.                 }
  106. }

  107. void Tetris::paint(int srcx, int dstx)
  108. {
  109.         if ( dstx > HEIGHT )
  110.                 dstx = HEIGHT-1;

  111.         for ( int i=srcx; i
  112.         {
  113.                 for ( int j=0; j
  114.                 {
  115.                         if ( board[i][j] == TYPE_BORDER )
  116.                         {
  117.                                 DRAW_BORDER();
  118.                                 continue;
  119.                         }
  120.                         
  121.                         if ( i>=curx && i=cury && j
  122.                         {
  123.                                 if ( board[i][j] == TYPE_BLANK && activeBlock[(i-curx)*4 + j-cury] == TYPE_BLANK )
  124.                                         DRAW_BLANK();
  125.                                 else
  126.                                         DRAW_ACTIVE_BLOCK();
  127.                         }
  128.                         else
  129.                         {
  130.                                 if ( board[i][j] == TYPE_BLOCK )
  131.                                         DRAW_BLOCK();
  132.                                 else
  133.                                         DRAW_BLANK();
  134.                         }
  135.                 }
  136.                 printf("\n");
  137.         }
  138. }

  139. void Tetris::drawScore()
  140. {
  141.         MOVETO(2, 2*WIDTH+2);

  142.         printf("\033[7m\033[1;33;46m");
  143.         printf("    SCORE: ");
  144.         printf("\033[1;37;40m %-8u", score);
  145.         printf("\033[0m");
  146. }

  147. void Tetris::drawLevel()
  148. {
  149.         MOVETO(6, 2*WIDTH+2);

  150.         printf("\033[7m\033[1;34;43m");
  151.         printf("    LEVEL: ");
  152.         printf("\033[1;31;42m %-8u", level+1);//不显等级0
  153.         printf("\033[0m");
  154. }

  155. void Tetris::drawNext()
  156. {
  157.         MOVETO(11, 2*WIDTH+2);
  158.         printf("\033[1;34;43m\tNEXT:\033[0m");
  159.         MOVELEFT(5);
  160.         MOVEDOWN(2);

  161.         for ( int i=0; i<16; ++i)
  162.         {
  163.                 if ( nextBlock[i] )
  164.                         DRAW_BLOCK();
  165.                 else
  166.                         //printf("  ");
  167.                         DRAW_BLANK();

  168.                 if ( (i+1)%4 == 0 )
  169.                 {
  170.                         MOVELEFT(8);
  171.                         MOVEDOWN(1);
  172.                 }
  173.         }
  174. }

  175. bool Tetris::isCollide()
  176. {
  177.         for ( int i=curx; i
  178.                 for ( int j=cury; j
  179.                         if ( board[i][j] && activeBlock[(i-curx) * 4 + j-cury]) 
  180.                                 return true;

  181.         return false;
  182. }

  183. bool Tetris::isEmptyLine(int line)
  184. {
  185.         for ( int j=1; j
  186.                 if ( board[line][j] != TYPE_BLANK )
  187.                         return false;
  188.         return true;
  189. }

  190. int Tetris::removeLines()
  191. {
  192.         int nLines = 0 ;
  193.         bool full;
  194.         
  195.         for ( int i=HEIGHT-2; i>0; --i)
  196.         {
  197.                 full = true;
  198.                 for ( int j=1; j
  199.                         if ( board[i][j] == TYPE_BLANK )
  200.                         {
  201.                                 full = false;
  202.                                 break;
  203.                         }

  204.                 if ( full )
  205.                 {
  206.                         ++nLines;
  207.                         int k, m;
  208.                         for ( k=i-1; k>0 && !isEmptyLine(k); --k)
  209.                                 for ( m=1; m
  210.                                         board[k+1][m] = board[k][m];
  211.                         // 消行了,k行一定为空
  212.                         assert ( k>=0 && isEmptyLine(k) );
  213.                         for ( m=1; m
  214.                                 board[k+1][m] = TYPE_BLANK;

  215.                         ++i;
  216.                 }
  217.         }

  218.         return nLines;
  219. }

  220. void handler(int sig, siginfo_t *info, void * context)
  221. {
  222.         Tetris * game = (Tetris *)info->si_value.sival_ptr;
  223.         struct aiocb *pcb = &(game->cb);

  224.         assert( !aio_error(pcb) && "aio_handler error" );

  225.         int nbytes = aio_return(pcb);
  226.         if ( nbytes == 0  )
  227.                 return;

  228.         int nread = -1;
  229.         if ( !game->active)
  230.                 goto end;

  231.         while ( ++nread < nbytes  )
  232.         {
  233.                 switch(game->inputBuf[nread])
  234.                 {
  235.                         case 'a':
  236.                         case 'A':
  237.                                 --game->cury;
  238.                                 if ( game->isCollide() )
  239.                                         ++game->cury;
  240.                                 else
  241.                                 {
  242.                                         MOVETO(game->curx+1, 0);
  243.                                         game->paint(game->curx, game->curx+4);
  244.                                 }
  245.                                 break;
  246.                         case 's':
  247.                         case 'S':
  248.                                 ++game->curx;
  249.                                 if ( game->isCollide( ) )
  250.                                         --game->curx;
  251.                                 else
  252.                                 {
  253.                                         MOVETO(game->curx, 0 );
  254.                                         game->paint(game->curx-1, game->curx+4 );
  255.                                 }
  256.                                 break;
  257.                         case 'd':
  258.                         case 'D':
  259.                                 ++game->cury;
  260.                                 if ( game->isCollide( ) )
  261.                                         --game->cury;
  262.                                 else
  263.                                 {
  264.                                         MOVETO(game->curx+1, 0);
  265.                                         game->paint(game->curx, game->curx+4);
  266.                                 }
  267.                                 break;
  268.                         case 'w':
  269.                         case 'W':
  270.                                 {
  271.                                         Tetris::Block saveblock = game->activeBlock;
  272.                                         int savedpos = game->curpos;

  273.                                         game->curpos = (game->curpos + 1) % 4;
  274.                                         game->activeBlock = game->styles[game->curstyle][game->curpos];
  275.                                         if ( game->isCollide( ) )
  276.                                         {
  277.                                                 game->curpos = savedpos;
  278.                                                 game->activeBlock = saveblock;
  279.                                         }
  280.                                         else
  281.                                         {
  282.                                                 MOVETO(game->curx+1, 0);
  283.                                                 game->paint(game->curx, game->curx+4);
  284.                                         }
  285.                                 }
  286.                                 break;
  287.                         case ' ':
  288.                                 {
  289.                                         int oldx = game->curx;
  290.                                         // 提高效率,一次移两行
  291.                                         while ( !game->isCollide() )
  292.                                                 game->curx += 2;
  293.                                         
  294.                                         if ( game->isCollide() )
  295.                                         {
  296.                                                 --game->curx;
  297.                                                 if ( game->isCollide() )
  298.                                                         --game->curx;
  299.                                         }
  300.                                         
  301.                                         if ( oldx == game->curx )
  302.                                                 break;
  303.                                                 
  304.                                         if ( game->curx - oldx < 4)
  305.                                         {
  306.                                                 MOVETO(oldx+1, 0);
  307.                                                 game->paint(oldx, game->curx+4);
  308.                                         }
  309.                                         else
  310.                                         {
  311.                                                 MOVETO(oldx+1, 0);
  312.                                                 game->paint(oldx, oldx+4);
  313.                                                 MOVETO(game->curx+1, 0);
  314.                                                 game->paint(game->curx, game->curx+4);
  315.                                         }

  316.                                         goto end;
  317.                                 }
  318.                                 break;
  319.                         default:
  320.                                 break;
  321.                 }
  322.         }

  323. end:
  324.         int ret = aio_read(pcb);
  325.         assert ( ret==0 && "aio_read error!!!");

  326.         return;
  327. }

  328. void Tetris::run()
  329. {
  330.         struct sigaction st;
  331.         sigemptyset(&st.sa_mask);
  332.         st.sa_flags = SA_SIGINFO;
  333.         st.sa_handler = reinterpret_cast(handler);
  334.         sigaction(SIGRTMAX, &st, NULL);

  335.         cb.aio_fildes = STDIN_FILENO;
  336.         cb.aio_offset = 0;
  337.         cb.aio_buf = inputBuf;
  338.         cb.aio_nbytes = 16;
  339.         cb.aio_sigevent.sigev_notify = SIGEV_SIGNAL;
  340.         cb.aio_sigevent.sigev_signo = SIGRTMAX;
  341.         cb.aio_sigevent.sigev_value.sival_ptr = (void *)this;

  342.         sleep(1);
  343.         
  344.         int ret = aio_read(&cb);
  345.         assert ( ret==0 && "aio_read error!!!");

  346.         CLEAR();
  347.         RESET_CURSOR();
  348.         paint();
  349.         drawScore();
  350.         drawLevel();

  351.         /*预先产生一个方块*/
  352.         int next = rand()%7;
  353.         nextBlock = styles[next][0];

  354.         
  355.         while ( true )
  356.         {
  357.                 if ( !active ) 
  358.                 {
  359.                         activeBlock = nextBlock;
  360.                         curstyle = next;
  361.                         curcolor = GET_COLOR();
  362.                         active = true;
  363.                         curpos = 0;

  364.                         next = rand()%7;
  365.                         nextBlock = styles[next][0];
  366.                         //画下一个方块
  367.                         drawNext();
  368.                         curx = 1;
  369.                         cury = WIDTH/2 - 2;
  370.                         if ( isCollide( ) )
  371.                         {
  372.                                 return; //GAME OVER
  373.                         }
  374.                         MOVETO(curx+1, 0);
  375.                         paint(curx, curx+4);
  376.                 }
  377.                 else
  378.                 {
  379.                         ++curx;
  380.                         if ( !isCollide() )
  381.                         {
  382.                                 MOVETO(curx, 0);
  383.                                 paint(curx-1, curx+4 );
  384.                         }
  385.                         else
  386.                         {
  387.                                 --curx;
  388.                                 for ( int i=curx; i
  389.                                         for( int j=cury; j
  390.                                         {
  391.                                                 if ( activeBlock[(i-curx)*4 + j-cury] )
  392.                                                         board[i][j] = TYPE_BLOCK;
  393.                                         }
  394.                                 active = false;
  395.                                 activeBlock = 0;
  396.                                 //判断是否得分
  397.                                 int n = removeLines();
  398.                                 if ( n != 0 )
  399.                                 {
  400.                                         score += bonus[n];
  401.                                         drawScore();

  402.                                         int newlevel = getLevel(score);
  403.                                         if ( newlevel > this->level)
  404.                                         {
  405.                                                 this->level = newlevel;
  406.                                                 drawLevel();
  407.                                         }
  408.                                         //RESET_CURSOR(); //暂时全部重画,以后优化
  409.                                         //paint();
  410.                                         MOVETO(2,0);
  411.                                         paint(1, curx+4);
  412.                                 }
  413.                                 else
  414.                                 {
  415.                                         MOVETO(curx+1, 0);
  416.                                         paint(curx, curx+4 );
  417.                                 }
  418.                         }
  419.                 }
  420.                 msleep(700 - level * 60);
  421.         }
  422.         return ;
  423. }
复制代码
///////////////   模板单例模式
  1. #ifndef BERT_SINGLETON_H
  2. #define BERT_SINGLETON_H

  3. /**
  4. * 模板单例模式
  5. */
  6. template
  7. class Singleton
  8. {
  9. protected:
  10.         Singleton() {}
  11.         ~Singleton() {}
  12.         
  13.         static T * instance;
  14.         
  15. public:
  16.         static T * getInstance()
  17.         {
  18.                 if ( instance == NULL )
  19.                         instance = new T();
  20.                 return instance;
  21.         }

  22.         static void delInstance()
  23.         {
  24.                 if ( instance )
  25.                 {
  26.                         delete instance;
  27.                         instance = NULL;
  28.                 }
  29.         }
  30. };

  31. template
  32. T * Singleton::instance = NULL;

  33. #endif
复制代码
/////////   自动设置终端属性
  1. #ifndef BERT_TTYAUTO_H
  2. #define BERT_TTYAUTO_H

  3. #include
  4. #include
  5. #include
  6. #include "Singleton.h"

  7. /**
  8. * 前向声明
  9. */
  10. namespace std
  11. {

  12. template
  13. class auto_ptr;

  14. }

  15. class TtyAuto : public Singleton
  16. {
  17.         friend class Singleton;
  18.         friend class std::auto_ptr;

  19.         struct termios state;
  20. protected:
  21.         /**
  22.          *  构造函数,自动设置非缓冲,非回显模式
  23.          */
  24.         TtyAuto()
  25.         {
  26.                 tcgetattr(STDIN_FILENO, &state);
  27.                 state.c_lflag &= ~ICANON;
  28.                 state.c_lflag &= ~ECHO;
  29.                 tcsetattr(STDIN_FILENO, TCSANOW, &state);
  30.         }
  31.         /**
  32.          *  析构函数,自动恢复缓冲,回显模式
  33.          */
  34.         ~TtyAuto()
  35.         {
  36.                 state.c_lflag |= ICANON;
  37.                 state.c_lflag |= ECHO;
  38.                 tcsetattr(STDIN_FILENO, TCSANOW, &state);
  39.         }
  40. };

  41. #endif
复制代码
阅读(1744) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~