Creating a Framework in Xcode

My game engine is currently in beta phase. I have not released the engine to the public yet. Part of it is due to unresolved bugs present in the engine. Although the engine can handle a full blown game, such as a soccer game , I'm afraid that it is not ready for prime time.

Regardless if I release the engine to the public or not, I am interested in knowing how I would create my engine into a distributable framework using Xcode. It turns out that creating a framework using Xcode is very simple.

These are the steps to create a framework in Xcode:

Creating a framework

Step 1:

Create a new project and select "Cocoa Touch Framework."

Step 2:

Give your framework a name and select "Objective-C" as the language. Make sure the "Include Unit Tests" box is unchecked.

Xcode will generate a header file with the name of your framework. In this header file, we will include the header files of your API. So let's create the API.

Step 3:

Create a file representing your API and give it a name. I called the files "MyAPI."

Step 4 (Optional):

You don't have to do this if you code in Objective-C. But I like to code in C++; thus I need to change the extension of the files to ".mm" as shown in the image below.

Step 5:

Declare and implement your API files. In this example, I created a class with a "printHello" method.

Step 6:

Your users will not be able to use your framework unless you make the API header files Public. If you select the MyAPI header file and open up Xcode Utility pane, you will notice that under "Target Membership" the header file is set to "Project."

Make sure to set the header file to "Public."

Step 7:

Now, we need to import all the public header files of your API. To do so, open up the framework header file "MyFirstFramework.h" and import all public header files of your API. See highlighted line in the image below:

Here is a larger image. See line 20.

Screen Shot 2017-05-20 at 12.10.54 PM.png

Step 8:

Select Product->Build to build the framework. You can also use Command+B to build it.

At this point, the framework is ready to be distributed to your users.

Step 9:

If you need to locate your framework, right-click on the framework and click "Show in Finder." See the image below.

Screen Shot 2017-05-20 at 10.26.41 AM.png

Testing your Framework

Step 1:

Let's test your framework in a new project. Create a new "Single View Application" project in Xcode.

Step 2:

Give it a name and select "Objective-C" as the language.

Step 3:

Copy your framework into the new project. To locate your framework, see Step 9 above.

Step 4:

We need to make sure that the new project has the proper settings before it can use the framework.

Click on Build Phases and make sure that your framework is in the Link Binary With Libraries section.

Step 5:

Click on General. You will notice that the Embedded Binaries section doesn't include the framework.

Add the framework in the Embedded Binaries section.

Step 6:

You will notice that the Linked Frameworks and Libraries section contains a duplicate copy of the framework. Remove one of the copies.

Step 7 (Optional):

Since I use C++ in the framework, I need to change the extension of the AppDelegate and ViewController files to use the ".mm" extension.

Step 8:

Now you are ready to use the framework in the new project. Import the framework as shown in line 11 in the image below. You can now call the API classes as shown in the "viewDidLoad" method in the image below.

Build and run the project. And that is it.

Hope this helps.

How to become a game engine developer

I started blogging about Game Engine Development a few years ago. I started doing so because the educational materials on game engine development were limited and most books on the subject were too dense.

It took me a lot of trial & errors to figure out how to develop a game engine. Now that I have developed one, I know the learning path that you need to take to become a game engine developer.

 
 

These are the steps:

Step 1: Learn Linear Algebra

First, learn Linear Algebra. No, I don't mean Algebra taught in middle school. I mean Linear Algebra concepts such as Vectors and Matrices. Focus on learning vector and matrices operations, especially dot product, cross product, space transformations.

Step 2: Learn C++ (or any language you want)

Next, learn how to program. I suggest learning C++. At this point, you don't have to be an expert in coding. But do learn about classes, methods, inheritance, polymorphism and encapsulation.

Step 3: Develop a Math Engine

Then, use your knowledge of vectors, matrices, and coding to develop a math engine. Operations such as Dot product, cross product and space transformation are used extensively in game engine development.

Step 4: Learn Computer Graphics

Now comes the fun part. Get acquainted with Computer Graphics concepts, especially the Rendering Pipeline and Shaders. Avoid coding and using the OpenGL API for now. The OpenGL API is confusing to understand if you have limited knowledge of computer graphics concepts.

Step 5: Learn OpenGL and do a lot of projects

Once you feel comfortable with Computer Graphics concepts, learn about the OpenGL API and OpenGL Shaders. Do as many projects as possible. Learn how to render characters, how to rotate and translate characters. Learn how texturing and lighting works with OpenGL. Again, this is the fun part, and I suggest doing as many projects as you can.

Step 6: Learn Design Patterns

The next step to becoming a game engine developer is to learn API architecture. A game engine is simply an API, a framework, that takes care of all the rendering, physics and mathematical operations. It is paramount that you develop an API that is modular, flexible, maintainable and adaptable.

To develop an API, you will need to learn Design Patterns. The most common design patterns are Singleton, Observer, Strategy, Composite, Factory among others.

Step 7: Develop a Rendering Engine

At this stage, you are ready to combine your knowledge of Linear Algebra, Computer Graphics, OpenGL and Design Patterns to develop a rendering engine.

Step 8: Review Newton's Laws of Motion

Once you have completed the rendering engine, is time to work on the hardest part of the engine, i.e., the Physics Engine.

The good news is that you don't need to be a physicist to develop a Physics Engine, but you do need to know Newton's Laws of Motion and how they are implemented using algorithms such as the Runge-Kutta algorithm.

Step 9: Learn Computational Geometry Algorithms

A game engine is not a game engine without collision detection. To develop a collision detection system, you need to learn about Computation Geometry algorithms such as GJK, BVH, and Sutherland-Hodgman. These algorithms are used to detect if a collision occurred, where did it occur and which objects are most likely to collide.

