SDL_RenderPresent 对比 SDL_UpdateWindowSurface
SDL_RenderPresent vs SDL_UpdateWindowSurface
我已经成功创建并绘制了位图图像,并使用渲染器向我的 SDL window 绘制了一条绿线。问题是我不确定如何在同一个 window.
上同时执行这两项操作
void draw::image(){
SDL_Surface *bmp = SDL_LoadBMP("C:\Users\Joe\Documents\Visual Studio 2013\Projects\SDL_APP1\map1.bmp");
SDL_BlitSurface(bmp, 0, SDL_GetWindowSurface(_window), 0);
SDL_Renderer * renderer = SDL_CreateRenderer(_window, -1, 0);
// draw green line across screen
SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
SDL_RenderDrawLine(renderer, 0, 0, 640, 320);
SDL_RenderPresent(renderer);
SDL_UpdateWindowSurface(_window);
SDL_Delay(20000);
// free resources
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(_window);
}
我的这个版本的代码将 bmp 文件绘制到 window 因为 SDL_UpdateWindowSurface();在 SDL_RenderPresent() 之后;然而,当我翻转它们时,它会在屏幕上画一条绿线。我如何在我的 BMP 上画绿线?
您不能同时使用这两种方法,必须选择其中一种。我建议使用 SDL_Renderer
。从您的 SDL_Surface
创建一个 SDL_Texture
并使用 SDL_RenderCopy
.
渲染它
如果您将图像存储在 RAM 中并使用 CPU 进行渲染(这称为软件渲染),您将使用 SDL_UpdateWindowSurface
。
通过调用此函数,您可以告诉 CPU 更新屏幕并使用软件渲染进行绘制。
您可以使用 SDL_Surface
将纹理存储在 RAM 中,但软件渲染效率低下。您可以使用 SDL_BlitSurface
.
进行绘制调用
SDL_UpdateWindowSurface is equivalent to the SDL 1.2 API SDL_Flip().
另一方面,当您使用 GPU 渲染纹理并将纹理存储在 GPU 上(这称为硬件加速渲染)时,您应该使用 SDL_RenderPresent
.
此函数告诉 GPU 渲染到屏幕。
您使用 SDL_Texture
在 GPU 上存储纹理。
使用此功能时,您可以使用 SDL_RenderCopy
进行绘图调用,或者如果您想要转换 SDL_RenderCopyEx
Therefore, when using SDL's rendering API, one does all drawing intended for the frame, and then calls this function once per frame to present the final drawing to the user.
你应该使用硬件渲染,它比软件渲染更有效!即使用户 运行 程序没有 GPU(这种情况很少见,因为大多数 CPU 都有集成的 GPU),SDL 将自行切换到软件渲染!
顺便说一下,您可以将图像加载为 SDL_Texture
而无需将图像加载为 SDL_Surface
并使用 SDL_image 将其转换为 SDL_Texture
库,你应该这样做,因为它支持多种图像格式,而不仅仅是 BMP,就像纯 SDL。 (SDL_image 由 SDL 的创建者制作)
只需使用 SDL_image!
中的 IMG_LoadTexture
我已经成功创建并绘制了位图图像,并使用渲染器向我的 SDL window 绘制了一条绿线。问题是我不确定如何在同一个 window.
上同时执行这两项操作void draw::image(){
SDL_Surface *bmp = SDL_LoadBMP("C:\Users\Joe\Documents\Visual Studio 2013\Projects\SDL_APP1\map1.bmp");
SDL_BlitSurface(bmp, 0, SDL_GetWindowSurface(_window), 0);
SDL_Renderer * renderer = SDL_CreateRenderer(_window, -1, 0);
// draw green line across screen
SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
SDL_RenderDrawLine(renderer, 0, 0, 640, 320);
SDL_RenderPresent(renderer);
SDL_UpdateWindowSurface(_window);
SDL_Delay(20000);
// free resources
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(_window);
}
我的这个版本的代码将 bmp 文件绘制到 window 因为 SDL_UpdateWindowSurface();在 SDL_RenderPresent() 之后;然而,当我翻转它们时,它会在屏幕上画一条绿线。我如何在我的 BMP 上画绿线?
您不能同时使用这两种方法,必须选择其中一种。我建议使用 SDL_Renderer
。从您的 SDL_Surface
创建一个 SDL_Texture
并使用 SDL_RenderCopy
.
如果您将图像存储在 RAM 中并使用 CPU 进行渲染(这称为软件渲染),您将使用 SDL_UpdateWindowSurface
。
通过调用此函数,您可以告诉 CPU 更新屏幕并使用软件渲染进行绘制。
您可以使用 SDL_Surface
将纹理存储在 RAM 中,但软件渲染效率低下。您可以使用 SDL_BlitSurface
.
SDL_UpdateWindowSurface is equivalent to the SDL 1.2 API SDL_Flip().
另一方面,当您使用 GPU 渲染纹理并将纹理存储在 GPU 上(这称为硬件加速渲染)时,您应该使用 SDL_RenderPresent
.
此函数告诉 GPU 渲染到屏幕。
您使用 SDL_Texture
在 GPU 上存储纹理。
使用此功能时,您可以使用 SDL_RenderCopy
进行绘图调用,或者如果您想要转换 SDL_RenderCopyEx
Therefore, when using SDL's rendering API, one does all drawing intended for the frame, and then calls this function once per frame to present the final drawing to the user.
你应该使用硬件渲染,它比软件渲染更有效!即使用户 运行 程序没有 GPU(这种情况很少见,因为大多数 CPU 都有集成的 GPU),SDL 将自行切换到软件渲染!
顺便说一下,您可以将图像加载为 SDL_Texture
而无需将图像加载为 SDL_Surface
并使用 SDL_image 将其转换为 SDL_Texture
库,你应该这样做,因为它支持多种图像格式,而不仅仅是 BMP,就像纯 SDL。 (SDL_image 由 SDL 的创建者制作)
只需使用 SDL_image!
中的IMG_LoadTexture