Search code examples
c++sdl

SDL_GetWindowSurface with SDL_CreateRenderer


Im programming a game with SDL2 in C++.

I want to use SDL_GetWindowSurface and SDL_CreateRenderer in this game, if I use only one of them, its works, but if I try to use both of them, the game crashes.

I think that this two functions don´t work if both are active.

Can this be the problem?


Solution

  • I think that this two functions don´t work if both are active.

    Can this be the problem?

    Yup, exactly as the SDL_GetWindowSurface() declaration comment indicates:

    /**
     *  \brief Get the SDL surface associated with the window.
     *
     *  \return The window's framebuffer surface, or NULL on error.
     *
     *  A new surface will be created with the optimal format for the window,
     *  if necessary. This surface will be freed when the window is destroyed.
     *
     *  \note You may not combine this with 3D or the rendering API on this window.
     *
     *  \sa SDL_UpdateWindowSurface()
     *  \sa SDL_UpdateWindowSurfaceRects()
     */
    extern DECLSPEC SDL_Surface * SDLCALL SDL_GetWindowSurface(SDL_Window * window);
    

    Quoting for emphasis:

    You may not combine this with 3D or the rendering API on this window.