PollEvent无法在没有鼠标移动的情况下工作[SDL]

时间:2015-02-08 14:09:16

标签: c++ sdl mouse

如果鼠标在窗口内移动或者在应该运行动画时没有按任何键,则绕过PollEvent。这是代码:

    SDL_Init(SDL_INIT_EVERYTHING);
    int imgFlags = IMG_INIT_PNG | IMG_INIT_JPG;
    if (IMG_Init(imgFlags) != imgFlags)
    {
        std::cout << IMG_GetError() << std::endl;
    }
    window = SDL_CreateWindow("NRG", 200, 200, 800, 600, SDL_WINDOW_SHOWN);
    renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED| SDL_RENDERER_PRESENTVSYNC);


    isRunning = true;
    while (isRunning)
    {
        while (SDL_PollEvent(&ev))
        {
            heroImg = LoadTxt("image.png", renderer);
            SDL_QueryTexture(heroImg, NULL, NULL, &textureWidth, &textureHeight);
            frameWidth = textureWidth / 3;
            frameHeight = textureHeight / 4;
            heroRct.x = 0;
            heroRct.y = 0;
            heroRct.h = frameHeight;
            heroRct.w = frameWidth;
            frameTime++;
            if (60 / frameTime == 4)
            {
                frameTime = 0;
                heroRct.x += frameWidth;
                if (heroRct.x >= textureWidth)
                    heroRct.x = 0;
            }
            SDL_RenderClear(renderer);
            SDL_RenderCopy(renderer, heroImg, &heroRct, NULL);
            SDL_RenderPresent(renderer);
        }
    }
    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);
    window = NULL;
    windowSurface = NULL;
    renderer = NULL;
    SDL_Quit();

其他东西,如SDL_Event ev;包含在.h文件中

1 个答案:

答案 0 :(得分:0)

对于遇到同样问题的人: 将你的渲染等放在 while(SDL_PollEvent(&amp; ev))循环之外:)

愚蠢的错误,很难为begginer解决:)