【问题标题】:SDL_CreateRGBSurfaceFrom / SDL_BlitSurface - I see old frames on my emulatorSDL_CreateRGBSurfaceFrom / SDL_BlitSurface - 我在模拟器上看到旧帧
【发布时间】:2019-05-16 01:00:03
【问题描述】:

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

问题是在输出窗口上我看到了自模拟开始以来的所有帧!

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

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 值,但没有任何变化

【问题讨论】:

    标签: c++ emulation sdl-2 framebuffer


    【解决方案1】:

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

    您可能想阅读 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);
    
        [...]
    }
    

    【讨论】:

    • 它有效,谢谢。现在我必须找到一种方法来旋转图像,因为在原始街机柜屏幕上旋转了 90 度。也许我必须使用硬件加速渲染
    猜你喜欢
    • 1970-01-01
    • 2013-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-23
    • 2022-11-04
    • 1970-01-01
    相关资源
    最近更新 更多