Sign in to follow this  
Followers 0
Icebone1000

How/Who create the GameObjects?

41 posts in this topic

[source lang="cpp"]class Game{

list < GameObjects * > m_gos;

void Update(float delta){

for(list::it goIt ..)
(*goIt)->Update(delta);
}

...
};[/source]


Pseudo code..

Id like to make the Class Game the sole responsible for the creation and deletion of the gameobjects.
I always do this with a template function:
[source lang="cpp"]template< class derivedGO>
derivedGO* Create(){

derivedGO *p = new derivedGO();
m_gos.add(p);
return p;

}[/source]
And this is the only way to give the Game class the objects (so if they got created externally, they will not be part of the game).
The only (really annoying imo) problem is that derivedGO must provide a compatible constructor, this sucks, cause I always, then, have to create a Init(params) function that is always called just after calling create..

Is this poor design? Edited by Icebone1000
0

Share this post


Link to post
Share on other sites
[quote name='L. Spiro' timestamp='1350950978' post='4992943']
For example, objects don’t exist inside the game, they exist inside scenes.
[/quote]

What about game objects that need to persist across scenes? Like puzzle logic state that spans levels? Or the main player character + all of his stats/score/whatever?
0

Share this post


Link to post
Share on other sites
Data that needs to persist across scenes/states goes on the Game class. That is specifically its job if nothing else.
But that does not mean the 3D or 2D rendering data for your main character etc. That just means your current level, current HP, etc. The bare minimum that could be accessed by any part of the game at any time.

[EDIT]
Note that I failed to mention that all that data that belongs just to one game or another should be part of your “MyGame” class which inherits from “Game”.
Game itself is general across all games and should obviously not be the place for that kind of data.
[/EDIT]


L. Spiro Edited by L. Spiro
0

Share this post


Link to post
Share on other sites
To represent different scenes:

enum { SCENE_LOGO, SCENE_TITLE, SCENE_OPTIONS, SCENE_PLAY, SCENE_GAME_OVER };
int c_scene=SCENE_LOGO; // current scene
-4

Share this post


Link to post
Share on other sites
nox_pp: I agree that OOP sucks :) C++ is for newbies who know nothing about real programming, but I must admit that uglybdavis presented some smart code. I admire that.

typedef struct {
void *p;
int x, y, w, h,
bpp, key, alpha;
} IMAGE;

int load_image(IMAGE *i, char *file);
void move_image(IMAGE *i, int x, int y);
void draw_image(IMAGE *i);

My ASM programming site: http://sungod777.zxq.net/
-22

Share this post


Link to post
Share on other sites
[quote name='rdragon1' timestamp='1350971519' post='4992990']
What about game objects that need to persist across scenes? Like puzzle logic state that spans levels? Or the main player character + all of his stats/score/whatever?
[/quote]

Switching betwen scenes also presents a pretty good chance to save your data to disk, then re-read it when the next scene loads. Rather than using globals to track things you could just treat data as data. This might not be a good case for a lot of things, but it works out very well with iCloud.

[quote name='uart777' timestamp='1350993240' post='4993065']

My ASM programming site: http://sungod777.zxq.net/
[/quote]
Assemblers are dumb and they suck. ASM is is for newbies who know nothing about how to code. Real programmers use butterflies. They open their hands and let the butterflies delicate wing flap once. The disturbance ripples outwards, changing the flow of the eddy currents in the upper atmosphere; these cause momentary pockets of high pressure air to form. These pockets act as lenses that deflect incoming cosmic rays focusing them to strike the drive platter and flip the desired bits. It's all explained right here: http://xkcd.com/378/
1

Share this post


Link to post
Share on other sites
"I don’t get the point of this post" - I merely demonstrated that OOP is not neccessary.

"When will an image have a negative width or height?" - When it's invalid/inverted (-1/0xFFFFFFFF).

"Why do you have to move the image in a separate step from drawing it?" - Because it's faster to send less parameters, but you don't know anything about push-call sequences.

L. Spiro: Let me show how to draw/paint/airbrush/sculpt anything: [url="http://www.facebook.com/uart777/photos_stream"]http://www.facebook....7/photos_stream[/url] You think you're so right, but you don't even know what your code converts to. How can you expect anyone to use your library?

uglybdavis: Forgive me for complimenting you [img]http://public.gamedev.net//public/style_emoticons/default/tongue.png[/img] "Assemblers are dumb and they suck. ASM is is for newbies who know nothing about how to code" - This statement shows how little you know about programming. You defined a PIXEL wrong. Alpha should be in leftmost byte (0x*AA*BBCCDD). Stop changing byte orders [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img] You disrespect ASM because you don't know anything about the processor's language.

