Handmade Hero»Forums»Code
Max
16 posts
Passing Opengl commands to the game dll

Hello, I have been trying to work on some small projects using some of the tools I learned in Handmade Hero. In my current project I have a the game code and platform layer separated. I am currently working on how I would like rendering to work as I would like to use opengl and maybe other apis in the future. It seems like a good idea to store all the needed opengl function logic in the game code. To do this, the platform layer would have to load all the needed opengl functions, fill out a struct of function pointers and pass it to the game code. This struct would need to have platform independent function pointers that I use to point to the opengl functions. I have been doing some reading and am somewhat confused how I would do this. I have mostly been using the article https://www.khronos.org/opengl/wiki/Load_OpenGL_Functions which suggest that you can define them as such: (example for glUseProgram) typedef void (APIENTRYP PFNGLUSEPROGRAMPROC) (GLuint program); However, I have read in a few placed that APIENTRY and APIENTRYP might be windows specific. Should I be defining these functions in a different way? Is this method of sending the opengl controls to the game code even a good idea? Any advice would be appreciated. Thanks!

Mārtiņš Možeiko
2562 posts / 2 projects
Passing Opengl commands to the game dll
Edited by Mārtiņš Možeiko on

APIENTRY things will be defined by opengl headers. So if you include gl.h/glcorearb.h/glext.h or similar headers to get OpenGL types, they will have all thse things defined properly for platform you are compiling.

In fact if you include glcorearb.h then you will have ready to use OpenGL function pointer types - whole PFNGLUSEPROGRAMPROC thing. If you use it in your struct then it will work correctly without defining anything extra:

struct YourPointers {
  PFNGLUSEPROGRAMPROC UseProgram;
  // ...
};

...

YourPointers* ptr = ...; // get it from somewhere
ptr->UseProgram(x);      // call glUseProgram
Max
16 posts
Passing Opengl commands to the game dll
Edited by Max on
Replying to mmozeiko (#26126)

Interesting, so I could just include glcorearb.h or glext.h in both the platform code and game code and should be fine (glcorearb.h sounds like the better pick). That is a relief, I was starting to get worried that calling opengl calls in the game code might be impossible while there are separate or might require some crazy work around.

I basically have what you are describing in my code, but I was just copying in all the "PFNGLUSEPROGRAMPROC thing" typedefs into my code.

It also seems like glcorearb.h might need khrplatform.h.

Thanks for the help!