简体   繁体   中英

SDL2 fullscreen and rendering to texture

I'm using a class called UICanvas, this class has a texture that is used by child-objects to render to.

I use SDL_SetRenderTarget to achieve this, and it works quite well, but when i toggle to fullscreen, the whole screen uses the canvas-texture as a screen texture for some reason. Is this a bug, or have i done something wrong?

This is the draw-function for the canvas class, the m_canvas is a texture used for the childobjects to draw to.

void UICanvas::draw( SDL_Renderer* onWhat, Uint32 deltaTime )
{
        /* Change rendering target to the canvas. */
        SDL_SetRenderTarget( onWhat, m_canvas );

        /* Fill with background color */
        Color old;
        SDL_GetRenderDrawColor( onWhat, &old.r, &old.g, &old.b, &old.a );
        SDL_SetRenderDrawColor( onWhat, m_color.r, m_color.g, m_color.b, m_color.a );
        SDL_RenderFillRect( onWhat, NULL );
        SDL_SetRenderDrawColor( onWhat, old.r, old.g, old.b, old.a );

        /* Draw child textures on the canvas. */
        drawChildren( onWhat, deltaTime );

        /* Reset to default rendering target */
        SDL_SetRenderTarget( onWhat, NULL );

        /* Position and render canvas */
        Point2Df abs( getAbsolutePosition() );
        Rectangle dest( static_cast<Sint32>( abs.x ), static_cast<Sint32>( abs.y ), m_width, m_height );
        SDL_RenderCopy( onWhat, m_canvas, NULL, &dest );
    }

Whenever I toggle fullscreen the whole screen goes white. This is the function I use to toggle fullscreen:

void Application::toggleFullscreen()
{
        Uint32 flags( SDL_GetWindowFlags( GE_SYS.window ) );
        flags ^= SDL_WINDOW_FULLSCREEN_DESKTOP;
        SDL_SetWindowFullscreen( GE_SYS.window, flags );

        SDL_DestroyRenderer( GE_SYS.renderer );
        GE_SYS.renderer = SDL_CreateRenderer( GE_SYS.window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_TARGETTEXTURE );
        //SDL_SetRenderTarget( GE_SYS.renderer, NULL );
}

I'm still very new to SDL (nevermind any nuances between SDL 1.2 and SDL 2), but I'm sure someone will be around to correct me (eventually) if I'm wrong.

From the code you posted, it looks like you're destroying and creating renderers every time you toggle fullscreen while using "fullscreen desktop" mode. This definitely can't be right. You shouldn't be forcing a specific driver either (as per your comment that forcing the OpenGL driver seemed to fix things).

Instead it seems to me that all you need to be doing is updating the window and/or logical rendering size.

void Application::toggleFullscreen()
{
        Uint32 flags(SDL_GetWindowFlags(GE_SYS.window));
        flags ^= SDL_WINDOW_FULLSCREEN_DESKTOP;
        SDL_SetWindowFullscreen(GE_SYS.window, flags);
        int w = 640, h = 480; // TODO: UPDATE ME! ;)
        if ((flags & SDL_WINDOW_FULLSCREEN_DESKTOP) != 0)
        {
                SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "linear");
                SDL_RenderSetLogicalSize(GE_SYS.renderer, w, h);
        }
        else
        {
                SDL_SetWindowSize(GE_SYS.window, w, h);
        }
}

This method of toggling fullscreen is working for me, and doesn't involve creating and destroying renderers all the time (when toggling). If this doesn't fully resolve the issue I apologize in advance, but if nothing else this should be a step in the right direction.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM