Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Linux Shell Game Input

Name: Squeezy !31sNfzlFrg 2010-10-08 15:40

I'm writing a simple Linux Game in C++ for school, I've been using getch for my input controls for Up Down Left Right. The problem is that getch blocks the program from continuing. Online I gathered that kbhit may be a better option, except there are over 10 different kbhit.h's and they dont fucking work. if(getch) in a while(1) loop would be totally fucking fine if i could have like a 1ms timeout for getch so if there is no key pressed the screen keeps going. What do?

Name: Anonymous 2010-10-08 15:50

H...IBT? If you use ncurses or whatever you can switch to non-blocking I/O. But if you want smooth keypressed/released detection, you're better off with SDL (Hold a key down in the shell and note the delay between the first printed character and the rest, this is guaranteed to happen in textual input, unless you change it globally in some config.)

Name: Anonymous 2010-10-08 15:50

Have you tried getting out yet

Name: Anonymous 2010-10-08 16:17

its an alternative to taking the rest of the class you little shit, the rest of the program is simple im just hung on this, ill dick around with curses i guess.

Name: Anonymous 2010-10-08 16:18

>>4
HEY FUCK YOU is all.

Name: Anonymous 2010-10-08 16:33

lrn 2 select()

Name: Anonymous 2010-10-08 16:37

this is you, faggot ->

Name: Anonymous 2010-10-08 16:42

troll do not know about my SDL?

Name: Anonymous 2010-10-08 17:35

Why don't you read your fucking curses man page, OP?

Name: Anonymous 2010-10-08 17:51

Read SICP.

Name: Anonymous 2010-10-08 18:47

spin getch() off into its own process and it won't block the original process. you can synchronize on a mutex or a channel or whatever you want. since the two processes would only be sharing a char, you probably don't need to synchronize at all

Name: Anonymous 2010-10-08 19:00

>>11
Better yet, have the new process simply report its input via a pipe.

Name: Anonymous 2010-10-08 19:12

>>12
LOL 0/10

Name: Anonymous 2010-10-08 20:33

>>1
The standard game loop is dead simple.


while (1) {
    read_input();
    update_state();
    redraw_screen();
}


On Linux, you have many options for making a read_input function that doesn't block waiting for input.

1. Use select or poll.  These functions tell you which file descriptors are ready for reading/writing so you can use them without blocking.  (See man 2 select)
2. Use non-blocking IO.  Put fd #0 in non-blocking mode, and it will give error EAGAIN / EWOULDBLOCK if there is nothing to read.
3. Use ncurses, which can do all of this for you automatically.
4. Use a GUI.  SDL is a library of mediocre quality in general, but it does do basic stuff like this pretty easily.

NOTE: Don't make the newbie mistake of reading only one event in your read_input function.  Read all pending events, and only redraw the screen once there are no more events in the queue.

Name: Anonymous 2010-10-08 23:38

>>14
YOU HELPED HIM!!!

Name: Anonymous 2010-10-09 0:07

>>14
SDL
U MENA ALLEGRO

Name: Anonymous 2010-10-09 4:53

>>16
He should mena SFML at this point.  It's 2010 for Sussman's sake!

Name: Anonymous 2010-10-09 11:35

>>17
C++, pig disgusting.

Name: Anonymous 2010-10-09 17:13

>>18
There are C bindings, silly :)

Name: Anonymous 2010-10-09 18:59

why are all you retards suggesting linking against some retarded library just to grab keyboard input? doing it yourself is like ten lines, tops.

Name: Anonymous 2010-10-09 20:09

>>17
SFML is written in C++ and provides an efficient, object-oriented design. It relies on standard patterns and idioms to provide a simple and robust framework.

Name: Anonymous 2010-10-09 21:30

>>21
everyone knows real ENTERPRISE software is written in Coffee

Name: Anonymous 2010-10-09 21:49

>>22
I think it would be better if you wrote Coffee.

Name: Anonymous 2010-10-09 22:01

>>23
Thanks for the tip, will do so in the future.

Name: Anonymous 2010-10-09 23:24

>>20
Yeah, you shouldn't link against libraries, ever.  Like, all you need is the syscall opcode which is like one line of assembler.  You especially shouldn't link against something like ncurses, which is well-tested, works practically everywhere, is installed practically everywhere, and allows the programmer to use ECMA-48 control codes on a wide variety of terminals portably without having to read the 108-page ECMA-48 specification.

Only trolls will tell you not to use libraries.  The ncurses library can put your terminal in raw mode so you don't have to look up a bunch of escape sequences and parse / emit them by hand.

Name: Anonymous 2010-10-09 23:25

>>25
DFTT

Name: Anonymous 2010-10-10 0:31

>>26
Discrete Fourier Transform Training?

Name: Anonymous 2010-10-10 2:25

>>25
so you don't have to look up a bunch of escape sequences and parse / emit them by hand.
Bah, who needs to look up escape sequences? ncurses isn't as universally portable as you might think. I found out the hard way that forwards compatibility between point releases is not something you should rely on. Still, use it.

