Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug on Windows: game crashes when the PC wakes up from sleep #688

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion Source/dx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,15 @@ void RenderPresent()
#ifndef USE_SDL1
if (renderer != nullptr) {
if (SDL_UpdateTexture(texture.get(), nullptr, surface->pixels, surface->pitch) <= -1) { //pitch is 2560
ErrSdl();
// We do handle SDL_RENDER_DEVICE_RESET, but I tried and it doesn't arrive
if (string_view(SDL_GetError()) == "LockRect(): INVALIDCALL") {
ReinitializeRenderer();
if (SDL_UpdateTexture(texture.get(), nullptr, surface->pixels, surface->pitch) <= -1) {
ErrSdl();
}
} else {
ErrSdl();
}
}

// Clear buffer to avoid artifacts in case the window was resized
Expand Down
4 changes: 4 additions & 0 deletions Source/miniwin/misc_msg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,10 @@ bool FetchMessage_Real(tagMSG *lpMsg)
}

break;

case SDL_RENDER_DEVICE_RESET:
ReinitializeRenderer();
break;
#endif
default:
return FalseAvail("unknown", e.type);
Expand Down
54 changes: 32 additions & 22 deletions Source/utils/display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,28 +218,7 @@ bool SpawnWindow(const char *lpWindowName)
refreshDelay = 1000000 / refreshRate;

if (sgOptions.Graphics.bUpscale) {
#ifndef USE_SDL1
Uint32 rendererFlags = SDL_RENDERER_ACCELERATED;

if (sgOptions.Graphics.bVSync) {
rendererFlags |= SDL_RENDERER_PRESENTVSYNC;
}

renderer = SDL_CreateRenderer(ghMainWnd, -1, rendererFlags);
if (renderer == nullptr) {
ErrSdl();
}

texture = SDLWrap::CreateTexture(renderer, SDL_PIXELFORMAT_RGB888, SDL_TEXTUREACCESS_STREAMING, width, height);

if (sgOptions.Graphics.bIntegerScaling && SDL_RenderSetIntegerScale(renderer, SDL_TRUE) < 0) {
ErrSdl();
}

if (SDL_RenderSetLogicalSize(renderer, width, height) <= -1) {
ErrSdl();
}
#endif
ReinitializeRenderer();
} else {
#ifdef USE_SDL1
const SDL_VideoInfo &current = *SDL_GetVideoInfo();
Expand All @@ -254,6 +233,37 @@ bool SpawnWindow(const char *lpWindowName)
return ghMainWnd != nullptr;
}

void ReinitializeRenderer()
{
#ifndef USE_SDL1
Uint32 rendererFlags = SDL_RENDERER_ACCELERATED;

if (sgOptions.Graphics.bVSync) {
rendererFlags |= SDL_RENDERER_PRESENTVSYNC;
}

texture.reset();
if (renderer != nullptr) {
SDL_DestroyRenderer(renderer);
}

renderer = SDL_CreateRenderer(ghMainWnd, -1, rendererFlags);
if (renderer == nullptr) {
ErrSdl();
}

texture = SDLWrap::CreateTexture(renderer, SDL_PIXELFORMAT_RGB888, SDL_TEXTUREACCESS_STREAMING, gnScreenWidth, gnScreenHeight);

if (sgOptions.Graphics.bIntegerScaling && SDL_RenderSetIntegerScale(renderer, SDL_TRUE) < 0) {
ErrSdl();
}

if (SDL_RenderSetLogicalSize(renderer, gnScreenWidth, gnScreenHeight) <= -1) {
ErrSdl();
}
#endif
}

SDL_Surface *GetOutputSurface()
{
#ifdef USE_SDL1
Expand Down
2 changes: 2 additions & 0 deletions Source/utils/display.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ extern SDL_Renderer *renderer;
extern SDLTextureUniquePtr texture;
#endif

void ReinitializeRenderer();

extern SDLPaletteUniquePtr Palette;
extern SDL_Surface *PalSurface;
extern unsigned int pal_surface_palette_version;
Expand Down