- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的应用程序有多个 View ,它们利用它们自己的 SDL_Window
和 SDL_Renderer
,使用渲染器将我所有的图 block 层绘制到主视图中(主视图包含一个渲染几层图 block 的合成结果,其他 View 旨在在 photoshop 样式的“图层面板”中显示每个单独的图层)。这里的问题是尝试获取纹理(或表面、渲染器等)并将其渲染到另一个独立的 View 。如果我使用 SetRenderTarget
和 SDL_RenderCopy
,它会失败,因为目标纹理属于不同的渲染器...
我也试过锁定每个 View 纹理的像素,试图手动复制像素,但锁定像素失败,除非纹理被创建为流纹理......而且, View 是不同的大小,因此需要将目标纹理向下拉伸(stretch)以适合,从而导致不必要的工作。
这导致我使用了 SDL_SoftStretch
...它有效,但当然是痛苦慢,因为它不能与硬件加速纹理一起使用。
是否有任何合理、合理有效的方法来获取一个 SDL_Window
的渲染输出,并将其缩小版本渲染到另一个?
编辑:
除了限制使用 SDL_SoftStretch
之外,这是我最接近解决问题的方式...它失败了,因为我在寻找从渲染器获取像素数据的方法时遇到了另一堵墙:
//...
BOOL SDLView::Init()
{
m_sdlWindow = SDL_CreateWindowFrom( (PVOID)m_hWndParent );
if(m_sdlWindow == NULL)
return false;
m_sdlRenderer = SDL_CreateRenderer(m_sdlWindow, -1, SDL_RendererFlags::SDL_RENDERER_ACCELERATED);
if(m_sdlRenderer == NULL)
return false;
memset(&m_rect, 0, sizeof(SDL_Rect));
SDL_GetRendererOutputSize(m_sdlRenderer, &m_rect.w, &m_rect.h);
m_pixelFormat = SDL_GetWindowPixelFormat(m_sdlWindow);
m_sdlTexture = SDL_CreateTexture(m_sdlRenderer, m_pixelFormat, SDL_TEXTUREACCESS_STREAMING, m_rect.w, m_rect.h);
SDL_SetRenderDrawColor(m_sdlRenderer, 0x64, 0x95, 0xED, 0xFF);
return true;
}
BOOL SDLView::Clear()
{
memset(&m_rect, 0, sizeof(SDL_Rect));
SDL_GetRendererOutputSize(m_sdlRenderer, &m_rect.w, &m_rect.h);
return SDL_RenderClear(m_sdlRenderer) == 0;
}
VOID SDLView::Present()
{
// Doesn't work because we still have no way of grabbing the rendered output!!
SDL_RenderPresent(m_sdlRenderer);
int result = SDL_RenderCopy(m_sdlRenderer, m_sdlTexture, NULL, NULL);
if(result != 0) {
std::string err(SDL_GetError());
DebugBreak();
}
}
void CLayerListItem::OnPaint()
{
CWnd::OnPaint();
CRect rc;
GetClientRect(&rc);
if(m_destView != nullptr)
{
m_destView->Clear();
if(m_srcView != nullptr)
{
int srcPitch = 0, destPitch = 0;
void *srcPixels = nullptr, *destPixels = nullptr;
// Lock the source pixels
int ret = SDL_LockTexture(m_srcView->GetTexture(), &m_srcView->GetRect(), &srcPixels, &srcPitch);
if(ret != 0) {
std::string err(SDL_GetError());
DebugBreak();
}
// Lock the destination pixels
ret = SDL_LockTexture(m_destView->GetTexture(), &m_destView->GetRect(), &destPixels, &destPitch);
if(ret != 0) {
std::string err(SDL_GetError());
DebugBreak();
}
// Unlock all pixels
SDL_UnlockTexture(m_destView->GetTexture());
SDL_UnlockTexture(m_srcView->GetTexture());
// Update our destinaition texture
// (I have tried every possible combination for this line, still no dice!)
int result = SDL_UpdateTexture(
m_destView->GetTexture(),
&m_destView->GetRect(),
srcPixels, srcPitch
);
if(ret != 0) {
std::string err(SDL_GetError());
DebugBreak();
}
SDL_RenderCopy(m_destView->GetRenderer(), m_destView->GetTexture(), NULL, NULL);
OutputDebugStringA("[RENDERED!]...\n");
}
m_destView->Present();
}
}
每隔几秒发送一次 OnPaint 事件以触发,这反过来会尝试获取渲染输出并将其存储在 m_destView
中(与 m_srcView
一样,它是一个 SDLView) .充其量,这似乎会导致正确呈现的主视图(“源 View ”),但目标 View 却是一片漆黑。我觉得此时我已经尝试了一切,除非有超出我已经尝试过的东西……有什么想法吗?
最佳答案
我已经有点使用有点慢的——(但仍然比SDL_SoftRect
)——函数SDL_RenderReadPixels
:
VOID SDLView::CopyTo(SDLView *targetView)
{
int result = SDL_RenderReadPixels(m_sdlRenderer, nullptr, m_pixelFormat, m_sdlSurface->pixels, m_sdlSurface->pitch);
if(result != 0) {
std::string err(SDL_GetError());
OutputDebugStringA(err.c_str());
return;
}
SDL_Texture *destTexture = targetView->GetTexture();
result = SDL_UpdateTexture(destTexture, nullptr, m_sdlSurface->pixels, m_sdlSurface->pitch);
if(result != 0) {
std::string err(SDL_GetError());
OutputDebugStringA(err.c_str());
return;
}
}
与 m_sdlSurface = SDL_GetWindowSurface(m_sdlWindow)
以及 m_pixelFormat = SDL_GetWindowPixelFormat(m_sdlWindow)
配对时效果很好...
但是请注意,这对缩小图像没有帮助,这可能意味着在将最终图像提供给目的地之前,我仍然需要在某个时候使用 RenderCopyEx 或等价物。但现在,我会将其标记为答案,直到有人可以提供更好的解决方案。
关于c++ - SDL2 渲染到纹理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24376704/
问题解决了 转到底部查看解决方案。 大家好,我用谷歌搜索了很长时间,但没有找到任何东西,所以,我们开始吧: 我有这样的设置: Compiler: MinGW IDE: Netbeans Library
如何在 SDL 中创建一个插入主窗口的新窗口?所以它可以是有重点的,有单独的绘图上下文和单独的处理事件。 最佳答案 窗中窗 您可以使用下面的示例在窗口中创建一个窗口。该示例将生成两个窗口,其中 sub
我是 SDL 的新手,我只是好奇为什么 sdl 使用静态和动态库?我的意思是,sdl.dll 中有哪些函数,为什么它是动态链接而不是静态链接?谢谢。 最佳答案 SDL.dll包含您在 SDL 中使用的
我通过 brew 在我的 mac 上安装了 SDL,但我无法包含它!这是我太简单的代码: #include int main(){ return 0; } 用cc编译时,CC找不到SDL.h
我正在尝试制作一个以恒定帧速率运行的 SDL 程序。但是我发现即使我的程序滞后很多并且跳过了很多帧(即使它以低帧运行并且没有渲染太多)。 你们有什么建议可以让我的程序运行更流畅吗? #include
我目前正在编写一款非常复杂和狡猾的游戏,它会让您充满敬畏和胜利 - 哦,好吧,这是 15 个谜题,我只是在熟悉 SDL。 我在窗口模式下运行,并使用 SDL_Flip 作为一般情况的页面更新,因为它会
我知道 SDL_TEXTUREACCESS_STATIC 和 SDL_TEXTUREACCESS_TARGET 之间的区别。我正在为一款游戏编写 SDL 后端,该游戏的平台无关核心不会告诉平台相关纹理
我们最近移植了 Bitfighter从 GLUT 到 SDL。这样做有很多好处,但也有一些缺点,特别是在窗口管理领域。 Bitfighter 在固定纵横比窗口(800x600 像素)中运行。用户可以将
我正在尝试使用 SDL 2.0 了解整个 2D 加速渲染过程。 所以我的问题是在屏幕上绘制圆圈最有效的方法是什么,为什么? 一些方法是: 首先创建一个软件表面,然后在该表面上绘制必要的像素,然后从该表
我正在尝试将纹理保存到 png 中,而我唯一得到的是 是屏幕的一部分的屏幕截图。 我的代码示例: src_texture = SDL_CreateTextureFromSurface( render
我正在使用 SDL1.2 在我的 openGL 框架中处理窗口管理。 程序运行时是否可以在不调用 SDL_Quit() 的情况下破坏窗口(表面)? ? 背景:我的框架实际上只是多媒体环境中的一个 gf
如何在 SDL 中缩放 Sprite ? 最佳答案 SDL 不直接提供缩放功能,但有 an additional library called SDL_gfx它提供旋转和缩放功能。还有another
我刚刚开始使用SDL2_ttf。我已经弄清楚如何使用 TTF_RenderText_Blished 在屏幕上获取一些文本,但是如何让它进行换行和自动换行? 似乎不支持\n;它只是创造一个空间而不是沿着
我安装了 FEDORA 和 SDL,并希望在编译时用 C 编程图形,我得到了很多对 SDL_MapRGB、SDL_Init 等的 undefined reference 我搜索了文件系统 SDL.dl
我在我的项目中使用 SDL 库,并且在 Windows 平台上工作。 当我决定将项目迁移到 SDL 2 时,我遇到了一个问题: SDL 1.2 中有一个选项可以将 stdout/stderr 的输出打
我正在尝试使用 Derelict 和 D 编写一个简单的图形测试程序。 当我尝试用 SDL 做几乎任何事情时,它都会出现段错误。这是有问题的代码: import std.stdio; import
我在考虑是否尝试使用 SDL 作为 DirectX 的替代品,似乎我只创建 2D 游戏,但是我找不到任何地方是否需要在最终用户计算机上安装 SDL 才能玩用 C# 开发的游戏,图形使用 SDL。 有人
我想使用 SDL_net 作为一个供少数程序使用的帮助程序库。然而,其中一些程序本身可能已经在使用 SDL。如果我理解正确的话,这意味着我在初始化/释放我的库时不能盲目使用 SDL_Init 和 SD
我希望我的游戏引擎停止将鼠标移动到中心(用于偏航和俯仰相机计算)。我写了一些应该处理它的代码,但鼠标在最小化时仍然移动。 void mainLoop() { // This is the ma
结构SDL_Keysym 有SDL_Scancode 和SDL_Keycode 成员。它们之间有什么区别?该文档并没有真正为我清除它。我都试过了,它们似乎做同样的事情。 最佳答案 参见 the SDL
我是一名优秀的程序员,十分优秀!