SDL2 C ++截屏 [英] SDL2 C++ Taking a screenshot

查看:175
本文介绍了SDL2 C ++截屏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我想知道是否可以使用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:


否可用的硬件加速渲染器。

No hardware accelerated renderers available.

我从此处

我认为另一种解决方案大约是将纹理转换为表面,但是我没有这么做...

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 ++截屏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