C基础 一个可以改变Linux的函数getch(2)

/* * error => 以后再说 * 跨平台的丑陋从这里开始 * __GNUC => linux 平台特殊操作 * __MSC_VER => window 平台特殊操作 */ #ifdef __GUNC__ // 下面是依赖GCC编译器实现 #include <termio.h> /* * 得到用户输入的一个字符 * : 返回得到字符 */ int sh_getch(void); #elif _MSC_VER // 下面是依赖Visual Studio编译器实现 #include <conio.h> // window 上用_getch 替代了getch, 这里为了让其回来 #define sh_getch _getch #else #error "error : Currently only supports the Visual Studio and GCC!" #endif

再看实现部分 (*.c 文件中插入)

// 为linux扩展一些功能 #if defined(__GUNC__) /* * 得到用户输入的一个字符 * : 返回得到字符 */ int sh_getch(void) { int cr; struct termios nts, ots; if (tcgetattr(0, &ots) < 0) // 得到当前终端(0表示标准输入)的设置 return EOF; nts = ots; cfmakeraw(&nts); // 设置终端为Raw原始模式,该模式下所有的输入数据以字节为单位被处理 if (tcsetattr(0, TCSANOW, &nts) < 0) // 设置上更改之后的设置 return EOF; cr = getchar(); if (tcsetattr(0, TCSANOW, &ots) < 0) // 设置还原成老的模式 return EOF; return cr; } #endif

这就是getch跨平台实现的关键了. 从这里开始,你就可以构建自己喜欢的游戏了, 通过 sh_getch 入口开始.

预备下次重构C字符串,再下次采用simplec框架重写一个老的灭龙传说V2.0.0游戏, 让其支持跨平台, 并支持配置扩展.

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/92283f5e4d43361082aebc7342a487ef.html