"It's all explained right here" - Dummy [img]http://public.gamedev.net//public/style_emoticons/default/tongue.png[/img] Edited by uart777
-12

Share this post


Link to post
Share on other sites
[quote name='uart777' timestamp='1351150014' post='4993705']
uglybdavis: Forgive me for complimenting you [...] This statement shows how little you know about programming.
[/quote]
He was joking... [i]very obviously joking[/i] -- he even linked the comic he was referencing. Did you [i]really[/i] think someone was [i]seriously recommending[/i] that you should use the disturbances caused by the flapping of a butterfly's wings to deflect cosmic rays at a drive platter rather than programming?

It is however a tongue-in-cheek observation of the fact that assembly simply isn't as important as you seem to think. Your constant recommendations to learn assembly (from your own mid-90s-style website no-less) just aren't relevant in most cases. Learning assembly [i]is[/i] valuable, and I'd even say it's something the majority of programmers should take the time to do eventually, but it just isn't useful to beginners and it isn't relevant to the original poster's question or any of the following discussion.

[quote name='uart777' timestamp='1351150014' post='4993705']
This statement shows how little you know about programming. You defined a PIXEL wrong. Alpha should be in leftmost byte (0x*AA*BBCCDD).
[/quote]
Actually, [i]THIS[/i] statement shows how little [i]YOU[/i] know about programming. You've now asserted the alpha should always be the leftmost byte of a pixel -- and that to do otherwise is incorrect -- [url="http://www.gamedev.net/topic/633214-struct-vs-classes/page__view__findpost__p__4993197"]more than once[/url], but as has [url="http://www.gamedev.net/topic/633214-struct-vs-classes/page__view__findpost__p__4993332"]already been pointed out[/url] to you, that's simply a convention and isn't significantly more or less popular than alternatives.

[quote name='uart777' timestamp='1351150014' post='4993705']
"I don’t get the point of this post" - I merely demonstrated that OOP is not neccessary.
[/quote]
Actually, you sort of showed -- in a rather poor way that was probably meaningless to the original poster given you neglected to to say what you were doing rather than just dumping some code -- that OO can be implemented in C even though the language doesn't explicitly support the paradigm through language features. OOP is a set of guiding principles for designing code -- not just use of the "class" (or related) keyword(s) -- and like any other paradigm is useful in some situations when used correctly, but isn't universally applicable.

Sharing your opinion and contributing to the discussion are good. Acting like you're better than everyone else while spouting nonsense and spamming your (often irrelevant) website link are not. Drop the attitude...

[quote name='uart777' timestamp='1351150014' post='4993705']
L. Spiro: Let me show how to draw/paint/airbrush/sculpt anything
[/quote]
...and consider this an official moderator instruction to stop bringing up irrelevant stuff to show off every time you feel you've been challenged. This topic isn't even slightly about art or yours -- or anyone else's -- artistic abilities, and I recall this isn't the first topic in which you've done so.
[size=2]But before you accuse me of abusing moderator powers or anything of the sort, note that you're welcome to challenge anything I've said as long as you stay within the site's rules -- you're only being instructed not to post things that are [i]completely off topic[/i].[/size]
4

Share this post


Link to post
Share on other sites
[quote name='uart777' timestamp='1351150014' post='4993705']
You think you're so right, but you don't even know what your code converts to.
Because it's faster to send less[sic] parameters, but you don't know anything about push-call sequences.
[/quote]
Actually I have written a C compiler, a disassembler, and an assembler. And a debugger and everything else listed here: [url="http://memoryhacking.com/feature.php"]http://memoryhacking.com/feature.php[/url]



[quote name='uart777' timestamp='1351150014' post='4993705']
L. Spiro: Let me show how to draw/paint/airbrush/sculpt anything: [url="http://www.facebook.com/uart777/photos_stream"]http://www.facebook....7/photos_stream[/url]
[/quote]
I don’t understand why you posted this completely unrelated bit, but, since we are sharing, I drew this when I was 12:
[img]http://fc02.deviantart.net/fs71/f/2011/052/8/9/age_12_raptor_by_l_spiro-dypf8c.jpg[/img]
[url="http://l-spiro.deviantart.com/gallery/4844241"]http://l-spiro.devia...gallery/4844241[/url]


[quote name='uart777' timestamp='1351150014' post='4993705']
uglybdavis: Forgive me for complimenting you [img]http://public.gamedev.net//public/style_emoticons/default/tongue.png[/img] "Assemblers are dumb and they suck. ASM is is for newbies who know nothing about how to code" - This statement shows how little you know about programming.
[/quote]
By the way, he didn’t attack you. It was a joke and a reference to xkcd.



