问题描述
您好,我想知道是否可以使用SDL2截屏。
我尝试了 SDL_GetWindowSurface
,但出现错误消息:
Hi I would like to know if it is possible to simply take a screenshot with SDL2.I tried SDL_GetWindowSurface
but I get an error saying:
我从。
我认为另一种解决方案大约是将纹理转换为表面,但是我没有这么做...
Another solution I thought about is converting a texture to a surface but I didn't manage to do so...
您有任何解决方案吗?
推荐答案
似乎您正在混合渲染系统。该方法仅在软件渲染的情况下有效。对于硬件渲染,应使用方法 SDL_RenderReadPixels()
。要保存屏幕截图,您需要这样的代码:
It seems like you are mixing the rendering systems. That method will only work in the context of software rendering. For hardware rendering you should use the method SDL_RenderReadPixels()
. To save the screenshot you would need a code like that:
SDL_Surface *sshot = SDL_CreateRGBSurface(0, w, h, 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000);
SDL_RenderReadPixels(renderer, NULL, SDL_PIXELFORMAT_ARGB8888, sshot->pixels, sshot->pitch);
SDL_SaveBMP(sshot, "screenshot.bmp");
SDL_FreeSurface(sshot);
其中 w 和 h 宽度和高度(您可以使用 SDL_GetRendererOutputSize()
获得这些值)。
Where w and h are the screen width and height (you can get these values using SDL_GetRendererOutputSize()
).
这篇关于SDL2 C ++截屏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!