以下是一些示例SDL2代码,我试图在运行带有KDE Plasma桌面环境的Ubuntu 18.04的Linux计算机上运行(如果相关,我会安装多个桌面环境):
#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
的{{1}}标志。
再次产生红屏的唯一方法是重新启动系统。
我什至使用IDE(CodeLite)进行编译和运行,但仍然得到相同的结果。
SO上的这个特定问题显示了类似的问题。但是OP没有使用Linux,问题也不完全相同。
此网站上的其他帖子都提到了事件处理,但我还没走那么远。如果有必要,我将不胜感激,因为documentation的解释并不多。
更新:该程序可以在另一台运行Lubuntu 18.10的计算机上正常运行。
答案 0 :(得分: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;
}
答案 1 :(得分:0)
当我切换到LXDE环境时,同一程序可以正常工作。 它也可以在另一台运行Lubuntu 18.10的Linux计算机上使用。