我正在构建一个 opengl/directx/vulkan 现代游戏引擎。我之前编写了一个引擎版本,其中我将 API 特定代码与宏分开,例如:
#ifdef CIANADE_API_OGL
OGLTexture::GPU_LoadTexture();
#elif defined(CIANADE_API_DX12)
DX12Texture::GPU_LoadTexture();
#elif //etc....
对于每个 API 和平台,我都有单独的构建(cianade_ogl4.exe、cianade_ogl3.exe、cianade_dx12.exe 等...)。
我的问题是在运行时动态加载opengl和directx api函数是否可能并且是个好主意(仅适用于PC平台,移动/控制台只有一个可执行文件),如果可以,如何实现?
My question is is it possible and would it be a good idea to load opengl and directx api functions dynamically at runtime (just for PC platforms, mobile/consoles would have only one executable) and if so, how could it be done ?
我不是那些图形引擎的专家,但是可以在运行时加载底层 DLL。
不过,您需要为特定的库函数提供包装器接口(interface):
struct IGraphLibTexture {
virtual void GPU_LoadTexture() = 0;
virtual ~IGraphLibTexture() {}
};
并在单独的 DLL 中提供不同的实现。
根据您在运行时确定的目标体系结构,然后显式加载其中一个 DLL。
如果这真的是个好主意,我会留给其他人回答。