Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411432 Posts in 69363 Topics- by 58417 Members - Latest Member: gigig987

April 20, 2024, 05:51:56 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Looking for generic advice for creating a game engine
Pages: [1]
Print
Author Topic: Looking for generic advice for creating a game engine  (Read 1812 times)
Deckhead
Level 1
*



View Profile WWW
« on: October 12, 2014, 02:54:03 PM »

Hi fellow devs,

Years ago, around the time of DirectX7, back when DirectDraw was separate to Direct3D, I made an RPG game engine (FF clone). It didn't have sound, but it did most of the cool stuff like tilemaps and I even implemented a rudimentary scripting language and characters talking etc. I never made a game out of it because I lost interest, partially because the project got too big for 17 year old me.

These past few months I've started work on a new game that I need to use 3D for. It's in space and all it really needs to be is pretty spheres and a UI. I thought that should be fairly simple.

So, I've been reading all about OpenGL and SDL2 and I've started to get some cubemapped spheres being displayed with some lighting. I'm working on shaders now for lens flare or something to make the sun look cool. Anyway, I'm already starting to see that my code is getting difficult to manage.

Does anyone have some generic advice on how to structure a 3D engine like this? I've got a need for objects to use different materials and different shaders; I've got the possibility of millions of objects in total. I've got a need for screen-wide post processing, probably in multiple passes.

It's all working well now, but I have only two objects. I'm concerned that as scene-complexity grows that it becomes unmanageable. Just looking for generic advice.
Logged

Dewfreak83
Level 1
*



View Profile WWW
« Reply #1 on: October 12, 2014, 08:18:21 PM »

The advice I have is indeed general - not targeting "games" and "engines" specifically but: Software Engineering; more specifically Software Architecture.

If you are a reader, I highly suggest reading through the following two books:

One of the most important things we have to worry about as programmers is figuring out how to organize our code. In that is one of the goals of Design Patterns - common ways others have solved similar problems. You may even create your own patterns (the key is to indeed follow some sort of pattern - if you do it wrong, at least do it consistently wrong).

What I think you are initially seeking here is a set of patterns that have worked for others - yes? But you'll also find that you may need to tweak, alter, or use a different pattern based on your specific (ever-changing) situation - so what you really should work on is finding away to identify and implement the appropriate patterns you want to put into place (these books help to identify and build these skills).

Throwing the term "Engineering" and "Architecture" can also lead to over-engineering or making things far more complicated than they need me. Partly this is resolved through experience, but the second book helps to identify some tools you can use to help keep yourself in check.

The books help, but now that you have some of the key pieces in place, and have identified some heartaches - I suggest you draw a picture of all your components. Show what components make up other components. You can use a white board or google draw. Try to keep it high-level - not describing every line of code, function or class - perhaps just capabilities. This presentation does a pretty good job on helping to identify how to make drawing a diagram useful (and not go over-board):

Once your current code is sketched out, draw a line between each of your components if they use or reference another one. The more of these lines you have - the worse. You first goal is to figure out a way to reduce these number of lines. A design pattern or two may help, but the key thing here is just "organizing away as many of the interdependent relationships" as you can.

After you have done this - start breaking down your high-level components into lower-level ones. Here you want to try and have each component be responsible for only one capability. Don't try to over-do this stage (where the second book helps), but perhaps just separate out the concerns one or two levels deeper than where you started. If it is not enough - your code will tell you in time (how you got to where you are today).

Lastly - depending on how large or long your project - you will revisit this again. The requirements and "what we know" change all through development - you may find it getting messy again and need to revisit these steps... just be pragmatic about it.

If you are on the fence regarding design or architecture, this is a nice short article on the matter that I think sells it well:

Probably not exactly what you were looking for, but mastering these techniques will help you in the long haul.
Logged

Boreal
Level 6
*


Reinventing the wheel


View Profile
« Reply #2 on: October 12, 2014, 08:35:58 PM »

