问题
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