On OS X, why the showimage example of SDL2_image needs to do RenderCopy() in the while loop?

风格不统一 提交于 2019-12-11 03:00:44

问题


For quick reference, the showimage.c example code in SDL2_image library has the following code:

    /* Show the window */
    SDL_SetWindowTitle(window, argv[i]);
    SDL_SetWindowSize(window, w, h);
    SDL_ShowWindow(window);

    done = 0;
    while ( ! done ) {
        while ( SDL_PollEvent(&event) ) {
            /* some event handling code... */
        }
        /* Draw a background pattern in case the image has transparency */
        draw_background(renderer, w, h);

        /* Display the image */
        SDL_RenderCopy(renderer, texture, NULL, NULL);
        SDL_RenderPresent(renderer);

        SDL_Delay(100);
    }
    SDL_DestroyTexture(texture);
}

SDL_RenderCopy() and SDL_RenderPresent() are called in the while(!done) block.

Since it only loads one image, I thought the texture should be created and rendered to frame buffer once and just left it there. So SDL_RenderCopy() and SDL_RenderPresent() should be called only once:

    /* Show the window */
    SDL_SetWindowTitle(window, argv[i]);
    SDL_SetWindowSize(window, w, h);
    SDL_ShowWindow(window);

    /* Draw a background pattern in case the image has transparency */
    draw_background(renderer, w, h);

    /* Display the image */
    SDL_RenderCopy(renderer, texture, NULL, NULL);
    SDL_RenderPresent(renderer);

    done = 0;
    while ( ! done ) {
        while ( SDL_PollEvent(&event) ) {
            /* some event handling code... */
        }

        SDL_Delay(100);
    }
    SDL_DestroyTexture(texture);
}

On my ubuntu 12.04, the image was shown as my expectation. However, on my MBA with OSX 10.9.2, it was all black.

Why the difference?


回答1:


The example code is simply demonstrating the most portable way to render to the screen. Your app doesn't necessarily control whether the OS uses double (or triple) buffering, so you can't rely on a single render being consistently presented to the user. Also, there are potential situations where the default framebuffer contents can be lost/altered.

In other words, you should be rendering every frame.



来源:https://stackoverflow.com/questions/22801113/on-os-x-why-the-showimage-example-of-sdl2-image-needs-to-do-rendercopy-in-the

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!