One of the most important things we have to worry about as programmers is figuring out how to organize our code. In that is one of the goals of Design Patterns - common ways others have solved similar problems. You may even create your own patterns (the key is to indeed follow some sort of pattern - if you do it wrong, at least do it consistently wrong).

What I think you are initially seeking here is a set of patterns that have worked for others - yes? But you'll also find that you may need to tweak, alter, or use a different pattern based on your specific (ever-changing) situation - so what you really should work on is finding away to identify and implement the appropriate patterns you want to put into place (these books help to identify and build these skills).

Throwing the term "Engineering" and "Architecture" can also lead to over-engineering or making things far more complicated than they need me. Partly this is resolved through experience, but the second book helps to identify some tools you can use to help keep yourself in check.

This is sound advice, but also be wary of overusing patterns where you don't need them.  It's almost as important to know when NOT to use something as it is to know when and how.
Logged

"In software, the only numbers of significance are 0, 1, and N." - Josh Barczak

magma - Reconstructed Mantle API
Deckhead
Level 1
*



View Profile WWW
« Reply #3 on: October 12, 2014, 09:12:59 PM »

Probably not exactly what you were looking for, but mastering these techniques will help you in the long haul.

You were right in that I was looking for specific patterns; but I think this is the best answer I will get. I'll start reading and understanding useful patterns before I get too much more code done.
Logged

ThemsAllTook
Administrator
Level 10
******



View Profile WWW
« Reply #4 on: October 13, 2014, 05:32:51 AM »

I'd advise caution in thinking about what you're writing as a "game engine" if your goal is to write a game. Tinkering around with tools is lots of fun, but it can lead to doing work that doesn't help you get your game done. When I'm working on the shared framework I use for my own games, I follow a strict rule that no feature can go into it that isn't directly needed in a game I'm currently writing. Speculative coding based on what you might need in the future when you don't know for sure that you need it today will eat away at your time and slow your project to a crawl.

If you're committed to designing this as a game engine for other developers to use, things change a bit, but if it were me I'd still try to avoid implementing something if I haven't needed it myself or gotten feedback from someone else who needs it. It might also make sense to finish at least one game with the thing (ideally at least two, so you can gauge how generic it is in practice) before opening it up to the public.
Logged

nikki
Level 10
*****


View Profile
« Reply #5 on: October 13, 2014, 11:35:22 PM »

what he said.
Logged
RandyGaul
Level 1
*

~~~


View Profile WWW
« Reply #6 on: October 14, 2014, 12:01:52 AM »

I'd recommend buying the book Essential Mathematics by Jim Van Verth. The book comes with some C++ source code for the makings of an engine, and gives a pretty decent idea of one might start structuring a 3D game engine. I refer to the source quite often!

I also liked the advice above me by ThemsAllTook.
Logged
nikki
Level 10
*****


View Profile
« Reply #7 on: October 14, 2014, 01:02:21 AM »

But if you are serious about wanting to write engine asopposed to games, you could take a look at some existing opensource ones, ogre, panda3d and irrlight come to mind, but there might be better/simpler ones I don't know about.
Logged
Layl
Level 3
***

professional jerkface


View Profile WWW
« Reply #8 on: October 14, 2014, 01:21:40 AM »

I found that especially looking at Ogre and Axiom helped me get my framework into a good direction. As well it might be a good idea to look at implementations of the entity-component (for example Unity) and entity-component-system (for example Artemis) patterns, since it's likely you'll want some form of entities.
Logged
SelfTitled
Level 1
*



View Profile WWW
« Reply #9 on: October 14, 2014, 03:39:54 AM »