Name: Anonymous 2010-10-10 5:12

>>25
do you think reading from stdin isn't well-tested, doesn't work practically everywhere, isn't installed practically everywhere, and has any need at all for ecma-48 control codes? if you do, then you're retarded

protip: all you need is the read syscall, which is like one line in language

Name: Anonymous 2010-10-10 5:20

>>29
I'm not quite sure you know what is trying to be achieved here.

Name: Anonymous 2010-10-10 6:20

>>29
protip: all you need is the read syscall, which is like one line in language
And a copy of the terminfo for your terminal. Oh wait.

Name: Anonymous 2010-10-10 7:47

>>31
>his terminal emulator doesn't pass keyboard input on stdin
laughingputin.jpg

Name: Anonymous 2010-10-10 14:57

>>32
There is a lot you don't know about terminal emulation.

Name: Anonymous 2010-11-02 10:09

Name: Anonymous 2010-11-02 12:09

Name: Anonymous 2011-02-03 5:47

Name: Anonymous 2013-09-01 14:37



             i\        \ この
  r‐─┐[][]   ノ::::::ヽ.       /
  ,二l |    /_::::::::::::::\      ̄i  ロリコン
  └─┘   ///´'ヽ、;;::::;__;ゝ;ニヽ.   |
       /-''| l、   `、  //`ヽl |  ∠.__  どもめ!
      /    \ヽ、.,__,'r-'、___,//ヽ.   |_
    /  _,ゝr-‐;>-‐'⌒ヽヘ‐‐'_/_!   _/ __/ ̄\/
    ,'__,r'"7-‐ァ' i  i    〉-、/ ヾ. //
    i_,,.!-'"//-‐ハ-/|  i ./i   `ヾ.! '´
    | イ/./ /!ァ';.ニ;ヽ| ,ハ/‐|、 i   ', l二二`i [][]
    | |/ ./イ ! l,l i レ' 'ァ;ニ;ヽハ. i i  ______| |
 ,. ‐‐'--'、ノi. ` ゝ‐'      il | ハ/!ヘノi l________|
 i-─---〈 ハ "      '  `'',,i. Y::::::|
 〉─---‐〉| .i、.    ,.-‐、   ,ハ ヽ、:;|_   ,、
 `r─---ノ  !へ、, └‐‐' ,.ィァ' ̄`ヽ.⌒ヽ∠」
  `i ̄iフ7|  ト、  `>-ァ'" !/       ',く.ノ|ヽ.
  ノ.ノ| /:::!  |、`ヽ、.,____i/ ,イ       i.|/ ノ
 (r:'  Y::::::', |へ/    / !、 ヽ.,   /'ヽ.(   l二二二 `i [][]
  )  ヽ;:::::ヽ!::::::`"'r--'i ァ'::>、.,_        \       | |
    //}>i」く{:::::|_|___rへ、:;__ヽ.,_,,..-、    ':,   ________| |
   //   ノl::::::/´  /      ̄         i l___________l
   `(   / |`'┴--,i__             ,ノ
        i /    |   ̄    ___,,.. -ァ ''i"
       `(     ヽ.       /´ヽ:::|
              `ヽ、.,____/    )'

Name: Anonymous 2013-09-01 16:09



                _,,.. -─ '' "´ ̄ ̄二;ー 、.,l`7ーァ    /
 _______________  〈\__,.'´          ⌒ヽヽ. i/∠._  / ネ :
´        `' く,__,!へ.,__ゝ__,.--、_  __i、.,_ノ ,ハ  ト、_」 ヽ.   ズ :
            ハ、.,___/ ,ゝ、.,__,!へへ.,__,,.イ_7ニ__ン」、_  i   ミ 覚
 い は す 前 //´   /              Y_ノ  |    え
 う  じ べ .回 |'  /  ,'  / /  i  /'|   i  ',  |.   |    て
 :  め て  の |  i  i  i 」___i ハ | |_|_」ニ、.i  |   |    お
 :  て. ク  課 |  |  .|  | _」=-‐r !_」  'i r| !」__」  ノ    き
 !   〃 リ  題 |  |  .|__レi'「 .!、_,り    `'´,,7 |   /     .な
   改. ア  を |', └‐ |  .| ,,,       '   ! |   ヽ.     さ
   良 .し    |ヘ   .|  ,ハ.    /´ ̄i   ハ |    |.    い
  〃  て    ,.>、''"´!イ   i`>.、._!___,ン,.イ |   |   //ヽ、.,_______
   .と      /    Y>.、|   ハ `ヽ、__,! |. | !  ト、._ ´
         /   /ヽ、ヘ!    '; !_,,. --‐'─,ァ  !/」
_____________,,..イ    ,ィ´   ハ !_,.r''7  、 ヾ!T´ヽ、イ<]
            |    _,.‐'" / / 、 ヽ,ン' !ヘ / ハ!  ヽ.

Don't change these.
Name: Email:
Entire Thread Thread List