Organizing my code (specifically regarding graphics)

Started by
7 comments, last by TheChubu 11 years, 2 months ago

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?

I'm working on a game! It's called "Spellbook Tactics". I'd love it if you checked it out, offered some feedback, etc. I am very excited about my progress thus far and confident about future progress as well!

http://infinityelephant.wordpress.com

Advertisement

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.

New game in progress: Project SeedWorld

My development blog: Electronic Meteor

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.

o3o

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

Have a look at the Model-View-Controller (MVC) pattern. :)

Start by doing what is necessary; then do what is possible; and suddenly you are doing the impossible.

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.

New game in progress: Project SeedWorld

My development blog: Electronic Meteor

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.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

Have a look at the Model-View-Controller (MVC) pattern. smile.png

Thanks, I spent some time reading up on MVC and it was well worth it.

I'm working on a game! It's called "Spellbook Tactics". I'd love it if you checked it out, offered some feedback, etc. I am very excited about my progress thus far and confident about future progress as well!

http://infinityelephant.wordpress.com

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).

"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

My journals: dustArtemis ECS framework and Making a Terrain Generator

This topic is closed to new replies.

Advertisement