Reputation:
I am trying to make a plugin-based game in Rust, and I am having some difficulties. The way I am making my plugins is through the method shared on this question. At runtime, I will dynamically load all the necessary functions from the plugins, and run them later when they need to be run.
My problem is in using OpenGL to draw things to the screen with the code of one of these plugins. In my game's executable, I load all the functions from the plugins (currently only one function), create the GLFW window and load the OpenGL function pointers, following the code shown below, from here.
gl::load_with(|symbol| window.get_proc_address(symbol) as *const _);
In the game's loop, I call the loaded function from the plugin, drawvao
.
while !window.should_close() {
process_events(&mut window, &events);
unsafe {
gl::ClearColor(0.2, 0.3, 0.3, 1.0);
gl::Clear(gl::COLOR_BUFFER_BIT);
drawvao(shaderProgram, VAO);
}
window.swap_buffers();
glfw.poll_events();
}
pub extern "C" fn drawvao(shaderProgram: u32, Vao: u32){
gl::UseProgram(shaderProgram);
gl::BindVertexArray(VAO);
gl::DrawArrays(gl::TRIANGLES, 0, 3);
}
The code in drawvao
fails, saying that it could not find the OpenGL function pointers.
How do I make this possible, while not loading the function pointers in again in each plugin?
I have thought of making an intermediate crate called something like api
that both of them have as a dependency, but that would still be loading in the function pointers for each plugin, and I am not sure if they would draw to the GLFW window.
I have also thought of loading the function pointers from an external DLL on both the plugins and game, but I do not know if that would help. I know that that solution would work in a .NET language, but, unless I am mistaken, it would not work the same way in Rust.
Upvotes: 3
Views: 334
Reputation: 473272
while not loading the function pointers in again in each plugin?
You can't. Not unless the executable you're working with exports all of those OpenGL function pointers, or you're importing a common DLL that exports those function pointers.
Each DLL and executable is distinct and for the most part entirely separate from other executables. Unless they explicitly share something, then it's not shared. And most programs don't explicitly share their OpenGL function pointers, so you'll have to load them yourself.
Upvotes: 1