Game loop in Windows console application

This is a discussion on Game loop in Windows console application within the Windows Programming forums, part of the Platform Specific Boards category; I'm writing a simple Tetris game to try out a small console manipulation class I made. However, it seems to ...

  1. #1
    The larch
    Join Date
    May 2006
    Posts
    3,573

    Game loop in Windows console application

    I'm writing a simple Tetris game to try out a small console manipulation class I made.

    However, it seems to me that I cannot have a proper message loop in a console application. So what would be a decent approach?

    I'm thinking of a busy loop with a small Sleep (not to burn CPU), measuring time with clock() (or Windows equivalent) and handling time-based updates when a certain time has elapsed, and reading keyboard input with GetAsyncKeyState (or may-be ReadConsoleInput). I'm not sure yet, whether I'll have problems with blocks moving too fast when a key is pressed...

    Does this approach sound OK?
    Last edited by anon; 11-28-2007 at 07:52 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Sounds about right to me. Another possibility is to have a separate thread for reading the keyboard, and making your own message system - but that's probably overcomplicated for the problem you are trying to solve.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    You don't need a message loop for a console app. What are you going to respond to?

  4. #4
    Registered User Ktulu's Avatar
    Join Date
    Oct 2006
    Posts
    62
    Indeed, sounds like for ( ; ; ) will do the trick...
    This parameter is reserved

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Well, the blocks move downward at a fixed rate (timer-based) and at the same time I want to get non-blocking keyboard input. I've discovered that GetAsyncKeyState is not good for that because it doesn't care about repeat rates (?) and blocks move uncontrollably.

    Basically the loop is going to be:
    Code:
    while (!game_over()) {
         handle_user_input();
        if (time_has_passed) {
            handle_automatic_movement();
        }
        if (needed) {
            update_display_as_needed();
        }
         Sleep(1);
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    What exactly does happen inside the while? Your handle_user_input does not return if the user keeps the key pressed down?

  7. #7
    Registered User
    Join Date
    Nov 2007
    Posts
    18
    I'm not sure if this would work for your compiler (it does for Dev-C++) but why not use
    Code:
    if (kbhit())
    {
         input = getch();   //or whatever variable for 'input'
         if statements for keys pressed
    }
    standard movement
    I'm not sure how compiler dependant this is or whether it would work well enough for you.

    The only thing though if you do go with this is that you'll need to create an output line of the input because getch() returns an integer value of the key pressed (ie: 1 is returned as 49, Esc button is 28, etc) so that you can figure out the numbers that you need 'input' to equal to get different responses.

    Oh, and 'input' is declared int.
    Last edited by teck; 11-29-2007 at 03:51 PM. Reason: Code would probably work better without standard movement in else statement

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Application Data folder on Windows
    By idelovski in forum Tech Board
    Replies: 24
    Last Post: 11-29-2008, 11:57 AM
  2. Told ya so...
    By Cheeze-It in forum A Brief History of Cprogramming.com
    Replies: 58
    Last Post: 09-12-2007, 09:12 PM
  3. Few problems with console application
    By GaPe in forum C Programming
    Replies: 12
    Last Post: 04-03-2002, 01:19 PM
  4. Game Console in OpenGL
    By HellRaiZer in forum Game Programming
    Replies: 2
    Last Post: 02-27-2002, 12:52 AM
  5. Just one Question?
    By Irish-Slasher in forum C++ Programming
    Replies: 6
    Last Post: 02-12-2002, 10:19 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21