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.

有帮助吗?

解决方案

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

其他提示

From http://sdl.beuc.net/sdl.wiki/SDL_CreateRGBSurface

The prototype of SDL_CreateRGBSurface is:

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

You are passing NULL for the bitsPerPixel argument. That should be a number like 8, 24 or 32 instead, depending on what you are after.

In any case, you can use SDL_GetError() to get the exact error message which will be more helpful:

surface = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 32,
                               rmask, gmask, bmask, amask);
if(surface == NULL) {
    fprintf(stderr, "CreateRGBSurface failed: %s\n", SDL_GetError());
    exit(1);
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top