SDL_event列表变得冗长而凌乱?

时间:2014-04-17 16:41:39

标签: c++ events sdl

进行SDL事件处理的好方法是什么? 你通常有:

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

问题是,当你开始玩游戏时,你可以做一些乱七八糟的控制,比如许多不同的键的上下检测。而且我想知道我使用的方法是否有效且干净。或者,如果我不顾一切地接近它......

我在mainLoop中有一个函数指针,可以指定它来快速更改事件的处理方式。 (假设我交换到另一个函数,它将使用这些事件) 但是,我认为事件列表无论如何都会变得混乱,所以我尝试添加区域来分解它。这是一个好主意吗?是的,如果我在正确的可读代码路径上,只需要一些输入。

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;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

两个建议:

删除{标签周围的大括号(}case)。除非你需要一个新的堆栈,否则你不需要它们。


我的第二个建议是将事情分解为功能。即使只从switch内调用它。将内容放入多个函数有助于使代码更易于阅读和理解。

例如:

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;
         }
    }
}

一般而言(稍微基于意见);如果你需要使用#pragma once来使代码可读,它可以(并且应该)分成更多的函数

相关问题