44// ////////////////////////////////////////////////////////////////////
55
66#include " stdafx.h"
7+
78#include " EngineAPI.h"
89#include " XR_IOConsole.h"
910
10- #include " xrCore/ModuleLookup.hpp"
1111#include " xrCore/xr_token.h"
12+ #include " xrCore/ModuleLookup.hpp"
1213#include " xrCore/Threading/ParallelForEach.hpp"
1314
1415#include " xrScriptEngine/ScriptExporter.hpp"
1516
17+ #include < array>
18+
1619extern xr_vector<xr_token> VidQualityToken;
1720
1821constexpr pcstr GET_RENDERER_MODULE_FUNC = " GetRendererModule" ;
1922
20- constexpr pcstr r4_library = " xrRender_R4" ;
21- constexpr pcstr gl_library = " xrRender_GL" ;
23+ using GetRendererModule = RendererModule*();
2224
23- constexpr pcstr RENDER_LIBRARIES[] =
25+ struct RendererDesc
2426{
25- r4_library,
26- gl_library
27+ pcstr libraryName;
28+ XRay::Module handle;
29+ RendererModule* module ;
2730};
2831
32+ std::array<RendererDesc, 2 > g_render_modules =
33+ {{
34+ { " xrRender_R4" , nullptr , nullptr },
35+ { " xrRender_GL" , nullptr , nullptr },
36+ }};
37+
2938// ////////////////////////////////////////////////////////////////////
3039// Construction/Destruction
3140// ////////////////////////////////////////////////////////////////////
@@ -99,56 +108,40 @@ void CEngineAPI::SelectRenderer()
99108 Log (" Selected renderer:" , selected_mode);
100109}
101110
102- void CEngineAPI::Initialize (void )
111+ void CEngineAPI::Initialize (GameModule* game )
103112{
104113 ZoneScoped;
105114
106115 SelectRenderer ();
107116
108- hGame = XRay::LoadModule (" xrGame" );
109117 if (!CanSkipGameModuleLoading ())
110118 {
111- R_ASSERT2 (hGame->IsLoaded (), " ! Game DLL raised exception during loading or there is no game DLL at all" );
112-
113- pCreate = (Factory_Create*)hGame->GetProcAddress (" xrFactory_Create" );
119+ gameModule = game;
120+ gameModule->initialize (pCreate, pDestroy);
114121 R_ASSERT (pCreate);
115-
116- pDestroy = (Factory_Destroy*)hGame->GetProcAddress (" xrFactory_Destroy" );
117122 R_ASSERT (pDestroy);
118-
119- pInitializeGame = (InitializeGameLibraryProc)hGame->GetProcAddress (" initialize_library" );
120- R_ASSERT (pInitializeGame);
121-
122- pFinalizeGame = (FinalizeGameLibraryProc)hGame->GetProcAddress (" finalize_library" );
123- R_ASSERT (pFinalizeGame);
124-
125- pInitializeGame ();
126123 }
127124
128125 CloseUnusedLibraries ();
129126}
130127
131- void CEngineAPI::Destroy (void )
128+ void CEngineAPI::Destroy ()
132129{
133130 ZoneScoped;
134- if (pFinalizeGame)
135- pFinalizeGame ();
136131
137- pInitializeGame = nullptr ;
138- pFinalizeGame = nullptr ;
132+ if (gameModule)
133+ gameModule->finalize ();
134+
139135 pCreate = nullptr ;
140136 pDestroy = nullptr ;
141137
142- hGame = nullptr ;
143-
144- renderers.clear ();
145138 XRC.r_clear_compact ();
146139}
147140
148141void CEngineAPI::CloseUnusedLibraries ()
149142{
150143 ZoneScoped;
151- for (RendererDesc& desc : renderers )
144+ for (RendererDesc& desc : g_render_modules )
152145 {
153146 if (desc.module != selectedRenderer)
154147 desc.handle = nullptr ;
@@ -162,35 +155,29 @@ void CEngineAPI::CreateRendererList()
162155
163156 ZoneScoped;
164157
165- const auto loadLibrary = [&](pcstr library ) -> bool
158+ const auto loadLibrary = [&](RendererDesc& desc ) -> bool
166159 {
167- auto handle = XRay::LoadModule (library );
160+ auto handle = XRay::LoadModule (desc. libraryName );
168161 if (!handle->IsLoaded ())
169162 return false ;
170163
171- const auto getModule = ( GetRendererModule) handle->GetProcAddress (GET_RENDERER_MODULE_FUNC);
164+ const auto getModule = reinterpret_cast < GetRendererModule*>( handle->GetProcAddress (GET_RENDERER_MODULE_FUNC) );
172165 RendererModule* module = getModule ? getModule () : nullptr ;
173166 if (!module )
174167 return false ;
175168
176- renderers.emplace_back (RendererDesc ({ library, std::move (handle), module }));
169+ desc.handle = std::move (handle);
170+ desc.module = module ;
177171 return true ;
178172 };
179173
180174 if (GEnv.isDedicatedServer )
181175 {
182- #if defined(XR_PLATFORM_WINDOWS)
183- R_ASSERT2 (loadLibrary (r4_library), " Dedicated server needs xrRender_R1 to work" );
184- #else
185- R_ASSERT2 (loadLibrary (gl_library), " Dedicated server needs xrRender_GL to work" );
186- #endif
176+ R_ASSERT2 (loadLibrary (g_render_modules[0 ]), " Dedicated server needs xrRender to work" );
187177 }
188178 else
189179 {
190- for (cpcstr library : RENDER_LIBRARIES)
191- {
192- loadLibrary (library);
193- }
180+ std::for_each (std::begin (g_render_modules), std::end (g_render_modules), loadLibrary);
194181 }
195182
196183 std::mutex mutex;
@@ -221,7 +208,7 @@ void CEngineAPI::CreateRendererList()
221208 }
222209 };
223210
224- xr_parallel_for_each (renderers , obtainModes);
211+ xr_parallel_for_each (g_render_modules , obtainModes);
225212
226213 auto & modes = VidQualityToken;
227214 Msg (" Available render modes[%d]:" , modes.size ());
0 commit comments