Step 10: Develop a Physics Engine

Once you are acquainted with the algorithms mentioned above, you should be able to develop a physics engine with a collision detection system.

Step 11: Develop a game, Test & Repeat

Congratulations, you now have a Game Engine. Develop as many games as you can and test the game engine as much as possible. Fix bugs, implement new features, develop games and repeat. Trust me; this is the best part of all.

Books to get started

Here is a list of books to help you get started:

3D Math

  1. 3D Math Primer For Graphics and Game Development

Rendering Engine

  1. OpenGL Superbible: Comprehensive Tutorial and Reference
  2. Graphics Shaders: Theory and Practice, Second Edition

Physics Engine

  1. Physics for Game Developers: Science, math, and code for realistic effects
  2. Game Physics Engine Development: How to Build a Robust Commercial-Grade Physics Engine for your Game
  3. Real-Time Collision Detection

Hope this helps

Should you develop a game engine or a game?

As someone who developed a 3D game engine from scratch, you may want to listen to what I have to say.

If your goal is to learn what makes a game engine tick, then go ahead and develop an engine. It is the best thing you can do. I guarantee you that you will become an expert in computer graphics, OpenGL, Linear Algebra, and computational geometry algorithms.

However, developing a game engine is an INSANELY complicated task. It took me about four years to develop the basic framework of the engine. That's right. Just to develop the basic framework of a 3D game engine took that long. By "Basic" I mean that the engine has a collision detection system, a physics engine, rendering engine, animation system, a Digital Asset importer, etc.

To test the features of the engine, I decided to develop a full blown soccer game. It is still a work in progress. Here is a video of the latest game update:

 
 

Here is another video that showcases what the engine can currently do:

 
 

And here is one of the very first demos showcasing the basic framework:

 
 

All in all, it has taken me about four years to develop the engine. However, the engine is not complete yet. There are bugs and issues with it. As I work on the soccer game, I'm finding more and more issues.

An engine requires a lot of maintenance and time; time that could be better spent developing your game instead. Thus, if you just want to develop a game, use an existing game engine instead.

You may be curious how a game engine works. If that is the case, I wrote several articles on game engine development that should give you a pretty good idea how it works.

How does a game engine works?

How does a rendering engine works?

How does a physics engine works?

And if you are interested, you can get a copy of my eBook: Components of a Game Engine

Enjoy

How I managed to develop a game engine?

Recently I was analyzing how I was able to develop a game engine. It was a complex project which took approximately 15,330 hours. All in all, the basic framework of the engine took about three years to complete. So, how was I able to complete such enormous task?

I turned coding into a habit

Developing a game, app or a game engine can turn into a chaotic process if you don't break it down into simple tasks. And it can become overwhelming if you try to code things all at once. For example, the hardest part of a game engine is developing the physics engine. There are so many moving parts in a physics engine that can overwhelm anyone.

I manage this complex project by doing the following: For a particular feature of the engine, I would write down which Class I should implement. Then, I would write down all the methods/functions necessary for the Class. Then, instead of saying: "Today, I will implement Class X," I would instead focus on implementing ONLY one method/function a day. The key here is that I trained my brain to code every day. Even if I coded for only 15 mins, I still showed up to code every day.

By the second year of development, coding had become a lifestyle for me; just like working out and eating healthy is to me. The day I didn't code, it felt awkward. And I would find myself coding even if I wasn't planning to do so.

I used pen and paper before coding

I learned that using pen and paper before coding can save you hours of development. The first iteration of the engine was a total mess. I ended up throwing it into the trash can. The second time around, I started sketching the game engine operations, methods, classes on paper way before I started to code.

Nowadays, if I'm about to implement a method/function, I usually draw a sketch of its operation and how it may affect other parts of the engine. Having a sketch of what you are about to implement not only saves you hours of work, but it can help you avoid bugs which you may be introducing into your app unconsciously.

I used the power of visualization

You may think this is crazy, but trust me, it works. Visualizing the finish line is the most useful thing if you want to achieve your goals.

It took me about a year to implement the Collision Detection System of the engine. As I implemented the collision detection features, I would visualize the features working way before they were completed. For example, instead of me saying, "I'm implementing the GJK algorithm" I would say to myself "I'm DONE implementing the GJK algorithm" and I would visualize 3D objects colliding on the screen.

Call me crazy, but for some reason visualizing the finish line way before you cross it is a powerful tool. It worked for me, who knows it may also work for you.

Hope these tips can be of use to you.

Game Engine Beta v0.0.4

It has been a month and a half since I gave you an update on the engine. I have been very busy implementing new features and fixing several bugs with the engine. Some of the new features are shown in the video below:

 
 

Improvements

One of the major features that I implemented in v0.0.4 is a particle system. To be honest, the particle system is very primitive. I am still learning how to create several particle effects, so expect more effects soon. As you can see from the video, I was able to implement a "kind of" explosion effect.

I also implemented collision filters. Collision filters are useful whenever you want a particular type of objects to collide with one another but not with any other kind. For example, object A and object B can collide; object A and object C can collide, but any collision between object B and object C is ignored.

A minor detail which I had ignored all along was to enable multi-touch in the engine.

Issues

While developing the second game demo, I started noticing glitches with the OpenGL manager. With a particular type of objects, the OpenGL manager would spit out an error. This issue was hard to detect, and it took me quite a few weeks to find it. I thought I had fixed the bug, but as I was developing this beta version, the OpenGL manager complained again (once). The problem with this bug is that it is intermittent and very hard to reproduce.

I'm considering porting the engine to work with the Apple Graphics API, Metal. However, I'm still weighing the pros and cons of using OpenGL vs. Metal. One thing I have noticed is that Metal is a lot easier to work with than OpenGL, but that is just my opinion.

Thanks for reading