0

以下代码旨在在黑色背景上显示一个绿色方块。它执行,但绿色方块没有出现。但是,如果我更改SDL_DisplayFormatAlphaSDL_DisplayFormat正方形,则会正确呈现。

那我有什么不明白的?在我看来,我正在*surface使用 alpha 蒙版进行创建,并且正在使用SDL_MapRGBA映射我的绿色,因此使用起来也是一致的SDL_DisplayFormatAlpha

(为了清楚起见,我删除了错误检查,但在此示例中没有任何 SDL API 调用失败。)

#include <SDL.h>

int main(int argc, const char *argv[])
{
    SDL_Init( SDL_INIT_EVERYTHING );

    SDL_Surface *screen = SDL_SetVideoMode(
        640, 480, 32, SDL_HWSURFACE | SDL_DOUBLEBUF
    );

    SDL_Surface *temp = SDL_CreateRGBSurface(
        SDL_HWSURFACE, 100, 100, 32, 0, 0, 0,
        ( SDL_BYTEORDER == SDL_BIG_ENDIAN ? 0x000000ff : 0xff000000 )
    );

    SDL_Surface *surface = SDL_DisplayFormatAlpha( temp );

    SDL_FreeSurface( temp );

    SDL_FillRect(
        surface, &surface->clip_rect, SDL_MapRGBA(
            screen->format, 0x00, 0xff, 0x00, 0xff
        )
    );

    SDL_Rect r;
    r.x = 50;
    r.y = 50;

    SDL_BlitSurface( surface, NULL, screen, &r );

    SDL_Flip( screen );

    SDL_Delay( 1000 );

    SDL_Quit();

    return 0;
}
4

1 回答 1

0

我为 SDL_MapRGBA 使用了错误的格式。本来应该

SDL_FillRect(
    surface, NULL, SDL_MapRGBA(
        surface->format, 0xff, 0xff, 0x00, 0xff
    )
);

surface->format而不是screen->format。)我认为两者是等价的。他们是在调用之后SDL_DisplayFormat(),而不是在调用之后SDL_DisplayFormatAlpha()。屏幕表面没有 Alpha 通道,因此两者的格式不同。

(来自gamedev.stackexchange.com的交叉发布)

于 2012-11-02T02:02:30.837 回答