使用 SDL 使用 SDL 颜色键替换颜色

发布于 2024-08-26 20:27:35 字数 1633 浏览 4 评论 0原文

我正在制作一个简单的 Roguelike 游戏,并使用 SDL 作为显示器。游戏的图形是Codepage 437的图像,背景为黑色,字体为白色。我不想使用许多已经着色的单独图像文件,而是想使用一个图像文件,并在将其加载到内存中时替换颜色。

将代码页拆分为精灵表的代码工作正常,但是当尝试以彩色打印时,所有内容都以白色显示。我过去让它工作过,但不知何故,我在将其从更改打印时的颜色更改为更改加载时的颜色时破坏了代码。这是加载图像的代码:

SDL_Surface *Screen,*Font[2];
SDL_Rect Character[256];

Uint8 ScreenY,ScreenX;
Uint16 PrintX,PrintY,ScreenSizeY,ScreenSizeX;
Uint32 Color[2];

void InitDisplay()
{
    if(SDL_Init(SDL_INIT_VIDEO) == -1) { printf("SDL Init failed\n"); return; }

    ScreenSizeY = 600;
    ScreenSizeX = 800;
    Screen = SDL_SetVideoMode(ScreenSizeX,ScreenSizeY,32,SDL_HWSURFACE | SDL_RESIZABLE);
    SDL_WM_SetCaption("Alpha",NULL);

    SDL_Surface *Load;
    Load = IMG_Load("resource/font.png");
    Font[0] = SDL_DisplayFormat(Load);
    SDL_FreeSurface(Load);

    Color[0] = SDL_MapRGB(Font[0]->format,255,255,255);
    Color[1] = SDL_MapRGB(Font[0]->format,255,0,0);

    Uint8 i,j,k = 0;

    PrintX = 0;
    PrintY = 0;

    for(i = 0; i < 16; i++) { for(j = 0; j < 16; j++)
    {
        Character[k].x = PrintX;
        Character[k].y = PrintY;
        Character[k].w = 8;
        Character[k].h = 12;
        k++;
        PrintX += 8;
    } PrintX = 0; PrintY += 12; }

    PrintX = 0;
    PrintY = 0;

    for(i = 1; i < 2; i++)
    {
        Font[i] = SDL_DisplayFormat(Font[0]);
        SDL_SetColorKey(Font[i],SDL_SRCCOLORKEY,Color[i]);
        SDL_FillRect(Font[i],&Font[i]->clip_rect,Color[i]);
        SDL_BlitSurface(Font[0],NULL,Font[i],NULL);
        SDL_SetColorKey(Font[0],0,Color[0]);
    }
}

问题出在上面的最后一个 for 循环上。我不明白为什么它不起作用。非常感谢任何帮助!

I am working an a simple Roguelike game, and using SDL as the display. The Graphics for the game is an image of Codepage 437, with the background being black, and the font white. Instead of using many seperate image files that are already colored, I want to use one image file, and replace the colors when it is being loaded into memory.

The code to split the codepage into a sprite sheet works properly, but when attempting to print in color, everything comes out in white. I had it working the past, but somehow I broke the code when changing it from change the color at print, to change the color on load. Here is the code to load the image:

SDL_Surface *Screen,*Font[2];
SDL_Rect Character[256];

Uint8 ScreenY,ScreenX;
Uint16 PrintX,PrintY,ScreenSizeY,ScreenSizeX;
Uint32 Color[2];

void InitDisplay()
{
    if(SDL_Init(SDL_INIT_VIDEO) == -1) { printf("SDL Init failed\n"); return; }

    ScreenSizeY = 600;
    ScreenSizeX = 800;
    Screen = SDL_SetVideoMode(ScreenSizeX,ScreenSizeY,32,SDL_HWSURFACE | SDL_RESIZABLE);
    SDL_WM_SetCaption("Alpha",NULL);

    SDL_Surface *Load;
    Load = IMG_Load("resource/font.png");
    Font[0] = SDL_DisplayFormat(Load);
    SDL_FreeSurface(Load);

    Color[0] = SDL_MapRGB(Font[0]->format,255,255,255);
    Color[1] = SDL_MapRGB(Font[0]->format,255,0,0);

    Uint8 i,j,k = 0;

    PrintX = 0;
    PrintY = 0;

    for(i = 0; i < 16; i++) { for(j = 0; j < 16; j++)
    {
        Character[k].x = PrintX;
        Character[k].y = PrintY;
        Character[k].w = 8;
        Character[k].h = 12;
        k++;
        PrintX += 8;
    } PrintX = 0; PrintY += 12; }

    PrintX = 0;
    PrintY = 0;

    for(i = 1; i < 2; i++)
    {
        Font[i] = SDL_DisplayFormat(Font[0]);
        SDL_SetColorKey(Font[i],SDL_SRCCOLORKEY,Color[i]);
        SDL_FillRect(Font[i],&Font[i]->clip_rect,Color[i]);
        SDL_BlitSurface(Font[0],NULL,Font[i],NULL);
        SDL_SetColorKey(Font[0],0,Color[0]);
    }
}

