未显示 SDL 窗口
SDL Window Not Appearing
我试图显示一个 SDL 窗口,但它似乎不起作用。程序将运行,显示窗口的功能将运行,没有错误,但屏幕上没有任何显示。我在扩展坞中只有一个图标,表示程序没有响应。这是我的代码:
int main(int argc, const char * argv[]) {
MainComponent mainComponent;
mainComponent.init();
char myVar;
cout << "Enter any key to quit...";
cin >> myVar;
return 0;
}
void MainComponent::init() {
//Initialize SDL
SDL_Init(SDL_INIT_EVERYTHING);
window = SDL_CreateWindow("My Game Window", 100, 100, 100, 100, SDL_WINDOW_SHOWN);
cout << screenWidth << " " << screenHeight << endl;
if(window == nullptr) {
cout << "Error could not create window" << SDL_GetError() << endl;
}
SDL_Delay(5000);
}
这是停靠 https://www.dropbox.com/s/vc01iqp0z07zs25/Screenshot%202016-02-02%2017.26.44.png?dl=0 上图标的屏幕截图如果我做错了什么,请告诉我,谢谢!
应该
初始化SDL_Renderer来处理渲染。此处对此进行了详细说明 什么是 SDL 渲染器?。
这是上面带有初始化渲染器的修改代码;
#include <SDL2/SDL.h>
#include <iostream>
using namespace std;
class MainComponent
{
public:
void init();
~MainComponent();
private:
SDL_Window *window;
SDL_Renderer* renderer;
};
MainComponent::~MainComponent()
{
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
}
void MainComponent::init() {
//Initialize SDL
SDL_Init(SDL_INIT_EVERYTHING);
int screenWidth = 400;
int screenHeight = 300;
window = SDL_CreateWindow("My Game Window", 100, 100, screenWidth, screenHeight, SDL_WINDOW_SHOWN);
renderer = SDL_CreateRenderer(window, -1, 0);
cout << screenWidth << " " << screenHeight << endl;
if(window == nullptr) {
cout << "Error could not create window" << SDL_GetError() << endl;
}
//change the background color
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
// Clear the entire screen to our selected color.
SDL_RenderClear(renderer);
// Up until now everything was drawn behind the scenes.
// This will show the new, red contents of the window.
SDL_RenderPresent(renderer);
SDL_Delay(5000);
}
int main(int argc, const char * argv[]) {
MainComponent mainComponent;
mainComponent.init();
char myVar;
cout << "Enter any key to quit...";
cin >> myVar;
SDL_Quit();
return 0;
}
这应该可以正确编译和运行。希望有帮助。
相关文章:
- SDL 窗口不会弹出
- 在 OSX 上打开 SDL 窗口时不要打开终端窗口
- 我可以在 SDL 窗口外跟踪我的鼠标 pos 吗?
- SDL 窗口在打开后关闭
- SDL窗口的错误
- SDL 窗口在 Eclipse 中未出现
- 使用 OpenGL 窗口(如 GLFW)或周围的窗口(如 GTK 或 SDL)是否存在性能差异?
- 为什么 SDL 在最后一个窗口关闭时不发出SDL_QUIT
- SDL 窗口在移动时消失
- SDL:窗口冻结
- 通过引用/地址传递SDL窗口
- SDL 窗口弹出,但它是空白的,完全没有响应
- SDL调整窗口尺寸
- 设置窗口大小后,SDL不会渲染到整个窗口
- SDL视频显示窗口:如何连接窗口关闭事件回调
- 最小化后无法恢复 SDL 窗口
- SDL窗口根本不显示
- c++ SDL - 在窗口的特定区域内呈现
- 如何保持我在 Visual Studio SDL 中编写的图像窗口处于打开状态
- c++半透明窗口SDL