I have a platform-agnostic core engine that is extended by platform-specific ‘back-ends’ for Android and LWJGL. One aspect of this design that I can’t make up my mind on is mapping of the various enumerations in the core part (e.g. primitive types, texture filters, etc) to the corresponding OpenGL constants in the back-end implementations (e.g. GL_TRIANGLE_STRIP, GL_CLAMP_TO_EDGE, etc).
Here are the approaches I’ve considered so far:
- Map enums to constants in each back-end
Each back-end implementation is responsible for mapping the enumerations to the constants. Easy to do using a switch statement, but laborious and possibly error-prone to implement (temptation to cut-and-paste), particularly irksome for some of the larger sets such as blending functions, also adds slight over-head of constantly having to perform the mapping.
- Hard-code OpenGL constants in the core engine
Assume that the constants are the same across all OpenGL implementations (Android and LWJGL) and hard-code them in the core engine - Is this assumption valid?
(Could probably minimise the hard-coding in most cases by using just one value and the ordinal() of the enum since most OpenGL constants are contiguous).
- Dodgy reflection lookup of OpenGL constants
Use some devious reflection logic to lookup the OpenGL constants by name, e.g. take the enumeration name(), prepen GL_ and reflect through the static GL classes to find the matching constant - Works but feels dirty!
So:
Any other approaches I’ve not thought of?
Any opinion on the best approach? (I’m inclined to #2 since that seems the least effort / risk)
Cheers for any thoughts.
- stride