SDL_CreateRGBSurfaceFrom / SDL_BlitSurface - 我在模拟器上看到旧帧

SDL_CreateRGBSurfaceFrom / SDL_BlitSurface - I see old frames on my emulator

我正在开发 Space Invaders 模拟器,我正在使用 SDL2 显示输出。

问题是在输出上 window 我看到了仿真开始后的所有帧!

基本上重要的代码是这样的:

Intel8080 mainObject; // My Intel 8080 CPU emulator
mainObject.loadROM();

//Main loop flag
bool quit = false;

//Event handler
SDL_Event e;

//While application is running
while (!quit)
{
    //Handle events on queue
    while (SDL_PollEvent(&e) != 0)
    {
        //User requests quit
        if (e.type == SDL_QUIT)
        {
            quit = true;
        }
    }

    if (mainObject.frameReady)
    {
        mainObject.frameReady = false;

        gHelloWorld = SDL_CreateRGBSurfaceFrom(&mainObject.frameBuffer32, 256, 224, 32, 4 * 256, 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);

        //Apply the image
        SDL_BlitSurface(gHelloWorld, NULL, gScreenSurface, NULL);

        //Update the surface
        SDL_UpdateWindowSurface(gWindow);
    }

    mainObject.executeROM();
}

其中 Intel8080 是我的 CPU 模拟器代码,mainObject.frameBuffer32 是 Space Invaders 的视频 RAM,我从 1bpp 转换为 32bpp 以使用 SDL_CreateRGBSurfaceFrom 功能。

仿真工作正常,但我看到自仿真器启动以来生成的所有帧!

我尝试更改每个 RGBA 像素的 4 个字节中的 Alpha 值,但没有任何变化

发生这种情况是因为您似乎在没有先清除 window 的情况下渲染游戏。基本上,您应该用一种颜色填充整个 window,然后不断地在其上进行渲染。这个想法是,在渲染之前用特定颜色填充 window 相当于擦除前一帧(大多数现代计算机都足够强大来处理这个)。

您可能需要阅读 SDL 的 SDL_FillRect 功能,它可以让您用特定颜色填充整个屏幕。

渲染伪代码:

while(someCondition)
{
    [...]

    // Note, I'm not sure if "gScreenSurface" is the proper variable to use here.
    // I got it from reading your code.
    SDL_FillRect(gScreenSurface, NULL, SDL_MapRGB(gScreenSurface->format, 0, 0, 0));

    SDL_BlitSurface(gHelloWorld, NULL, gScreenSurface, NULL);

    SDL_UpdateWindowSurface(gWindow);

    [...]
}