Short-time lurker, first-time poster. Hey everyone.
I’m working on a tile-based graphics engine as a design pattern exercise. The way I have it laid out now, I’m providing an abstract factory for abstract classes representing key objects in the engine. Specifically, a “TileSet”-derived class encapsulates a BufferedImage when using the AWT renderer, and a texture when using the JOGL renderer. Similarly, a “Renderer”-derived class encapsulates an AWT Canvas or JOGL GLCanvas. The idea here is that, by implementing a few concrete classes, the engine can use AWT, JOGL, LWJGL, or any other rendering mechanism supported by Java.
At client startup, a renderer is selected, the corresponding factory is created, and away we go. (This may be standard stuff, but I want to make sure all is clear.) The main game loop goes something like this:
static void main(...) {
instantiate concrete factory for selected graphics kit
instantiate concrete renderer class
...
while(the game is running) {
receive input from the user
update game state
renderer.beginScene();
for(each game object that needs to be drawn) {
renderer.drawThing(thingToDraw, location);
}
renderer.endScene();
}
}
The AWT renderer fits into this scheme really well (despite dogging it pretty hard), because I can draw to its wrapped Canvas any time I want. Unfortunately, I’m finding that JOGL’s GLCanvas is not so cooperative… the game crashes whenever I try to call any GL methods outside of the GLEventListener methods. (I was caching the GL reference after init() and display().) Incidentally, this restriction also makes it a huge pain to load textures mid-game.
So here’s my question: is there a way I can somehow make JOGL fit into the game loop, the way I want it? The only other alternative I can see would be putting the game loop inside display(), but I think that would break my abstract approach. Ideally (at least, from my “clueless newbie” perspective) I’d be able to call GL methods against the GLCanvas at any time…
Can this be done? Is my design stupid/misguided? Or am I just missing something entirely?