xcode SDL 窗口不显示
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34424816/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SDL window does not show
提问by Michael
This is my code:
这是我的代码:
#include <iostream>
#include <SDL2/SDL.h>
int main(int argc, const char * argv[]) {
SDL_Init(SDL_INIT_VIDEO);
SDL_Window *_window;
_window = SDL_CreateWindow("Game Engine", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 700, 500, SDL_WINDOW_RESIZABLE);
SDL_Delay(20000);
SDL_DestroyWindow(_window);
SDL_Quit();
return 0;
}
Im working in Xcode. I've downloaded SDL2 and imported the library to the projects build phases. I've tested that the SDL2 works correctly.
我在 Xcode 工作。我已经下载了 SDL2 并将库导入到项目构建阶段。我已经测试过 SDL2 工作正常。
The problem is that window never shows up. I just get a "spinning-mac-wheel" and then the program quits after the delay. I've made sure that the window is not hidden behind somewhere.
问题是窗口永远不会出现。我只是得到一个“spinning-mac-wheel”,然后程序在延迟后退出。我已经确保窗户没有藏在某个地方。
Ideas?
想法?
回答by Toad
You have to give the system a chance to have it's event loop run.
您必须让系统有机会运行它的事件循环。
easiest is to poll for events yourself:
最简单的是自己轮询事件:
SDL_Event e;
bool quit = false;
while (!quit){
while (SDL_PollEvent(&e)){
if (e.type == SDL_QUIT){
quit = true;
}
if (e.type == SDL_KEYDOWN){
quit = true;
}
if (e.type == SDL_MOUSEBUTTONDOWN){
quit = true;
}
}
}
instead of the wait loop
而不是等待循环
回答by aden
You have to load a bitmap image, or display something on the window, for Xcode to start displaying the window.
您必须加载位图图像,或在窗口上显示某些内容,Xcode 才能开始显示窗口。
#include <SDL2/SDL.h>
#include <iostream>
using namespace std;
int main() {
SDL_Window * window = nullptr;
SDL_Surface * window_surface = nullptr;
SDL_Surface * image_surface = nullptr;
SDL_Init(SDL_INIT_VIDEO);
window = SDL_CreateWindow("Window", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN);
window_surface = SDL_GetWindowSurface(window);
image_surface = SDL_LoadBMP("image.bmp");
SDL_BlitSurface(image_surface, NULL, window_surface, NULL);
SDL_UpdateWindowSurface(window);
SDL_Delay(5000);
SDL_DestroyWindow(window);
SDL_FreeSurface(image_surface);
SDL_Quit();
}
回答by Pierre
You need to initialize SDL with SDL_Init(SDL_INIT_VIDEO)
before creating the window.
您需要SDL_Init(SDL_INIT_VIDEO)
在创建窗口之前初始化 SDL 。
回答by user10495052
Please remove the sdl_delay()
and replace it with the below mentioned code. I don't have any reason for it but I tried on my own and it works
请删除sdl_delay()
并用下面提到的代码替换它。我没有任何理由,但我自己尝试过并且有效
bool isquit = false;
SDL_Event event;
while (!isquit) {
if (SDL_PollEvent( & event)) {
if (event.type == SDL_QUIT) {
isquit = true;
}
}
}