need help w/ program structure for opengl games

Put your problem here if it does not fit any of the other categories.

need help w/ program structure for opengl games

Postby edzillion » Wed Dec 02, 2009 2:20 pm

posted this on the android mailing list and didn't get any response; this is probably a better forum for this kind of question:

I am having trouble understanding how threads, activities and
renderers fit together.

My current program structure is this:

- main activity, nothing in it really, just initialising things.

mGLView = new GLSurfaceView(this);
mGLView.setEGLConfigChooser(false);
mGLView.setRenderer(new MyRenderer(this));

- renderer, which also contains some gameplay classes (e.g. MyShip)
and includes the input event handlers.

public class MyRenderer implements GLSurfaceView.Renderer {

- map, which contains the vertex, texture etc and includes draw()
function. which is called from the onDrawFrame of MyRenderer.

At this stage I want to move all the input stuff into its own thread
(AFAIK this is most efficient?), I am not sure where this should be
initialised and how the input information should be sent to the
renderer.

I found this article 'Writing Real Time Games for Android' (
http://www.scribd.com/doc/16917369/Writ ... or-Android
) which outlines a program structure, which I think is what I am
looking for.

Here a diagram of the structure: http://imgur.com/WqZua.jpg

Can you describe how this is supposed to work (how it is all
initialised, how the program flows etc.)

TIA

Ed

ps. perhaps if we figure this out, I could edit it down and add it to the tutes section (?)
edzillion
Junior Developer
Junior Developer
 
Posts: 17
Joined: Fri Oct 16, 2009 8:48 pm

Top

Postby appforce » Wed Dec 02, 2009 4:43 pm

Hi,

Describing a whole game design isn't a trivial task, but I want to answer about threads. You are using a GLSurfaceView, and it is starting it's own thread for rendering, so it's separated from the user input thread (which is the main thread). About synchronization - you should synchronize the user events (touch, trackball, key) if they are changing some parameters related to rendering (most likely they do). For example if you have a float[] matrix that you use for MODEL_VIEW and touch events are modifying it, synchronize for a common mutex only the blocks that modify (in dispatchTouchEvent for example) and that read (onDraw for example).

AppForce developers
appforce
Experienced Developer
Experienced Developer
 
Posts: 60
Joined: Mon Nov 23, 2009 8:28 pm

Postby edzillion » Wed Dec 02, 2009 11:57 pm

Ok, I got you about using synchronised with the input and rendering - I think the Lunar Lander example uses that technique.

So there is one thread doing the rendering;
What I *think* is the way this should be set up goes like this:

1. Activity starts
2. creates rendering thread and gameloop thread
3. gameloop iterates through main loop
4. Any UI events are processed, and changes are made to the game objects (map, ships etc) which are all in one seperate class called gameBoard or something.
5. Rendering goes through each of the game objects in gameBoard and paints them to screen.

It works something like this at the moment, but I don't know how to create my gameloop thread, (just subclass thread?)

Just writing this has cleared it up somewhat in my head. Am I right with that basic structure above though?
edzillion
Junior Developer
Junior Developer
 
Posts: 17
Joined: Fri Oct 16, 2009 8:48 pm

Postby appforce » Thu Dec 03, 2009 12:16 am

Hi,

Here's a useful tutorial for general game loops:

http://dewitters.koonsolo.com/gameloop.html

Basically a game loop does this:

while (true) {
updateGame(); // process user input, then update the objects states
renderFrame(); // iterate over objects and draw
sleep(); // compute sleep time based on something
}

User input should modify some parameters (or states of objects) that are needed in updateGame(). If you use a physics engine, you are calling it's updateWorld() (or something) method/function after taking user action in count. That's just an example, there are many ways depending on the game itself. Sometimes is better to queue user action events parallel to the game loop.


AppForce developers
appforce
Experienced Developer
Experienced Developer
 
Posts: 60
Joined: Mon Nov 23, 2009 8:28 pm

Postby hardcoras » Thu Dec 03, 2009 2:11 am

hardcoras
Experienced Developer
Experienced Developer
 
Posts: 62
Joined: Sat Nov 14, 2009 2:31 pm
Location: Lithunia

Postby edzillion » Thu Dec 03, 2009 10:35 pm

thanks thats exactly what I was looking for.
edzillion
Junior Developer
Junior Developer
 
Posts: 17
Joined: Fri Oct 16, 2009 8:48 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 14 guests