Jump to content
  • Advertisement
Sign in to follow this  
ScottC

Structuring a games code...

This topic is 4826 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've made a complete mess of code thats completely unorganized while working on an Asteroids game, I am wondering if anyone has some tips to better organize the code?

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
Do read some books on OOP, this should help. (I presume ofcourse you are using a OOP language).

Share this post


Link to post
Share on other sites
You should firstly organize your ideas in your brain, and then start painting your code here and there. Try to break apart the game into smaller parts, possible unrelated to each other and work each of them one by one.

Share this post


Link to post
Share on other sites
Redo the same project many times

I used to be awefull when it came to "losing" sourcecode, I'd end up writing the same project 2 or 3 times -_-
But each time I did I noticed something - the code was getting smaller & more precise
Because I'd done it before my brain knew the design patterns it was going to follow
I was able to think 2 or 3 stages ahead & because of that real structure started to form

If you want to make the source better just throw it away & start again
Slowly the different design pattersn start to stick; you start generalising & structure emerges. It becomes automatic & you're able to think further ahead

There's no substitute for practice really, but generally the best way I've found to practice is to redo what you've just done - but from scratch

sorry if I'm repeating myself but it is totally true ;] trust meh

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Here's one strategy that I've found very helpful:

For every project I do, I link against a second project that contains all my general, reusable code. When I start a new gaming or graphics project, I've usually trying something new so the code gets a bit long and messy. Once I get everything working though, I try to go back and find common reusable pieces of code - for example, a generic functions for finding an intersection between a line and plane - and move that out of the gaming/graphics project and into my general coding library. And, very importantly, I clean up the code before putting it into my generic code library; no messy code in my common code base! Then I repeat this process on the game/graphics code until my project contains only code that's really specific to that project.

I use this generic libray in all my projects - so when I start the next project, I no longer have to rewrite those tedious bits of framework code. Of course, you might argue I should be using a third-party library for that type of code anyway, but since I wrote all the code, I know how it works, and I have complete freedom to change it - which can sometimes be a pain with third-party libraries even if they're open-source. (Not to mention I'm usually just coding this stuff for the learning/fun of it).

Another benefit is if I end up scrapping that initial gaming project and never manage to get it to compile again, it wasn't "wasted" time since all my other projects (existing and future) benefit from everything I added to the generic library. [I used to be really bothered about putting lots of time and effort into projects that died about 10-75% through...now, I don't feel so bad because later projects usually benefit from what I added during that development.]

I then also make a serious effort to keep the code in my general programming library organized, documented, and correct. I run doxygen on it, I browse old files I haven't looked at every once in a while, and even just go through purely to clean-up the formatting and comments from time to time. Keeping the code organized is actually a bit easier in a general programming library though, because the code going into this library is supposed to be general (i.e. no or few dependencies on other code, data structures, etc.) and I find it's easier to categorize general code. I'm working in C++, so I just drop the code into different namespaces, such as MyLibText, MyLibGui, MyLibGeometry, MyLibDirectX, etc. Also, there are lots of good generic gaming libraries out which you can use as a basis for how to organize your own code (i.e. browse their documentation and realize they have a really cool Image API and model yours after theirs).

Also, I will note that over time I've restarted my generic library several times from scratch because as I learn more, I realize better ways of doing and organizing things.

Maintaining an organized codebase of my own reusable code has been a huge help to the many small projects I like to occasionally throw together.

Share this post


Link to post
Share on other sites
Just a quick guide:

Reusable Code:

Rendering Engine
Texture Manager
Image Loader
Audio Engine
Math Libary
Utility Classes
Physics Engine
Scene Graph
Pathfinding
General AI Routines (fuzzy logic, state machines, etc).
User Interface
Game Object Manager
Configuration File
XML Loader

New Code:

Game Logic
Game Objects/Characters
Model Manager (could be standardised with good planning)
Level Loader (never has a project where the file format hasn't changed!)

Share this post


Link to post
Share on other sites
I apologize for the lack of info, I am using C++ with SDL and OpenGL, and i've made a real mess of my asteroids code.
http://rafb.net/paste/results/6O2hU981.html

Thanks for the articles, and information, I won't be buying any books though.

Edit: I wanted to know about like what to store in functions and stuff like that...

Share this post


Link to post
Share on other sites
Woah, that's a severe lack of abstraction you have in your code there son! :)

A good heuristic is for each function to do only a single thing. For example, you could break your code up and have an UpdatePlayer function, as well as a HandleInput function as well as a RenderPlayer function etc. Don't make your UpdatePlayer function render your player as well.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!