为什么此SDL2图形代码无法在Kubuntu 18.04中按预期方式工作?

问题描述 投票:0回答:1

以下代码在具有gdm3桌面环境的Ubuntu 18.04中生成一个彩色窗口。但是,在具有KDE桌面环境的Kubuntu 18.04中,相同的代码不会产生彩色窗口。取而代之的是,窗口看起来是透明的,仅显示窗口框架。只要将窗口拖到某处,只要不破坏该窗口,它就会冻结整个UI。

#include <stdio.h>
#include <SDL2/SDL.h>

SDL_Window * window = NULL;
SDL_Surface * surface = NULL;

int main() {
    SDL_Init(SDL_INIT_VIDEO);   
    window = SDL_CreateWindow(
                                "Title of window",
                                SDL_WINDOWPOS_UNDEFINED, //horizontal position of window
                                SDL_WINDOWPOS_UNDEFINED, //vertical position of window
                                640, //width of window
                                480, //height of window
                                SDL_WINDOW_SHOWN //flags
                                );
    if(window == NULL) fprintf(stderr, "Window couldnt be created.\n");
    else
    {
        surface = SDL_GetWindowSurface(window);
        if(surface == NULL) fprintf(stderr, "Could not get window surface\n");
        else {
            SDL_FillRect(
                            surface,
                            NULL,
                            SDL_MapRGB(
                                        surface->format,
                                        0x00,
                                        0xff,
                                        0xff
                                        )
                            );
            SDL_UpdateWindowSurface(window);
            SDL_Delay(10000);
            SDL_DestroyWindow(window);
            SDL_Quit();
        }
    }
    return 0;
}

ubuntu graphics ubuntu-18.04 sdl-2
1个回答
1
投票

解决方案是在发生SDL_WINDOWEVENT_EXPOSED事件后在事件循环中重绘窗口;如libsdl中所述。最终代码如下所示。

#include <stdio.h>
#include <SDL2/SDL.h>

SDL_Window * window = NULL;
SDL_Surface * surface = NULL;
SDL_Event event;

int main() {
    int show = 1;
    SDL_Init(SDL_INIT_VIDEO);   
    window = SDL_CreateWindow(
                                "Title of window",
                                SDL_WINDOWPOS_UNDEFINED, //horizontal position of window
                                SDL_WINDOWPOS_UNDEFINED, //vertical position of window
                                640, //width of window
                                480, //height of window
                                SDL_WINDOW_SHOWN //flags
                                );
    if(window == NULL) fprintf(stderr, "Window couldnt be created.\n");
    else
    {
        surface = SDL_GetWindowSurface(window);
        if(surface == NULL) fprintf(stderr, "Could not get window surface\n");
        else {
            SDL_FillRect(
                            surface,
                            NULL,
                            SDL_MapRGB(
                                        surface->format,
                                        0x00,
                                        0xff,
                                        0xff
                                        )
                            );
            while(show) {
                while(SDL_PollEvent(&event)) {
                    if(event.type == SDL_WINDOWEVENT) {
                        switch(event.window.event) {
                            case SDL_WINDOWEVENT_EXPOSED:
                                printf("SDL_WINDOWEVENT_EXPOSED event occured\n");
                                SDL_UpdateWindowSurface(window);
                                break;
                            default:
                                printf("other events\n");
                        }
                    } else if(event.type == SDL_QUIT) {
                        show = 0;
                        break;
                    }
                }
            }
            SDL_DestroyWindow(window);
            SDL_Quit();
        }
    }
    return 0;
}
© www.soinside.com 2019 - 2024. All rights reserved.