The problem is with the last for loop above. I can't figure out why it isn't working. Any help is greatly appreciated!

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

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

发布评论

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

评论(1

冷…雨湿花 2024-09-02 20:27:35

我不久前解决了这个问题。问题源于以错误的顺序进行颜色键控。这是解决的代码:

uint8_t i;
uint8_t j;
uint8_t k = 0;

SDL_FillRect(Screen,NULL,0x00000000);

SDL_Rect Offset;
SDL_Surface *Load;
SDL_Surface *LoadFont;

Load = IMG_Load("resource/font.png");
LoadFont = SDL_DisplayFormat(Load);
SDL_FreeSurface(Load);

Offset.x = 0;
Offset.y = 0;

for(i = 0; i < 16; i++) { for(j = 0; j < 16; j++)
{
    Character[k].x = Offset.x;
    Character[k].y = Offset.y;
    Character[k].w = XWIDTH;
    Character[k].h = YHIEGHT;
    k++;
    Offset.x += XWIDTH;
} Offset.x = 0; Offset.y += 12; }

Color[0][0] = SDL_MapRGB(LoadFont->format,255,255,255);
Color[0][1] = SDL_MapRGB(LoadFont->format,96,96,96);
Color[1][0] = SDL_MapRGB(LoadFont->format,255,0,0);
Color[1][1] = SDL_MapRGB(LoadFont->format,96,0,0);
Color[2][0] = SDL_MapRGB(LoadFont->format,0,255,0);
Color[2][1] = SDL_MapRGB(LoadFont->format,0,96,0);
Color[3][0] = SDL_MapRGB(LoadFont->format,0,0,255);
Color[3][1] = SDL_MapRGB(LoadFont->format,0,0,96);
Color[4][0] = SDL_MapRGB(LoadFont->format,255,255,0);
Color[4][1] = SDL_MapRGB(LoadFont->format,96,96,0);

SDL_SetColorKey(LoadFont,SDL_SRCCOLORKEY,Color[0][0]);

for(i=0; i<6; i++) { for(j=0; j<2; j++)
{
    Font[i][j] = SDL_DisplayFormat(LoadFont);
    SDL_FillRect(Font[i][j],&Font[i][j]->clip_rect,Color[i][j]);
    SDL_BlitSurface(LoadFont,NULL,Font[i][j],NULL);
    SDL_SetColorKey(Font[i][j],SDL_SRCCOLORKEY,SDL_MapRGB(Font[i][j]->format,0,0,0));
}}

SDL_FreeSurface(LoadFont);

I solved this one a while back. The problem stemmed from doing the color keying in the wrong order. Here is the solved code:

uint8_t i;
uint8_t j;
uint8_t k = 0;

SDL_FillRect(Screen,NULL,0x00000000);

SDL_Rect Offset;
SDL_Surface *Load;
SDL_Surface *LoadFont;

Load = IMG_Load("resource/font.png");
LoadFont = SDL_DisplayFormat(Load);
SDL_FreeSurface(Load);

Offset.x = 0;
Offset.y = 0;

for(i = 0; i < 16; i++) { for(j = 0; j < 16; j++)
{
    Character[k].x = Offset.x;
    Character[k].y = Offset.y;
    Character[k].w = XWIDTH;
    Character[k].h = YHIEGHT;
    k++;
    Offset.x += XWIDTH;
} Offset.x = 0; Offset.y += 12; }

Color[0][0] = SDL_MapRGB(LoadFont->format,255,255,255);
Color[0][1] = SDL_MapRGB(LoadFont->format,96,96,96);
Color[1][0] = SDL_MapRGB(LoadFont->format,255,0,0);
Color[1][1] = SDL_MapRGB(LoadFont->format,96,0,0);
Color[2][0] = SDL_MapRGB(LoadFont->format,0,255,0);
Color[2][1] = SDL_MapRGB(LoadFont->format,0,96,0);
Color[3][0] = SDL_MapRGB(LoadFont->format,0,0,255);
Color[3][1] = SDL_MapRGB(LoadFont->format,0,0,96);
Color[4][0] = SDL_MapRGB(LoadFont->format,255,255,0);
Color[4][1] = SDL_MapRGB(LoadFont->format,96,96,0);

SDL_SetColorKey(LoadFont,SDL_SRCCOLORKEY,Color[0][0]);

for(i=0; i<6; i++) { for(j=0; j<2; j++)
{
    Font[i][j] = SDL_DisplayFormat(LoadFont);
    SDL_FillRect(Font[i][j],&Font[i][j]->clip_rect,Color[i][j]);
    SDL_BlitSurface(LoadFont,NULL,Font[i][j],NULL);
    SDL_SetColorKey(Font[i][j],SDL_SRCCOLORKEY,SDL_MapRGB(Font[i][j]->format,0,0,0));
}}

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