Jump to content
  • Advertisement
Sign in to follow this  
mrhodes

How to structure game code

This topic is 2949 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

Hey,

I'm working on a engine / game. Just enough of an engine to make small, simple 2D games and I have a question about the engine code. I've been hacking away at my project here and so far I have everything in one project (lib file). I'd like to make my games cross platform, but I'd also like to use native code for things like input and GUI coding.

So, I am thinking of setting up interfaces for things like input (keyboard, Mouse, and HID devices) and an interface for video.

I guess I am just wondering what do other people here like to do? One thing I am considering is making separate lib files for these devices. (libInputWin, libInputMac, libInputLin, etc) and then link in the right one based on platform.

I'm still playing around with ideas here, and not set on any particular method. Perhaps I am over thinking this, I just don't want to keep building onto this library over time and realize I did something wrong and hit a roadblock.

Thanks for any tips / advice.

Michael

Share this post


Link to post
Share on other sites
Advertisement
Quote:

I just don't want to keep building onto this library over time and realize I did something wrong and hit a roadblock.

Put away your compiler and go find a different hobby then.

Programming -- building software -- is about making mistakes. So is learning. You will need to make mistakes to figure out good and bad ways to do things. If you are going to be afraid about making mistakes and wasting time, that will hold you back. Get over it. You can always refactor your code, and in fact refactoring is an extremely important skill that's good to practice. Making mistakes, is, overall, a win-win situation.

As to your actual question: until you can actually make the code cross-platform, until you actually start doing it, don't worry too much about it. Yes, it's good to encapsulate things and isolate platform-specific code and all, but don't spend a lot of time thinking about it -- and when you do do it, focus on doing it at the source code level rather than at the object file level -- i.e., by creating different libraries -- just use platform-specific pre-processor magic for now, it will suffice.

The problem you will run into is that you don't know what you're doing -- your suggestion of multiple libraries is generally not workable at all, for example, because different platforms have different library formats. You'll spend a lot of time in build system maintainence mode so you can wrangle all the different build methods and options for all different kinds of formats and compilers, and I doubt that's what you want to be doing.

Similarly if you're not familiar with all the APIs you're going to use to make a particularly thing, such as input or graphics, work across all your platforms your interfaces are likely to be too tailored to the only API you know (the first one you implement). This will result in sub-par implementations anyhow, which you'll have to refactor so that your interfaces are not just a least-common denominator wrapper.

For now, focus on making your game. Worry about how to change that games code to support another platform when you actually start developing on that other platform. When you do start developing on another platform (or if you already are), design the interface you want with the system you know best, implement it, and then begin porting it, refactoring as necessary.

Do not be afraid to screw up.

Share this post


Link to post
Share on other sites
Thanks for the good advice :)

I ran into the problem before where I was working on making a scrolling 2d tile map. It was several years back, but what happened was that I got to the point where I could load a map or any size, I have a player character able to walk around and even had it so that certain tiles would be portals to other maps. It was great, then I was going to add onto it to make a simple 2d rpg... but I found my code kinda was at a dead end because of how I made the map format :(

I did learn a lot from that... Now I try to plan things better and look at the bigger picture. That's partly why I asked the question about organizing code from the begining with cross platform in mind.

But, you're right, I need to just focus on my game and get something done first.



Thanks again,

Michael

Share this post


Link to post
Share on other sites
Quote:
Original post by mrhodes

But, you're right, I need to just focus on my game and get something done first.



Just make a list of simple tasks and work on them one at a time. Once you finish the list, make a new one. That is what works best for me!

Share this post


Link to post
Share on other sites
I would second (third?) just not worrying about this at all. If this is your first or even second or third engine I can promise that you will HATE the code by the time you're done. For your next project you're going to want to start either from scratch or from scratch with little bits ported over. The only important thing is finishing your project, not making perfect code; such a thing does not exist. Finishing, even with code that will embarrass you later in life is the primary and most valuable learning experience.

Get used to throwing away code. That's how programming works. You throw away less over time, but it still happens pretty frequently.

-me

Share this post


Link to post
Share on other sites
Quote:
Original post by Palidine
If this is your first or even second or third engine I can promise that you will HATE the code by the time you're done.


This could not be more true. I really wish I understood this 6 years ago.

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!