下面是我的代码:
#include <SDL2/SDL.h>
#include <iostream>
//Variables
////////////////////////////////
//The window we'll be rendering to
SDL_Window* gWindow = NULL;
//The surface contained by the window
SDL_Surface* gScreenSurface = NULL;
//The image we will load and show on the screen
SDL_Surface* gHelloWorld = NULL;
////////////////////////////////
//Starts up SDL and creates window
bool init() {
//Initialization flag
bool success = true;
//Initialize SDL
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
success = false;
}
else {
//Create window
gWindow = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_SHOWN);
if (gWindow == NULL) {
printf("Window could not be created! SDL_Error: %s\n", SDL_GetError());
success = false;
}
else {
//Get window surface
gScreenSurface = SDL_GetWindowSurface(gWindow);
}
}
return success;
}
//Loads media
bool loadMedia() {
//Loading success flag
bool success = true;
const char* imageFilePath = "res/image.bmp";
//Load splash image
gHelloWorld = SDL_LoadBMP(imageFilePath);
if(gHelloWorld == NULL) {
printf("Unable to load image %s! SDL Error: %s\n", imageFilePath, SDL_GetError());
success = false;
}
return success;
}
//Frees media and shuts down SDL
void close() {
//Deallocate surface
SDL_FreeSurface(gHelloWorld);
gHelloWorld = NULL;
//Destroy window
SDL_DestroyWindow(gWindow);
gWindow = NULL;
//Quit SDL subsystems
SDL_Quit();
}
int main(int argc, char* args[]) {
//Start up SDL and create window
if(!init())
printf("Failed to initialize!\n");
else {
//Load media
if(!loadMedia())
printf("Failed to load media!\n");
else {
//Apply the image
SDL_BlitSurface(gHelloWorld, NULL, gScreenSurface, NULL);
//Update the surface
SDL_UpdateWindowSurface(gWindow);
//Wait two seconds
SDL_Delay(2000);
}
}
//Free resources and close SDL
close();
return 0;
}
SDL2成功地创建了一个窗口,但窗口上没有任何渲染。我在Windows 10中使用Sublime Text 3,使用WSL1 Ubuntu 20.04和Xming编译并运行我的代码。在窗口关闭之前,我多次收到上述错误消息。我尝试安装了一些我认为相关的库(大多数是X11的东西),有人有线索吗?
1条答案
按热度按时间lvmkulzt1#
要在编译时关闭SDL 2中的MIT-SHM支持,请将
-DNO_SHARED_MEMORY=1
添加到SDL的CMakeCache.txt中的CMAKE_C_FLAGS。