Thread: How do I make it so the program carries on if there is no user input?

  1. #1
    Registered User Swarvy's Avatar
    Join Date
    Apr 2008
    Location
    United Kingdom
    Posts
    195

    How do I make it so the program carries on if there is no user input?

    I am having one or two slight problems with my tetris game, but as they say, the thrill of the kill is in the chase. Anyway, the problem is that the way I have programmed it so far is that in order for the shape to drop down one place, the user has to move it. In other words, the shape will not drop down by one space after a specified time, but the user must move the piece to the side in order for it then to drop down.

    The problem is that the program waits for the user input before it continues with the game loop. How exactly can I make it so that if there is no user input when it reaches that part of the code just to move on, and carry on as normal? I have a feeling that it may involve multithreading, which doesn't fill me with joy.

    Code:
    while(RUNNING)
    {
    // Take user movement input
    controls();                        <------------------------------ It stays here until there is user input, but I want it so it carries on if there isn't any user input
    // Translate shape down by one
    translate();
    }

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Asynchronous input or multi-threading would be the key I think.
    Async API would probably be the best to avoid complexity. I don't know of any such, though (except for DirectInput).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    This has nothing to do with multi-threading. You need a keyboard function that 'polls' the keyboard instead of waiting for it. In most systems the keyboard is polled every frame and then if any keys were pressed they are dealt with during Update().

    DirectInput does this when you retrieve the keyboard state. It fills up an array that represents each key. Then you use pre-defined constants as indices into the array to see if the key was up or down. If the array element is 1 the key is down, if it is 0 the key is up.

    Window programs accomplish this by processing WM_KEYDOWN and other keyboard related messages (like WM_CHAR, WM_KEYUP, etc) in their message loops. Windows will send these messages to the window that has the focus and then it is the window's job to respond to the messages.

    I can think of very few times that you would want to actually halt everything and just 'wait' for a keypress. Most of the time keyboard functions are polling functions. Even buffered functions will somehow notify you there is something in the buffer. Waiting on the keyboard is a huge waste of cycles.

  4. #4
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Mind telling us what library are you using?

    AFAIK there is no way to achieve this using only standard C++.

  5. #5
    Registered User guesst's Avatar
    Join Date
    Feb 2008
    Location
    Lehi, UT
    Posts
    179
    "Standard" C does not have a function to poll the keyboard. However, there are several libraries that you can install and use that will do the trick.

    If you are using curses getch() can be configured to, as Bubba said, Poll the keyboard. Curses is perfect for Tetris. But if you want something a little more graphical SDL will do it for you. I believe OpenGL does too, but my experience above text based graphics is (at the moment) limited.
    Type-ins are back! Visit Cymon's Games at http://www.cymonsgames.com for a new game every week!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. User determined input question
    By lyoncourt in forum C Programming
    Replies: 8
    Last Post: 09-30-2007, 06:10 PM
  3. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  4. Structure and Linked List User Input Question
    By kevndale79 in forum C Programming
    Replies: 16
    Last Post: 10-05-2006, 11:09 AM
  5. Replies: 4
    Last Post: 04-21-2004, 04:18 PM