Question

What is a good way of doing SDL event handling? You usually have the:

while (SDL_PollEvent(&event)) {
    //Handles all the events when in the menu screen...
    eventsMenu(event);
}

Problem is, when you get going with a game, there's usually a mess of a list of controls that you can do, like up and down detetion for many different keys. And I'm wondering weither the method I'm using is efficient and clean. Or if I should approach it diffrently...

I have a function pointer in the mainLoop, that can be assigned to quickly change how the events will be handled. (Say I swap to another function, and it will use those events) However, I think the list of events get messy anyways, so I tried adding regions to break it up. Is that a good idea? And yeah, just want some input if I'm on the right path to a readable code.

void Window::eventsMenu(SDL_Event event) {
    switch (event.type) {
        #pragma region "Button Down"
        case SDL_MOUSEBUTTONDOWN: {
            //printf("Mouse button down!\n");
            glClearColor(0.1, 0.1, 0.1, 1);
            if (event.button.button == SDL_BUTTON_LEFT) {
                mouseButtonLeft = true;
            }
            break;
        }
        #pragma endregion;
        #pragma region "Button Up"
        case SDL_MOUSEBUTTONUP: {
            //printf("Mouse button up!\n");
            glClearColor(0, 0, 0, 1);
            if (event.button.button == SDL_BUTTON_LEFT) {
                mouseButtonLeft = false;
            }
            break;
        }
        #pragma endregion;
        #pragma region "Mouse Motion"
        case SDL_MOUSEMOTION: {
            //printf("Mouse moved!\n");
            if (mouseButtonLeft) {
                rotX += event.motion.xrel;
                rotY += event.motion.yrel;
            }
            break;
        }
        #pragma endregion;
        #pragma region "Mouse Wheel"
        case SDL_MOUSEWHEEL: {
            if (event.wheel.y != 0) {
                musicVolume += ((event.wheel.y > 0) ? 1 : -1);
                if (musicVolume > 100) {
                    musicVolume = 100;
                }
                else if (musicVolume < 0) {
                    musicVolume = 0;
                }
                Mix_VolumeMusic(musicVolume);
                printf("Volume: %i%c\n", musicVolume, '%');
            }

            if (event.wheel.y > 0) {
                //printf("Scroll forward!\n");
            }
            else {
                //printf("Scroll backward!\n");
            }
            break;
        }
        #pragma endregion;
        #pragma region "Key Down"
        case SDL_KEYDOWN: {
            printf("Button [%s] pressed\n", SDL_GetKeyName(event.key.keysym.sym));
            switch (event.key.keysym.sym) {
                case SDLK_1: {
                    Mix_PlayChannel(-1, sound1, 0);
                    break;
                }
                case SDLK_2: {
                    Mix_PlayChannel(-1, sound2, 0);
                    break;
                }
            }
            break;
        }
        #pragma endregion;
        case SDL_QUIT: {
            running = false;
        }
    }
}
Was it helpful?

Solution

Two suggestions :

Remove the braces ( { and } ) around the case labels. You don't need them unless you need a new stack.


My second suggestion is to split things into function. Even if it will only be called from within switch. Putting things into several functions helps makes the code easier to read and understand.

So for instance :

case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP:
    HandeMouseButton( event );
    break;

void HandeMouseButton( const SDL_Event &event )
{
    if ( event.type ==  MOUSEBUTTONDOWN )
    {
        glClearColor(0.1, 0.1, 0.1, 1);
        if (event.button.button == SDL_BUTTON_LEFT) {
            mouseButtonLeft = true;
    }
    else if ( event.type ==  MOUSEBUTTONUP )
         glClearColor(0, 0, 0, 1);
         if (event.button.button == SDL_BUTTON_LEFT) {
             mouseButtonLeft = false;
         }
    }
}

And generally ( slightly opinion based ) ; if you need to use #pragma once to make the code readable, it can ( and should ) be split into more function

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top