c++ - 如何正确使用 SDL_BlitSurface() 和 SDL_CreateRGBSurface()?

标签 c++ sdl

(请参阅下面的“编辑 2”了解解决方案。)

我需要从头开始创建 SDL 表面,而不是从文件中加载它们。不幸的是,当与通过 SDL_CreateRGBSurface() 生成的表面一起使用时,SDL_BlitSurface() 似乎将所有颜色渲染为黑色。这是我的代码:

int main(int argc, char** argv)
{
    SDL_Surface* screen = SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE);
    SDL_Surface* layer = SDL_CreateRGBSurface(SDL_HWSURFACE, 100, 100,
        screen->format->BitsPerPixel,
        screen->format->Rmask,
        screen->format->Gmask,
        screen->format->Bmask,
        screen->format->Amask
    );
    SDL_Rect rect;

    rect.x = 0;
    rect.y = 0;
    rect.w = 100;
    rect.h = 100;

    Uint32 blue = SDL_MapRGB(screen->format, 0, 0, 255);
    SDL_FillRect(layer, &rect, blue);
    SDL_BlitSurface(screen, NULL, layer, NULL);
    SDL_Flip(screen);
    SDL_Delay(3000);
    return 0;
}

我得到的是黑屏,而不是 100x100 的蓝色矩形。我可以通过谷歌搜索找到的内容似乎对我没有帮助,因为这些问题要么适用于 8 位表面(和设置调色板 - 我的 bpp 在这里是 32)要么没有得到回答。

所以,我想知道如何正确地将生成的表面 blit 到 SDL 屏幕上。

编辑:我发现这是参数排序错误。有问题的行应该是

SDL_BlitSurface(layer, NULL, screen, NULL);

不过,我仍然无法在更复杂的 C++ 程序中实现相同的效果。我将在此处发布代码的相关部分:

主要.cpp:

int main(int argc, char** argv)
{
    SDLScreen screen(1024, 700, "Hello, SDL!");
SDL_Event event;
    SDLMenu menu;
    bool shouldQuit = false;

    menu.setBounds(200, 100, 200, 600);
    menu.setFontName("NK211.otf");
    menu.setFontSize(36);
    menu.setEffect(sdlteShadowText);
    menu.addItem("New game");
    menu.addItem("Load game");
    menu.addItem("Save game");
    menu.addItem("Exit");
    menu.render();

    while (!shouldQuit)
    {
        menu.draw(screen.getSurface());
        SDL_Flip(screen.getSurface());
        SDL_Delay(10);
        while (SDL_PollEvent(&event))
        {
            if (event.type == SDL_QUIT)
            {
                shouldQuit = true;
            }
            else if (event.type == SDL_KEYUP)
            {
                if (event.key.keysym.sym == SDLK_q)
                {
                    shouldQuit = true;
                }
            }
        }
    }
}

SDLMenu.cpp:

void
SDLMenu::setSelectionColorRGB(int r, int g, int b)
{
    SDL_VideoInfo* info = (SDL_VideoInfo*)SDL_GetVideoInfo();
    selectionColor = SDL_MapRGB(info->vfmt, r, g, b);
}

void
SDLMenu::render()
{
    SDLText* current = NULL;
    SDL_VideoInfo* info = (SDL_VideoInfo*)SDL_GetVideoInfo();

    if (!items->empty())
    {
        current = getItemAt(currentItem);
        selectionRect = getItemRect(current);
        setSelectionColorRGB(0,0,255);
        selectionCanvas = SDL_CreateRGBSurface(SDL_HWSURFACE,
            selectionRect->w, selectionRect->h,
            info->vfmt->BitsPerPixel,
            info->vfmt->Rmask,
            info->vfmt->Gmask,
            info->vfmt->Bmask,
            info->vfmt->Amask);
        SDL_FillRect(selectionCanvas, selectionRect, selectionColor);
        SDL_SaveBMP(selectionCanvas, "selection.bmp"); // debug
    }

    for (list<SDLText*>::iterator i = items->begin();
        i != items->end(); i++)
    {
        (*i)->render();
    }
}

void
SDLMenu::draw(SDL_Surface* canvas)
{
    int currentY = bounds.y;

    if (selectionCanvas != NULL)
    {
        SDL_BlitSurface(selectionCanvas, NULL, canvas, selectionRect);
    }

    for (list<SDLText*>::iterator i = items->begin();
        i != items->end(); i++)
    {
        (*i)->draw(bounds.x, currentY, canvas);
        currentY += fontSize + itemGap;
    }
}

SDLScreen.cpp:

SDLScreen::SDLScreen(int w, int h, string t, int d)
    : width(w), height(h), depth(d), title(t)
{
    SDL_Init(SDL_INIT_EVERYTHING);
    SDL_WM_SetCaption(title.c_str(), NULL);
    refresh();
}

void
SDLScreen::refresh()
{
    screen = SDL_SetVideoMode(width, height, 32, SDL_HWSURFACE);
}

事件菜单项的选择矩形应该是蓝色的,但它显示为黑色。文件 selection.bmp 也是全黑的。

编辑 2: 我找出了造成问题的原因。 selectionRect 是相对于屏幕设置的,而 selectionCanvas 具有特定菜单项的宽度和高度。因此,填充是在 selectionCanvas 的范围之外完成的。为填充添加单独的 SDL_Rect 解决了这个问题。

SDL_Rect fillRect;
fillRect.x = 0;
fillRect.y = 0;
fillRect.w = selectionRect->w;
fillRect.h = selectionRect->h;
SDL_FillRect(selectionCanvas, &fillRect, selectionColor);

// and later...

SDL_BlitSurface(selectionCanvas, NULL, canvas, selectionRect);

最佳答案

你颠倒了来源和目的地。要在屏幕上 blit,应该是

SDL_BlitSurface(layer, NULL, screen, NULL);

doc for SDL_BlitSurface

关于c++ - 如何正确使用 SDL_BlitSurface() 和 SDL_CreateRGBSurface()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9469850/

相关文章:

opengl - 将 SDL2 纹理绑定(bind)到 GLSL 着色器

c - pb用C程序: double free or corruption (! prev)

从文件读取对象的c++编译错误

C++ 模板友元运算符重载

C++ 编译器错误 "was not declared in this scope"

c++ - swift 中 uint8_t 的等价物是什么?

c++ - 将指向成员函数的指针作为指向函数的指针传递

c++ - SDL:程序运行正常;只绘制两行像素

c++ - 更新链表时遇到问题

c++ - 绘制小 map (游戏)