为什么我不能对 SDL 纹理使用 colorkey?

发布于 2024-07-27 14:08:43 字数 2055 浏览 2 评论 0原文

我编写了这个简单的 C 程序来测试基本的 SDL 1.3 功能。 一切正常,只有一个小问题。 colorkey 不会被转换。 我正在加载一个 8 位 PNG 精灵文件,其中调色板索引 #0 是背景颜色。 我希望只看到显示的精灵,但我得到的是整个图像,包括背景颜色。

知道发生了什么事或如何解决它吗? 这是用 Visual C++ 2005 编写的。

// SDL test.c : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "sdl.h"
#include "sdl_image.h"
#include <stdio.h>

int _tmain(int argc, _TCHAR* argv[])
{
    int windowID;
    int textureID;
    SDL_Surface* surface;
    char* dummy = "";
    SDL_Color color;

    SDL_Init(SDL_INIT_VIDEO);

    windowID = SDL_CreateWindow("SDL Test", 400, 400, 320, 240, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
    if (windowID == 0)
    {
        printf("Unable to create window: %s\n", SDL_GetError());
    }
    else printf("Window created: %d\n", windowID);

    if (SDL_CreateRenderer(windowID, -1, SDL_RENDERER_PRESENTFLIP2) != 0)
    {
        printf("Unable to create renderer: %s\n", SDL_GetError());
    }
    else printf("Renderer created successfully.\n");

    if (SDL_SelectRenderer(windowID) != 0)
    {
        printf("Unable to select renderer: %s\n", SDL_GetError());
    }
    else printf("Renderer selected successfully\n");

    SDL_RenderPresent();

    surface = IMG_Load("<INSERT FILENAME HERE>");
    if (!surface)
    {
        printf("Unable to load image!\n");
    }
    else printf("Image Loaded\n");

    color = surface->format->palette->colors[0];
    SDL_SetColorKey(surface, 1, SDL_MapRGB(surface->format, color.r, color.g, color.b));

    textureID = SDL_CreateTextureFromSurface(0, surface);
    if (textureID == 0)
    {
        printf("Unable to create texture: %s\n", SDL_GetError());
    }
    else printf("Texture created: %d\n", textureID);

    SDL_FreeSurface(surface);

    if (SDL_RenderCopy(textureID, NULL, NULL) != 0)
    {
        printf("Unable to render texture: %s", SDL_GetError());
    }

    SDL_RenderPresent();

    scanf_s("%s", dummy);
    return 0;
}

编辑:事实证明,这是由于 SDL_CreateTextureFromSurface 中的错误造成的,我最终为此提交了补丁。

I wrote up this simple C program to test basic SDL 1.3 functionality. Everything works, with one minor problem. The colorkey doesn't get converted. I'm loading an 8-bit PNG sprite file where palette index #0 is the background color. I'd expect to see only the sprites displayed, but what I get is the entire image, including the background color.

Any idea what's going on or how to fix it? This was written in Visual C++ 2005.

// SDL test.c : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "sdl.h"
#include "sdl_image.h"
#include <stdio.h>

int _tmain(int argc, _TCHAR* argv[])
{
    int windowID;
    int textureID;
    SDL_Surface* surface;
    char* dummy = "";
    SDL_Color color;

    SDL_Init(SDL_INIT_VIDEO);

    windowID = SDL_CreateWindow("SDL Test", 400, 400, 320, 240, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
    if (windowID == 0)
    {
        printf("Unable to create window: %s\n", SDL_GetError());
    }
    else printf("Window created: %d\n", windowID);

    if (SDL_CreateRenderer(windowID, -1, SDL_RENDERER_PRESENTFLIP2) != 0)
    {
        printf("Unable to create renderer: %s\n", SDL_GetError());
    }
    else printf("Renderer created successfully.\n");

    if (SDL_SelectRenderer(windowID) != 0)
    {
        printf("Unable to select renderer: %s\n", SDL_GetError());
    }
    else printf("Renderer selected successfully\n");

    SDL_RenderPresent();

    surface = IMG_Load("<INSERT FILENAME HERE>");
    if (!surface)
    {
        printf("Unable to load image!\n");
    }
    else printf("Image Loaded\n");

    color = surface->format->palette->colors[0];
    SDL_SetColorKey(surface, 1, SDL_MapRGB(surface->format, color.r, color.g, color.b));

    textureID = SDL_CreateTextureFromSurface(0, surface);
    if (textureID == 0)
    {
        printf("Unable to create texture: %s\n", SDL_GetError());
    }
    else printf("Texture created: %d\n", textureID);

    SDL_FreeSurface(surface);

    if (SDL_RenderCopy(textureID, NULL, NULL) != 0)
    {
        printf("Unable to render texture: %s", SDL_GetError());
    }

    SDL_RenderPresent();

    scanf_s("%s", dummy);
    return 0;
}

EDIT: This turned out to be due to a bug in SDL_CreateTextureFromSurface, which I ended up submitting a patch for.

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

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

发布评论

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

评论(2

楠木可依 2024-08-03 14:08:43

这里有两个示例可能会对您有所帮助。 它们在 SDL1.3 上非常适合我。

if (color == white)
{
    SDL_SetColorKey(bmp_surface, 1,
                    SDL_MapRGB(bmp_surface->format, 255, 255, 255));
}

if (color == blue)
{
    SDL_SetColorKey(bmp_surface, 1,
                    SDL_MapRGB(bmp_surface->format, 0, 0, 254));
}

Here are two samples that might help you. They work perfect for me with SDL1.3.

if (color == white)
{
    SDL_SetColorKey(bmp_surface, 1,
                    SDL_MapRGB(bmp_surface->format, 255, 255, 255));
}

if (color == blue)
{
    SDL_SetColorKey(bmp_surface, 1,
                    SDL_MapRGB(bmp_surface->format, 0, 0, 254));
}
违心° 2024-08-03 14:08:43

您可能应该在使用之前将表面转换为显示格式。

...
surface = IMG_Load("<INSERT FILENAME HERE>");
SDL_DisplayFormat(surface);
SDL_SetColorKey(surface, 1, SDL_MapRGB(surface->format, color.r, color.g, color.b));
...

这样做总是一个好主意,这样您就可以绝对确定所有图像都具有相同的格式,并在使用 SDL 函数时给出正确的结果。

虽然我只熟悉1.2版本,不知道1.3版本改变了什么。

You should probably convert the surface to the display format before use.

...
surface = IMG_Load("<INSERT FILENAME HERE>");
SDL_DisplayFormat(surface);
SDL_SetColorKey(surface, 1, SDL_MapRGB(surface->format, color.r, color.g, color.b));
...

Always a good idea to do this so you're absolutely sure that all images has the same format and gives the right results when using the SDL functions.

Although I'm only familiar with the 1.2 version and don't have a clue what's been changed in the 1.3.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文