Sign in to follow this  
Plethora

Organizing my code (specifically regarding graphics)

Recommended Posts

So, in taking all the CS classes I took in school something that the department seemed to want to stress was to keep graphics related classes apart from the rest of your code.  I never really questioned this and for the small projects I've worked on since it always worked well enough. 

 

As an example, let's say I am making a very simple soccer game.  I'm basically going to draw the field, the movable objects, and then an overlay of some sort that might say the time or the score.  Well based on the above I would have a graphics class and it would have a drawField method, a drawPlayers method, and a drawOverlay method.  I would have some kind of "main" method that would pass the player objects (for example) to the Graphics class and it would draw them, simple simple.  The field class, overlay class, and player class wouldn't even know what graphics api I was even using.

 

Well, as I've been trying to build some bigger and more complex games this method of doing things seems less than optimal.  I find I'm passing various objects all over the place and that my Graphics class can "see" basically every single bit of my code, which doesn't seem quite right to me.  I've looked up some open source games to see how they did it and I've seen a few different ways of doing things, but it seems like many have drawable objects drawing themselves.  So is that a better way to do things?  Is my original way of organizing ok?  Is there a "standard" way to do it?

Share this post


Link to post
Share on other sites

Having a separate drawing system is better than having all objects draw themselves. Your college department was correct in wanting to keep graphics decoupled from your objects.

 

In your drawing code, it's all right to have separate functions to draw different kinds of things at first, but as your project grows it would make sense to have a standardized data structure which to feed into fewer "final" drawing functions or just one, if possible. "Final" meaning in this case, being the step that sends all the data to the GPU.

The open source games that you were looking at probably had completeness and functionality as a greater priority over code organization and maintenance.

Share this post


Link to post
Share on other sites
I would rather have a "physical" game logic side representation of the object, and a graphical representation.

Then you make the actual object which links all those together.

So in a way the object still draws itself, but the drawing is separated and there is no dependency between the game logic and rendering.

You can then make the "glue" class also link player controls and lets say networking or sound to the game logic side representation.

This will also let you store the graphical versions in one place so you can quickly iterate through them telling them to render themselves.

Share this post


Link to post
Share on other sites

Having a separate drawing system is better than having all objects draw themselves.


 

so its mean we can optimize our game by using the same sprite/texture which only loaded once on the Drawing class, then apply to all game objects ??

 

correct me if i'm wrong smile.png

Edited by Fs02

Share this post


Link to post
Share on other sites

Having a separate drawing system is better than having all objects draw themselves.


 

so its mean we can optimize our game by using the same sprite/texture which only loaded once on the Drawing class, then apply to all game objects ??

 

correct me if i'm wrong smile.png

 

That's correct.  Load every texture you need just once, and have each object reference the sprite texture. All sprite objects have the following in common- a location, a size (dimensions), and a texture. Those get passed to the Drawing class and it's the minimum amount of information that the Drawing class needs to draw the object.

Share this post


Link to post
Share on other sites

Also it means that changing the way you handle graphics can be done without completely re-writing all of your entities and physics code, etc. Decoupling is a very good idea in general. You want to try to design things such that changes to one aspect of the program inflict minimal redesign in other areas.

Share this post


Link to post
Share on other sites

As an example, for each Minecraft mob there is a dedicated class that knows how to render it (from what I've "researched"). It probably was good idea when they had only creepers but when you have 20 or more different kind of enemies (without even counting NPCs, wildlife, structures, items, etc), it can easily get out of hand.

 

Lots of boilerplate code (hard to maintain/improve), state switches (bad for performance) and so on.

 

MVC patter is pretty nice for those things since it defines a clear barrier between what you're showing to the player (ie, guy getting hit by an arrow) and what is happening at the "game logic" level (instance of class Guy got its health reduced by 20 units).

 

You can change what is shown to the player (ie, arrows got changed by flying magical projectiles that now have some sparkle effect) and the behavior of the logic of the game can remain the same (guy instance got hit, health reduction by 20).

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