Codechange: Pass std::string_view from drivers instead of char *.

This commit is contained in:
Peter Nelson 2024-04-09 02:47:14 +01:00 committed by Peter Nelson
parent a42aa1a086
commit 332cbca36e
52 changed files with 161 additions and 161 deletions

View File

@ -148,15 +148,15 @@ bool DriverFactoryBase::SelectDriverImpl(const std::string &name, Driver::Type t
Driver *newd = d->CreateInstance();
*GetActiveDriver(type) = newd;
const char *err = newd->Start({});
if (err == nullptr) {
auto err = newd->Start({});
if (!err) {
Debug(driver, 1, "Successfully probed {} driver '{}'", GetDriverTypeName(type), d->name);
delete oldd;
return true;
}
*GetActiveDriver(type) = oldd;
Debug(driver, 1, "Probing {} driver '{}' failed with error: {}", GetDriverTypeName(type), d->name, err);
Debug(driver, 1, "Probing {} driver '{}' failed with error: {}", GetDriverTypeName(type), d->name, *err);
delete newd;
if (type == Driver::DT_VIDEO && _video_hw_accel && d->UsesHardwareAcceleration()) {
@ -192,10 +192,10 @@ bool DriverFactoryBase::SelectDriverImpl(const std::string &name, Driver::Type t
/* Found our driver, let's try it */
Driver *newd = d->CreateInstance();
const char *err = newd->Start(parms);
if (err != nullptr) {
auto err = newd->Start(parms);
if (err) {
delete newd;
UserError("Unable to load driver '{}'. The error was: {}", d->name, err);
UserError("Unable to load driver '{}'. The error was: {}", d->name, *err);
}
Debug(driver, 1, "Successfully loaded {} driver '{}'", GetDriverTypeName(type), d->name);

View File

@ -23,9 +23,9 @@ public:
/**
* Start this driver.
* @param parm Parameters passed to the driver.
* @return nullptr if everything went okay, otherwise an error message.
* @return std::nullopt if everything went okay, otherwise an error message.
*/
virtual const char *Start(const StringList &parm) = 0;
virtual std::optional<std::string_view> Start(const StringList &parm) = 0;
/**
* Stop this driver.
@ -47,7 +47,7 @@ public:
* Get the name of this driver.
* @return The name of the driver.
*/
virtual const char *GetName() const = 0;
virtual std::string_view GetName() const = 0;
};
DECLARE_POSTFIX_INCREMENT(Driver::Type)
@ -62,8 +62,8 @@ private:
Driver::Type type; ///< The type of driver.
int priority; ///< The priority of this factory.
const char *name; ///< The name of the drivers of this factory.
const char *description; ///< The description of this driver.
std::string_view name; ///< The name of the drivers of this factory.
std::string_view description; ///< The description of this driver.
typedef std::map<std::string, DriverFactoryBase *> Drivers; ///< Type for a map of drivers.
@ -92,9 +92,9 @@ private:
* @param type The type of driver to get the name of.
* @return The name of the type.
*/
static const char *GetDriverTypeName(Driver::Type type)
static std::string_view GetDriverTypeName(Driver::Type type)
{
static const char * const driver_type_name[] = { "music", "sound", "video" };
static const std::string_view driver_type_name[] = { "music", "sound", "video" };
return driver_type_name[type];
}
@ -135,7 +135,7 @@ public:
* Get a nice description of the driver-class.
* @return The description.
*/
const char *GetDescription() const
std::string_view GetDescription() const
{
return this->description;
}

View File

@ -26,7 +26,7 @@ static MIDI *_midi = nullptr;
*/
extern int _allegro_instance_count;
const char *MusicDriver_Allegro::Start(const StringList &)
std::optional<std::string_view> MusicDriver_Allegro::Start(const StringList &)
{
if (_allegro_instance_count == 0 && install_allegro(SYSTEM_AUTODETECT, &errno, nullptr)) {
Debug(driver, 0, "allegro: install_allegro failed '{}'", allegro_error);
@ -46,7 +46,7 @@ const char *MusicDriver_Allegro::Start(const StringList &)
return "No sound card found";
}
return nullptr;
return std::nullopt;
}
void MusicDriver_Allegro::Stop()

View File

@ -15,7 +15,7 @@
/** Allegro's music player. */
class MusicDriver_Allegro : public MusicDriver {
public:
const char *Start(const StringList &param) override;
std::optional<std::string_view> Start(const StringList &param) override;
void Stop() override;
@ -26,7 +26,7 @@ public:
bool IsSongPlaying() override;
void SetVolume(uint8_t vol) override;
const char *GetName() const override { return "allegro"; }
std::string_view GetName() const override { return "allegro"; }
};
/** Factory for allegro's music player. */

View File

@ -18,9 +18,9 @@
/** Factory for BeOS' midi player. */
static FMusicDriver_BeMidi iFMusicDriver_BeMidi;
const char *MusicDriver_BeMidi::Start(const StringList &parm)
std::optional<std::string_view> MusicDriver_BeMidi::Start(const StringList &parm)
{
return nullptr;
return std::nullopt;
}
void MusicDriver_BeMidi::Stop()

View File

@ -18,7 +18,7 @@
/** The midi player for BeOS. */
class MusicDriver_BeMidi : public MusicDriver {
public:
const char *Start(const StringList &param) override;
std::optional<std::string_view> Start(const StringList &param) override;
void Stop() override;
@ -29,7 +29,7 @@ public:
bool IsSongPlaying() override;
void SetVolume(uint8_t vol) override;
const char *GetName() const override { return "bemidi"; }
std::string_view GetName() const override { return "bemidi"; }
private:
BMidiSynthFile *midi_synth_file = nullptr;

View File

@ -79,11 +79,11 @@ static void DoSetVolume()
/**
* Initialized the MIDI player, including QuickTime initialization.
*/
const char *MusicDriver_Cocoa::Start(const StringList &)
std::optional<std::string_view> MusicDriver_Cocoa::Start(const StringList &)
{
if (NewMusicPlayer(&_player) != noErr) return "failed to create music player";
return nullptr;
return std::nullopt;
}

View File

@ -14,7 +14,7 @@
class MusicDriver_Cocoa : public MusicDriver {
public:
const char *Start(const StringList &param) override;
std::optional<std::string_view> Start(const StringList &param) override;
void Stop() override;
@ -25,7 +25,7 @@ public:
bool IsSongPlaying() override;
void SetVolume(uint8_t vol) override;
const char *GetName() const override { return "cocoa"; }
std::string_view GetName() const override { return "cocoa"; }
};
class FMusicDriver_Cocoa : public DriverFactoryBase {

View File

@ -1072,7 +1072,7 @@ static const char *LoadDefaultDLSFile(const char *user_dls)
}
const char *MusicDriver_DMusic::Start(const StringList &parm)
std::optional<std::string_view> MusicDriver_DMusic::Start(const StringList &parm)
{
/* Initialize COM */
if (FAILED(CoInitializeEx(nullptr, COINIT_MULTITHREADED))) return "COM initialization failed";
@ -1153,7 +1153,7 @@ const char *MusicDriver_DMusic::Start(const StringList &parm)
if (!StartNewThread(&_dmusic_thread, "ottd:dmusic", &MidiThreadProc)) return "Can't create MIDI output thread";
return nullptr;
return std::nullopt;
}

View File

@ -17,7 +17,7 @@ class MusicDriver_DMusic : public MusicDriver {
public:
virtual ~MusicDriver_DMusic();
const char *Start(const StringList &param) override;
std::optional<std::string_view> Start(const StringList &param) override;
void Stop() override;
@ -28,7 +28,7 @@ public:
bool IsSongPlaying() override;
void SetVolume(uint8_t vol) override;
const char *GetName() const override { return "dmusic"; }
std::string_view GetName() const override { return "dmusic"; }
};
/** Factory for the DirectX music player. */

View File

@ -35,10 +35,10 @@
/** Factory for the midi player that uses external players. */
static FMusicDriver_ExtMidi iFMusicDriver_ExtMidi;
const char *MusicDriver_ExtMidi::Start(const StringList &parm)
std::optional<std::string_view> MusicDriver_ExtMidi::Start(const StringList &parm)
{
if (strcmp(VideoDriver::GetInstance()->GetName(), "allegro") == 0 ||
strcmp(SoundDriver::GetInstance()->GetName(), "allegro") == 0) {
if (VideoDriver::GetInstance()->GetName() == "allegro" ||
SoundDriver::GetInstance()->GetName() == "allegro") {
return "the extmidi driver does not work when Allegro is loaded.";
}
@ -62,7 +62,7 @@ const char *MusicDriver_ExtMidi::Start(const StringList &parm)
this->song.clear();
this->pid = -1;
return nullptr;
return std::nullopt;
}
void MusicDriver_ExtMidi::Stop()

View File

@ -22,7 +22,7 @@ private:
void DoStop();
public:
const char *Start(const StringList &param) override;
std::optional<std::string_view> Start(const StringList &param) override;
void Stop() override;
@ -33,7 +33,7 @@ public:
bool IsSongPlaying() override;
void SetVolume(uint8_t vol) override;
const char *GetName() const override { return "extmidi"; }
std::string_view GetName() const override { return "extmidi"; }
};
class FMusicDriver_ExtMidi : public DriverFactoryBase {

View File

@ -58,7 +58,7 @@ static void RenderMusicStream(int16_t *buffer, size_t samples)
fluid_synth_write_s16(_midi.synth, samples, buffer, 0, 2, buffer, 1, 2);
}
const char *MusicDriver_FluidSynth::Start(const StringList &param)
std::optional<std::string_view> MusicDriver_FluidSynth::Start(const StringList &param)
{
std::lock_guard<std::mutex> lock{ _midi.synth_mutex };
@ -110,7 +110,7 @@ const char *MusicDriver_FluidSynth::Start(const StringList &param)
_midi.player = nullptr;
return nullptr;
return std::nullopt;
}
void MusicDriver_FluidSynth::Stop()

View File

@ -15,7 +15,7 @@
/** Music driver making use of FluidSynth. */
class MusicDriver_FluidSynth : public MusicDriver {
public:
const char *Start(const StringList &param) override;
std::optional<std::string_view> Start(const StringList &param) override;
void Stop() override;
@ -26,7 +26,7 @@ public:
bool IsSongPlaying() override;
void SetVolume(uint8_t vol) override;
const char *GetName() const override { return "fluidsynth"; }
std::string_view GetName() const override { return "fluidsynth"; }
};
/** Factory for the fluidsynth driver. */

View File

@ -15,7 +15,7 @@
/** The music player that does nothing. */
class MusicDriver_Null : public MusicDriver {
public:
const char *Start(const StringList &) override { return nullptr; }
std::optional<std::string_view> Start(const StringList &) override { return std::nullopt; }
void Stop() override { }
@ -26,7 +26,7 @@ public:
bool IsSongPlaying() override { return true; }
void SetVolume(uint8_t) override { }
const char *GetName() const override { return "null"; }
std::string_view GetName() const override { return "null"; }
};
/** Factory for the null music player. */

View File

@ -367,7 +367,7 @@ void MusicDriver_Win32::SetVolume(uint8_t vol)
_midi.new_volume = vol;
}
const char *MusicDriver_Win32::Start(const StringList &parm)
std::optional<std::string_view> MusicDriver_Win32::Start(const StringList &parm)
{
Debug(driver, 2, "Win32-MIDI: Start: initializing");
@ -416,7 +416,7 @@ const char *MusicDriver_Win32::Start(const StringList &parm)
if (timeBeginPeriod(_midi.time_period) == MMSYSERR_NOERROR) {
/* success */
Debug(driver, 2, "Win32-MIDI: Start: timer resolution is {}", _midi.time_period);
return nullptr;
return std::nullopt;
}
}
midiOutClose(_midi.midi_out);

View File

@ -15,7 +15,7 @@
/** The Windows music player. */
class MusicDriver_Win32 : public MusicDriver {
public:
const char *Start(const StringList &param) override;
std::optional<std::string_view> Start(const StringList &param) override;
void Stop() override;
@ -26,7 +26,7 @@ public:
bool IsSongPlaying() override;
void SetVolume(uint8_t vol) override;
const char *GetName() const override { return "win32"; }
std::string_view GetName() const override { return "win32"; }
};
/** Factory for Windows' music player. */

View File

@ -579,7 +579,7 @@ struct GameOptionsWindow : Window {
break;
case WID_GO_VIDEO_DRIVER_INFO:
SetDParamStr(0, VideoDriver::GetInstance()->GetInfoString());
SetDParamStr(0, std::string{VideoDriver::GetInstance()->GetInfoString()});
DrawStringMultiLine(r, STR_GAME_OPTIONS_VIDEO_DRIVER_INFO);
break;
@ -641,7 +641,7 @@ struct GameOptionsWindow : Window {
changed |= wid->UpdateVerticalSize(y);
wid = this->GetWidget<NWidgetResizeBase>(WID_GO_VIDEO_DRIVER_INFO);
SetDParamStr(0, VideoDriver::GetInstance()->GetInfoString());
SetDParamStr(0, std::string{VideoDriver::GetInstance()->GetInfoString()});
y = GetStringHeight(STR_GAME_OPTIONS_VIDEO_DRIVER_INFO, wid->current_x);
changed |= wid->UpdateVerticalSize(y);

View File

@ -50,7 +50,7 @@ void SoundDriver_Allegro::MainLoop()
*/
extern int _allegro_instance_count;
const char *SoundDriver_Allegro::Start(const StringList &parm)
std::optional<std::string_view> SoundDriver_Allegro::Start(const StringList &parm)
{
if (_allegro_instance_count == 0 && install_allegro(SYSTEM_AUTODETECT, &errno, nullptr)) {
Debug(driver, 0, "allegro: install_allegro failed '{}'", allegro_error);
@ -74,7 +74,7 @@ const char *SoundDriver_Allegro::Start(const StringList &parm)
_buffer_size = GetDriverParamInt(parm, "samples", 1024) * hz / 11025;
_stream = play_audio_stream(_buffer_size, 16, true, hz, 255, 128);
MxInitialize(hz);
return nullptr;
return std::nullopt;
}
void SoundDriver_Allegro::Stop()

View File

@ -15,12 +15,12 @@
/** Implementation of the allegro sound driver. */
class SoundDriver_Allegro : public SoundDriver {
public:
const char *Start(const StringList &param) override;
std::optional<std::string_view> Start(const StringList &param) override;
void Stop() override;
void MainLoop() override;
const char *GetName() const override { return "allegro"; }
std::string_view GetName() const override { return "allegro"; }
};
/** Factory for the allegro sound driver. */

View File

@ -44,7 +44,7 @@ static OSStatus audioCallback(void *, AudioUnitRenderActionFlags *, const AudioT
}
const char *SoundDriver_Cocoa::Start(const StringList &parm)
std::optional<std::string_view> SoundDriver_Cocoa::Start(const StringList &parm)
{
struct AURenderCallbackStruct callback;
AudioStreamBasicDescription requestedDesc;
@ -108,7 +108,7 @@ const char *SoundDriver_Cocoa::Start(const StringList &parm)
}
/* We're running! */
return nullptr;
return std::nullopt;
}

View File

@ -14,10 +14,10 @@
class SoundDriver_Cocoa : public SoundDriver {
public:
const char *Start(const StringList &param) override;
std::optional<std::string_view> Start(const StringList &param) override;
void Stop() override;
const char *GetName() const override { return "cocoa"; }
std::string_view GetName() const override { return "cocoa"; }
};
class FSoundDriver_Cocoa : public DriverFactoryBase {

View File

@ -15,10 +15,10 @@
/** Implementation of the null sound driver. */
class SoundDriver_Null : public SoundDriver {
public:
const char *Start(const StringList &) override { return nullptr; }
std::optional<std::string_view> Start(const StringList &) override { return std::nullopt; }
void Stop() override { }
const char *GetName() const override { return "null"; }
std::string_view GetName() const override { return "null"; }
bool HasOutput() const override { return false; }
};

View File

@ -30,7 +30,7 @@ static void CDECL fill_sound_buffer(void *, Uint8 *stream, int len)
MxMixSamples(stream, len / 4);
}
const char *SoundDriver_SDL::Start(const StringList &parm)
std::optional<std::string_view> SoundDriver_SDL::Start(const StringList &parm)
{
SDL_AudioSpec spec;
SDL_AudioSpec spec_actual;
@ -52,7 +52,7 @@ const char *SoundDriver_SDL::Start(const StringList &parm)
SDL_AudioDeviceID dev = SDL_OpenAudioDevice(nullptr, 0, &spec, &spec_actual, SDL_AUDIO_ALLOW_FREQUENCY_CHANGE);
MxInitialize(spec_actual.freq);
SDL_PauseAudioDevice(dev, 0);
return nullptr;
return std::nullopt;
}
void SoundDriver_SDL::Stop()

View File

@ -30,7 +30,7 @@ static void CDECL fill_sound_buffer(void *, Uint8 *stream, int len)
MxMixSamples(stream, len / 4);
}
const char *SoundDriver_SDL::Start(const StringList &parm)
std::optional<std::string_view> SoundDriver_SDL::Start(const StringList &parm)
{
SDL_AudioSpec spec;
@ -51,7 +51,7 @@ const char *SoundDriver_SDL::Start(const StringList &parm)
MxInitialize(spec.freq);
SDL_OpenAudio(&spec, &spec);
SDL_PauseAudio(0);
return nullptr;
return std::nullopt;
}
void SoundDriver_SDL::Stop()

View File

@ -15,10 +15,10 @@
/** Implementation of the SDL sound driver. */
class SoundDriver_SDL : public SoundDriver {
public:
const char *Start(const StringList &param) override;
std::optional<std::string_view> Start(const StringList &param) override;
void Stop() override;
const char *GetName() const override { return "sdl"; }
std::string_view GetName() const override { return "sdl"; }
};
/** Factory for the SDL sound driver. */

View File

@ -59,7 +59,7 @@ static DWORD WINAPI SoundThread(LPVOID)
return 0;
}
const char *SoundDriver_Win32::Start(const StringList &parm)
std::optional<std::string_view> SoundDriver_Win32::Start(const StringList &parm)
{
WAVEFORMATEX wfex;
wfex.wFormatTag = WAVE_FORMAT_PCM;
@ -89,7 +89,7 @@ const char *SoundDriver_Win32::Start(const StringList &parm)
return error;
}
return nullptr;
return std::nullopt;
}
void SoundDriver_Win32::Stop()

View File

@ -15,10 +15,10 @@
/** Implementation of the sound driver for Windows. */
class SoundDriver_Win32 : public SoundDriver {
public:
const char *Start(const StringList &param) override;
std::optional<std::string_view> Start(const StringList &param) override;
void Stop() override;
const char *GetName() const override { return "win32"; }
std::string_view GetName() const override { return "win32"; }
};
/** Factory for the sound driver for Windows. */

View File

@ -135,10 +135,10 @@ static HRESULT CreateXAudio(API_XAudio2Create xAudio2Create)
* Initialises the XAudio2 driver.
*
* @param parm Driver parameters.
* @return An error message if unsuccessful, or nullptr otherwise.
* @return An error message if unsuccessful, or std::nullopt otherwise.
*
*/
const char *SoundDriver_XAudio2::Start(const StringList &parm)
std::optional<std::string_view> SoundDriver_XAudio2::Start(const StringList &parm)
{
HRESULT hr = CoInitializeEx(nullptr, COINIT_MULTITHREADED);
@ -257,7 +257,7 @@ const char *SoundDriver_XAudio2::Start(const StringList &parm)
return "Failed to submit the first audio buffer";
}
return nullptr;
return std::nullopt;
}
/**

View File

@ -15,10 +15,10 @@
/** Implementation of the XAudio2 sound driver. */
class SoundDriver_XAudio2 : public SoundDriver {
public:
const char *Start(const StringList &param) override;
std::optional<std::string_view> Start(const StringList &param) override;
void Stop() override;
const char *GetName() const override { return "xaudio2"; }
std::string_view GetName() const override { return "xaudio2"; }
};
/** Factory for the XAudio2 sound driver. */

View File

@ -420,7 +420,7 @@ bool VideoDriver_Allegro::PollEvent()
*/
int _allegro_instance_count = 0;
const char *VideoDriver_Allegro::Start(const StringList &param)
std::optional<std::string_view> VideoDriver_Allegro::Start(const StringList &param)
{
if (_allegro_instance_count == 0 && install_allegro(SYSTEM_AUTODETECT, &errno, nullptr)) {
Debug(driver, 0, "allegro: install_allegro failed '{}'", allegro_error);
@ -450,7 +450,7 @@ const char *VideoDriver_Allegro::Start(const StringList &param)
this->is_game_threaded = !GetDriverParamBool(param, "no_threads") && !GetDriverParamBool(param, "no_thread");
return nullptr;
return std::nullopt;
}
void VideoDriver_Allegro::Stop()

View File

@ -15,7 +15,7 @@
/** The allegro video driver. */
class VideoDriver_Allegro : public VideoDriver {
public:
const char *Start(const StringList &param) override;
std::optional<std::string_view> Start(const StringList &param) override;
void Stop() override;
@ -33,7 +33,7 @@ public:
std::vector<int> GetListOfMonitorRefreshRates() override;
const char *GetName() const override { return "allegro"; }
std::string_view GetName() const override { return "allegro"; }
protected:
void InputLoop() override;

View File

@ -20,12 +20,12 @@ class VideoDriver_CocoaOpenGL : public VideoDriver_Cocoa {
uint8_t *anim_buffer; ///< Animation buffer from OpenGL back-end.
std::string driver_info; ///< Information string about selected driver.
const char *AllocateContext(bool allow_software);
std::optional<std::string_view> AllocateContext(bool allow_software);
public:
VideoDriver_CocoaOpenGL() : VideoDriver_Cocoa(true), gl_context(nullptr), anim_buffer(nullptr), driver_info(this->GetName()) {}
const char *Start(const StringList &param) override;
std::optional<std::string_view> Start(const StringList &param) override;
void Stop() override;
bool HasEfficient8Bpp() const override { return true; }
@ -40,9 +40,9 @@ public:
uint8_t *GetAnimBuffer() override { return this->anim_buffer; }
/** Return driver name */
const char *GetName() const override { return "cocoa-opengl"; }
std::string_view GetName() const override { return "cocoa-opengl"; }
const char *GetInfoString() const override { return this->driver_info.c_str(); }
std::string_view GetInfoString() const override { return this->driver_info; }
void AllocateBackingStore(bool force = false) override;

View File

@ -185,10 +185,10 @@ static bool _allowSoftware;
static FVideoDriver_CocoaOpenGL iFVideoDriver_CocoaOpenGL;
const char *VideoDriver_CocoaOpenGL::Start(const StringList &param)
std::optional<std::string_view> VideoDriver_CocoaOpenGL::Start(const StringList &param)
{
const char *err = this->Initialize();
if (err != nullptr) return err;
auto err = this->Initialize();
if (err) return err;
int bpp = BlitterFactory::GetCurrentBlitter()->GetScreenDepth();
if (bpp != 8 && bpp != 32) {
@ -198,7 +198,7 @@ const char *VideoDriver_CocoaOpenGL::Start(const StringList &param)
/* Try to allocate GL context. */
err = this->AllocateContext(GetDriverParamBool(param, "software"));
if (err != nullptr) {
if (err) {
this->Stop();
return err;
}
@ -224,7 +224,7 @@ const char *VideoDriver_CocoaOpenGL::Start(const StringList &param)
this->is_game_threaded = !GetDriverParamBool(param, "no_threads") && !GetDriverParamBool(param, "no_thread");
return nullptr;
return std::nullopt;
}
@ -252,7 +252,7 @@ void VideoDriver_CocoaOpenGL::ClearSystemSprites()
OpenGLBackend::Get()->ClearCursorCache();
}
const char *VideoDriver_CocoaOpenGL::AllocateContext(bool allow_software)
std::optional<std::string_view> VideoDriver_CocoaOpenGL::AllocateContext(bool allow_software)
{
[ OTTD_CGLLayer setAllowSoftware:allow_software ];

View File

@ -72,7 +72,7 @@ protected:
void GameSizeChanged();
const char *Initialize();
std::optional<std::string_view> Initialize();
void UpdateVideoModes();
@ -109,11 +109,11 @@ public:
VideoDriver_CocoaQuartz();
const char *Start(const StringList &param) override;
std::optional<std::string_view> Start(const StringList &param) override;
void Stop() override;
/** Return driver name */
const char *GetName() const override { return "cocoa"; }
std::string_view GetName() const override { return "cocoa"; }
void AllocateBackingStore(bool force = false) override;

View File

@ -122,7 +122,7 @@ void VideoDriver_Cocoa::Stop()
}
/** Common driver initialization. */
const char *VideoDriver_Cocoa::Initialize()
std::optional<std::string_view> VideoDriver_Cocoa::Initialize()
{
if (!MacOSVersionIsAtLeast(10, 7, 0)) return "The Cocoa video driver requires Mac OS X 10.7 or later.";
@ -130,12 +130,12 @@ const char *VideoDriver_Cocoa::Initialize()
_cocoa_video_started = true;
/* Don't create a window or enter fullscreen if we're just going to show a dialog. */
if (!CocoaSetupApplication()) return nullptr;
if (!CocoaSetupApplication()) return std::nullopt;
this->UpdateAutoResolution();
this->orig_res = _cur_resolution;
return nullptr;
return std::nullopt;
}
/**
@ -589,10 +589,10 @@ VideoDriver_CocoaQuartz::VideoDriver_CocoaQuartz()
this->cgcontext = nullptr;
}
const char *VideoDriver_CocoaQuartz::Start(const StringList &param)
std::optional<std::string_view> VideoDriver_CocoaQuartz::Start(const StringList &param)
{
const char *err = this->Initialize();
if (err != nullptr) return err;
auto err = this->Initialize();
if (err) return err;
int bpp = BlitterFactory::GetCurrentBlitter()->GetScreenDepth();
if (bpp != 8 && bpp != 32) {
@ -615,7 +615,7 @@ const char *VideoDriver_CocoaQuartz::Start(const StringList &param)
this->is_game_threaded = !GetDriverParamBool(param, "no_threads") && !GetDriverParamBool(param, "no_thread");
return nullptr;
return std::nullopt;
}

View File

@ -103,7 +103,7 @@ extern bool SafeLoad(const std::string &filename, SaveLoadOperation fop, Detaile
static FVideoDriver_Dedicated iFVideoDriver_Dedicated;
const char *VideoDriver_Dedicated::Start(const StringList &)
std::optional<std::string_view> VideoDriver_Dedicated::Start(const StringList &)
{
this->UpdateAutoResolution();
@ -131,7 +131,7 @@ const char *VideoDriver_Dedicated::Start(const StringList &)
#endif
Debug(driver, 1, "Loading dedicated server");
return nullptr;
return std::nullopt;
}
void VideoDriver_Dedicated::Stop()

View File

@ -15,7 +15,7 @@
/** The dedicated server video driver. */
class VideoDriver_Dedicated : public VideoDriver {
public:
const char *Start(const StringList &param) override;
std::optional<std::string_view> Start(const StringList &param) override;
void Stop() override;
@ -26,7 +26,7 @@ public:
bool ChangeResolution(int w, int h) override;
bool ToggleFullscreen(bool fullscreen) override;
const char *GetName() const override { return "dedicated"; }
std::string_view GetName() const override { return "dedicated"; }
bool HasGUI() const override { return false; }
};

View File

@ -19,7 +19,7 @@
/** Factory for the null video driver. */
static FVideoDriver_Null iFVideoDriver_Null;
const char *VideoDriver_Null::Start(const StringList &parm)
std::optional<std::string_view> VideoDriver_Null::Start(const StringList &parm)
{
#ifdef _MSC_VER
/* Disable the MSVC assertion message box. */
@ -39,7 +39,7 @@ const char *VideoDriver_Null::Start(const StringList &parm)
/* Do not render, nor blit */
Debug(misc, 1, "Forcing blitter 'null'...");
BlitterFactory::SelectBlitter("null");
return nullptr;
return std::nullopt;
}
void VideoDriver_Null::Stop() { }

View File

@ -18,7 +18,7 @@ private:
uint ticks; ///< Amount of ticks to run.
public:
const char *Start(const StringList &param) override;
std::optional<std::string_view> Start(const StringList &param) override;
void Stop() override;
@ -29,7 +29,7 @@ public:
bool ChangeResolution(int w, int h) override;
bool ToggleFullscreen(bool fullscreen) override;
const char *GetName() const override { return "null"; }
std::string_view GetName() const override { return "null"; }
bool HasGUI() const override { return false; }
};

View File

@ -462,9 +462,9 @@ void SetupDebugOutput()
* Create and initialize the singleton back-end class.
* @param get_proc Callback to get an OpenGL function from the OS driver.
* @param screen_res Current display resolution.
* @return nullptr on success, error message otherwise.
* @return std::nullopt on success, error message otherwise.
*/
/* static */ const char *OpenGLBackend::Create(GetOGLProcAddressProc get_proc, const Dimension &screen_res)
/* static */ std::optional<std::string_view> OpenGLBackend::Create(GetOGLProcAddressProc get_proc, const Dimension &screen_res)
{
if (OpenGLBackend::instance != nullptr) OpenGLBackend::Destroy();
@ -520,9 +520,9 @@ OpenGLBackend::~OpenGLBackend()
/**
* Check for the needed OpenGL functionality and allocate all resources.
* @param screen_res Current display resolution.
* @return Error string or nullptr if successful.
* @return Error string or std::nullopt if successful.
*/
const char *OpenGLBackend::Init(const Dimension &screen_res)
std::optional<std::string_view> OpenGLBackend::Init(const Dimension &screen_res)
{
if (!BindBasicInfoProcs()) return "OpenGL not supported";
@ -729,7 +729,7 @@ const char *OpenGLBackend::Init(const Dimension &screen_res)
this->PrepareContext();
(void)_glGetError(); // Clear errors.
return nullptr;
return std::nullopt;
}
void OpenGLBackend::PrepareContext()

View File

@ -72,7 +72,7 @@ private:
OpenGLBackend();
~OpenGLBackend();
const char *Init(const Dimension &screen_res);
std::optional<std::string_view> Init(const Dimension &screen_res);
bool InitShaders();
void InternalClearCursorCache();
@ -85,7 +85,7 @@ public:
{
return OpenGLBackend::instance;
}
static const char *Create(GetOGLProcAddressProc get_proc, const Dimension &screen_res);
static std::optional<std::string_view> Create(GetOGLProcAddressProc get_proc, const Dimension &screen_res);
static void Destroy();
void PrepareContext();

View File

@ -15,7 +15,7 @@
/** The SDL video driver using default SDL backend. */
class VideoDriver_SDL_Default : public VideoDriver_SDL_Base {
public:
const char *GetName() const override { return "sdl"; }
std::string_view GetName() const override { return "sdl"; }
protected:
bool AllocateBackingStore(int w, int h, bool force = false) override;

View File

@ -53,13 +53,13 @@ bool VideoDriver_SDL_OpenGL::CreateMainWindow(uint w, uint h, uint flags)
return this->VideoDriver_SDL_Base::CreateMainWindow(w, h, flags | SDL_WINDOW_OPENGL);
}
const char *VideoDriver_SDL_OpenGL::Start(const StringList &param)
std::optional<std::string_view> VideoDriver_SDL_OpenGL::Start(const StringList &param)
{
const char *error = VideoDriver_SDL_Base::Start(param);
if (error != nullptr) return error;
auto error = VideoDriver_SDL_Base::Start(param);
if (error) return error;
error = this->AllocateContext();
if (error != nullptr) {
if (error) {
this->Stop();
return error;
}
@ -81,7 +81,7 @@ const char *VideoDriver_SDL_OpenGL::Start(const StringList &param)
/* Main loop expects to start with the buffer unmapped. */
this->ReleaseVideoPointer();
return nullptr;
return std::nullopt;
}
void VideoDriver_SDL_OpenGL::Stop()
@ -105,7 +105,7 @@ void VideoDriver_SDL_OpenGL::ToggleVsync(bool vsync)
SDL_GL_SetSwapInterval(vsync);
}
const char *VideoDriver_SDL_OpenGL::AllocateContext()
std::optional<std::string_view> VideoDriver_SDL_OpenGL::AllocateContext()
{
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);

View File

@ -14,7 +14,7 @@ class VideoDriver_SDL_OpenGL : public VideoDriver_SDL_Base {
public:
VideoDriver_SDL_OpenGL() : VideoDriver_SDL_Base(true), gl_context(nullptr), anim_buffer(nullptr) {}
const char *Start(const StringList &param) override;
std::optional<std::string_view> Start(const StringList &param) override;
void Stop() override;
@ -31,7 +31,7 @@ public:
void ToggleVsync(bool vsync) override;
const char *GetName() const override { return "sdl-opengl"; }
std::string_view GetName() const override { return "sdl-opengl"; }
protected:
bool AllocateBackingStore(int w, int h, bool force = false) override;
@ -44,7 +44,7 @@ private:
void *gl_context; ///< OpenGL context.
uint8_t *anim_buffer; ///< Animation buffer from OpenGL back-end.
const char *AllocateContext();
std::optional<std::string_view> AllocateContext();
void DestroyContext();
};

View File

@ -503,34 +503,34 @@ bool VideoDriver_SDL_Base::PollEvent()
return true;
}
static const char *InitializeSDL()
static std::optional<std::string_view> InitializeSDL()
{
/* Check if the video-driver is already initialized. */
if (SDL_WasInit(SDL_INIT_VIDEO) != 0) return nullptr;
if (SDL_WasInit(SDL_INIT_VIDEO) != 0) return std::nullopt;
if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) return SDL_GetError();
return nullptr;
return std::nullopt;
}
const char *VideoDriver_SDL_Base::Initialize()
std::optional<std::string_view> VideoDriver_SDL_Base::Initialize()
{
this->UpdateAutoResolution();
const char *error = InitializeSDL();
if (error != nullptr) return error;
auto error = InitializeSDL();
if (error) return error;
FindResolutions();
Debug(driver, 2, "Resolution for display: {}x{}", _cur_resolution.width, _cur_resolution.height);
return nullptr;
return std::nullopt;
}
const char *VideoDriver_SDL_Base::Start(const StringList &param)
std::optional<std::string_view> VideoDriver_SDL_Base::Start(const StringList &param)
{
if (BlitterFactory::GetCurrentBlitter()->GetScreenDepth() == 0) return "Only real blitters supported";
const char *error = this->Initialize();
if (error != nullptr) return error;
auto error = this->Initialize();
if (error) return error;
#ifdef SDL_HINT_MOUSE_AUTO_CAPTURE
if (GetDriverParamBool(param, "no_mouse_capture")) {
@ -566,7 +566,7 @@ const char *VideoDriver_SDL_Base::Start(const StringList &param)
this->is_game_threaded = !GetDriverParamBool(param, "no_threads") && !GetDriverParamBool(param, "no_thread");
#endif
return nullptr;
return std::nullopt;
}
void VideoDriver_SDL_Base::Stop()

View File

@ -19,7 +19,7 @@ class VideoDriver_SDL_Base : public VideoDriver {
public:
VideoDriver_SDL_Base(bool uses_hardware_acceleration = false) : VideoDriver(uses_hardware_acceleration), sdl_window(nullptr), buffer_locked(false) {}
const char *Start(const StringList &param) override;
std::optional<std::string_view> Start(const StringList &param) override;
void Stop() override;
@ -41,7 +41,7 @@ public:
std::vector<int> GetListOfMonitorRefreshRates() override;
const char *GetInfoString() const override { return this->driver_info.c_str(); }
std::string_view GetInfoString() const override { return this->driver_info; }
protected:
struct SDL_Window *sdl_window; ///< Main SDL window.
@ -73,7 +73,7 @@ private:
void LoopOnce();
void MainLoopCleanup();
bool CreateMainSurface(uint w, uint h, bool resize);
const char *Initialize();
std::optional<std::string_view> Initialize();
#ifdef __EMSCRIPTEN__
/* Convert a constant pointer back to a non-constant pointer to a member function. */

View File

@ -576,7 +576,7 @@ bool VideoDriver_SDL::PollEvent()
return true;
}
const char *VideoDriver_SDL::Start(const StringList &param)
std::optional<std::string_view> VideoDriver_SDL::Start(const StringList &param)
{
char buf[30];
_use_hwpalette = GetDriverParamInt(param, "hw_palette", 2);
@ -607,7 +607,7 @@ const char *VideoDriver_SDL::Start(const StringList &param)
this->is_game_threaded = !GetDriverParamBool(param, "no_threads") && !GetDriverParamBool(param, "no_thread");
return nullptr;
return std::nullopt;
}
void VideoDriver_SDL::SetupKeyboard()

View File

@ -15,7 +15,7 @@
/** The SDL video driver. */
class VideoDriver_SDL : public VideoDriver {
public:
const char *Start(const StringList &param) override;
std::optional<std::string_view> Start(const StringList &param) override;
void Stop() override;
@ -31,7 +31,7 @@ public:
bool ClaimMousePointer() override;
const char *GetName() const override { return "sdl"; }
std::string_view GetName() const override { return "sdl"; }
protected:
void InputLoop() override;

View File

@ -174,7 +174,7 @@ public:
return Clamp(dpi_scale * 100, MIN_INTERFACE_SCALE, MAX_INTERFACE_SCALE);
}
virtual const char *GetInfoString() const
virtual std::string_view GetInfoString() const
{
return this->GetName();
}

View File

@ -1030,7 +1030,7 @@ void VideoDriver_Win32Base::UnlockVideoBuffer()
static FVideoDriver_Win32GDI iFVideoDriver_Win32GDI;
const char *VideoDriver_Win32GDI::Start(const StringList &param)
std::optional<std::string_view> VideoDriver_Win32GDI::Start(const StringList &param)
{
if (BlitterFactory::GetCurrentBlitter()->GetScreenDepth() == 0) return "Only real blitters supported";
@ -1044,7 +1044,7 @@ const char *VideoDriver_Win32GDI::Start(const StringList &param)
this->is_game_threaded = !GetDriverParamBool(param, "no_threads") && !GetDriverParamBool(param, "no_thread");
return nullptr;
return std::nullopt;
}
void VideoDriver_Win32GDI::Stop()
@ -1237,9 +1237,9 @@ static OGLProc GetOGLProcAddressCallback(const char *proc)
/**
* Set the pixel format of a window-
* @param dc Device context to set the pixel format of.
* @return nullptr on success, error message otherwise.
* @return std::nullopt on success, error message otherwise.
*/
static const char *SelectPixelFormat(HDC dc)
static std::optional<std::string_view> SelectPixelFormat(HDC dc)
{
PIXELFORMATDESCRIPTOR pfd = {
sizeof(PIXELFORMATDESCRIPTOR), // Size of this struct.
@ -1265,7 +1265,7 @@ static const char *SelectPixelFormat(HDC dc)
if (format == 0) return "No suitable pixel format found";
if (!SetPixelFormat(dc, format, &pfd)) return "Can't set pixel format";
return nullptr;
return std::nullopt;
}
/** Bind all WGL extension functions we need. */
@ -1280,7 +1280,7 @@ static void LoadWGLExtensions()
HDC dc = GetDC(wnd);
/* Set pixel format of the window. */
if (SelectPixelFormat(dc) == nullptr) {
if (SelectPixelFormat(dc) == std::nullopt) {
/* Create rendering context. */
HGLRC rc = wglCreateContext(dc);
if (rc != nullptr) {
@ -1320,7 +1320,7 @@ static void LoadWGLExtensions()
static FVideoDriver_Win32OpenGL iFVideoDriver_Win32OpenGL;
const char *VideoDriver_Win32OpenGL::Start(const StringList &param)
std::optional<std::string_view> VideoDriver_Win32OpenGL::Start(const StringList &param)
{
if (BlitterFactory::GetCurrentBlitter()->GetScreenDepth() == 0) return "Only real blitters supported";
@ -1332,8 +1332,8 @@ const char *VideoDriver_Win32OpenGL::Start(const StringList &param)
this->MakeWindow(_fullscreen);
/* Create and initialize OpenGL context. */
const char *err = this->AllocateContext();
if (err != nullptr) {
auto err = this->AllocateContext();
if (err) {
this->Stop();
_cur_resolution = old_res;
return err;
@ -1358,7 +1358,7 @@ const char *VideoDriver_Win32OpenGL::Start(const StringList &param)
this->is_game_threaded = !GetDriverParamBool(param, "no_threads") && !GetDriverParamBool(param, "no_thread");
return nullptr;
return std::nullopt;
}
void VideoDriver_Win32OpenGL::Stop()
@ -1391,12 +1391,12 @@ void VideoDriver_Win32OpenGL::ToggleVsync(bool vsync)
}
}
const char *VideoDriver_Win32OpenGL::AllocateContext()
std::optional<std::string_view> VideoDriver_Win32OpenGL::AllocateContext()
{
this->dc = GetDC(this->main_wnd);
const char *err = SelectPixelFormat(this->dc);
if (err != nullptr) return err;
auto err = SelectPixelFormat(this->dc);
if (err) return err;
HGLRC rc = nullptr;
@ -1438,7 +1438,7 @@ bool VideoDriver_Win32OpenGL::ToggleFullscreen(bool full_screen)
if (_screen.dst_ptr != nullptr) this->ReleaseVideoPointer();
this->DestroyContext();
bool res = this->VideoDriver_Win32Base::ToggleFullscreen(full_screen);
res &= this->AllocateContext() == nullptr;
res &= this->AllocateContext() == std::nullopt;
this->ClientSizeChanged(this->width, this->height, true);
return res;
}

View File

@ -79,13 +79,13 @@ class VideoDriver_Win32GDI : public VideoDriver_Win32Base {
public:
VideoDriver_Win32GDI() : dib_sect(nullptr), gdi_palette(nullptr), buffer_bits(nullptr) {}
const char *Start(const StringList &param) override;
std::optional<std::string_view> Start(const StringList &param) override;
void Stop() override;
bool AfterBlitterChange() override;
const char *GetName() const override { return "win32"; }
std::string_view GetName() const override { return "win32"; }
protected:
HBITMAP dib_sect; ///< System bitmap object referencing our rendering buffer.
@ -120,7 +120,7 @@ class VideoDriver_Win32OpenGL : public VideoDriver_Win32Base {
public:
VideoDriver_Win32OpenGL() : VideoDriver_Win32Base(true), dc(nullptr), gl_rc(nullptr), anim_buffer(nullptr), driver_info(this->GetName()) {}
const char *Start(const StringList &param) override;
std::optional<std::string_view> Start(const StringList &param) override;
void Stop() override;
@ -141,9 +141,9 @@ public:
void ToggleVsync(bool vsync) override;
const char *GetName() const override { return "win32-opengl"; }
std::string_view GetName() const override { return "win32-opengl"; }
const char *GetInfoString() const override { return this->driver_info.c_str(); }
std::string_view GetInfoString() const override { return this->driver_info; }
protected:
HDC dc; ///< Window device context.
@ -160,7 +160,7 @@ protected:
void ReleaseVideoPointer() override;
void PaletteChanged(HWND) override {}
const char *AllocateContext();
std::optional<std::string_view> AllocateContext();
void DestroyContext();
};