C base a function that can change Linux Getch

Source: Internet
Author: User
Tags rewind

Introduction-Getch Brief Introduction

Refer to the old TC version getch description. (the article introduces a bit narrow, the application point is some dinosaur game era development details)

#include <conio.h>

/**/int __cdecl getch (void);

Remember reading a book three years ago. <<c Expert Programming >> there is a chapter on getting the input characters from the standard input immediately (a Linux implementation is also described later, which is not available for the Linux version now).

The author evaluates that Linux is missing many game developers because of the unfriendly support of Linux for ' Getch '.

Now, of course, there's no such function on window. Change it to the next one.

# include <conio.h>    _check_return_     int __cdecl _getch (void);

"Instant interaction" is the entrance to game development. It is necessary.

Preface-Learn getch from practical examples

Now test the code in Visual Studio Update3 for a piece of getch to get the results immediately MAIN.C

#include <stdio.h>#include<stdlib.h>#include<conio.h>/** Production wait, function*/intMainintargcChar*argv[]) {printf ("Please enter any character to end the program ..."); intRT =_getch (); printf ("%d =%c\n", RT, RT); RT=_getch (); printf ("%d =%c\n", RT, RT); System ("Pause"); return 0;}

Run results

As can be seen from the above, the _getch name has changed, but the function and getch have not changed.

Here we encapsulate. Look at the new file, a demo small demo

#include <stdio.h>#include<conio.h>/** Define the Unified interface Sh_getch understand get player input *: return input int value, error EOF*/#defineSh_getch _getch/** Wait function*/Static void_pause (void) {printf ("Please press any key to continue ...");
Rewind (stdin); Sh_getch ();}/** Continue to wait for function*/intMainintargcChar*argv[]) {_pause (); return 0;}

Instead of the system ("pause") on the original window, the Linux pause (). Rewind resets the file * stream, clearing the input stream to ensure that the current stream is clean.

Text-Implement a getch on Linux, receive immediately

Linux requires the use of the Termio.h terminal Control header file. The main implementation is as follows

#include <termio.h>/** Get a character entered by the user *: Returns the resulting character*/intSh_getch (void) {    intCR; structTermios NTS, OTS; if(Tcgetattr (0, &ots) <0)//get the settings for the current terminal (0 for standard input)        returnEOF; NTS=ots; Cfmakeraw (&nts);//sets the terminal to raw raw mode, in which all input data is processed in bytes    if(Tcsetattr (0, Tcsanow, &nts) <0)//Settings after the change        returnEOF; CR=GetChar (); if(Tcsetattr (0, Tcsanow, &ots) <0)//set Restore to old mode        returnEOF; returnCR;}

The main is to set the terminal for the original receive character mode, can receive immediate return, and then restore the old environment settings. Terminal buffering is also due to efficiency considerations, otherwise the programming is too complex.

Also test a getch.c

#include <stdio.h>#include<termio.h>/** Get a character entered by the user *: Returns the resulting character*/intSh_getch (void);/** Test standard fast input*/intMainintargcChar*argv[]) {    intch; printf ("Please press any key to continue ..."); CH=Sh_getch (); printf ("%d =%c\n", CH, ch); CH=Sh_getch (); printf ("%d =%c\n", CH, ch); return 0;}

Demo results on Linux

Gcc-wall-ggdb3-o Getch.  out GETCH.C

Everything is fine.

Here we close the Getch cross-platform implementation details are determined. Then we implement a cross-platform version of Getch. First, the File Declaration section (*.h file insertion).

/** ERROR = say it later * cross-platform ugliness starts here * __gnuc = Linux Platform Special Operations * __msc_ver = Windows platform Special Operations*/#ifdef __gunc__//The following is a dependency on the GCC compiler implementation#include<termio.h>/** Get a character entered by the user *: Returns the resulting character*/intSh_getch (void);#elif_msc_ver//The following is a dependency on the Visual Studio compiler implementation#include<conio.h>//window with _getch instead of getch, here in order to get it back#defineSh_getch _getch#else    #error"Error:currently only supports the Visual Studio and gcc!"#endif

Look again at the implementation section (inserted in the *.c file)

//extend some functionality for Linux#ifDefined (__gunc__)/** Get a character entered by the user *: Returns the resulting character*/intSh_getch (void) {    intCR; structTermios NTS, OTS; if(Tcgetattr (0, &ots) <0)//get the settings for the current terminal (0 for standard input)        returnEOF; NTS=ots; Cfmakeraw (&nts);//sets the terminal to raw raw mode, in which all input data is processed in bytes    if(Tcsetattr (0, Tcsanow, &nts) <0)//Settings after the change        returnEOF; CR=GetChar (); if(Tcsetattr (0, Tcsanow, &ots) <0)//set Restore to old mode        returnEOF; returnCR;}#endif

This is the key to the Getch cross-platform implementation. From here, you can build your favorite games and start with the Sh_getch portal.

Prepare for the next refactoring of the C string, and next use the SIMPLEC framework to rewrite an old dragon legend V2.0.0 game, allowing it to support cross-platform and support configuration extensions.

PostScript-the future has ∞ times

Get over http://music.163.com/#/song?id=22771653

C base a function that can change Linux Getch

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.