SDL2在窗口调整大小

时间:2016-10-28 20:10:36

标签: c++ crash sdl-2 window-resize

我在我的C ++游戏中使用SDL2,并且我正在尝试实现即时进出全屏的能力(说实话,我只是最大化窗口)。问题是,每当我调用SDL_MaximizeWindowSDL_RestoreWindow时,它会立即崩溃并发生段错误,转储堆栈并退出。请注意,当我使用鼠标手动调整窗口大小时,它也会执行此操作。

以下是一些有用的数据:

void init()
{
    SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO);
    SDL_CreateWindowAndRenderer(320 * 2, 240 * 2, SDL_WINDOW_BORDERLESS | SDL_WINDOW_RESIZABLE, &sdlWindow, &sdlRenderer);
    SDL_RenderSetLogicalSize(sdlRenderer, 320, 240);
    MAIN_SCREEN = SDL_CreateTexture(sdlRenderer, SDL_PIXELFORMAT_RGB565, SDL_TEXTUREACCESS_STREAMING, 320, 240);
}
void toggleFullscreen()
{
    if(SDL_GetWindowFlags(sdlWindow) & SDL_WINDOW_MAXIMIZED)
        SDL_RestoreWindow(sdlWindow);
    else
        SDL_MaximizeWindow(sdlWindow);
}

每次手动调整大小和每次调用toggleFullscreen时游戏都会崩溃。我在Windows上使用mingw-w64和Msys2编译g ++,这是堆栈转储。

Exception: STATUS_ACCESS_VIOLATION at rip=0006C79976B
rax=0000000000000000 rbx=0000000000000000 rcx=0000000000000000
rdx=0000000002F1BE20 rsi=000000000004DD80 rdi=0000000000046078
r8 =00000000FFFFC66C r9 =00000000FFFFC800 r10=0000000000000001
r11=0000000000000000 r12=00000000FFFFC800 r13=0000000000040010
r14=0000000000000001 r15=000000018021CE41
rbp=0000000000046240 rsp=00000000FFFFC580
program=nKaruga.exe, pid 96868, thread main
cs=0033 ds=002B es=002B fs=0053 gs=002B ss=002B
Stack trace:
Frame        Function    Args
00000046240  0006C79976B (0006C79B8D4, 0000004DE80, 00000000000, 00000000000)
00000000224  0006C799C6E (00000000002, 00001FF5488, 00000000000, 0018021CE41)
000FFFFC7E0  0006C79A64B (00016362004, 00002F85480, 00000000500, 00000000001)
000FFFFC870  0006C7937D3 (00100414C74, 00600000000, 0000000FFFF, 280802601C0)
000FFFFC870  00100418FBA (00000000000, 00600000000, 00100000000, 00000000000)
000FFFFC900  00100416AD4 (00000040010, 000FFFFCC20, 0018004607A, 000004638EF)
00000040030  0010041DA1A (000FFFFCCC0, 000004638F1, 000FFFFCB48, 00100000068)
000FFFFCCC0  00100425013 (00000000020, FF0700010302FF00, 001800479BE, 00000000000)
000FFFFCCC0  00180047A2F (00000000000, 00000000000, 00000000000, 00000000000)
00000000000  00180045753 (00000000000, 00000000000, 00000000000, 00000000000)
000FFFFFFF0  00180045804 (00000000000, 00000000000, 00000000000, 00000000000)
End of stack trace

请告诉我,如果我做错了什么,谢谢你的提前!

编辑:这是一些崩溃的代码。

#include <SDL.h>
#include <stdio.h>

int main(int argc, char *argv[])
{
    SDL_Window *sdlWindow;
    SDL_Renderer *sdlRenderer;
    SDL_Texture *s;
    SDL_Event event;
    bool cont = true;

    SDL_Init(SDL_INIT_VIDEO);
    SDL_CreateWindowAndRenderer(320 * 2, 240 * 2, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE, &sdlWindow, &sdlRenderer);
    SDL_RenderSetLogicalSize(sdlRenderer, 320, 240);
    s = SDL_CreateTexture(sdlRenderer, SDL_PIXELFORMAT_RGB565, SDL_TEXTUREACCESS_STREAMING, 320, 240);

    while(cont)
    {
        SDL_PollEvent(&event);
        if(event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_CLOSE)
            cont = false;

        SDL_RenderCopy(sdlRenderer, s, NULL, NULL);
        SDL_RenderPresent(sdlRenderer);
    }

    SDL_DestroyTexture(s);
    SDL_DestroyRenderer(sdlRenderer);
    SDL_DestroyWindow(sdlWindow);

    return 0;
}

编辑2:GDB告诉我有关崩溃的事实(gcc -Og -g3):

#0  0x000000006c79976b in SDL_LogCritical () from /c/Users/###/nKaruga-git/bin/win/SDL2.dll
#1  0x000000006c799c6e in SDL_LogCritical () from /c/Users/###/nKaruga-git/bin/win/SDL2.dll
#2  0x000000006c79a64b in SDL_LogCritical () from /c/Users/###/nKaruga-git/bin/win/SDL2.dll
#3  0x000000006c7937d3 in SDL_LogCritical () from /c/Users/###/nKaruga-git/bin/win/SDL2.dll
#4  0x0000000100401194 in SDL_main (argc=argc@entry=1, argv=argv@entry=0x40010) at test.c:23
#5  0x00000001004019fa in main_utf8 (argv=0x40010, argc=<optimized out>) at ../src/main/windows/SDL_windows_main.c:126
#6  WinMain (hInst=hInst@entry=0x100400000, hPrev=hPrev@entry=0x0, szCmdLine=szCmdLine@entry=0x3c38ef "", sw=<optimized out>) at ../src/main/windows/SDL_windows_main.c:189
#7  0x0000000100401ce3 in main () at /msys_scripts/msys2-runtime/src/msys2-runtime/winsup/cygwin/lib/libcmain.c:37

test.c的第23行是:

SDL_RenderCopy(sdlRenderer, s, NULL, NULL);

1 个答案:

答案 0 :(得分:4)

好吧,经过大量的研究,我发现一个GitHub问题,有些人在1月份的游戏中遇到了同样的问题,问题(https://github.com/Attnam/ivan/pull/135)一直指向我这个问题( https://forums.libsdl.org/viewtopic.php?t=11913),然后告诉我提示SDL2使用OpenGL渲染器而不是D3D,这对我有效。

基本上,问题在于当SDL_Texture使用标记SDL_TEXTUREACCESS_STREAMING时,调整窗口大小会破坏纹理并且不会再次创建纹理,因为D3D不会检查使用它(或其他东西)时的NULL指针。显然它是唯一有此问题的渲染器,因此在您的程序中使用以下内容将解决它:

SDL_SetHint(SDL_HINT_RENDER_DRIVER, "opengl");

开发人员说它应该在下一个SDL2版本中修复(那将是2.0.5),但出于某种原因我不能用它来编译我的游戏。这是一个全新的问题,现在这个问题已得到解决。