Skip to content

Commit

Permalink
Merge pull request #395 from orbitersim/startup_cleanup
Browse files Browse the repository at this point in the history
Solving Orbiter startup issues
  • Loading branch information
jarmonik authored Jan 9, 2024
2 parents 205f8dd + 1d922a8 commit c31ea84
Show file tree
Hide file tree
Showing 12 changed files with 87 additions and 38 deletions.
12 changes: 7 additions & 5 deletions OVP/D3D9Client/D3D9Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,14 +196,15 @@ DLLCLBK void ExitModule(HINSTANCE hDLL)
delete Config;
delete g_pConst;

DebugControls::Release();
AtmoControls::Release();

if (g_client) {
oapiUnregisterGraphicsClient(g_client);
delete g_client;
g_client = 0;
}

DebugControls::Release();
AtmoControls::Release();

#ifdef _NVAPI_H
if (bNVAPI) if (NvAPI_Unload()==NVAPI_OK) LogAlw("[nVidia API Unloaded]");
#endif
Expand Down Expand Up @@ -329,16 +330,17 @@ bool D3D9Client::clbkInitialise()
}
else {
oapiWriteLog("[D3D9][ERROR] Failed to create DirectX9");
FailedDeviceError();
return false;
}

// Perform default setup
if (GraphicsClient::clbkInitialise()==false) return false;

//Create the Launchpad video tab interface
oapiWriteLog("[D3D9] Initialize VideoTab...");
vtab = new VideoTab(this, ModuleInstance(), OrbiterInstance(), LaunchpadVideoTab());
oapiWriteLog("[D3D9] VideoTab Created...");
return true;
return vtab->Initialise();
}


Expand Down
4 changes: 2 additions & 2 deletions OVP/D3D9Client/D3D9Frame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,10 @@ HRESULT CD3DFramework9::Initialize(HWND _hWnd, GraphicsClient::VIDEODATA *vData)
bAAEnabled = (Config->SceneAntialias != 0);
bIsFullscreen = vData->fullscreen;
bNoVSync = vData->novsync;
dwFSMode = (vData->modeidx>>8)&0xFF;
dwFSMode = vData->style;
bGDIBB = vData->trystencil;
Adapter = vData->deviceidx;
Mode = vData->modeidx&0xFF;
Mode = vData->modeidx;

LogAlw("[VideoConfiguration] Adapter=%u, ModeIndex=%u", Adapter, Mode);

Expand Down
9 changes: 9 additions & 0 deletions OVP/D3D9Client/Log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ void MissingRuntimeError()
"D3D9Client Initialization Failed", MB_OK);
}

//-------------------------------------------------------------------------------------------
//
void FailedDeviceError()
{
MessageBoxA(NULL,
"DirectX9 Device Failed. Try to enable EnableDX12Wrapper from D3D9Client.cfg",
"D3D9Client Initialization Failed", MB_OK);
}

//-------------------------------------------------------------------------------------------
//
void RuntimeError(const char* File, const char* Fnc, UINT Line)
Expand Down
1 change: 1 addition & 0 deletions OVP/D3D9Client/Log.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ double D3D9GetTime();
void D3D9SetTime(D3D9Time &inout, double ref);

void MissingRuntimeError();
void FailedDeviceError();
void LogAttribs(DWORD attrib, DWORD w, DWORD h, LPCSTR origin);

#define HALT() { RuntimeError(__FILE__,__FUNCTION__,__LINE__); }
Expand Down
46 changes: 25 additions & 21 deletions OVP/D3D9Client/VideoTab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ VideoTab::VideoTab(D3D9Client *gc, HINSTANCE _hInst, HINSTANCE _hOrbiterInst, HW
hTab = hVideoTab;
aspect_idx = 0;
SelectedAdapterIdx = 0;

Initialise();
}

VideoTab::~VideoTab()
Expand Down Expand Up @@ -160,7 +158,7 @@ BOOL VideoTab::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
// ==============================================================
// Initialise the Launchpad "video" tab

void VideoTab::Initialise()
bool VideoTab::Initialise()
{
D3DDISPLAYMODE mode, curMode;
D3DADAPTER_IDENTIFIER9 info;
Expand All @@ -171,7 +169,7 @@ void VideoTab::Initialise()
data->trystencil = false;

SendDlgItemMessage(hTab, IDC_VID_DEVICE, CB_RESETCONTENT, 0, 0);
SendDlgItemMessageA(hTab, IDC_VID_MODE, CB_RESETCONTENT, 0, 0);
SendDlgItemMessage(hTab, IDC_VID_MODE, CB_RESETCONTENT, 0, 0);
SendDlgItemMessage(hTab, IDC_VID_BPP, CB_RESETCONTENT, 0, 0);

ScanAtmoCfgs();
Expand All @@ -181,7 +179,8 @@ void VideoTab::Initialise()

if (nAdapter == 0) {
LogErr("VideoTab::Initialize() No DirectX9 Adapters Found");
return;
FailedDeviceError();
return false;
}

if (data->deviceidx < 0 || (data->deviceidx)>=nAdapter) data->deviceidx = 0;
Expand All @@ -203,6 +202,7 @@ void VideoTab::Initialise()

if (nModes == 0) {
LogErr("VideoTab::Initialize() No Display Modes Available");
FailedDeviceError();
}

for (UINT k=0;k<nModes;k++) {
Expand All @@ -216,13 +216,13 @@ void VideoTab::Initialise()
SendDlgItemMessageA(hTab, IDC_VID_BPP, CB_ADDSTRING, 0, (LPARAM)"True Full Screen (no alt-tab)");
SendDlgItemMessageA(hTab, IDC_VID_BPP, CB_ADDSTRING, 0, (LPARAM)"Full Screen Window");
SendDlgItemMessageA(hTab, IDC_VID_BPP, CB_ADDSTRING, 0, (LPARAM)"Window with Taskbar");
SendDlgItemMessageA(hTab, IDC_VID_BPP, CB_SETCURSEL, (data->modeidx>>8)&0xFF, 0);
SendDlgItemMessageA(hTab, IDC_VID_BPP, CB_SETCURSEL, data->style, 0);

//SetWindowText(GetDlgItem(hTab, IDC_VID_STATIC5), "Resolution");
SetWindowText(GetDlgItem(hTab, IDC_VID_STATIC6), "Full Screen Mode");


SendDlgItemMessage(hTab, IDC_VID_MODE, CB_SETCURSEL, data->modeidx&0xFF, 0);
SendDlgItemMessage(hTab, IDC_VID_MODE, CB_SETCURSEL, data->modeidx, 0);
SendDlgItemMessage(hTab, IDC_VID_VSYNC, BM_SETCHECK, data->novsync ? BST_CHECKED : BST_UNCHECKED, 0);

SetWindowText(GetDlgItem(hTab, IDC_VID_WIDTH), std::to_string(data->winw).c_str());
Expand All @@ -242,13 +242,15 @@ void VideoTab::Initialise()
SendDlgItemMessage(hTab, IDC_VID_ENUM, BM_SETCHECK, data->forceenum, 0);
SendDlgItemMessage(hTab, IDC_VID_PAGEFLIP, BM_SETCHECK, data->pageflip, 0); // Full scrren Window

SelectAdapter(data->deviceidx);
bool bRet = SelectAdapter(data->deviceidx);

SelectFullscreen(data->fullscreen);

ShowWindow (GetDlgItem (hTab, IDC_VID_INFO), SW_SHOW);

SetWindowText(GetDlgItem(hTab, IDC_VID_INFO), "Advanced");
SetWindowText(GetDlgItem(hTab, IDC_VID_INFO), "Advanced");

return bRet;
}


Expand All @@ -258,32 +260,32 @@ void VideoTab::SelectMode(DWORD index)
{
GraphicsClient::VIDEODATA *data = gclient->GetVideoData();
SendDlgItemMessage(hTab, IDC_VID_MODE, CB_GETITEMDATA, index, 0);
data->modeidx = index + data->modeidx&0xFF00;
data->modeidx = index;
}


// ==============================================================
// Respond to user adapter selection
//
void VideoTab::SelectAdapter(DWORD index)
bool VideoTab::SelectAdapter(DWORD index)
{

SelectedAdapterIdx = index;

GraphicsClient::VIDEODATA *data = gclient->GetVideoData();

// Create the Direct3D9 ---------------------------------------------
//

if (g_pD3DObject == NULL) LogErr("VideoTab::SelectAdapter(%u) Direct3DCreate9 Failed",index);
if (g_pD3DObject == NULL) {
LogErr("VideoTab::SelectAdapter(%u) Direct3DCreate9 Failed", index);
return false;
}
else {

char cbuf[32];
D3DDISPLAYMODE mode, curMode;

if (g_pD3DObject->GetAdapterCount()<=index) {
LogErr("Adapter Index out of range");
return;
return false;
}

HR(g_pD3DObject->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &curMode));
Expand All @@ -293,8 +295,7 @@ void VideoTab::SelectAdapter(DWORD index)
DWORD nModes = g_pD3DObject->GetAdapterModeCount(index, D3DFMT_X8R8G8B8);

if (nModes == 0) {
LogErr("VideoTab::SelectAdapter() No Display Modes Available");
return;
LogErr("VideoTab::SelectAdapter() No Display Modes Available");
}

for (DWORD k=0;k<nModes;k++) {
Expand All @@ -304,8 +305,10 @@ void VideoTab::SelectAdapter(DWORD index)
SendDlgItemMessageA(hTab, IDC_VID_MODE, CB_SETITEMDATA, k, (LPARAM)(mode.Height<<16 | mode.Width));
}

SendDlgItemMessage(hTab, IDC_VID_MODE, CB_SETCURSEL, data->modeidx&0xFF, 0);
SendDlgItemMessage(hTab, IDC_VID_MODE, CB_SETCURSEL, data->modeidx, 0);
}

return true;
}


Expand Down Expand Up @@ -394,8 +397,9 @@ void VideoTab::UpdateConfigData()
GraphicsClient::VIDEODATA *data = gclient->GetVideoData();

// device parameters
data->deviceidx = (int)SendDlgItemMessageA(hTab, IDC_VID_DEVICE, CB_GETCURSEL, 0, 0);
data->modeidx = (int)SendDlgItemMessage(hTab, IDC_VID_MODE, CB_GETCURSEL, 0, 0) + ((int)SendDlgItemMessageA(hTab, IDC_VID_BPP, CB_GETCURSEL, 0, 0)<<8);
data->deviceidx = (int)SendDlgItemMessage (hTab, IDC_VID_DEVICE, CB_GETCURSEL, 0, 0);
data->modeidx = (int)SendDlgItemMessage (hTab, IDC_VID_MODE, CB_GETCURSEL, 0, 0);
data->style = SendDlgItemMessage (hTab, IDC_VID_BPP, CB_GETCURSEL, 0, 0);
data->fullscreen = (SendDlgItemMessage (hTab, IDC_VID_FULL, BM_GETCHECK, 0, 0) == BST_CHECKED);
data->novsync = (SendDlgItemMessage (hTab, IDC_VID_VSYNC, BM_GETCHECK, 0, 0) == BST_CHECKED);
data->pageflip = (SendDlgItemMessage (hTab, IDC_VID_PAGEFLIP, BM_GETCHECK, 0, 0) == BST_CHECKED);
Expand Down
8 changes: 3 additions & 5 deletions OVP/D3D9Client/VideoTab.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,13 @@ class VideoTab {
void UpdateConfigData();
// copy dialog state back to parameter structure

protected:

void Initialise();
bool Initialise();
// Initialise dialog elements

protected:
void SelectFullscreen(bool);
void SelectMode(DWORD index);
void SelectAdapter(DWORD index);
bool SelectAdapter(DWORD index);
// Update dialog after user device selection

void SelectWidth();
Expand All @@ -52,7 +51,6 @@ class VideoTab {
void SaveSetupState(HWND hWnd);
void ScanAtmoCfgs();
bool GetConfigName(const char* file, string& cfg, string& planet);
void LoadAtmoCfg();

oapi::D3D9Client *gclient;
HINSTANCE hOrbiterInst; // orbiter instance handle
Expand Down
10 changes: 6 additions & 4 deletions Orbitersdk/include/GraphicsAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -842,10 +842,12 @@ class OAPIFUNC GraphicsClient: public Module {
bool trystencil; ///< stencil buffer flag
bool novsync; ///< no vsync flag
bool pageflip; ///< allow page flipping in fullscreen
int deviceidx; ///< video device index
int modeidx; ///< video mode index
int winw; ///< window width
int winh; ///< window height
int deviceidx; ///< video device (adapter) index
int modeidx; ///< video mode index (fullscreen resolution)
int winw; ///< window/screen width
int winh; ///< window/screen height
int outputidx; ///< video output
int style; ///< true fullscreen, fulscreen window, window with taskbar
};

/**
Expand Down
7 changes: 7 additions & 0 deletions Src/Orbiter/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ CFG_RECPLAYPRM CfgRecPlayPrm_default = {
CFG_DEVPRM CfgDevPrm_default = {
-1, // Device_idx (-1=undefined)
0, // Device_mode
0,
0,
true, // bForceEnum (enumerate devices at each simulation start)
false, // bFullscreen (default to window mode)
false, // bStereo (no stereo support)
Expand Down Expand Up @@ -515,6 +517,8 @@ bool Config::Load(const char *fname)
// Device information
GetInt (ifs, "DeviceIndex", CfgDevPrm.Device_idx);
if (GetInt (ifs, "ModeIndex", i)) CfgDevPrm.Device_mode = (DWORD)i;
if (GetInt(ifs, "OutputIndex", i)) CfgDevPrm.Device_out = (DWORD)i;
if (GetInt(ifs, "Style", i)) CfgDevPrm.Device_style = (DWORD)i;
GetBool (ifs, "DeviceForceEnum", CfgDevPrm.bForceEnum);
GetBool (ifs, "Fullscreen", CfgDevPrm.bFullscreen);
GetBool (ifs, "Stereo", CfgDevPrm.bStereo);
Expand Down Expand Up @@ -1219,6 +1223,9 @@ BOOL Config::Write (const char *fname) const
ofs << "\n; === Device settings ===\n";
ofs << "DeviceIndex = " << CfgDevPrm.Device_idx << '\n';
ofs << "ModeIndex = " << CfgDevPrm.Device_mode << '\n';
ofs << "OutputIndex = " << CfgDevPrm.Device_out << '\n';
ofs << "Style = " << CfgDevPrm.Device_style << '\n';

if (CfgDevPrm.bForceEnum != CfgDevPrm_default.bForceEnum || bEchoAll)
ofs << "DeviceForceEnum = " << BoolStr (CfgDevPrm.bForceEnum) << '\n';
if (CfgDevPrm.bFullscreen != CfgDevPrm_default.bFullscreen || bEchoAll)
Expand Down
2 changes: 2 additions & 0 deletions Src/Orbiter/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,8 @@ struct CFG_RECPLAYPRM {
struct CFG_DEVPRM {
int Device_idx; // index of default device
DWORD Device_mode; // index of default fullscreen mode
DWORD Device_out; // device output
DWORD Device_style; // Rendering layout
bool bForceEnum; // force enumeration, bypass device.dat
bool bFullscreen; // use window mode
bool bStereo; // use stereo mode
Expand Down
4 changes: 4 additions & 0 deletions Src/Orbiter/GraphicsAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ GraphicsClient::GraphicsClient (HINSTANCE hInstance): Module (hInstance)
VideoData.pageflip = true;
VideoData.deviceidx = -1;
VideoData.modeidx = 0;
VideoData.outputidx = 0;
VideoData.style = 1;
VideoData.winw = 1024;
VideoData.winh = 768;
surfBltTgt = RENDERTGT_NONE;
Expand Down Expand Up @@ -98,6 +100,8 @@ bool GraphicsClient::clbkInitialise ()
VideoData.novsync = cfg->CfgDevPrm.bNoVsync;
VideoData.pageflip = cfg->CfgDevPrm.bPageflip;
VideoData.deviceidx = cfg->CfgDevPrm.Device_idx;
VideoData.outputidx = cfg->CfgDevPrm.Device_out;
VideoData.style = cfg->CfgDevPrm.Device_style;
VideoData.modeidx = (int)cfg->CfgDevPrm.Device_mode;
VideoData.winw = (int)cfg->CfgDevPrm.WinW;
VideoData.winh = (int)cfg->CfgDevPrm.WinH;
Expand Down
18 changes: 17 additions & 1 deletion Src/Orbiter/Orbiter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,22 @@ HINSTANCE Orbiter::LoadModule (const char *path, const char *name)
}
}

#ifndef INLINEGRAPHICS
// Can't initialize DirectX in DllMain(), let's do it over here (jarmonik 28.12.2023)
if (hDLL) {
if (register_module == gclient && gclient != NULL) {
if (gclient->clbkInitialise() == false) {
// If graphics initialization fails remove client
RemoveGraphicsClient(gclient);
FreeLibrary(hDLL);
LOGOUT_ERR("Client Initialization Failed. Unloading %s", name);
hDLL = NULL;
return NULL;
}
}
}
#endif

if (hDLL) {
DLLModule module = { hDLL, register_module ? register_module : new oapi::Module(hDLL), std::string(name), !register_module };
// If the DLL doesn't provide a Module interface, create a default one which provides the legacy callbacks
Expand Down Expand Up @@ -2873,7 +2889,7 @@ bool Orbiter::AttachGraphicsClient (oapi::GraphicsClient *gc)
if (gclient) return false; // another client is already attached
register_module = gc;
gclient = gc;
gclient->clbkInitialise();
//gclient->clbkInitialise(); // Cannot initialize with-in DllMain() will result DirectX device failure.
return true;
}

Expand Down
4 changes: 4 additions & 0 deletions Src/Orbiter/TabVideo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ void orbiter::DefVideoTab::SetConfig (Config *cfg)
cfg->CfgDevPrm.WinH = data->winh;
cfg->CfgDevPrm.Device_idx = data->deviceidx;
cfg->CfgDevPrm.Device_mode = data->modeidx;
cfg->CfgDevPrm.Device_out = data->outputidx;
cfg->CfgDevPrm.Device_style= data->style;
} else {
// should not be required
cfg->CfgDevPrm.bFullscreen = false;
Expand All @@ -138,6 +140,8 @@ void orbiter::DefVideoTab::SetConfig (Config *cfg)
cfg->CfgDevPrm.WinH = 300;
cfg->CfgDevPrm.Device_idx = 0;
cfg->CfgDevPrm.Device_mode = 0;
cfg->CfgDevPrm.Device_out = 0;
cfg->CfgDevPrm.Device_style= 1;
}
cfg->CfgDevPrm.bStereo = false; // not currently set
}
Expand Down

0 comments on commit c31ea84

Please sign in to comment.