Advertisement

Hi There! (Newbie with a question.)

Started by February 09, 2003 09:01 AM
7 comments, last by SpaceRogue 21 years, 9 months ago
I''m a novice programmer trying to learn game programming. I''ve been reading "Tricks of the Windows Game Programmng Gurus" and studying the DirectX9 SDK documentation. So far things have been going OK (creating little 3D demos that involve rotating simple objects and such), but I have some questions that don''t seem to be answered anywhere...My biggest issue is one of scope: Supposing you have a game loop that checks for windows messages, gets input, renders the screen etc. like this: --------------------------------------------- while (!game_ending) { if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) ) { TranslateMessage( &msg ); DispatchMessage( &msg ); } ProcessInput(); Game_Logic(); Render(); } --------------------------------------------- Now if i have ojects that represent things like ships, people, etc. obviously they need to be referenced in more than one of the above functions, i.e. they need to be used in Game_Logic, Render, and Process_Input. My question is how do I do something like this without making ALL of my game objects global? Everyone says global=bad, so I''d like to know how people accomplish this. Surely you don''t pass every object in the game as parameters to each and every function call. I seem to be missing something fundamental here. Can anyone shed some light on this for me?
Oops, I forgot to put my second question:

Does each object you intend to render normally get it''s own vertex buffer or do you combine objects somehow? For example, in a scene with 20 spaceships flying around would you have 20+ vertex buffers?
Advertisement
I keep my vars from being global by making classes for almost everything.

Try making seperate classes for your ships, people, player and whatever else (if you haven''t already), then make an engine class that will have one or more instances of each of the other classes ie 1 player, 5 ships and an array of people. Then, you can put your functions in the class, and all of them will be able to see the player,people, etc. Your new message loop could end up looking like this:

---------------------------------------------
(CEngine class defined somewhere else...)

while (!game_ending)
{
CEngine MyEngine;
if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
MyEngine.ProcessInput();
MyEngine.Game_Logic();
MyEngine.Render();
}
---------------------------------------------

Hope that helps

Later, Mosh.

PS: Think out how your classes will fit together first, because otherwise it can get pretty messy and hard to read.

Life is all about expression, so express yourself.
quote:
Does each object you intend to render normally get it''s own vertex buffer or do you combine objects somehow? For example, in a scene with 20 spaceships flying around would you have 20+ vertex buffers?

If those 20 ships are all different (geometrically), then each one gets a separate vertex buffer. If they''re all the same model, then you use a single vertex buffer, and reference all 20 instances from there.
I use everything as globals, but maybe im doing it wrong? Seems to work fine for me.
ah well yea, isntead of classes i use the c way, struct's and stuff.

-plasmicsoup
Gilgamesh Games

[edited by - plasmicSoup on February 9, 2003 10:19:53 AM]
-plasmicsoup
Thanks guys.

Mosh - I planned on putting my objects into classes, but couldn''t quite see how they fit together. I''m still not entirely sure about the details, but it does make more sense now.

Yann L - Aha! That makes sense. So, ships with the same skin can use the same buffer, just translated differently. Cool, I doubt I would have thought of that anytime soon.
Advertisement
I can''t see the logic in making a GameEngine class or something similar. There is no point in making a class that there will always be one, and only one of. It just results in a whole lot of dereferences of "this" pointers that wouldn''t happen with global variables. I just use classes when I need more than one of something, like a spaceship class.
but creating a singleton (as this is called) creates better structured code, since all of your functions and variables are in one, easier to maintain, place
futher, if you employ some encapsulation of data, then you would lower the chance of some code somewhere changing a value it shouldnt at some future point in your program
-PoesRaven
I would create a class like CObjectManager or whatever you need to use. You''d make CObjectManager just handle a linked list, all you''d need to be global was the manager.

This topic is closed to new replies.

Advertisement