SDL help

I am trying to get my keyboard input into a function, so that I can call a function like updatebuttons() and set global variables to 0 if the button is not down at the moment and 1 if it is. It seems simple, but I can't seem to get it to work right; I am not use to working outside of windows, so I am still a beginner with SDL.
Can you show us some code, in particular, the function in which you are trying to isolate the keyboard input?
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
void updateinput()
{


        while(SDL_PollEvent(&event))
        {
            switch(event.type)
            {
            case SDL_QUIT:
            running = false;
            break;
            case SDL_KEYDOWN:
            switch(event.key.keysym.sym)
               {
               case SDLK_RIGHT:
                pad[0] = 1;
                break;

                case SDLK_UP:
                pad[1] = 1;
                break;

                case SDLK_LEFT:
                pad[2] = 1;
                break;

                case SDLK_DOWN:
                pad[3] = 1;
                break;
               }

              case SDL_KEYUP:
            switch(event.key.keysym.sym)
               {
               case SDLK_RIGHT:
                //pad[0] = 0;
                break;

                case SDLK_UP:
                pad[1] = 0;
                break;

                case SDLK_LEFT:
                pad[2] = 0;
                break;

                case SDLK_DOWN:
                pad[3] = 0;
                break;
               }


            }


       }


all verbals not declared are global.
Right, use SDL_PollEvent only in your main loop and handle all non-related to the input events in there.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 
void updateinput()
{
    switch (event.type):
        case SDL_KEYDOWN:
    ...
}


int main(int argc, char *argv[]) 
{
    // Resources initialization 

    while (!done)
    { 
        while (SDL_PollEvent(&event))
        {
            updateinput(); 
            
            if (event.type == SDL_QUIT) 
                done = true; 
         }
     }
     .....

That's just an example, your loop may look different.
So is there no good way to take the event loop out of main?
Topic archived. No new replies allowed.