Skip to content

Commit

Permalink
Add wzPromptToChangeGfxBackendOnFailure
Browse files Browse the repository at this point in the history
  • Loading branch information
past-due authored and pull[bot] committed Jan 1, 2023
1 parent eeb149b commit 103058f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
1 change: 1 addition & 0 deletions lib/framework/wzapp.h
Expand Up @@ -72,6 +72,7 @@ struct screeninfo
void wzMain(int &argc, char **argv);
bool wzMainScreenSetup(optional<video_backend> backend, int antialiasing = 0, WINDOW_MODE fullscreen = WINDOW_MODE::windowed, int vsync = 1, int lodDistanceBiasPercentage = 0, bool highDPI = true);
video_backend wzGetDefaultGfxBackendForCurrentSystem();
bool wzPromptToChangeGfxBackendOnFailure(std::string additionalErrorDetails = "");
void wzGetGameToRendererScaleFactor(float *horizScaleFactor, float *vertScaleFactor);
void wzGetGameToRendererScaleFactorInt(unsigned int *horizScalePercentage, unsigned int *vertScalePercentage);
void wzMainEventLoop(std::function<void()> onShutdown);
Expand Down
44 changes: 41 additions & 3 deletions lib/sdl/main_sdl.cpp
Expand Up @@ -32,6 +32,7 @@
#include "lib/ivis_opengl/screen.h"
#include "lib/exceptionhandler/dumpinfo.h"
#include "lib/gamelib/gtime.h"
#include "src/configuration.h"
#include "src/warzoneconfig.h"
#include "src/game.h"
#include "gfx_api_sdl.h"
Expand Down Expand Up @@ -2354,16 +2355,16 @@ static SDL_WindowFlags SDL_backend(const video_backend& backend)
return SDL_WindowFlags{};
}

bool shouldResetGfxBackendPrompt(video_backend currentBackend, video_backend newBackend, std::string failedToInitializeObject = "graphics", std::string additionalErrorDetails = "")
bool shouldResetGfxBackendPrompt_internal(video_backend currentBackend, video_backend newBackend, std::string failureVerb = "initialize", std::string failedToInitializeObject = "graphics", std::string additionalErrorDetails = "")
{
// Offer to reset to the specified gfx backend
std::string resetString = std::string("Reset to ") + to_display_string(newBackend) + "";
const SDL_MessageBoxButtonData buttons[] = {
{ SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT, 1, resetString.c_str() },
{ SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT, 2, "Not Now" },
};
std::string titleString = std::string("Warzone: Failed to initialize ") + failedToInitializeObject;
std::string messageString = std::string("Failed to initialize ") + failedToInitializeObject + " for backend: " + to_display_string(currentBackend) + ".\n\n";
std::string titleString = std::string("Warzone: Failed to ") + failureVerb + " " + failedToInitializeObject;
std::string messageString = std::string("Failed to ") + failureVerb + " " + failedToInitializeObject + " for backend: " + to_display_string(currentBackend) + ".\n\n";
if (!additionalErrorDetails.empty())
{
messageString += "Error Details: \n\"" + additionalErrorDetails + "\"\n\n";
Expand Down Expand Up @@ -2391,6 +2392,11 @@ bool shouldResetGfxBackendPrompt(video_backend currentBackend, video_backend new
return false;
}

bool shouldResetGfxBackendPrompt(video_backend currentBackend, video_backend newBackend, std::string failedToInitializeObject = "graphics", std::string additionalErrorDetails = "")
{
return shouldResetGfxBackendPrompt_internal(currentBackend, newBackend, "initialize", failedToInitializeObject, additionalErrorDetails);
}

void resetGfxBackend(video_backend newBackend, bool displayRestartMessage = true)
{
war_setGfxBackend(newBackend);
Expand All @@ -2401,6 +2407,38 @@ void resetGfxBackend(video_backend newBackend, bool displayRestartMessage = true
}
}

bool wzPromptToChangeGfxBackendOnFailure(std::string additionalErrorDetails /*= ""*/)
{
if (!WZbackend.has_value())
{
ASSERT(false, "Can't reset gfx backend when there is no gfx backend.");
return false;
}
video_backend defaultBackend = wzGetNextFallbackGfxBackendForCurrentSystem(WZbackend.value());
if (WZbackend.value() != defaultBackend)
{
if (shouldResetGfxBackendPrompt_internal(WZbackend.value(), defaultBackend, "draw", "graphics", additionalErrorDetails))
{
resetGfxBackend(defaultBackend);
saveGfxConfig(); // must force-persist the new value before returning!
return true;
}
}
else
{
// Display message that there was a failure with the gfx backend (but there's no other backend to offer changing it to?)
std::string title = std::string("Graphics Error: ") + to_display_string(WZbackend.value());
std::string messageString = std::string("An error occured with graphics backend: ") + to_display_string(WZbackend.value()) + ".\n\n";
if (!additionalErrorDetails.empty())
{
messageString += "Error Details: \n\"" + additionalErrorDetails + "\"\n\n";
}
messageString += "Warzone 2100 will now close.";
wzDisplayDialog(Dialog_Error, title.c_str(), messageString.c_str());
}
return false;
}

bool wzSDLOneTimeInit()
{
const Uint32 sdl_init_flags = SDL_INIT_EVENTS | SDL_INIT_TIMER;
Expand Down

0 comments on commit 103058f

Please sign in to comment.