★ TouchArcade needs your help. Click here to support us on Patreon.

Making your own engine

05-01-2011, 05:37 AM
#1
Joined: Apr 2011
Location: Kyoto, Japan
Posts: 281
Making your own engine

Hi there
I would like to start a discussion with other people who are building their own engines, rather than using existing ones.
When I came to iOS development, I already had a large personal library of code that took little modification to get working. I have a very basic, bare bones objective C front end, with a few hooks to my C++ library.
I come from a PC background and have a very old Mac, so my system is fully cross platform. I do 95% of my development on PC.
Anyone else chosen a similar path? What kind of architecture do you have?
I am eager to discuss and share ideas/strategies/optimisations/code snippets with people.

Examples:
- Maths library: vectors matrices, quaternions, etc
- OpenGLES wrapper
- Texture/Audio/Input managers
- Sprites, particle systems, tiled backgrounds, etc
- UI components

And anything else you can think of. I am enjoying building my engine almost as much as I am enjoying making games that use it.

What are your thoughts? I'd love to hear from you.

Cheers

Game source code for PC, Mac, Linux and iOS here.
05-01-2011, 07:42 AM
#2
Joined: Oct 2009
Location: Melbourne
Posts: 250
What I do is kind of similar. I enjoy making game engines too I've started from scratch numerous times, learning a bit more each time.. my first proper attempts were with the BlitzMax language on PC, then later C and C++.

My iOS game engine started as a Win/OSX thing. It's basically wrappers for graphics and sound handling. On Win/OSX I was using SFML for those, then when I made the iOS version of the engine I changed it to GLES and OpenAL, of course.

Aside from that it's mainly a bunch of sprite/particle systems. I haven't used any physics or maths libs yet, or any scripting stuff.. my games so far are pretty simple.

In my first iOS game the engine is really complicated.. I was learning C++ and trying to be all clever.. everything is super generic, all types of object inheriting from one base class. All object behaviours are in small modular plugin-like functions which can be added to any given object.. These behaviours can themselves load new behaviours, or swap between different sets of behaviours etc. Or an object could swap its behaviours with those of another object, or choose a random behaviour.