It would generally be better if you didn’t try to make yourself sound amazing in a place such as this.
You will invariably find others who are much more knowledgeable and skilled than yourself. Of course, that is usually a [i]good[/i] thing, just not when you are trying to pick fights.


L. Spiro Edited by L. Spiro
4

Share this post


Link to post
Share on other sites
Back on topic....

To OP: I follow a similar pattern. I consider myself somewhat of a novice at designing game architectures, so take what I say with a grain of salt:
I have a base class which every game object inherits from (already may be a bad idea). The class is abstract and pretty much worries about assigning an incrementing ID, possibly accepting a given name, maintaining a queue of messages, and enforcing the implementation of update and init functions for classes which inherit from it, and acting as a generic object which can be used by containers (pretty much polymorphism). That's it. Any inheriting classes will extend this base class.

Here is the C# code for my base class:
[code]
public abstract class GBO
{
UInt64 m_ID;
static UInt64 m_nextID = 0;
protected string m_name;
public Queue<GameMessage> Messages = new Queue<GameMessage>(10);

public GBO()
{
m_ID = m_nextID++;
}

public GBO(string Name)
{
m_ID = m_nextID++;
m_name = Name;
}
public UInt64 ID
{
get { return m_ID;}
}
public string Name
{
get { return m_name;}
set { m_name = value;}
}
public abstract void Update();
public abstract void Start(); //I use "Start" instead of "Init" for Unity3D
}
[/code]

Looking at my base object and thinking about it, it does have some weaknesses:
If I decide to create particle engine and each particle is a GBO class, do I really care about the name of a particle or any game messages it may have generated? Not really. I could slice those two variables out. The ID is mostly used as a key for dictionaries and hash tables, but would a particle ever be stored in a hash table or dictionary? Not really. So, if I slice that out too, then my base class would just have an abstract "Start()" and "Update()" method. Do I even need those? I already know that all of my game objects have to implement initialization and update functions, so enforcing it is a bit of a moot point and possibly restrictive since they don't have input parameters. I might as well have a completely blank base class to support the most flexibility... but why even have an empty class if it doesn't do anything? Do I even NEED a base class?
"Oh, what about using the base class as a generic container object? That way, you can have a single list of all your objects in the game and call their update() method!"
Well, every inheriting class would have a corresponding manager class. The manager class itself can have update called on it and we'll let the manager worry about updating its objects.
Instead of:
[code]
foreach(GBO obj in m_allObjects)
obj.update();
[/code]

we can do this:
[code]
MageMgr.Update();
MonsterMgr.Update();
PlayerMgr.Update();
BulletsMgr.Update();
[/code]

Is this "better"? One immediate disadvantage is that we explicitly have to create and call the update functions for every list of items we have in the game. This adds extra programmer overhead. But, is there an advantage to asking the manager to update its contained items? I think so. The manager can worry about the game logic in regards to the object. So, for example, your mage manager would not only call update() on all of the mages in its list, it would also manage the list of mages by removing any mages which are dead (hitpoints >= 0) or perform any other trivial object specific management.
If you really like the super simple single update, you can let your manager classes derive from an abstract manager class which has an update function. Then, you'd have a list of managers, for which you update every frame:
[code]
foreach(GameObjMgr mgr in m_managers)
mgr.update();
[/code]
Then, you just have to worry about instantiating a manager class and inserting it into the manager list.

Anyways, I really don't know much about "good" software engineering and game architecture. I may be oversimplifying the core of game development: Managing lists of "stuff" and applying rules to them.
3

Share this post


Link to post
Share on other sites
spiro: Cool drawing [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]

jbadams: Sorry, it just seems that gamedev has changed so much since it was released. This defensive-ness is caused by the disrespect towards ASM programmers like Lamothe and myself. "Acting like you're better than everyone else" - Ultimately, no one is better than anyone else. We're all just little specks of Stardust. I apologize if this is your perception of me. "You've now asserted the alpha should always be the leftmost byte of a pixel" - Yes, that's how it was originally: AA.RR.GG.BB. Otherwise, it requires shift+and. Why change it? Why store things upside down and backwards? Why cause millions of headaches?

Back to the subject: OOP is not required. Never needed it. Edited by uart777
-5

Share this post


Link to post
Share on other sites
[quote name='uart777' timestamp='1351174823' post='4993797']
spiro: Cool drawing [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]
[/quote]
Coming from an artist such as yourself, that is a compliment. Thank you.
Your Simba t-shirt is also top-notch quality.


