JOGL Design

Hey all,
I’m creating a simple tile-based map editor, and I have some funamental java/JOGL OOP design questions.

First, how does everyone deal with the GLCanvas and implementing the GLEventListener methods, so that the GLCanvas->display() method has access to all of the game information it needs to render a scene?

Right now, I have a large class whose data members are all of my game data (the map, tile information, images, etc), and one GLCanvas on which to draw. With this class I implement the GLEventListener methods, so that my GLCanvas can have access to the data it needs to render. For example:

public class myClass implements GLEventListener {
private map myMap;
private image myImage;
private GLCanvas canvas;

init() etc

then, in the implemented display, I can just do this:

display(){
myMap.Render(myImage, canvas); etc etc
}
}

I can’t think of a better way to let the canvas talk with all of the other objects in my map editor… How does everyone else implement this design? I would like to keep the rendering device separate from the game data as much as possible, but it seems like I have to entertwine them at some point.

Also, how can I do runtime texture loading with JOGL? It seems that the GLContext is only valid during init() and display(), so it seems impossible to load textures anywhere outside of init(). How can I make a map editor that lets users load in a new tileset whenever they wish? Will I have to destroy/create a new canvas each time I want to load a new set of textures?

That’s all I can think of for now… Sorry if none of this made sense, I just need some direction on good OOP practices with JOGL =]

Sorry, no answer. I just REALLY want someone else to answer this, as it’s been bugging me to no end! I can do simple tests, and the demos are fine, but what about when I want to really start writing a big game where I have to load an undefined number of models and textures, what then? How does one organise this all?

In C++, I’d make an object that includes its own gl code in some kind of “render()” funtion and just draw that, but it doesn’t seem like I CAN here.

Anyway… BUMP!

Please help us out.

–Lareon

Sorry for the late response. I’ll try to explain a few techniques you can use with JOGL.

First of all, you should pass the GLDrawable to the rendered classes. You could have, for instance:


public interface GLRenderable
{
public void render(GLDrawable glDrawable);
}

public class MyMap implements GLRenderable
{
public void render(GLDrawable glDrawable)
{
GL gl = glDrawable.getGL();
// rendering code here
}
}

Then, the class with the GL event methods would be something like the following:


public class MyMapEditor implements GLEventListener
{
...
public void init()
{
// Init some GL resources here
}

public void display(GLDrawable glDrawable)
{
// Render the map
myMap.render(glDrawable);
}
}

Now, if you wish to load some new textures at run-time, you could do the following additions, for instance:


public interface GLResourceRequest
{
public void execute(GLDrawable glDrawable) throws Exception
}

public class MyTextureResourceRequest implements GLResourceRequest
{
private File textureFile;

public MyTextureResourceRequest(File file)
{
textureFile = file;
}

public void execute(GLDrawable glDrawable) throws Exception
{
GL gl = glDrawable.getGL();
// Load the texture file here...or throw an exception if something fails
}
}

Now, the main class could have the following modifications:


public class MyMapEditor implements GLEventListener
{
// This list works as queue to the resource requests
private ArrayList glResourceRequestQueue;

// This method is called from a mouse event handler, for instance
public void loadTexture(File file)
{
// Add a new resource request to the queue
glResourceRequestQueue.add(new MyTextureResourceRequest(textureFile));

// Repaint the GL canvas
glCanvas.repaint();
}

public void init()
{
// Init some GL resources here
}

public void display(GLDrawable glDrawable)
{
// Execute all queued resource requests
for (int i = 0; i < glResourceRequestQueue.size(); ++i)
((GLResourceRequest) glResourceRequestQueue.get(i)).execute(glDrawable);

// Render the map
myMap.render(glDrawable);
}
}

So there :). This is just one way of doing things, but it might get you started. I’m sorry if there are typos, writing with this small window makes me crazy ;)…

Cheers

So, umm… I’m not sure if I understand the abstract of what you’re saying – am I to assume that I can have any class render to the GLDrawable object just by passing the GLDrawable and getting the gl from that?

If so, then would it be “good programming” (or at least not “bad programming”) to have a main thread, or perhaps (more basically) a main loop which calls all of the objects to render, and only have the event handling code call back to my rendering method? (Oy, I’m afraid I may be cmore confusing than not.)

Basically, can I render to the context by passing IT along rather than only rendering inside the even handling code? Should I ONLY render inside the event handling section? If so, it looks like it IS okay to call OTHER methods from within this “render(…)” method, correct?

(Sorry for the clarification request, I tend to understand things in different way than most people.)

–Lareon