Thread: Input Problems

  1. #1
    napKINfolk.com napkin111's Avatar
    Join Date
    Apr 2002
    Posts
    310

    Input Problems

    I'm trying to accept input for movement using the Windows event handler. I use keys EDSF for up/down/left/right. When you hold a key down, it moves fine for the first few frames, but then it gets a bit jumpy and scrolls slower. I'm not sure what is doing this but I'm thinking its something with the key buffer getting full or something...?

    The attachment is a program demonstrating the problem I'm having. E/D/S/F=up/down/left/right and esc to exit. Notice if you tap the key repeatedly it moves fine, but when you hold it down it blogs down.

    Thanks!

    //napKIN
    "The best way to get answers is to just keep working the problem, recognizing when you are stalled, and directing the search pattern.....Don’t just wait for The Right Thing to strike you – try everything you think might even be in the right direction, so you can collect clues about the nature of the problem."
    -John Carmack

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    162
    No offense, but now how are we going to be able to tell what the problem is without any source code. Looking at it though I would say it runs fine holding it down and repetive pressing. I would make the movement a little more smooth, but post up the source and I'll take a look at it.
    Last edited by Crossbow; 08-01-2003 at 07:36 PM.

  3. #3
    napKINfolk.com napkin111's Avatar
    Join Date
    Apr 2002
    Posts
    310
    Code:
    LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    static HGLRC hRC;	
    static HDC hDC;			
    switch(message)
    {
    case WM_CHAR:
    {
    switch (toupper(wParam))
    {
    case 'E':
    {
    player1.y+=player1.speed;
    return 0;
    }
    case 'F':
    {
    player1.x+=player1.speed;
    return 0;
    }
    case 'D':
    {
    player1.y-=player1.speed;
    return 0;
    }
    case 'S':
    {
    player1.x-=player1.speed;
    return 0;
    }
    default:
    break;
    }
    }
    break;
    
    default:
    break;
    }
    return (DefWindowProc(hwnd, message, wParam, lParam));
    }
    There is the movement message handline. I'm not sure what else would be relevant to include. I scrolled NE across the map without input, and it scrolled fine, so I know that it is something to do with the input :\

    //napKIN

    edit: The code didn't format correctly...I deleted all the whitespace in it, hopefully it will work now. Oh yeah and the camera is focused on (player1.x, player1.y), both coords are floats.
    Last edited by napkin111; 08-02-2003 at 04:59 PM.

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Well windows messages are pretty slow as far as performance concerns go. I would look into something like GetAsyncKeyState to handle your input. Make a function that checks input every frame and updates accordingly and do NOT handle it in your msg handler. Here is the info on the function.

    http://msdn.microsoft.com/library/de...nckeystate.asp
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  5. #5
    napKINfolk.com napkin111's Avatar
    Join Date
    Apr 2002
    Posts
    310

    Thumbs up

    Thanks *alot*! It works perfectly, just what I needed. (Good thing you responded cause I was starting to get extremely annoyed)

    //napKIN

  6. #6
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Just a couple things, instead of "switch(toupper(wParam))" it should be either switch((int)wParam) or just switch(wParam). It will always be a capital anyways. **oops, only for WM_KEYDOWN and WM_KEYUP, didn't notice you were using WM_CHAR

    Also, if you want to use windows messages, that's fine too. This will cut out the jerky movement:
    Code:
    (global variable or something)
    bool e, f, d, s;
    
    (windows procedure)
    switch(msg)
    {
    case WM_KEYDOWN:
         switch((int)wParam)
         {
         case 'E':
              e = true;
              break;
         case 'F':
              f = true;
              break;
         (etc.)
         }
         return true;
    
    case WM_KEYUP:
         switch((int)wParam)
         {
         case 'E':
              e = false;
              break;
         (etc.)
         }
         return true;
    }
    Then you can check the state of the variables each frame, in the same way that you would use GetAsyncKeyState(). On the other hand, if you go with GetAsyncKeyState, you probably want to make macro or inline function to wrap around it like KEY_DOWN() or something, since GetAsync... is pretty long to type.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need some help with C program writing
    By The_PC_Gamer in forum C Programming
    Replies: 9
    Last Post: 02-12-2008, 09:12 PM
  2. Problems with input and fflush
    By edugarcia in forum Linux Programming
    Replies: 1
    Last Post: 11-24-2004, 01:52 PM
  3. Custom Made Safe Input Function
    By Beast() in forum C Programming
    Replies: 6
    Last Post: 08-21-2004, 10:19 PM
  4. Input Problems and C# (CSharp) Tutorials
    By Grayson_Peddie in forum C# Programming
    Replies: 4
    Last Post: 02-27-2003, 10:45 PM
  5. Problems with commas as input
    By Yojimbo III in forum C++ Programming
    Replies: 3
    Last Post: 02-05-2003, 09:18 PM