Thread: Buffer problem

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    20

    Buffer problem

    alrighty im making this game kinda like snake. My problem is that i want to see if there is something in the keyboard buffer or not and not wait for the user to really push a key

    f.e
    Code:
    int running = 1;
    while(running)
    {
     char c=0;
     int move;
    
     somefucntionidontknow(c);
     switch(c)
     {
       case 't':
        goto(0,-1)
       case 'g':
        goto(0,1)
       case 'f':
        goto(-1,0)
       case 'h':
        goto(0,-1)
       default
        goto(2,2)
      }
     sleep(200);
    }
    goto would remember in what direction the head is and when it gets 2,2 go on in that direction. Problem is , if i use scanf() the computer waits till input if there isnt anything in the keyboard buffer. getch() does the same . I just want to check if a there is something in the buffer , if not go on without waiting. I thought about using threads which seems kinda hard in mingw (im doing this in C only no C++). Any suggestion or is threads my only option???



    thank you for you attention if you made it this far

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    There is no way to help you without more information. We live in a computer world filled with different platforms and APIs.

    In Windows you check for messages to see if keys are down. In DirectInput you can either poll the device or you can use buffered input. In DOS you would normally write your own keyboard interrupt handler which still works under XP but I do not recommend any type of DOS programming anymore.

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    20
    thx for your replies

    im using mingw (devc++)
    hmmz i'll take a close look at the faq thingie but i know getch() this is not what am looking for :s I want the fctn to move on even if there hasnt been a key pushed


    btw ill post a version of the game tommorow eve cause i got to go to work now
    Last edited by Narcose; 03-24-2006 at 09:33 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with float and argv
    By DonFord81 in forum C Programming
    Replies: 5
    Last Post: 03-04-2009, 02:47 PM
  2. Cout redirection problem
    By MrLucky in forum C++ Programming
    Replies: 6
    Last Post: 06-06-2007, 11:11 AM
  3. clear buffer
    By justins in forum C Programming
    Replies: 5
    Last Post: 05-19-2007, 06:16 AM
  4. buffer contents swapping
    By daluu in forum C++ Programming
    Replies: 7
    Last Post: 10-14-2004, 02:34 PM
  5. memory allocation problem....help..
    By CyC|OpS in forum C Programming
    Replies: 8
    Last Post: 10-18-2002, 09:26 AM