Can load dynamic library (dll) instead of linking against.

Allow to not link against d3d12 lib
This commit is contained in:
2025-01-12 17:26:58 -05:00
parent 312e139a97
commit 6c80168e8c
3 changed files with 145 additions and 1 deletions

View File

@@ -70,7 +70,7 @@
<SubSystem>
</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>ws2_32.lib;d3d12.lib;dxgi.lib;dxguid.lib;$(CoreLibraryDependencies);%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>ws2_32.lib;dxgi.lib;dxguid.lib;$(CoreLibraryDependencies);%(AdditionalDependencies)</AdditionalDependencies>
</Link>
<Lib>
<AdditionalDependencies>
@@ -112,6 +112,7 @@
<ClInclude Include="include\Core\Common\NonNullPtr.h" />
<ClInclude Include="include\Core\Common\Singleton.h" />
<ClInclude Include="include\Core\Container\Vector.h" />
<ClInclude Include="include\Core\DynLib\DynamicLibrary.h" />
<ClInclude Include="include\Core\HAL\Display\Display.h" />
<ClInclude Include="include\Core\HAL\Event\SystemEvent.h" />
<ClInclude Include="include\Core\HAL\Keyboard\Keyboard.h" />
@@ -155,6 +156,60 @@
<ItemGroup>
<ClCompile Include="src\Core\Application\ApplicationManager.cpp" />
<ClCompile Include="src\Core\Common\CoreUtils.cpp" />
<ClCompile Include="src\Core\DynLib\Win32\DynamicLibrary.cpp">
<RuntimeLibrary>MultiThreadedDebugDll</RuntimeLibrary>
<Optimization>Disabled</Optimization>
<SupportJustMyCode>true</SupportJustMyCode>
<AssemblerOutput>NoListing</AssemblerOutput>
<AssemblerListingLocation>W:\Classified\Juliet\Intermediate\Juliet\x64\Debug\</AssemblerListingLocation>
<UndefineAllPreprocessorDefinitions>false</UndefineAllPreprocessorDefinitions>
<BrowseInformationFile>W:\Classified\Juliet\Intermediate\Juliet\x64\Debug\</BrowseInformationFile>
<CompileAs>Default</CompileAs>
<ConformanceMode>Default</ConformanceMode>
<DiagnosticsFormat>Column</DiagnosticsFormat>
<ExceptionHandling>false</ExceptionHandling>
<EnableASAN>false</EnableASAN>
<EnableFiberSafeOptimizations>false</EnableFiberSafeOptimizations>
<EnableEnhancedInstructionSet>NotSet</EnableEnhancedInstructionSet>
<FloatingPointModel>Fast</FloatingPointModel>
<InlineFunctionExpansion>Default</InlineFunctionExpansion>
<IntrinsicFunctions>false</IntrinsicFunctions>
<LanguageStandard>stdcpp20</LanguageStandard>
<LanguageStandard_C>Default</LanguageStandard_C>
<ModuleDependenciesFile>W:\Classified\Juliet\Intermediate\Juliet\x64\Debug\</ModuleDependenciesFile>
<OmitDefaultLibName>false</OmitDefaultLibName>
<FavorSizeOrSpeed>Neither</FavorSizeOrSpeed>
<ObjectFileName>W:\Classified\Juliet\Intermediate\Juliet\x64\Debug\</ObjectFileName>
<CallingConvention>Cdecl</CallingConvention>
<PrecompiledHeader>Use</PrecompiledHeader>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<PrecompiledHeaderOutputFile>W:\Classified\Juliet\Intermediate\Juliet\x64\Debug\Juliet.pch</PrecompiledHeaderOutputFile>
<PreprocessToFile>false</PreprocessToFile>
<PreprocessKeepComments>false</PreprocessKeepComments>
<PreprocessSuppressLineNumbers>false</PreprocessSuppressLineNumbers>
<ScanSourceForModuleDependencies>false</ScanSourceForModuleDependencies>
<ShowIncludes>false</ShowIncludes>
<SourceDependenciesFile>W:\Classified\Juliet\Intermediate\Juliet\x64\Debug\</SourceDependenciesFile>
<SuppressStartupBanner>true</SuppressStartupBanner>
<BufferSecurityCheck>true</BufferSecurityCheck>
<SmallerTypeCheck>false</SmallerTypeCheck>
<StructMemberAlignment>Default</StructMemberAlignment>
<TrackerLogDirectory>W:\Classified\Juliet\Intermediate\Juliet\x64\Debug\Juliet.tlog\</TrackerLogDirectory>
<MinimalRebuildFromTracking>true</MinimalRebuildFromTracking>
<TreatWarningAsError>false</TreatWarningAsError>
<WarningLevel>Level3</WarningLevel>
<XMLDocumentationFileName>W:\Classified\Juliet\Intermediate\Juliet\x64\Debug\</XMLDocumentationFileName>
<DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
<IntelJCCErratum>false</IntelJCCErratum>
<BuildStlModules>false</BuildStlModules>
<TreatExternalTemplatesAsInternal>true</TreatExternalTemplatesAsInternal>
<PreprocessorDefinitions>_DEBUG;JULIET_EXPORT;JULIET_WIN32;_WINDLL;_UNICODE;UNICODE;</PreprocessorDefinitions>
<RuntimeTypeInfo>false</RuntimeTypeInfo>
<LinkCompiled>true</LinkCompiled>
<ClangClMode>true</ClangClMode>
<MSCVersion>Default</MSCVersion>
<AdditionalOptions>--target=amd64-pc-windows-msvc </AdditionalOptions>
</ClCompile>
<ClCompile Include="src\Core\HAL\Display\Display.cpp" />
<ClCompile Include="src\Core\HAL\Display\Win32\Win32DisplayDevice.cpp" />
<ClCompile Include="src\Core\HAL\Display\Win32\Win32DisplayEvent.cpp" />

