SDL_GetWindowSurface 和 SDL_CreateRenderer

SDL_GetWindowSurface with SDL_CreateRenderer

我正在用 C++ 使用 SDL2 编写游戏。

我想在这个游戏中使用 SDL_GetWindowSurface 和 SDL_CreateRenderer,如果我只使用其中一个,它可以工作,但如果我尝试同时使用它们,游戏就会崩溃。

我认为这两个功能如果都处于活动状态则不起作用。

这会是问题所在吗?

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

Can this be the problem?

是的,正如 the SDL_GetWindowSurface() declaration comment 所示:

/**
 *  \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);

为强调而引用:

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

surface = SDL_GetWindowSurface(window);
renderer = SDL_CreateSoftwareRenderer(surface);
if (!renderer) {
    SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Render creation for surface fail : %s\n",SDL_GetError());
    return 1;
}