[quote name='uart777' timestamp='1351174823' post='4993797']
jbadams: Sorry, it just seems that gamedev has changed so much since it was released. This defensive-ness is caused by the disrespect towards ASM programmers like Lamothe and myself. "Acting like you're better than everyone else" - Ultimately, no one is better than anyone else. We're all just little specks of Stardust. "You've now asserted the alpha should always be the leftmost byte of a pixel" - Yes, that's how it was originally: AA.RR.GG.BB. Otherwise, it requires shift+and. Why change it? Why store things upside down and backwards? Why cause millions of headaches?
[/quote]
How it was originally?
I feel that you are a prime example of what was mentioned [url="http://www.gamedev.net/topic/633214-struct-vs-classes/page__st__20__p__4993348#entry4993348"]here[/url].
You learned early-on what compilers do internally and took it to heart.
You changed the way you coded based on what you learned from one compiler. You didn’t know that other compilers behave differently and may easily generate different code.
I can tell you for sure that there are rare cases in which my C compiler will generate horribly slow code for some switch cases.

How C++ code becomes machine-language is not strictly specified and you should understand that what my code “becomes” can vary depending on the compiler I use.

Don’t spend your time studying how some compiler created some code.
It would be better to spend your time reading the C/C++ specifications, and if you are so inclined make your own compiler. You will definitely learn a lot that way.


L. Spiro
2

Share this post


Link to post
Share on other sites
[quote name='uart777' timestamp='1351174823' post='4993797']
jbadams: Sorry, it just seems that gamedev has changed so much since it was released.
[/quote]

Ah, yes, I remember the old gamedev.net. You know, back in August of 2012 when you joined. Those were the days, eh?

[quote]
This defensive-ness is caused by the disrespect towards ASM programmers like Lamothe and myself.
[/quote]

I've heard of Lamothe. Got a couple of his books. Much respect for that dude, he's been around awhile.

Never heard of you, though.

[quote]
"You've now asserted the alpha should always be the leftmost byte of a pixel" - Yes, that's how it was originally: AA.RR.GG.BB. Otherwise, it requires shift+and. Why change it? Why store things upside down and backwards? Why cause millions of headaches?
[/quote]

How things were originally has exactly 0 bearing on how things are now. Pixel formats now have everything to do with hardware support. Modern hardware can handle RGBA data in so many different formats, that you are basically free to pick your preferred method.
0

Share this post


Link to post
Share on other sites
Spiro: My only intention is to defend ASM programmers like Andre Lamothe, Michael Abrash and Diane Gruber, the queen of graphics programming. She's hot, too :) Wouldn't you love to have a girl like her who does programming?
:) "Coming from an artist such as yourself, that is a compliment" - Thank you ;) Simba's my baby.

"I can tell you for sure that there are rare cases in which my C compiler will generate horribly slow code" - Creating a HL compiler is all about converting standard/infix expressions to RPN. You can perform optimizations in RPN format: Resolve constant subexpressions, replace mul/div with shifts by power of 2, reorder cumulative operations, double jmps/jxx, etc.

Jason: Sorry again for being way off subject, but the issue was brought up and I responded. Spiro, let's create another post about writing HL compilers :)
0

Share this post


Link to post
Share on other sites
fleblanc: "Modern hardware can handle RGBA data in so many different formats, that you are basically free to pick your preferred method" - Yes, but RGBA requires shift c>>8 and &0xFFFFFF. Please respond to this.
-2

Share this post


Link to post
Share on other sites
Who gives a shit? That's my response. Any code where you might conceivably have to shift and & is not going to be performance critical, and if it is performance critical, and you are noticeably slowed down by a shift and bitwise &, then you have seriously fouled up your design somewhere and you really ought to go back to the design table rather than worrying about pixel formats. Why are you worried about the performance of shifting and bit-wise operations anyway? Is this still 1996? If you need to switch something around, just swizzle in your shader and stop bringing up old shit and thinking it's a valid argument in the modern world.
2

Share this post


Link to post
Share on other sites
"Why are you worried about the performance of shifting and bit-wise operations anyway?" - Because it occurs milions of times per second in game/graphics programming. For example, one 1024x768x32 screen is 786,432 pixels which translates to 3,145,728 bytes. Multiply this by FPS.
-2

Share this post


Link to post
Share on other sites
[quote name='uart777' timestamp='1351182549' post='4993837']
"Why are you worried about the performance of shifting and bit-wise operations anyway?" - Because it occurs milions of times per second in game/graphics programming. For example, one 1024x768x32 screen is 786,432 pixels which translates to 3,145,728 bytes. Multiply this by FPS.
[/quote]

are you writing software renderers?
0

Share this post


Link to post
Share on other sites
Are you sure about that? Which driver team (NVidia, ATI, Intel) are you a member of, that you know what the driver is doing millions of times per second?

Like I said, is this still 1996? These days, performance critical code runs on the GPU, and the optimization landscape for modern GPUs is so much more complicated than "We must optimize out bitshifting and AND for performance" that it's actually hilarious that you still think the way you do.
2

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0