gpt4 book ai didi

ubuntu - 为什么这个 SDL2 图形代码在 Kubuntu 18.04 中不能按预期工作?

转载 作者:行者123 更新时间:2023-12-04 19:09:41 29 4
gpt4 key购买 nike

以下代码在 Ubuntu 18.04 中生成一个带有 的彩色窗口gdm3 桌面环境。但是相同的代码不会在 Kubuntu 18.04 中生成具有 的彩色窗口KDE 桌面环境;相反,窗口看起来有点透明,只显示窗框。当窗口被拖到某个地方时,只要窗口没有被破坏,它就会卡住整个 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;
}

最佳答案

解决方法是在 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;
}

关于ubuntu - 为什么这个 SDL2 图形代码在 Kubuntu 18.04 中不能按预期工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59631142/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com