View File

@@ -0,0 +1,14 @@
#pragma once
#include <core/Common/NonNullPtr.h>
namespace Juliet
{
struct DynamicLibrary
{
};
extern JULIET_API DynamicLibrary* LoadDynamicLibrary(const char* filename);
extern JULIET_API FunctionPtr LoadFunction(NonNullPtr<DynamicLibrary> lib, const char* functionName);
extern JULIET_API void UnloadDynamicLibrary(NonNullPtr<DynamicLibrary> lib);
} // namespace Juliet

View File

@@ -0,0 +1,75 @@
#include <pch.h>
#include <Core/DynLib/DynamicLibrary.h>
#include <Core/HAL/Win32.h>
namespace Juliet
{
namespace
{
// TODO : Move into string file
// Use portable code + pass the memory array into parameter and not use new
// This is from http://www.rohitab.com/discuss/topic/41257-char-to-lpcwstr/
wchar_t* UTF8ToWideChar(const char* utf8)
{
wchar_t* w;
int len = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, 0, 0);
if (len <= 0)
{
return nullptr;
}
w = new wchar_t[len];
if (!w)
{
return nullptr;
}
if (MultiByteToWideChar(CP_UTF8, 0, utf8, -1, w, len) <= 0)
{
delete[] w;
return nullptr;
}
return w;
}
} // namespace
DynamicLibrary* LoadDynamicLibrary(const char* filename)
{
if (!filename)
{
Log(LogLevel::Error, LogCategory::Core, "Library filename is invalid (empty)");
return nullptr;
}
LPWSTR wstr = UTF8ToWideChar(filename);
HMODULE handle = LoadLibraryW(wstr);
delete[] wstr;
// Generate an error message if all loads failed
if (!handle)
{
Log(LogLevel::Error, LogCategory::Core, "Failed loading %s", filename);
return nullptr;
}
return reinterpret_cast<DynamicLibrary*>(handle);
}
FunctionPtr LoadFunction(NonNullPtr<DynamicLibrary> lib, const char* functionName)
{
auto function = (FunctionPtr)GetProcAddress(reinterpret_cast<HMODULE>(lib.Get()), functionName);
if (!function)
{
Log(LogLevel::Error, LogCategory::Core, "Failed loading %s", functionName);
}
return function;
}
void UnloadDynamicLibrary(NonNullPtr<DynamicLibrary> lib)
{
FreeLibrary(reinterpret_cast<HMODULE>(lib.Get()));
}
} // namespace Juliet