A great book I particularly like is Game Engine Architecture (http://www.gameenginebook.com/) by Jason Gregory.

Some advice that is always given is to abstract your hardware layer systems (i.e. Input, Audio, Graphics). SDL2 provides an abstraction for a lot of these systems but I would write a wrapper around SDL. Makes porting your engine to different hardware a whole less painful.

I think memory management system should be one of the first, if not the first, system you write. Mainly because it is a lot of work to retro fit an engine with a new memory management system. I have no idea how to handle how SDL allocates memory though.

Some advice I've been given in the past was not to worry about optimisations at first. Get the systems in and go back and optimise them as when needed.
Logged

Adam_
Level 1
*



View Profile WWW
« Reply #10 on: October 16, 2014, 11:47:44 PM »

I'd advise caution in thinking about what you're writing as a "game engine" if your goal is to write a game. Tinkering around with tools is lots of fun, but it can lead to doing work that doesn't help you get your game done. When I'm working on the shared framework I use for my own games, I follow a strict rule that no feature can go into it that isn't directly needed in a game I'm currently writing. Speculative coding based on what you might need in the future when you don't know for sure that you need it today will eat away at your time and slow your project to a crawl.

I couldn't agree more.

If you're committed to designing this as a game engine for other developers to use, things change a bit, but if it were me I'd still try to avoid implementing something if I haven't needed it myself or gotten feedback from someone else who needs it. It might also make sense to finish at least one game with the thing (ideally at least two, so you can gauge how generic it is in practice) before opening it up to the public.

In my experience, it doesn't make sense to write a game engine purely for others to use. You can't efficiently develop an engine without developing a game at the same time in order to test what you're doing. Chances are if you don't use your engine in the beginning, no-one will - and developing without feedback or thorough testing is a dangerous endeavor.

I've been developing the Duality engine since 2011 and I try to put in "example" tasks to write documentation and develop small games whenever possible. The amount of bugs and usability problems I've discovered during these tasks is terrifying, despite the fact that the engine is already being used in a commercial project and several hobbyist games. They're fixed now, but they wouldn't be if I had been doing "pure" engine development. Now go figure what happens when you've got no users at all and don't ever develop a game with your engine.

That's why this is one of the most important sentences of your initial posting:

Quote
These past few months I've started work on a new game that I need to use 3D for. It's in space and all it really needs to be is pretty spheres and a UI. I thought that should be fairly simple.

So your engine has a purpose and testbed right from the beginning. This is great! Smiley Just don't get carried away by all those features "for future use" that spring to your mind. Try to create a clever API that lets you extend your engine in the future, but don't do so unless you need it. But again, I think others have already said this Smiley
Logged

anthnich
Level 1
*



View Profile WWW
« Reply #11 on: October 24, 2014, 09:16:15 AM »

A great book I particularly like is Game Engine Architecture (http://www.gameenginebook.com/) by Jason Gregory.

Personally, I think this book is an absolute must read if you are going to code your own engine. A ton of insight on handling resource, states, etc. It even goes into game console memory concerns. Great read.
Logged

Turnover @ Steam
eigenbom
Level 10
*****


@eigenbom


View Profile WWW
« Reply #12 on: November 01, 2014, 09:33:41 PM »

I too recommend Gregory.

As for your engine, Keep it simple and focussed on the specific case of your game. Also don't take OO too seriously or you'll end up with a horrible mess like Ogre3D.
Logged

KyleBatteson
Level 0
*


View Profile
« Reply #13 on: November 03, 2014, 01:05:20 PM »

Hi fellow devs,

Years ago, around the time of DirectX7, back when DirectDraw was separate to Direct3D, I made an RPG game engine (FF clone). It didn't have sound, but it did most of the cool stuff like tilemaps and I even implemented a rudimentary scripting language and characters talking etc. I never made a game out of it because I lost interest, partially because the project got too big for 17 year old me.

These past few months I've started work on a new game that I need to use 3D for. It's in space and all it really needs to be is pretty spheres and a UI. I thought that should be fairly simple.

So, I've been reading all about OpenGL and SDL2 and I've started to get some cubemapped spheres being displayed with some lighting. I'm working on shaders now for lens flare or something to make the sun look cool. Anyway, I'm already starting to see that my code is getting difficult to manage.

Does anyone have some generic advice on how to structure a 3D engine like this? I've got a need for objects to use different materials and different shaders; I've got the possibility of millions of objects in total. I've got a need for screen-wide post processing, probably in multiple passes.

It's all working well now, but I have only two objects. I'm concerned that as scene-complexity grows that it becomes unmanageable. Just looking for generic advice.

Hey, I've just finished building an engine for a 3d top-down shooter supporting large maps and
a lot of enemies and objects. You're going to have to use an ide that supports virtual folders.
They help with organizing your source files under the correct categories. Example:

[Workspace]My_Engine:

[Folder]Display:

   - display_init.cpp
   - opengl.cpp
   - directx.cpp
   - shaders.cpp

[Folder]Map_Components:

   - actors.cpp
   - objects.cpp
   - effectors.cpp
   - particle_emitters.cpp
   - particles.cpp
   - projectiles.cpp
   - map.cpp

[Folder]Sound:

   - sound_init.cpp
   - soundfx.cpp
   - streams.cpp

[Folder]Controllers:

   - keyboard.cpp
   - mouse.cpp
   - controller.cpp

[Folder]GUI:

   - hud.cpp
   - menu.cpp

[Folder]Backend:

   - main.cpp
   - file_loader.cpp
   - events.cpp
   - maths.cpp
   - memory.cpp

[Folder]Headers:

   - myengine_typedefs.h
   - myengine_functions.h
   - myengine_global_variables.h

This is just if you feel the need to organize your source files and if you ever decide to add more things to
your game. 3D scenes aren't that easy to manage. So, I hope you find my knowledge useful.

To manage a lot of objects in memory, I would use array variables as they're easier to sort and access.
You have to limit them though, but you can still allocate memory to an enemy array of say 1000000 elements.
This is also better than using linked lists which are the worst bits of code to work with.

In c/c++ it would look like this: ENEMY *enemy_array = (ENEMY **)malloc(sizeof(ENEMY *)*1000000);

But, you must be careful to allocate memory to each element that you use before adjusting enemy's properties. This
technique applies to scene objects as well.

Also, if you can use structures instead of classes, it will be easier to manage. I don't know what
language you're programming with, but you can always google up derivatives between c/c++ and whatever you're using.

The last thing that is VERY important with managing a scene and displaying it is limiting what you display based
on distance or visibility within your screen. You will have to create an array that will contain visible nodes
and all your nodes.

(Nodes represent every object on your map - enemies, your character, objects, static soundfx, etc.)

You will attach each class or structure (enemy, projectile, object) to a node so that it can
be sorted into what's visible and what's not. Whatever gets put into your visible nodes array
will be the ones that you draw, the rest will be skipped.

You can use this technique and modify it for other derivatives. Anyway, I hope you find this useful because
you're gonna struggle to get find generic 3d scene info for your game development. Otherwise, you should look
into using an engine like Unity. Most of the work's been done and a lot of guys use it to build all kinds
of indie games. 
 
Logged
oahda
Level 10
*****



View Profile
« Reply #14 on: November 04, 2014, 12:46:39 AM »

I'm currently doing what I've seen suggested before: codeveloping an engine/framework alongside my actual game that I want to make with it.

I wrote the foundation for the engine to the point where I started being able to set up a window, populate it with components and use scripts and detecting input. The barebones. Now I've already started working on the game with lots and lots missing from the framework, and I only add new features when the game needs them (with a little bit of obvious thinking ahead, pulling from experience of the needs of most other games I've made or played – even if my game only needs to detect whether a key was pressed at the moment, I still add the corresponding functions to check whether it was released or is currently being held down as well, of course).

That way I won't be wasting time on premature optimisations or a bunch of speculations as to what a game making framework needs and I have the game itself as a sharp testing environment to make sure the framework both performs and is reasonably smooth to work with.

I also refactor some older things when I realise it would benefit the game to have them work in a different way (so long as it's not game-specific – the goal is obviously to be able to use this for the next game too).

I find this to be working neatly so far and I get much more productive when I don't have to plan ahead for a year and probably end up rewriting half the stuff before I can actually make a game. Codevelopment!
Logged

Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic