SDL2 - Problems creating texture from surface (Screenshot)
下面的代码在我的Macbook上运行良好,但是在Linux上运行时会出现段错误:
gdb说它正在这里发生(第81行):
1 | mBackgroundTexture = SDL_CreateTextureFromSurface(mSDLRenderer, saveSurface); |
GDB输出:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | Thread 1"Testapp.bin" received signal SIGSEGV, Segmentation fault. _int_malloc (av=av@entry=0x7ffff6b9fb20 <main_arena>, bytes=bytes@entry=147479) at malloc.c:3728 3728 malloc.c: No such file or directory. (gdb) bt #0 _int_malloc (av=av@entry=0x7ffff6b9fb20 <main_arena>, bytes=bytes@entry=147479) at malloc.c:3728 #1 0x00007ffff685f5a4 in __GI___libc_malloc (bytes=147479) at malloc.c:2914 #2 0x00007fffedb126a9 in ?? () from /usr/lib/nvidia-367/libGLX_nvidia.so.0 #3 0x00007fffecb1eaf7 in ?? () from /usr/lib/nvidia-367/libnvidia-glcore.so.367.57 #4 0x00007fffecb0c30d in ?? () from /usr/lib/nvidia-367/libnvidia-glcore.so.367.57 #5 0x00007fffecbe40c4 in ?? () from /usr/lib/nvidia-367/libnvidia-glcore.so.367.57 #6 0x00007fffecbe5a0b in ?? () from /usr/lib/nvidia-367/libnvidia-glcore.so.367.57 #7 0x00007fffec871799 in ?? () from /usr/lib/nvidia-367/libnvidia-glcore.so.367.57 #8 0x00007fffec87b64c in ?? () from /usr/lib/nvidia-367/libnvidia-glcore.so.367.57 #9 0x00007fffec87f6b1 in ?? () from /usr/lib/nvidia-367/libnvidia-glcore.so.367.57 #10 0x00007fffec899127 in ?? () from /usr/lib/nvidia-367/libnvidia-glcore.so.367.57 #11 0x00007ffff7b22850 in ?? () from /usr/lib/x86_64-linux-gnu/libSDL2-2.0.so.0 #12 0x00007ffff7b1bbeb in ?? () from /usr/lib/x86_64-linux-gnu/libSDL2-2.0.so.0 #13 0x00007ffff7b1bf0b in ?? () from /usr/lib/x86_64-linux-gnu/libSDL2-2.0.so.0 #14 0x000000000043110a in MinimapRenderer::createBGTexture (this=0xc4f300, viewport=...) at renderers/MinimapRenderer.cpp:81 |
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | void MinimapRenderer::createBGTexture(const Viewport &viewport) { SDL_Surface *sshot = SDL_CreateRGBSurface(0, viewport.getRect().w, viewport.getRect().h, 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000); if (sshot != NULL) { if (SDL_RenderReadPixels(mSDLRenderer, NULL, SDL_PIXELFORMAT_ARGB8888, sshot->pixels, sshot->pitch) == 0) { SDL_Surface *saveSurface = SDL_CreateRGBSurfaceFrom(sshot->pixels, viewport.getRect().w, viewport.getRect().h, 32, sshot->pitch, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000); if (saveSurface != NULL) { mBackgroundTexture = SDL_CreateTextureFromSurface(mSDLRenderer, saveSurface); if (mBackgroundTexture != NULL) { SDL_SaveBMP(saveSurface,"winning.bmp"); } else { SDL_Log("MinimapRenderer::createBGTexture -- SDL_CreateTextureFromSurface error: %s \ ", SDL_GetError()); } SDL_FreeSurface(saveSurface); } else { SDL_Log("MinimapRenderer::createBGTexture -- SDL_CreateRGBSurfaceFrom error: %s \ ", SDL_GetError()); } } else { SDL_Log("MinimapRenderer::createBGTexture -- SDL_RenderReadPixels error: %s \ ", SDL_GetError()); } SDL_FreeSurface(sshot); } else { SDL_Log("MinimapRenderer::createBGTexture -- SDL_CreateRGBSurface error: %s \ ", SDL_GetError()); } } |
赞赏进一步调试的任何想法,请让我知道是否有其他我可以提供的有用信息
我能够跟踪问题SDL_RenderReadPixels,而不是传递NULL rect而不是传递rect似乎已经解决了问题。简化版本的代码可以在不进行错误检查的情况下运行,希望它可以对某人有所帮助:
1 2 3 4 5 6 | SDL_Rect rendRect = { 0, 0, viewport.getRect().w, viewport.getRect().h }; SDL_Surface *sshot = SDL_CreateRGBSurface(0, viewport.getRect().w, viewport.getRect().h, 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000); SDL_RenderReadPixels(mSDLRenderer, &rendRect, SDL_PIXELFORMAT_ARGB8888, sshot->pixels, sshot->pitch); mBackgroundTexture = SDL_CreateTextureFromSurface(mSDLRenderer, sshot); SDL_SaveBMP(sshot,"screenshot.bmp"); SDL_FreeSurface(sshot); |