Very flexible and kind of cool (well I think so, but I've never used any professionally made game engines, this sort of thing is probably bog standard ) but in the end it's just over complicated I think - my game doesn't even use half the features of it!

So my second game I stripped all that out and just have a very simple sprite class with each type of object having its behaviours coded in specifically. Nowhere near as flexible, but since I'm not actually *using* that flexibility for anything this doesn't matter.


In the Win/OSX version of my engine I made a full GUI system with moveable windows, buttons, list boxes, text fields etc.. and a kind of complex tilemap system with built in map/object editor (was going to make a platformer at the time)...

I haven't put any of that in my iOS engine because I haven't needed it. So my engine isn't properly multi platform, the computer and iThing versions are out of sync... I'd like to get back to that eventually and fix it all up though.

As it is now the two versions of the engine are pretty easy to port stuff between. It took me about 15 minutes to get simple Win/OSX versions of my iOS games up and running.


As for optimisations, I don't have that much experience to share.. my games could definitely be optimised much more, but I don't know how yet.. luckily they're simple 2D pixelly affairs so it's not that important.

I did spend a lot of time trying to get the openGL drawing to be nice and fast though, so I could chuck around lots of sprites. The biggest improvement I made to that was when I learnt how to add every quad to a list and draw them all with a single call - instead of drawing each one individually. FPS went from ~40 to 60.. sorted!

~

I think doing all this engine coding is lots of fun, and a great way to learn... but tends to mean it's a looong time before you get up to actually making games. But, thinking about it now, I'm starting to get the urge to start a new game engine from scratch again, with all the new stuff I've learnt since last time... eek!

Cool thread idea btw Charybdis, looking forward to reading more replies!

05-01-2011, 08:08 PM
#3
Joined: Mar 2009
Location: Singapore
Posts: 1,770
Send a message via AIM to kohjingyu
I would like to ask - how does one make an engine? How do you start? Making it a framework, or a bunch of files like the great cocos2d?
05-01-2011, 09:51 PM
#4
Joined: Apr 2011
Location: Kyoto, Japan
Posts: 281
Quote:
Originally Posted by kohjingyu View Post
I would like to ask - how does one make an engine? How do you start? Making it a framework, or a bunch of files like the great cocos2d?
Well, I started just by having a load of files that I imported for each project. But eventually it got to cumbersome and I made a framework.
I am very new to Mac and XCode, so I learned the bare minimum to get it working.
As I said, I work mainly with Visual Studio. I really like the way a workspace can have multiple projects. I haven't yet been able to do that under XCode, but I keep on trying.
I also have a master 'shell' project. Whenever I start a new project I just copy it and it has all basic stuff done.

Game source code for PC, Mac, Linux and iOS here.
05-01-2011, 10:01 PM
#5
Joined: Apr 2011
Location: Kyoto, Japan
Posts: 281
Quote:
Originally Posted by nyarla View Post
So my second game I stripped all that out and just have a very simple sprite class with each type of object having its behaviours coded in specifically. Nowhere near as flexible, but since I'm not actually *using* that flexibility for anything this doesn't matter.

Nyarla, thaks for the great response.
Your engine sounds pretty cool. Mine is still fairly immature, but it's developing day by day.
I agree with you, it is easy to start getting over complicated, throwing lots of clever OO programming at it. But at the end of the day, simple is best.
I try to keep my stuff clean, but dirty hack do find there way in during the development process.
I will probably need to sit down and do a complete refactor/overhaul at some point.

Game source code for PC, Mac, Linux and iOS here.
05-01-2011, 10:07 PM
#6
Joined: Apr 2011
Location: Kyoto, Japan
Posts: 281
If anyone is interested, I made a very minimal 2D vector class. The one I use in my engine is a bit more advance, with const correctness and a bit of template goodness, but if you need something simple to slap into your own engine, feel free to use it.
Let me know if you find any bugs or better ways of doing it.
You can read the blog post about it here.

Code:
/*
 *  v2.h
 *  Minimal 2D vector class
 *  by Dale Thomas
 *  Copyright (C) 2011 Scylla Games
 *  www.scyllagames.com
 */

#ifndef __SCYLLAGAMES_V2_H__
#define __SCYLLAGAMES_V2_H__

#include <math.h>

class v2
{
private:
	float data[2];
public:
	v2() { data[0] = 0.f; data[1] = 0.f; }
	v2(float _XValue, float _YValue) { data[0] = _XValue; data[1] = _YValue; }
	float& operator[](int _Index) { return data[_Index]; }
	v2 operator+(float _Scalar) { return v2(data[0]+_Scalar,data[1]+_Scalar); }
	v2 operator+(v2& _Vector) { return v2(data[0]+_Vector[0],data[1]+_Vector[1]); }
	v2 operator-(float _Scalar) { return v2(data[0]-_Scalar,data[1]-_Scalar); }
	v2 operator-(v2& _Vector) { return v2(data[0]-_Vector[0],data[1]-_Vector[1]); }
	v2 operator*(float _Scalar) { return v2(data[0]*_Scalar,data[1]*_Scalar); }
	v2 operator*(v2& _Vector) { return v2(data[0]*_Vector[0],data[1]*_Vector[1]); }
	v2 operator/(float _Scalar) { return v2(data[0]/_Scalar,data[1]/_Scalar); }
	v2 operator/(v2& _Vector) { return v2(data[0]/_Vector[0],data[1]/_Vector[1]); }
	void operator+=(float _Scalar) { data[0]+=_Scalar; data[1]+=_Scalar; }
	void operator+=(v2& _Vector) { data[0]+=_Vector[0]; data[1]+=_Vector[1]; }
	void operator-=(float _Scalar) { data[0]-=_Scalar; data[1]-=_Scalar; }
	void operator-=(v2& _Vector) { data[0]-=_Vector[0]; data[1]-=_Vector[1]; }
	void operator*=(float _Scalar) { data[0]*=_Scalar; data[1]*=_Scalar; }
	void operator*=(v2& _Vector) { data[0]*=_Vector[0]; data[1]*=_Vector[1]; }
	void operator/=(float _Scalar) { data[0]/=_Scalar; data[1]/=_Scalar; }
	void operator/=(v2& _Vector) { data[0]/=_Vector[0]; data[1]/=_Vector[1]; }
	float lengthSquared() { return data[0]*data[0]+data[1]*data[1]; }
	float length() { return sqrtf(lengthSquared()); }
	v2 unit() { return *this/length(); }
	float norm() { float l=length(); *this/=l; return l; }
	static float dot(v2& _Vector1,v2& _Vector2) { return _Vector1[0]*_Vector2[0]+_Vector1[1]*_Vector2[1]; }
};

#endif

Game source code for PC, Mac, Linux and iOS here.

Last edited by Charybdis; 05-01-2011 at 10:44 PM.
05-02-2011, 01:35 AM
#7
Joined: Mar 2009
Location: Singapore
Posts: 1,770
Send a message via AIM to kohjingyu
I have a suggestion: make the code cleaner :P

The curly braces should take up a line each in my opinion - I always MUST have it like that. Some people like

Code:
method {
}
though. But

Code:
method { }
is too messy for me
05-02-2011, 01:52 AM
#8
Joined: Apr 2011
Location: Kyoto, Japan
Posts: 281
Quote:
Originally Posted by kohjingyu View Post
I have a suggestion: make the code cleaner :P

The curly braces should take up a line each in my opinion - I always MUST have it like that. Some people like

Code:
method {
}
though. But

Code:
method { }
is too messy for me
I totally agree with you. I always have curly braces on their own line.
EXCEPT when the function is very small.
Or, as in this case, where I wanted to save space for posting code online.

Game source code for PC, Mac, Linux and iOS here.
05-02-2011, 02:18 AM
#9
Joined: Mar 2009
Location: Oslo, Norway
Posts: 731
Really? I prefer:

Code:
-(void) insanelyGreatMethod{

    // amazing stuff goes here

}
Where the method starts shold be obvious, so the only question remaining is where it ends.
05-02-2011, 04:09 AM
#10
Joined: May 2009
Location: UK
Posts: 741
Quote:
Originally Posted by kohjingyu View Post
I have a suggestion: make the code cleaner :P

The curly braces should take up a line each in my opinion - I always MUST have it like that. Some people like

Code:
method {
}
though. But

Code:
method { }
is too messy for me
Personally, I find
method {
}

too messy, I like to see the braces match up vertically:

method
{
}

same for ifs, for loops etc. I've worked at places where this is an enforced coding standard.

Personally, I use method{ } for inline stuff but only if the entire code fits on one line without scrolling.

Mor controversially, I also do brace free stuff like:

if(a>b) c=a;
else c=b;

when stuff is very simple as I can't see the point of eating up yet more lines of source code when I use a lot of white space and my vertically aligned brace thing 'wastes' lines anyway. In fact I've also used:

for(y=0; y<MAXX; y++) for(x=0; x<MAXX; x++)
{
doSomething(x,y);
}


On the subject of the original post, Your system sounds a lot like mine, the exceptions being that my Graphics wrapper uses D3D under the hood on the Windows version. I also have a pretty cool custom memory manager (designed by a colleague at a previous company) which forces me to allocate in a slightly odd way but more than pays for itself with the level of control it gives.

-=< Fat Owl With A Jetpack >=-
-=< Topia World Builder >=-
-=< Twitter >=-
-=< Blog >=-