Search code examples
c++sdl-2pixelformat

Unknown pixel format error SDL2


I've been working on a project in SDL, and I've narrowed a problem to a surface being NULL. The surface is initialized like so:

boardSurface = SDL_CreateRGBSurface(0, 780, 480, NULL, 0, 0, 0, 0);
    if (boardSurface == NULL)
    { 
        std::cout << "SURFACE ERROR " << SDL_GetError() << std::endl;
    }

It prints "SURFACE ERROR Unknown pixel format". I assume its referring to the last four arguments in the SDL_CreateRGBSurface function, but I don't know what could be causing. Google has been.. unhelpful. And so I turn to you.


Solution

  • The fourth parameter depth can't be NULL. Try changing it to 32.

    The function is declared as:

    SDL_Surface* SDL_CreateRGBSurface(Uint32 flags,
                                      int    width,
                                      int    height,
                                      int    depth,
                                      Uint32 Rmask,
                                      Uint32 Gmask,
                                      Uint32 Bmask,
                                      Uint32 Amask)
    

    See the SDL 2.0 documentation: https://wiki.libsdl.org/SDL_CreateRGBSurface