Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try getting renderer before creating it #17

Merged
merged 2 commits into from
Jun 27, 2016

Conversation

seven332
Copy link
Contributor

SDL_GetWindowSurface is called in ZeroEngine::Init_SDL, renderer might be created on Android and maybe other platforms. SDL_CreateRenderer will fail if renderer is created. So check existing renderer first.

@Lyoko-Jeremie
Copy link
Contributor

but. Who create render befor this?
and, the unique_ptr will destroy the render in close.
So, this may case duplicate destroy?( Smart ptr one, Creater two).

@seven332
Copy link
Contributor Author

It doesn't matter to unique_ptr.
Please check SDL source code.

SDL_CreateRenderer might be called in SDL_GetWindowSurface.

SDL_Surface *
SDL_GetWindowSurface(SDL_Window * window)
{
    CHECK_WINDOW_MAGIC(window, NULL);

    if (!window->surface_valid) {
        if (window->surface) {
            window->surface->flags &= ~SDL_DONTFREE;
            SDL_FreeSurface(window->surface);
        }
        window->surface = SDL_CreateWindowFramebuffer(window);
        if (window->surface) {
            window->surface_valid = SDL_TRUE;
            window->surface->flags |= SDL_DONTFREE;
        }
    }
    return window->surface;
}
static SDL_Surface *
SDL_CreateWindowFramebuffer(SDL_Window * window)
{
    ...
    if (!_this->CreateWindowFramebuffer || !_this->UpdateWindowFramebuffer) {
        return NULL;
    }

    if (_this->CreateWindowFramebuffer(_this, window, &format, &pixels, &pitch) < 0) {
        return NULL;
    }
    ...
}
int
SDL_VideoInit(const char *driver_name)
{
...
    /* Add the renderer framebuffer emulation if desired */
    if (ShouldUseTextureFramebuffer()) {
        _this->CreateWindowFramebuffer = SDL_CreateWindowTexture;
        _this->UpdateWindowFramebuffer = SDL_UpdateWindowTexture;
        _this->DestroyWindowFramebuffer = SDL_DestroyWindowTexture;
    }
...
}
static int
SDL_CreateWindowTexture(SDL_VideoDevice *unused, SDL_Window * window, Uint32 * format, void ** pixels, int *pitch)
{
    SDL_WindowTextureData *data;

    data = SDL_GetWindowData(window, SDL_WINDOWTEXTUREDATA);
    if (!data) {
        SDL_Renderer *renderer = NULL;
        int i;
        const char *hint = SDL_GetHint(SDL_HINT_FRAMEBUFFER_ACCELERATION);

        /* Check to see if there's a specific driver requested */
        if (hint && *hint != '0' && *hint != '1' &&
            SDL_strcasecmp(hint, "software") != 0) {
            for (i = 0; i < SDL_GetNumRenderDrivers(); ++i) {
                SDL_RendererInfo info;
                SDL_GetRenderDriverInfo(i, &info);
                if (SDL_strcasecmp(info.name, hint) == 0) {
                    renderer = SDL_CreateRenderer(window, i, 0);
                    break;
                }
            }
        }

        if (!renderer) {
            for (i = 0; i < SDL_GetNumRenderDrivers(); ++i) {
                SDL_RendererInfo info;
                SDL_GetRenderDriverInfo(i, &info);
                if (SDL_strcmp(info.name, "software") != 0) {
                    renderer = SDL_CreateRenderer(window, i, 0);
                    if (renderer) {
                        break;
                    }
                }
            }
        }
        if (!renderer) {
            return SDL_SetError("No hardware accelerated renderers available");
        }
        ...
    }
    ...
}

And SDL_CreateRenderer should not be called twice without calling SDL_DestroyRenderer.

SDL_Renderer *
SDL_CreateRenderer(SDL_Window * window, int index, Uint32 flags)
{
    ...
    if (SDL_GetRenderer(window)) {
        SDL_SetError("Renderer already associated with window");
        return NULL;
    }
    ...
}

@Lyoko-Jeremie
Copy link
Contributor

话说我还是第一次用git这个PR功能
现在不知道怎么合并修改了

要不你再处理一下冲突然后合并?@seven332

@seven332
Copy link
Contributor Author

弄好了

@seven332 seven332 merged commit d516623 into ZeroCastleGameStudio:master Jun 27, 2016
@Lyoko-Jeremie
Copy link
Contributor

麻烦你了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants