Skip to content

Commit 18ef7a0

Browse files
committed
Refactor available video modes processing mechanism
1 parent 3e6922e commit 18ef7a0

File tree

5 files changed

+81
-165
lines changed

5 files changed

+81
-165
lines changed

src/Include/xrAPI/xrAPI.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ class XRAPI_API EngineGlobalEnvironment
2121
IRender* Render;
2222
IDebugRender* DRender;
2323
CDUInterface* DU;
24-
xr_token* vid_mode_token;
2524
IUIRender* UIRender;
2625
CGameMtlLibrary* PGMLib;
2726
IRenderFactory* RenderFactory;

src/Layers/xrRender/HW.cpp

Lines changed: 29 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,11 @@
44
#include "xrEngine/XR_IOConsole.h"
55
#include "xrCore/xr_token.h"
66

7-
#ifndef _EDITOR
7+
extern ENGINE_API xr_vector<xr_token> AvailableVideoModes;
8+
89
void fill_vid_mode_list(CHW* _hw);
910
void free_vid_mode_list();
1011

11-
void fill_render_mode_list();
12-
void free_render_mode_list();
13-
#else
14-
void fill_vid_mode_list(CHW* _hw) {}
15-
void free_vid_mode_list() {}
16-
void fill_render_mode_list() {}
17-
void free_render_mode_list() {}
18-
#endif
19-
2012
CHW HW;
2113

2214
CHW::CHW()
@@ -240,9 +232,7 @@ void CHW::DestroyDevice()
240232

241233
DestroyD3D();
242234

243-
#ifndef _EDITOR
244235
free_vid_mode_list();
245-
#endif
246236
}
247237

248238
//////////////////////////////////////////////////////////////////////
@@ -348,9 +338,9 @@ void CHW::selectResolution(u32& dwWidth, u32& dwHeight, BOOL bWindowed)
348338
string64 buff;
349339
xr_sprintf(buff, sizeof(buff), "%dx%d", psCurrentVidMode[0], psCurrentVidMode[1]);
350340

351-
if (_ParseItem(buff, GEnv.vid_mode_token) == u32(-1)) // not found
341+
if (_ParseItem(buff, AvailableVideoModes.data()) == u32(-1)) // not found
352342
{ // select safe
353-
xr_sprintf(buff, sizeof(buff), "vid_mode %s", GEnv.vid_mode_token[0].name);
343+
xr_sprintf(buff, sizeof(buff), "vid_mode %s", AvailableVideoModes[0].name);
354344
Console->Execute(buff);
355345
}
356346

@@ -558,73 +548,55 @@ void CHW::updateWindowProps(HWND m_hWnd)
558548
#endif
559549
}
560550

561-
struct _uniq_mode
551+
struct uniqueRenderingMode
562552
{
563-
_uniq_mode(LPCSTR v) : _val(v) {}
564-
LPCSTR _val;
565-
bool operator()(LPCSTR _other) { return !xr_stricmp(_val, _other); }
553+
uniqueRenderingMode(pcstr v) : value(v) {}
554+
pcstr value;
555+
bool operator()(const xr_token other) const { return !xr_stricmp(value, other.name);}
566556
};
567557

568-
#ifndef _EDITOR
569-
570558
void free_vid_mode_list()
571559
{
572-
for (int i = 0; GEnv.vid_mode_token[i].name; i++)
573-
{
574-
xr_free(GEnv.vid_mode_token[i].name);
575-
}
576-
xr_free(GEnv.vid_mode_token);
577-
GEnv.vid_mode_token = nullptr;
560+
for (auto& mode : AvailableVideoModes)
561+
xr_free(mode.name);
562+
AvailableVideoModes.clear();
578563
}
579564

580565
void fill_vid_mode_list(CHW* _hw)
581566
{
582-
if (GEnv.vid_mode_token != nullptr)
567+
if (!AvailableVideoModes.empty())
583568
return;
584-
xr_vector<LPCSTR> _tmp;
585-
xr_vector<D3DDISPLAYMODE> modes;
569+
570+
xr_vector<D3DDISPLAYMODE> displayModes;
586571

587572
// Get the number of display modes available
588-
UINT cnt = _hw->pD3D->GetAdapterModeCount(_hw->DevAdapter, _hw->Caps.fTarget);
573+
const auto cnt = _hw->pD3D->GetAdapterModeCount(_hw->DevAdapter, _hw->Caps.fTarget);
589574

590575
// Get the list of display modes
591-
modes.resize(cnt);
576+
displayModes.resize(cnt);
592577
for (auto i = 0; i < cnt; ++i)
593-
_hw->pD3D->EnumAdapterModes(_hw->DevAdapter, _hw->Caps.fTarget, i, &modes[i]);
578+
_hw->pD3D->EnumAdapterModes(_hw->DevAdapter, _hw->Caps.fTarget, i, &displayModes[i]);
594579

595-
for (auto &i : modes)
580+
int i = 0;
581+
auto& AVM = AvailableVideoModes;
582+
for (const auto& it : displayModes)
596583
{
597584
string32 str;
598585

599-
if (i.Width < 800)
586+
if (it.Width < 800)
600587
continue;
601588

602-
xr_sprintf(str, sizeof(str), "%dx%d", i.Width, i.Height);
589+
xr_sprintf(str, sizeof(str), "%dx%d", it.Width, it.Height);
603590

604-
if (_tmp.end() != std::find_if(_tmp.begin(), _tmp.end(), _uniq_mode(str)))
591+
if (AVM.cend() != std::find_if(AVM.cbegin(), AVM.cend(), uniqueRenderingMode(str)))
605592
continue;
606593

607-
_tmp.push_back(nullptr);
608-
_tmp.back() = xr_strdup(str);
594+
AVM.emplace_back(xr_token(xr_strdup(str), i));
595+
++i;
609596
}
597+
AVM.emplace_back(xr_token(nullptr, -1));
610598

611-
u32 _cnt = _tmp.size() + 1;
612-
613-
GEnv.vid_mode_token = xr_alloc<xr_token>(_cnt);
614-
615-
GEnv.vid_mode_token[_cnt - 1].id = -1;
616-
GEnv.vid_mode_token[_cnt - 1].name = nullptr;
617-
618-
#ifdef DEBUG
619-
Msg("Available video modes[%d]:", _tmp.size());
620-
#endif // DEBUG
621-
for (auto i = 0; i < _tmp.size(); ++i)
622-
{
623-
GEnv.vid_mode_token[i].id = i;
624-
GEnv.vid_mode_token[i].name = _tmp[i];
625-
#ifdef DEBUG
626-
Msg("[%s]", _tmp[i]);
627-
#endif // DEBUG
628-
}
599+
Msg("Available video modes[%d]:", AVM.size());
600+
for (const auto& mode : AVM)
601+
Msg("[%s]", mode.name);
629602
}
630-
#endif

src/Layers/xrRenderDX10/dx10HW.cpp

Lines changed: 29 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,11 @@
77
#include "StateManager/dx10SamplerStateCache.h"
88
#include "StateManager/dx10StateCache.h"
99

10-
#ifndef _EDITOR
10+
extern ENGINE_API xr_vector<xr_token> AvailableVideoModes;
11+
1112
void fill_vid_mode_list(CHW* _hw);
1213
void free_vid_mode_list();
1314

14-
void fill_render_mode_list();
15-
void free_render_mode_list();
16-
#else
17-
void fill_vid_mode_list(CHW* _hw) {}
18-
void free_vid_mode_list() {}
19-
void fill_render_mode_list() {}
20-
void free_render_mode_list() {}
21-
#endif
22-
2315
CHW HW;
2416

2517
CHW::CHW()
@@ -186,7 +178,7 @@ void CHW::CreateDevice(HWND m_hWnd, bool move_window)
186178
// Create render target and depth-stencil views here
187179
UpdateViews();
188180

189-
size_t memory = Desc.DedicatedVideoMemory;
181+
const auto memory = Desc.DedicatedVideoMemory;
190182
Msg("* Texture memory: %d M", memory / (1024 * 1024));
191183
//Msg("* DDI-level: %2.1f", float(D3DXGetDriverLevel(pDevice)) / 100.f);
192184
#ifndef _EDITOR
@@ -228,9 +220,7 @@ void CHW::DestroyDevice()
228220

229221
DestroyD3D();
230222

231-
#ifndef _EDITOR
232223
free_vid_mode_list();
233-
#endif
234224
}
235225

236226
//////////////////////////////////////////////////////////////////////
@@ -286,9 +276,9 @@ void CHW::selectResolution(u32& dwWidth, u32& dwHeight, BOOL bWindowed)
286276
string64 buff;
287277
xr_sprintf(buff, sizeof(buff), "%dx%d", psCurrentVidMode[0], psCurrentVidMode[1]);
288278

289-
if (_ParseItem(buff, GEnv.vid_mode_token) == u32(-1)) // not found
279+
if (_ParseItem(buff, AvailableVideoModes.data()) == u32(-1)) // not found
290280
{ // select safe
291-
xr_sprintf(buff, sizeof(buff), "vid_mode %s", GEnv.vid_mode_token[0].name);
281+
xr_sprintf(buff, sizeof(buff), "vid_mode %s", AvailableVideoModes[0].name);
292282
Console->Execute(buff);
293283
}
294284

@@ -418,31 +408,26 @@ void CHW::updateWindowProps(HWND m_hWnd)
418408
SetForegroundWindow(m_hWnd);
419409
}
420410

421-
struct _uniq_mode
411+
struct uniqueRenderingMode
422412
{
423-
_uniq_mode(LPCSTR v) : _val(v) {}
424-
LPCSTR _val;
425-
bool operator()(LPCSTR _other) { return !xr_stricmp(_val, _other); }
413+
uniqueRenderingMode(pcstr v) : value(v) {}
414+
pcstr value;
415+
bool operator()(const xr_token other) const { return !xr_stricmp(value, other.name); }
426416
};
427417

428-
#ifndef _EDITOR
429-
430418
void free_vid_mode_list()
431419
{
432-
for (int i = 0; GEnv.vid_mode_token[i].name; i++)
433-
{
434-
xr_free(GEnv.vid_mode_token[i].name);
435-
}
436-
xr_free(GEnv.vid_mode_token);
437-
GEnv.vid_mode_token = nullptr;
420+
for (auto& mode : AvailableVideoModes)
421+
xr_free(mode.name);
422+
AvailableVideoModes.clear();
438423
}
439424

440425
void fill_vid_mode_list(CHW* _hw)
441426
{
442-
if (GEnv.vid_mode_token != nullptr)
427+
if (!AvailableVideoModes.empty())
443428
return;
444-
xr_vector<LPCSTR> _tmp;
445-
xr_vector<DXGI_MODE_DESC> modes;
429+
430+
xr_vector<DXGI_MODE_DESC> displayModes;
446431

447432
IDXGIOutput* pOutput;
448433
//_hw->m_pSwapChain->GetContainingOutput(&pOutput);
@@ -457,45 +442,33 @@ void fill_vid_mode_list(CHW* _hw)
457442
pOutput->GetDisplayModeList(format, flags, &cnt, nullptr);
458443

459444
// Get the list of display modes
460-
modes.resize(cnt);
461-
pOutput->GetDisplayModeList(format, flags, &cnt, &modes.front());
445+
displayModes.resize(cnt);
446+
pOutput->GetDisplayModeList(format, flags, &cnt, displayModes.data());
462447

463448
_RELEASE(pOutput);
464449

465-
for (auto &i : modes)
450+
int i = 0;
451+
auto& AVM = AvailableVideoModes;
452+
for (const auto& it : displayModes)
466453
{
467454
string32 str;
468455

469-
if (i.Width < 800)
456+
if (it.Width < 800)
470457
continue;
471458

472-
xr_sprintf(str, sizeof(str), "%dx%d", i.Width, i.Height);
459+
xr_sprintf(str, sizeof(str), "%dx%d", it.Width, it.Height);
473460

474-
if (_tmp.end() != std::find_if(_tmp.begin(), _tmp.end(), _uniq_mode(str)))
461+
if (AVM.cend() != std::find_if(AVM.cbegin(), AVM.cend(), uniqueRenderingMode(str)))
475462
continue;
476463

477-
_tmp.push_back(nullptr);
478-
_tmp.back() = xr_strdup(str);
464+
AVM.emplace_back(xr_token(xr_strdup(str), i));
465+
++i;
479466
}
467+
AVM.emplace_back(xr_token(nullptr, -1));
480468

481-
u32 _cnt = _tmp.size() + 1;
482-
483-
GEnv.vid_mode_token = xr_alloc<xr_token>(_cnt);
484-
485-
GEnv.vid_mode_token[_cnt - 1].id = -1;
486-
GEnv.vid_mode_token[_cnt - 1].name = nullptr;
487-
488-
#ifdef DEBUG
489-
Msg("Available video modes[%d]:", _tmp.size());
490-
#endif // DEBUG
491-
for (auto i = 0; i < _tmp.size(); ++i)
492-
{
493-
GEnv.vid_mode_token[i].id = i;
494-
GEnv.vid_mode_token[i].name = _tmp[i];
495-
#ifdef DEBUG
496-
Msg("[%s]", _tmp[i]);
497-
#endif // DEBUG
498-
}
469+
Msg("Available video modes[%d]:", AVM.size());
470+
for (const auto& mode : AVM)
471+
Msg("[%s]", mode.name);
499472
}
500473

501474
void CHW::UpdateViews()
@@ -540,4 +513,3 @@ void CHW::UpdateViews()
540513

541514
_RELEASE(pDepthStencil);
542515
}
543-
#endif

0 commit comments

Comments
 (0)