Skip to content

Commit

Permalink
Fix #513 by getting window size with SDL2
Browse files Browse the repository at this point in the history
When under XMonad, game window size using XGetGeometry() is 2 pixels
shorter (width and height) than using SDL_GetWindowSize(). As a
result, calling SDL_RenderReadPixels() to copy the screen pixels
resulted in a crash (__memmove_sse2_unaligned_erms), probably something
about accelerated rendering and not having width/height multiple of 4.
  • Loading branch information
clementgallet committed Dec 28, 2022
1 parent 379ab9b commit 292fe39
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

* Fix OSD SDL2 renderer memory leak
* Fix SDL_GameController functions as they raised the maximum number of buttons
* Get game window size from SDL2 when possible (#513)

## [1.4.4] - 2022-10-25
### Added
Expand Down
9 changes: 7 additions & 2 deletions src/library/ScreenCapture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,8 @@ int ScreenCapture::init()
} else
#endif
if ((game_info.video & GameInfo::SDL2_RENDERER) || (game_info.video & GameInfo::SDL2_SURFACE)) {
LINK_NAMESPACE_SDL2(SDL_GetWindowSize);
orig::SDL_GetWindowSize(sdl::gameSDLWindow, &width, &height);
LINK_NAMESPACE_SDL2(SDL_GetWindowPixelFormat);
Uint32 sdlpixfmt = orig::SDL_GetWindowPixelFormat(sdl::gameSDLWindow);
pixelSize = sdlpixfmt & 0xFF;
Expand Down Expand Up @@ -691,8 +693,11 @@ int ScreenCapture::copyScreenToSurface()
* So copying the screen pixels into the texture. */
void* tex_pixels;
int tex_pitch;
orig::SDL_LockTexture(screenSDLTex, nullptr, &tex_pixels, &tex_pitch);
int ret = orig::SDL_RenderReadPixels(sdl_renderer, NULL, 0, tex_pixels, pitch);
int ret = orig::SDL_LockTexture(screenSDLTex, nullptr, &tex_pixels, &tex_pitch);
if (ret < 0) {
debuglogstdio(LCF_DUMP | LCF_SDL | LCF_ERROR, "SDL_LockTexture failed: %s", orig::SDL_GetError());
}
ret = orig::SDL_RenderReadPixels(sdl_renderer, NULL, 0, tex_pixels, tex_pitch);
if (ret < 0) {
debuglogstdio(LCF_DUMP | LCF_SDL | LCF_ERROR, "SDL_RenderReadPixels failed: %s", orig::SDL_GetError());
}
Expand Down

0 comments on commit 292fe39

Please sign in to comment.