global variables/objects and "engine/demo" designing.

Started by
5 comments, last by GekkoCube 22 years, 2 months ago
So I have this demo (definitely not an engine since Im just hacking code in)...and in the beginning I have a bunch of global stuff...such as all of my direct3dsurfaces, directdrawsurfaces, pointers to my input class, sound class, and all other pointers (such as *AI, *SkyBox, *terrain...etc)! So my question is this: How are engines and games coded to accomodate all the objects necessary for a game? I have so many class such as CTriangle, CCube, CAI, CSound, CInput....etc...and to use each one of them I need to create an instance of each! And I can''t find a better way (or place) to put these instances other than simply placing them as globals (at the very top of the main file). And I just know this is NOT the way games are made!! So how is this done? Thank you. ~ I am a DirectX to OpenGL convert! ~
Advertisement
Did you say CTriangle?!

Like many OO converts, you''re going overboard. OO is great, but get a grip! Somethings are still procedures and therefore best modeled using procedural methods. I''ve written a DirectX encapsulation that was very much like OpenGL (procedural, state machine, etc). I hid the private data by using static data (visible only to the file).

Anyway, you''ll want to develop higher-level constructs that "manage" all of the lower-level structures such as triangles, cubes, models, levels, etc. This is called scene graph management. There are lots of articles and tutorials on the subject, but I suggest you see if your local university library has a copy of David Eberly''s excellent 3D Game Engine Design for a complete discussion.

Good luck!

I wanna work for Microsoft!
[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!
What is procedural?
And can you please elaborate....perhaps sample code or examples?
im a visual learner so that would help alot.

Using procedural methods, how could i display a bunch of triangles and spin them around on the screen???

thnaks.

~ I am a DirectX to OpenGL convert! ~
Procedural: do something (process) to some data
Object-oriented: ask some data to do something to itself.


The difference between procedural and object-oriented code can be illustrated by the function calls Draw(&object, ...) and object->Draw(). Draw is a procedure. In procedural code it takes the "object" to draw as well as other additional parameters (perhaps position, color, etc) while in OO form the object encapsulates all such necessary information.

In deciding whether to go OO or procedural (there are other programming styles/methods that C++ supports, such as functional, but they are irrelevant to this discussion) you should ask yourself whether the class would encapsulate a logic object with data properties, or whether it would simply be a set of operations.

Using procedural methods, displaying a bunch of trianlges would be similar to the DirectX 8 SDK example: define an array of vertices, initialize them and then loop through displaying and modifying them until exit.
// trianlgesvertex trianlges[10][3]; // 10 trianglesInitializeTriangles(&triangles);while(bRunning){  DisplayTriangles(&triangles);  UpdateTriangles(&triangles);} 

I recently wrote a window wrapper class for Win32 windows. However, here''s an application that uses it:
Window *g_wnd = 0;HINSTANCE hinstance;.int WINAPI WinMain(HINSTANCE hInstance, ...){  try  {    InitInstance();    // other init code.    // main application loop    while(true)    {      if(!g_wnd->HandleMessages())        break;      // other main loop processes    }  }  catch(exception &e)  {    // handle exceptions  }  return g_wnd->ExitCode();}//void InitInstance(){  g_wnd = new Window(hinstance);  if(!g_wnd)    throw std::runtime_error(...);  g_wnd->Create(...);  // other init code} 

InitInstance is a procedural method, because it makes sense for it to be (as opposed to my having a ApplicationInstance object and so forth), but it is free to manipulate OO entities.

Hope that helped.

I wanna work for Microsoft!
[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!
thanks!
So basically, things like CTriangles should be managed procedurally?!

Where would the procedural Draw() function be?
If it''s in a separate file, wouldnt you need to include all necessary header files that that Draw() function will end up drawing?


This all makes great sense...but I can''t imagine a game being produced like this...

for example:

suppose I have a game where everytime there is collision, a bunch of small triangles fly out (a particle system?).
In this case, the particle system would take care of the triangles...but would these triangles be managed procedurally???



~ I am a DirectX to OpenGL convert! ~
quote:Original post by GekkoCube
So basically, things like CTriangles should be managed procedurally?!

Depends on the needs of the application, and that''s one of the skills that you gain over time. Sometimes design decisions are actually judgement calls; it helps if you have someone to go over them with.

quote:Where would the procedural Draw() function be?

Declare it in a header and define it in a source file. Don''t tell me you''ve never written a procedural function!

quote:This all makes great sense...but I can''t imagine a game being produced like this...

Games have been written in C. And assembly.

quote:suppose I have a game where everytime there is collision, a bunch of small triangles fly out (a particle system?).
In this case, the particle system would take care of the triangles...but would these triangles be managed procedurally?

You can have a list of triangles (and you''ll probably see a lot of things like that), and have the particle effect manage the list.

I wanna work for Microsoft!
[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!
The Right Way, I think, would be to make a single class that encapsulates many triangles that have a logical relation to each other. Like a mesh. Then you can easily stuff those triangles in a nifty hardware structure thingie. (vertex/index buffer, compiled vertex whatchamacallit, etc...)

As for all the little classes, I generally have a CEngine class whose job is to take all the pieces and toss them around as needed.

"Eagles may soar, but weasels don''t get sucked into jet engines."
"There is only one everything"

This topic is closed to new replies.

Advertisement