SDL_Surface 到 IDirect3DTexture

发布于 2024-08-28 05:09:12 字数 67 浏览 6 评论 0原文

任何人都可以帮助将 SDL_Surface 对象(从文件加载的纹理)转换为 IDirect3DTexture9 对象吗?

Can anybody help with converting an SDL_Surface object, a texture loaded from a file, into an IDirect3DTexture9 object.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空城缀染半城烟沙 2024-09-04 05:09:12

老实说,我不知道你为什么想要这样做。由于各种原因,这听起来确实是一个可怕的想法,所以请告诉我们您为什么要这样做,以便我们可以说服您不要这样做;)。

同时,快速概述一下您将如何进行操作:

IDirect3DTexture9* pTex = NULL;
HRESULT            hr   = S_OK;
hr = m_d3dDevice->CreateTexture(
    surface->w,
    surface->h,
    1,
    usage,
    format,
    D3DPOOL_MANAGED,
    &pTex,
    NULL);

这将创建具有 SDL_Surface 的大小和格式的实际纹理。您必须自己填写用法,具体取决于您想要如何使用它(请参阅 D3DUSAGE)。您还必须自己确定格式 - 您不能直接将 SDL_PixelFormat 映射到 D3DFORMAT。这并不容易,除非您确切知道 SDL_Surface 是什么像素格式。

现在,您需要将数据写入纹理中。您不能在这里直接使用 memcpy,因为 SDL_Surface 和实际纹理可能有不同的步长。以下是一些未经测试的代码,可以为您执行此操作:

HRESULT        hr;
D3DLOCKED_RECT lockedRect;

// lock the texture, so that we can write into it
// Note: if you used D3DUSAGE_DYNAMIC above, you should 
// use D3DLOCK_DISCARD as the flags parameter instead of 0.
hr = pTex->LockRect(0, &lockedRect, NULL, 0);
if(SUCCEEDED(hr))
{
    // use char pointers here for byte indexing
    char*  src     = (char*) surface->pixels;
    char*  dst     = (char*) lockedRect->pBits;
    size_t numRows = surface->h;
    size_t rowSize = surface->w * surface->format->BytesPerPixel;

    // for each row...
    while(numRows--)
    {
        // copy the row
        memcpy(dst, src, rowSize);

        // use the given pitch parameters to advance to the next
        // row (since these may not equal rowSize)
        src += surface->pitch;
        dst += lockedRect->Pitch;
    }

    // don't forget this, or D3D won't like you ;)
    hr = pTex->UnlockRect(0);
}

I honestly don't know why you would ever want to do this. It sounds like a truly horrible idea for a variety of reasons, so please tell us why you want to do this so we can convince you not to ;).

In the meanwhile, a quick overview of how you'd go about it:

IDirect3DTexture9* pTex = NULL;
HRESULT            hr   = S_OK;
hr = m_d3dDevice->CreateTexture(
    surface->w,
    surface->h,
    1,
    usage,
    format,
    D3DPOOL_MANAGED,
    &pTex,
    NULL);

This creates the actual texture with the size and format of the SDL_Surface. You'll have to fill in the usage on your own, depending on how you want to use it (see D3DUSAGE). You'll also have to figure out the format on your own - you can't directly map a SDL_PixelFormat to a D3DFORMAT. This won't be easy, unless you know exactly what pixel format your SDL_Surface is.

Now, you need to write the data into the texture. You can't use straight memcpy here, since the SDL_Surface and the actual texture may have different strides. Here's some untested code that may do this for you:

HRESULT        hr;
D3DLOCKED_RECT lockedRect;

// lock the texture, so that we can write into it
// Note: if you used D3DUSAGE_DYNAMIC above, you should 
// use D3DLOCK_DISCARD as the flags parameter instead of 0.
hr = pTex->LockRect(0, &lockedRect, NULL, 0);
if(SUCCEEDED(hr))
{
    // use char pointers here for byte indexing
    char*  src     = (char*) surface->pixels;
    char*  dst     = (char*) lockedRect->pBits;
    size_t numRows = surface->h;
    size_t rowSize = surface->w * surface->format->BytesPerPixel;

    // for each row...
    while(numRows--)
    {
        // copy the row
        memcpy(dst, src, rowSize);

        // use the given pitch parameters to advance to the next
        // row (since these may not equal rowSize)
        src += surface->pitch;
        dst += lockedRect->Pitch;
    }

    // don't forget this, or D3D won't like you ;)
    hr = pTex->UnlockRect(0);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文