Register
handmade.network»Forums»Problem with input
Roman
8 posts

Roman Shvayko.I love low-level programming and games.

Problem with input
1 month, 1 week ago Edited by Roman on April 25, 2020, 1:16 p.m.
Hello. I have been trying do input as casey did in early episodes but I got a problem.
it is for processing input messages
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
case WM_SYSKEYDOWN:
            case WM_KEYDOWN:
            case WM_KEYUP:
            {
                bool isDown = (message.lParam & (1 << 31)) == 0;
                bool wasDown = (message.lParam & (1 << 30)) != 0;
                switch(message.wParam)
                {
                    if(isDown != wasDown)
                    {
                        case 'W':
                        {
                            win32KeyboardProccessing(&keyboardController->buttonUp, isDown);
                            OutputDebugStringA("W\n");
                        }break;
                        case 'A':
                        {
                            win32KeyboardProccessing(&keyboardController->buttonLeft, isDown);
                            OutputDebugStringA("A\n");
                        }break;
                        case 'S':
                        {
                            win32KeyboardProccessing(&keyboardController->buttonDown, isDown);
                            OutputDebugStringA("S\n");
                        }break;
                        case 'D':
                        {
                            win32KeyboardProccessing(&keyboardController->buttonRight, isDown);
                            OutputDebugStringA("D\n");
                        }break;

here we check isDown != wasDown. But probem what wasDown is doesn't work. it is always 0. Then I get assert message here:
1
2
3
4
5
6
7
internal void win32KeyboardProccessing(game_button_state *newState, bool isDown)
{
    assert(newState->endedDown != isDown);
    
    newState->endedDown = isDown;
    ++newState->halfTransitionCount;    
}

Maybe I misunderstand lParam bits or something else.
Other code
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
game_input input[2] = {};
    game_input *newInput = &input[0];
    game_input *oldInput = &input[1];
while(gameIsRunning)
{
game_controller_input *oldKeyboardController = &oldInput->controllers[0];
        game_controller_input *newKeyboardController = &newInput->controllers[0];
        game_controller_input zeroController = {};
        *newKeyboardController = zeroController;

for(s32 buttonIndex = 0; buttonIndex < 5; buttonIndex++)
        {
            newKeyboardController->buttons[buttonIndex].endedDown = oldKeyboardController->buttons[buttonIndex].endedDown;
        }

win32KeyboardMessageProccessing(&winState,newKeyboardController);


game_input *temp = newInput;
        newInput = oldInput;
        oldInput = temp;
}

Thanks for any help!!!!
Simon Anciaux
794 posts
Problem with input
1 month, 1 week ago
You have an "error" in the switch structure: you can't have code outside of "case" or "default". Switch statements will jump to to one of the case and not execute code that isn't in a "case" or "default" statement.

The compiler should generate warnings if you didn't suppress them and if you used "Treat warnings as error" the code would not compile. Try adding /WX to your compiler flags and verify that there isn't a /wd4702 (disabling warning about unreachable code).

For example the following code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
b32 test = false;
u32 a = 1;

switch ( a ) {

    if ( test ) {

        case 1: {
            __debugbreak( );
        } break;

        case 2: {
            __debugbreak( );
        } break;

    } else {
        __debugbreak( );
    }
}


Gives me the following error:
1
2
3
error C2220: warning treated as error - no 'object' file generated
warning C4702: unreachable code
warning C4702: unreachable code


You should move the if outside of your switch that test which key was pressed:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
b32 test = false;
u32 a = 1;

if ( test ) {

    switch ( a ) {
        case 1: {
            __debugbreak( );
        } break;

        case 2: {
            __debugbreak( );
        } break;
    }
} else {
    __debugbreak( );
}
Roman
8 posts

Roman Shvayko.I love low-level programming and games.

Problem with input
1 month, 1 week ago
It helped! Thanks for the help. I will add some flags.