调整大小

时间:2017-06-12 19:55:55

标签: c sdl-2

我写了一个小SDL2应用程序,它通过重新排列其内容来对调整大小做出反应。它在Windows 10上工作得很好,但在我的Linux机器上却不行。

这是(有希望)重现问题的MCVE:

#include <SDL2/SDL.h>

int main(int argc, char **argv)
{
    SDL_Init(SDL_INIT_VIDEO);

    SDL_Window *w = SDL_CreateWindow("MCVE",
            SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 250, 250,
            SDL_WINDOW_ALLOW_HIGHDPI|SDL_WINDOW_RESIZABLE);

    SDL_Renderer *r = SDL_CreateRenderer(w, -1, SDL_RENDERER_ACCELERATED);

    SDL_Surface *shape = SDL_CreateRGBSurface(0, 500, 500, 32, 0, 0, 0, 0);
    SDL_Rect rect = { .x = 100, .y = 100, .h = 300, .w = 300 };
    SDL_FillRect(shape, &rect, SDL_MapRGB(shape->format, 0xff, 0, 0));
    SDL_Texture *t = SDL_CreateTextureFromSurface(r, shape);
    SDL_RenderClear(r);
    SDL_RenderCopy(r, t, 0, 0);
    SDL_RenderPresent(r);

    SDL_Event ev;
    while (SDL_WaitEvent(&ev))
    {
        switch (ev.type)
        {
        case SDL_QUIT:
            SDL_Quit();
            return 0;
        case SDL_WINDOWEVENT:
            if (ev.window.event == SDL_WINDOWEVENT_SIZE_CHANGED)
            {
                SDL_RenderClear(r);
                SDL_RenderCopy(r, t, 0, 0);
                SDL_RenderPresent(r);
            }
        }
    }
    return 1;
}

编译命令:

gcc -std=c11 -Wall -Wextra -Wno-unused-parameter -pedantic -osdlresize sdlresize.c -lSDL2main -lSDL2

请注意,为简洁起见,将跳过任何错误检查和清理,real code is here以供参考。

调整窗口大小时,我在Linux机器上得到的内容通常如下所示:

screenshot of resized window

如果您看不到图片:内容未正确刷新,则会在桌面上显示右侧和底部的某些区域,其中包含以下窗口中的内容。

我处理SDL_WINDOWEVENT_SIZE_CHANGED时有什么问题吗?如果我处理SDL_WINDOWEVENT_RESIZED而不是......

,就会发生同样的事情

0 个答案:

没有答案