Linux中的SDL2空透明窗口

问题描述 投票:2回答:2

下面是我尝试在运行Ubuntu 18.04和KDE等离子桌面环境的Linux计算机上运行的一些示例SDL2代码(如果相关,我安装了多个桌面环境):

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

int main(int argc, char** argv)
{
    if(SDL_Init(SDL_INIT_VIDEO) != 0){
        std::cerr << "SDL_Init() Error: " << SDL_GetError() << std::endl;
        return 1;
    }

    SDL_Window* win = SDL_CreateWindow(
        "Hello world",
        SDL_WINDOWPOS_CENTERED,
        SDL_WINDOWPOS_CENTERED,
        640,480,
        0
    );

    if(win == nullptr){
        std::cerr << "SDL_CreateWindow() Error: " << SDL_GetError() << std::endl;
        return 1;  
    }

    //Create and init the renderer
    SDL_Renderer* ren = SDL_CreateRenderer(win, -1, 0);
    if(ren == nullptr){
        std::cerr << "SDL_CreateRenderer() Error: " << SDL_GetError() << std::endl;
        SDL_DestroyWindow(win);
        return 1;
    }

    //Render something
    SDL_RenderSetLogicalSize(ren,640,480);

    //Set colour of renderer
    SDL_SetRenderDrawColor(ren,255,0,0,255);

    //Clear the screen to the set colour
    SDL_RenderClear(ren);

    //Show all the has been done behind the scenes
    SDL_RenderPresent(ren);

    //Delay so that we can see what is on the screen
    SDL_Delay(5000);

    //Clean Up
    SDL_DestroyRenderer(ren);
    SDL_DestroyWindow(win);
    SDL_Quit();

    return 0;
}

当我第一次运行程序时,应该出现的红色窗口只出现一次。所有后续执行都会生成一个空透明窗口,其中包含背景中的任何内容。背景图像与窗口一起拖动。

我在SDL_WINDOW_SHOWN尝试过SDL_CreateWindow()旗帜,在SDL_RENDER_ACCELERATED尝试过SDL_CreateRenderer()旗帜。

再次生成红色屏幕的唯一方法是重启系统。

我甚至使用IDE(CodeLite)编译并运行它,我仍然得到相同的结果。

关于SO的这个特殊问题显示了类似的问题。但是OP没有使用Linux,问题也不尽相同。

本网站上的其他帖子提到了事件处理,但我还没有那么远。如果有必要,我会感激它的一些资源,因为documentation没有解释太多。

更新:此程序在另一台运行Lubuntu 18.10的计算机上运行正常。

c++ sdl sdl-2
2个回答
1
投票

替换SDL_Delay()(阻止所有事件处理,如通知X11 / Wayland和你的窗口管理器,你的进程仍然存在),循环以某种方式调用SDL_PumpEvents(),直接(如下)或间接通过SDL_PollEvent() / SDL_WaitEvent()

const Uint32 startMs = SDL_GetTicks();
while( SDL_GetTicks() - startMs < 5000 )
{
    SDL_PumpEvents();

    //Render something
    SDL_RenderSetLogicalSize(ren,640,480);

    //Set colour of renderer
    SDL_SetRenderDrawColor(ren,255,0,0,255);

    //Clear the screen to the set colour
    SDL_RenderClear(ren);

    //Show all the has been done behind the scenes
    SDL_RenderPresent(ren);
}

全部一起:

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

int main( int argc, char** argv )
{
    if( SDL_Init( SDL_INIT_VIDEO ) != 0 )
    {
        std::cerr << "SDL_Init() Error: " << SDL_GetError() << std::endl;
        return 1;
    }

    SDL_Window* win = SDL_CreateWindow
        (
        "Hello world",
        SDL_WINDOWPOS_CENTERED,
        SDL_WINDOWPOS_CENTERED,
        640, 480,
        0
        );

    if( win == nullptr )
    {
        std::cerr << "SDL_CreateWindow() Error: " << SDL_GetError() << std::endl;
        return 1;
    }

    //Create and init the renderer
    SDL_Renderer* ren = SDL_CreateRenderer( win, -1, 0 );
    if( ren == nullptr )
    {
        std::cerr << "SDL_CreateRenderer() Error: " << SDL_GetError() << std::endl;
        SDL_DestroyWindow( win );
        return 1;
    }

    const Uint32 startMs = SDL_GetTicks();
    while( SDL_GetTicks() - startMs < 5000 )
    {
        SDL_PumpEvents();

        //Render something
        SDL_RenderSetLogicalSize( ren, 640, 480 );

        //Set colour of renderer
        SDL_SetRenderDrawColor( ren, 255, 0, 0, 255 );

        //Clear the screen to the set colour
        SDL_RenderClear( ren );

        //Show all the has been done behind the scenes
        SDL_RenderPresent( ren );
    }

    //Clean Up
    SDL_DestroyRenderer( ren );
    SDL_DestroyWindow( win );
    SDL_Quit();

    return 0;
}

0
投票

当我切换到LXDE环境时,同样的程序工作得很好。它也适用于运行Lubuntu 18.10的另一台Linux计算机。

© www.soinside.com 2019 - 2024. All rights reserved.