SDL引发“无效窗口”错误,无法在窗口上绘制任何内容

时间:2019-09-11 20:04:34

标签: c++ mingw sdl

我的SDL程序(用Mingw编译的C ++)窗口无法绘制任何内容。调用SDL_GetError()时,我得到了错误“无效窗口”,但GetError函数中的pushFramebufferToScreen除外,在这种情况下,我得到了"SDL_UpperBlit: passed a Null surface"。该程序可以很好地编译,并且该库似乎也可以很好地链接。该程序的目的是测试如何在模拟器的窗口上绘制帧缓冲区。

  • 对可能引起问题的零件进行故障排除
  • 确保SDL正确初始化了
  • 查看其他SDL项目,看看我做错了什么
  • 确保我已正确链接SDL,并且我所有的SDL dll都在我的可执行文件夹中

setup.h:

bool init_SDLComponents(SDL_Window* window, SDL_Renderer* renderer, SDL_Joystick* joystick, SDL_Surface* surface)
{

    if (SDL_Init(SDL_INIT_EVERYTHING)<0)
    {
        std::cout<<"Error: "<<SDL_GetError();
    }

    else
    {
        std::cout<<"SDL initiated properly\n\n";
    }

    window = SDL_CreateWindow("Name",
                               SDL_WINDOWPOS_CENTERED,
                               SDL_WINDOWPOS_CENTERED,
                               320, 288,
                               0);

    if (!window)
    {  
        std::cout << "Can't make window\n";
        return false;
    }


    renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);

    if (!renderer)
    {
        std::cout << "Can't make renderer" << SDL_GetError();
        return false;
    }

    surface = SDL_GetWindowSurface(window);

    if (!surface)
    {
        std::cout << "Can't make surface" << SDL_GetError();
        return false;
    }

    return true;
}

ppu.h:

SDL_Surface** windowSurface; //pointer to the surface of my window
const int HEIGHT = 160;
const int WIDTH = 144;
const int CHANNELS = 3;
uint8_t framebuffer [HEIGHT * WIDTH * CHANNELS];

ppu.cpp:

PPU::pushFramebufferToScreen()
{
    memset (framebuffer, 0x00, sizeof(framebuffer);
    SDL_Surface* surface = SDL_CreateRGBSurfaceFrom ((void*) framebuffer,
                                                             WIDTH,
                                                             HEIGHT,
                                                             CHANNELS * 8, //bits per channel
                                                             WIDTH * CHANNELS, /pitch
                                                             0x0000FF,
                                                             0x00FF00,
                                                             0xFF0000
                                                    );

    SDL_BlitSurface (surface, NULL, *windowSurface, NULL);
    std::cout<<SDL_GetError();
    SDL_FreeSurface(surface);

}

最后是main.cpp:


SDL_Window* window = NULL;
SDL_Renderer* renderer = NULL;
SDL_Joystick* joystick = NULL;
SDL_Texture* texture = NULL;
SDL_Surface* surface = NULL;

init_SDLComponents(window, renderer, joystick, surface);
//std::cout<<SDL_GetError();

PPU ppu;
ppu.windowSurface = &surface;   
ppu.pushBufferToScreen();
SDL_UpdateWindowSurface(window);
SDL_Delay(2000);
SDL_DestroyWindow(window);
SDL_Quit();

return 0;

我除了将窗口的颜色设置为空白,而不是默认的白色外。但是,我得到的只是invalidWindow错误,以及帧缓冲函数中的UpperBlit错误。

0 个答案:

没有答案