Xlib: extension "MIT-SHM" missing on display ":0". Error trying to display a surface to the window in SDL2

960 views Asked by At

Here's my code:

#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 successfully creates a window, but nothing renders on the window. I'm using Sublime Text 3 in Windows 10, compiling and running my code using WSL1 Ubuntu 20.04 with Xming. I get the said error message multiple times before the window closes. I've tried installing some libraries I thought were related (X11 stuff mostly), anyone have a clue?

1

There are 1 answers

0
Nathan Mills On

To turn off MIT-SHM support in SDL2 when compiling, add -DNO_SHARED_MEMORY=1 to CMAKE_C_FLAGS in SDL's CMakeCache.txt.