Skip to content

Commit 96c64ac

Browse files
committed
Handle irrlicht device destruction via shared pointer in DataHandler
Slightly cleaner code and fixes an issue when using sdl mixer audio backend and sdl device in irrlicht, where irrlicht's device was being terminated before the audio backend, thus calling SDL_Quit before the audio backend could do its cleanup
1 parent b4f2414 commit 96c64ac

File tree

6 files changed

+21
-19
lines changed

6 files changed

+21
-19
lines changed

gframe/data_handler.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class DataHandler {
2727
public:
2828
DataHandler();
2929
~DataHandler();
30-
irr::IrrlichtDevice* tmp_device{ nullptr };
30+
std::shared_ptr<irr::IrrlichtDevice> tmp_device{ nullptr };
3131
std::unique_ptr<DeckManager> deckManager{ nullptr };
3232
std::unique_ptr<RepoManager> gitManager{ nullptr };
3333
std::unique_ptr<GameConfig> configs{ nullptr };

gframe/game.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,13 @@ void Game::Initialize() {
139139
#endif
140140
filesystem = device->getFileSystem();
141141
filesystem->grab();
142-
skinSystem = new CGUISkinSystem(epro::format(EPRO_TEXT("{}/skin"), Utils::GetWorkingDirectory()).data(), device);
142+
skinSystem = new CGUISkinSystem(epro::format(EPRO_TEXT("{}/skin"), Utils::GetWorkingDirectory()).data(), device.get());
143143
if(!skinSystem)
144144
throw std::runtime_error("Couldn't create skin system");
145145
linePatternGL = 0x0f0f;
146146
menuHandler.prev_sel = -1;
147147
driver = device->getVideoDriver();
148-
imageManager.SetDevice(device);
148+
imageManager.SetDevice(device.get());
149149
imageManager.Initial();
150150
RefreshAiDecks();
151151
if(!discord.Initialize())

gframe/game.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -702,7 +702,7 @@ class Game final : public info_panel_elements, public main_menu_panel_elements,
702702
ClientField dField;
703703
DeckBuilder deckBuilder;
704704
MenuHandler menuHandler;
705-
irr::IrrlichtDevice* device;
705+
std::shared_ptr<irr::IrrlichtDevice> device;
706706
irr::video::IVideoDriver* driver;
707707
irr::scene::ISceneManager* smgr;
708708
irr::scene::ICameraSceneNode* camera;

gframe/gframe.cpp

+3-4
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ int edopro_main(const args_t& args) {
183183
do {
184184
Game _game{};
185185
ygo::mainGame = &_game;
186-
ygo::mainGame->device = std::exchange(data->tmp_device, nullptr);
186+
std::swap(data->tmp_device, ygo::mainGame->device);
187187
try {
188188
ygo::mainGame->Initialize();
189189
}
@@ -195,13 +195,13 @@ int edopro_main(const args_t& args) {
195195
return EXIT_FAILURE;
196196
}
197197
if(firstlaunch) {
198-
joystick = std::make_unique<JWrapper>(ygo::mainGame->device);
198+
joystick = std::make_unique<JWrapper>(ygo::mainGame->device.get());
199199
gJWrapper = joystick.get();
200200
firstlaunch = false;
201201
CheckArguments(args);
202202
}
203203
reset = ygo::mainGame->MainLoop();
204-
data->tmp_device = ygo::mainGame->device;
204+
std::swap(data->tmp_device, ygo::mainGame->device);
205205
if(reset) {
206206
auto device = data->tmp_device;
207207
device->setEventReceiver(nullptr);
@@ -216,6 +216,5 @@ int edopro_main(const args_t& args) {
216216
env->clear();
217217
}
218218
} while(reset);
219-
data->tmp_device->drop();
220219
return EXIT_SUCCESS;
221220
}

gframe/utils_gui.cpp

+8-6
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ static inline irr::video::E_DRIVER_TYPE getDefaultDriver(irr::E_DEVICE_TYPE devi
7979
#endif
8080
}
8181

82-
irr::IrrlichtDevice* GUIUtils::CreateDevice(GameConfig* configs) {
82+
std::shared_ptr<irr::IrrlichtDevice> GUIUtils::CreateDevice(GameConfig* configs) {
8383
irr::SIrrlichtCreationParameters params{};
8484
params.AntiAlias = configs->antialias;
8585
params.Vsync = (!EDOPRO_MACOS) && configs->vsync;
@@ -181,10 +181,12 @@ irr::IrrlichtDevice* GUIUtils::CreateDevice(GameConfig* configs) {
181181
EDOPRO_SetWindowRect(driver->getExposedVideoData().OpenGLOSX.Window, gGameConfig->windowStruct.data());
182182
#endif
183183
device->getLogger()->setLogLevel(irr::ELL_ERROR);
184-
return device;
184+
return std::shared_ptr<irr::IrrlichtDevice>(device, [](irr::IrrlichtDevice* ptr){
185+
ptr->drop();
186+
});
185187
}
186188

187-
void GUIUtils::ChangeCursor(irr::IrrlichtDevice* device, /*irr::gui::ECURSOR_ICON*/ int _icon) {
189+
void GUIUtils::ChangeCursor(std::shared_ptr<irr::IrrlichtDevice>& device, /*irr::gui::ECURSOR_ICON*/ int _icon) {
188190
#if !EDOPRO_ANDROID && !EDOPRO_IOS
189191
auto icon = static_cast<irr::gui::ECURSOR_ICON>(_icon);
190192
auto cursor = device->getCursorControl();
@@ -208,7 +210,7 @@ bool GUIUtils::TakeScreenshot(irr::IrrlichtDevice* device) {
208210
return written;
209211
}
210212
#if (IRRLICHT_VERSION_MAJOR==1 && IRRLICHT_VERSION_MINOR==9)
211-
void GUIUtils::ToggleFullscreen(irr::IrrlichtDevice* device, bool& fullscreen) {
213+
void GUIUtils::ToggleFullscreen(std::shared_ptr<irr::IrrlichtDevice>& device, bool& fullscreen) {
212214
(void)fullscreen;
213215
#if EDOPRO_MACOS
214216
EDOPRO_ToggleFullScreen();
@@ -226,7 +228,7 @@ static BOOL CALLBACK callback(HMONITOR hMon, HDC hdc, LPRECT lprcMonitor, LPARAM
226228
return TRUE;
227229
}
228230
#endif
229-
void GUIUtils::ToggleFullscreen(irr::IrrlichtDevice* device, bool& fullscreen) {
231+
void GUIUtils::ToggleFullscreen(std::shared_ptr<irr::IrrlichtDevice>& device, bool& fullscreen) {
230232
(void)fullscreen;
231233
#if EDOPRO_MACOS
232234
EDOPRO_ToggleFullScreen();
@@ -395,7 +397,7 @@ void GUIUtils::ToggleSwapInterval(irr::video::IVideoDriver* driver, int interval
395397
SetSwapInterval(driver, interval);
396398
}
397399

398-
std::string GUIUtils::SerializeWindowPosition(irr::IrrlichtDevice* device) {
400+
std::string GUIUtils::SerializeWindowPosition(std::shared_ptr<irr::IrrlichtDevice>& device) {
399401
#if EDOPRO_WINDOWS
400402
auto hWnd = GetWindowHandle(device->getVideoDriver());
401403
WINDOWPLACEMENT wp;

gframe/utils_gui.h

+6-5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define UTILS_GUI_H
33

44
#include "text_types.h"
5+
#include <memory>
56

67
namespace irr {
78
class IrrlichtDevice;
@@ -16,13 +17,13 @@ struct GameConfig;
1617

1718
namespace GUIUtils {
1819

19-
irr::IrrlichtDevice* CreateDevice(GameConfig* configs);
20-
void ChangeCursor(irr::IrrlichtDevice* device, /*irr::gui::ECURSOR_ICON*/ int icon);
21-
bool TakeScreenshot(irr::IrrlichtDevice* device);
22-
void ToggleFullscreen(irr::IrrlichtDevice* device, bool& fullscreen);
20+
std::shared_ptr<irr::IrrlichtDevice> CreateDevice(GameConfig* configs);
21+
void ChangeCursor(std::shared_ptr<irr::IrrlichtDevice>& device, /*irr::gui::ECURSOR_ICON*/ int icon);
22+
bool TakeScreenshot(std::shared_ptr<irr::IrrlichtDevice>& device);
23+
void ToggleFullscreen(std::shared_ptr<irr::IrrlichtDevice>& device, bool& fullscreen);
2324
void ShowErrorWindow(epro::stringview context, epro::stringview message);
2425
void ToggleSwapInterval(irr::video::IVideoDriver* driver, int interval);
25-
std::string SerializeWindowPosition(irr::IrrlichtDevice* device);
26+
std::string SerializeWindowPosition(std::shared_ptr<irr::IrrlichtDevice>& device);
2627

2728
}
2829

0 commit comments

Comments
 (0)