窗口关闭太快
Window closes too fast
本文关键字:窗口 更新时间:2023-10-16
所以我希望这段代码创建一个窗口,在它上面有一个图像(hello world),然后使用SDL2, visual studio和c++在5秒后退出。我觉得应该很简单。我写了代码,当我建立它没有错误,但问题是,窗口退出一旦它被创建。我最初认为添加SDL_Delay(5000)会产生预期的效果,但我猜它并没有这样工作。有人能告诉我为什么会这样吗?
#include <SDL.h>
#include <iostream>
bool init();
bool load_media();
void close();
const int s_height = 300;
const int s_width = 400;
SDL_Window* new_window = NULL;
SDL_Surface* new_surface = NULL;
SDL_Surface* new_image = NULL;
using namespace std;
bool init()
{
bool success = true;
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
cout << "couldn't initialize" << endl;
success = false;
}
else
{
new_window = SDL_CreateWindow(
"SDL Tutorial 2",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
s_width,
s_height,
SDL_WINDOW_SHOWN);
if (new_window == NULL)
{
cout << "there's no window" << endl;
success = false;
}
else
{
new_surface = SDL_GetWindowSurface(new_window);
}
}
return success;
}
bool load_media()
{
bool success = true;
new_image = SDL_LoadBMP("SDL Tutorial 2/hello_world.bmp");
if (new_image == NULL)
{
cout << "couldn't load image" << endl;
success = false;
}
return success;
}
void close()
{
SDL_FreeSurface(new_image);
SDL_DestroyWindow(new_window);
SDL_Quit;
}
int main(int argc, char *argv[])
{
if (!init())
{
cout << "FAILED to Initialize!" << endl;
if (!load_media())
{
cout << "FAILED to Load Media!" << endl;
}
else
{
SDL_BlitSurface(new_image, NULL, new_surface, NULL);
SDL_UpdateWindowSurface(new_window);
SDL_Delay(5000);
SDL_Quit;
}
}
close();
return 0;
}
您只初始化了视频子系统;如果你想使用SDL_Delay
和其他与时间相关的功能,你需要初始化定时器子系统。
将SDL_Init(SDL_INIT_VIDEO)
更改为SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER)
第一行应该是…
if (init())
调用'if (!init())'只在init()失败时才继续执行其余代码。我认为窗口短暂出现是因为在init()中,窗口被创建,但其余的代码(包括计时)被跳过,窗口立即关闭。
我将修改为:
int main(int argc, char* argv[])
{
if( init()) {
//other code executes if init() is successful
SDL_Delay(5000);
}
else {
cout << "Failed to initialize!";
}
close();
return 0;
}
这里你有工作的代码,所有的意见和其他答案的建议:
#include <SDL.h>
#include <iostream>
int const s_height = 300;
int const s_width = 400;
SDL_Window *new_window = NULL;
SDL_Surface *new_surface = NULL;
SDL_Surface *new_image = NULL;
using namespace std;
bool init(void) {
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) < 0) {
cout << "couldn't initialize" << endl;
return false;
}
new_window = SDL_CreateWindow("SDL Tutorial 2",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
s_width,
s_height,
SDL_WINDOW_SHOWN
);
if (new_window == NULL) {
cout << "there's no window" << endl;
return false;
}
new_surface = SDL_GetWindowSurface(new_window);
if (new_surface == NULL) {
cout << "there's no surface" << endl;
return false;
}
return true;
}
bool load_media(void) {
new_image = SDL_LoadBMP("SDL Tutorial 2/hello_world.bmp");
if (new_image == NULL) {
cout << "couldn't load image" << endl;
return false;
}
return true;
}
void finish(void) {
if (new_image) {
SDL_FreeSurface(new_image);
}
if (new_window) {
SDL_DestroyWindow(new_window);
}
SDL_Quit();
}
int main(int argc, char *argv[]) {
if (init()) {
if (load_media()) {
SDL_BlitSurface(new_image, NULL, new_surface, NULL);
SDL_UpdateWindowSurface(new_window);
SDL_Delay(5000);
finish();
} else {
cout << "FAILED to Load Media!" << endl;
}
} else {
cout << "FAILED to Initialize!" << endl;
}
return 0;
}
相关文章:
- 如何在Qt窗口小部件中使用QStringView(或QStringRef)
- 问:如何使用C++中的按钮从窗口打开窗口
- SDL 窗口不会弹出
- 在createdialog创建的窗口中捕获用于编辑控件的OnMouseMove消息
- 如何在cpp文件之间切换窗口?在Qt中
- QuadTree只在窗口的右上角绘制
- VS Code "command":"make"与终端窗口中的命令行"make"不同
- 如何在C++中找到active directory中禁用和锁定的窗口帐户
- 处理闪烁窗口事件
- 如何通过按下第三个窗口中的按钮,将QString从一个窗口获取到另一个窗口
- C++win32 API创建多个类似视口的窗口
- SFML RenderWindow打开窗口需要很长时间
- 如何将不同的可执行文件合并到一个窗口框架中进行编码?像浏览器一样
- 获取 SFML 窗口的 HWND 和高可用性?
- 如何获取 GLFW 窗口 ID?
- GLEW/GLUT:调用init并创建一个窗口后,取消初始化并重新初始化?
- 用于窗口的 HID 终端
- SFML 文本未绘制在窗口上
- 如何为窗口截屏
- 由于无效的 ValidateRgn() 子窗口不会收到WM_PAINT消息