失焦时SDL2窗口不起作用

时间:2018-05-31 15:50:55

标签: c++ sdl-2

我想创建一个不断在窗口上运行的矩形。只有当我用任何输入(窗口周围的光标浮动或按键)提供窗口时,才会调出矩形移动。有没有办法使它不停运行?

正在运行的矩形的代码:

#include <stdio.h>
#include <SDL_image.h>
#include <SDL.h>


const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
SDL_Window *gWindow = nullptr;
SDL_Renderer *gRenderer = nullptr;

bool init()
{
    bool in = true;
    if (SDL_Init(SDL_INIT_VIDEO)<0)
    {
        printf("SDL_ did not init, %s", SDL_GetError());
        in = false;
    }
    else
    {
        if (!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1"))
        {
            printf("linear texture filtering boned.\n");
            in = false;
        }
        else
        {
            gWindow = SDL_CreateWindow("CELLULA_CHARLIE", 1000, SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN | SDL_WINDOW_ALWAYS_ON_TOP);
            if (gWindow == nullptr)
            {
                printf("gwindow did not load, %s", SDL_GetError());
                in = false;
            }
            else
            {
                gRenderer = SDL_CreateRenderer(gWindow, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
                if (gRenderer == nullptr)
                {
                    printf("main renderer failed loading, %s", SDL_GetError());
                    in = false;
                }
                else
                {
                    SDL_SetRenderDrawColor(gRenderer, 255, 255, 255, 255);
                    int imgFlags = IMG_INIT_PNG;
                    if (!(IMG_Init(imgFlags)&imgFlags))
                    {
                        printf("Img did not init,  %s", IMG_GetError());
                        in = false;
                    }
                }
            }
        }
    }
    return in;
}


void close()
{
    SDL_DestroyRenderer(gRenderer);
    SDL_DestroyWindow(gWindow);
    //gMap = nullptr;
    gWindow = nullptr;
    gRenderer = nullptr;
    IMG_Quit();
    SDL_Quit();
}

int main(int argc, char*args[])
{
    int i = 0;
    SDL_Rect rectangle;
    rectangle.h = rectangle.w = 32;
    rectangle.x = rectangle.y = 0;

    if (!init())
    {
        printf("init failed\n");
    }
    else
    {

        bool quit = false;
        SDL_Event e;
        while (!quit)
        {
            while (SDL_PollEvent(&e) != 0)
            {
                if (e.type == SDL_QUIT)
                {
                    quit = true;
                }

                SDL_SetRenderDrawColor(gRenderer, 255, 255, 255, 255);
                SDL_RenderClear(gRenderer);


                rectangle.x = i * 32;
                rectangle.y = rectangle.x;
                i++;
                if (rectangle.x>500)
                {
                    i = 0;
                }
                SDL_SetRenderDrawColor(gRenderer, 0, 0, 0, 255);
                SDL_RenderFillRect(gRenderer, &rectangle);

                SDL_RenderPresent(gRenderer);
            }

        }
    }

    //the end//
    close();
    return 0;
}

1 个答案:

答案 0 :(得分:2)

你的循环以事件为中心。如果窗口没有焦点,它将无法接收任何输入事件,并且通常是您获得的所有事件。

你有选择。

  1. 设置一个计时器,定期注入一个自定义事件,当没有收到任何其他事件时,它会驱动你的循环。

  2. 让循环不依赖于事件,但只是连续运行然后在循环内部检查是否有可能要处理的事件,如果没有,则执行其他操作。