Creating Game Engine
What do you really need to do when you decide to create a game engine.
Join the DZone community and get the full member experience.Join For Free
I have created the Tyracorn engine in Java pretty much from scratch, only using a small number of basic libraries. If you are considering doing the same then continue reading.
If your goal is to publish the games quickly, then just use Unity, Unreal Engine, or another great product available. However, if you have different expectations, and time for that, then go ahead. Here are mine.
- Learn and relax.
- Ok to spend significant time studying and pivoting without visible progress.
- Code is done according to the Robust Java Standards book, including many unit tests.
- The goal is to produce decent-looking 3D games. No need to compete with AAA titles.
- Targetting PC and Android.
Even with these, it took me more than a year to produce a reasonably complete engine (did plenty of other stuff as well, otherwise could be like 4 months).
I have chosen Java with Maven to be the basics. Literally started with two projects tyracorn-core and tyracorn-desktop having the following idea for the future.
- tyracorn-code - this is the library that contains all the platform-independent code. For example, driver interfaces, data structures, rigid body engines, and other platform-independent things.
- tyracorn-desktop - this is also the library, that has tyracorn-core as a transitive dependency. Implements the tyracorn-core interfaces for desktop.
- Game development - add tyracorn-desktop as a dependency and write code following Tyracorn engine standards.
- Production compilation - production compilation (e.g. for Desktop, Android) is done by a separate tool (more on that later).
Graphics with simple controls. Very enjoyable since progress is fast and visual. I have chosen to use OpenGL via JOGL binding library. And here are good resources to start with.
- Gordon, V. Scott. Computer Graphics Programming in OpenGL with JAVA Second Edition
- Learn OpenGL
The very nice thing about the JOGL library is that it is fully available in the Maven Central repository, and works well for desktops. Just use the following dependencies.
<dependency> <groupId>org.jogamp.gluegen</groupId> <artifactId>gluegen-rt-main</artifactId> <version>2.3.1</version> </dependency> <dependency> <groupId>org.jogamp.jogl</groupId> <artifactId>jogl-all-main</artifactId> <version>2.3.1</version> </dependency>
I have focused only on the desktop. Created interfaces in tyracorn-core, and implemented them in tyracorn-desktop. Also made plenty of unit tests and small POC applications, which I am maintaining even now. The result...
Write once, run anywhere. I heard this before... And yes, I wanted to write the app on a desktop (with reasonable preconditions in mind) and be able to run it on Android phones. So I dug into an official Android OpenGL ES documentation.
Turned out, that the documentation is pretty good. So I created a project in Android Studio, included tyracorn-core as a dependency, and copied the code of sample applications over so that I can run them on my phone. Then find out that there are slight differences in the flavors OpenGL, Java, and for example how resources are handled in those systems. So took a bit of abstraction and shuffling to be able to run the same code on both systems. Then the result is like this.
Now the POC was working. The very inconvenience was that I had to keep copying files across two projects. Life is tough );
Remember the planned project setup? Each project is developed on a desktop by including tyracorn-desktop dependency. This allows super easy development and debugging just from IDE.
Based on this, I have started the tyracorn-showpark project and ported several test applications there. Also implemented screen transitions so that originally independent applications are bundled into a single one and it's possible to switch them in runtime. The current version is available in the play store.
This relates to the previous point. Things like screen transition, loading screens, buttons, on-screen joysticks, and so on. They are relatively small, pieces, but you won't be able to create much without them.
Enough of file copying like I did during Android POC. Added tyracorn-toolchain project to the suite. In summary, this project allows me to compile desktop projects into various platforms by just running a single command. It can also deploy the dev version to the phone, or build the signed apk that can be uploaded to the play store right away. More details about how it works are in the Personal Mini Toolchains article.
Find out that it's difficult to create without being able to render at least a basic text. The greenhorn way for this is to create a tilemap from fonts (use a program like BMFont) and then render the characters as simple sprites. Although there are some problems with scaling, I took it as enough for now.
World of Actors
All top engines have a structure where there is a World managing Actor tree. And each Actor is a container for components like models, colliders, or scripts. So I wanted to define my objects like this.
Actor sphere = Actor.create(RandomStringUtils.randomAlphabetic(32)). addComponent(TransformComponent.create(). move(Vec3.create(0, 8, 0))). addComponent(ModelComponent.create(). setModelId(sphereModelId). setTransform(Mat44.scale(1))). addComponent(RigidBodyComponent.create(). setKinematic(false). setMass(1). setInertia(Inertias.sphere(1, 1)). setVelocity(getRandomVelocity())). addComponent(ColliderComponent.create(). setLayer(CollisionLayer.OBJECT). setType(ColliderComponent.Type.SPHERE). setRadius(1). setMaterialId(objectColMatId)); world.actors().add(ActorId.ROOT, sphere);
Took some time to make that transition, but find this pretty powerful. It's widely used and here is a nice article about it.
Yeah, that's just a must. And it's fun to play with. I liked the articles written by Allen Chou. I have made it up to this point.
Loading time on mobile is an issue. At first, I just made it in a way that things work. Haven't worried about the amount of data fetched from storage, and wasn't considered necessary in-memory transformations as well. The result... 2-minute loading screen on a mobile phone. Hmmm, probably that's worth the optimization.
For that, I have created a compressed asset file type (calling them TAP files). They can hold any type of asset, and the asset is ready to be used by the engine without any further transformation. As a bonus, it supports the schema versions, so the format and data can evolve over time.
I found a few other pieces worth optimizing using the profiler and small test applications. The method for doing that is described in the Unit-Level Performance Testing article.
Java on the desktop has built-in functions for simple audio reading. And I wasn't able to find the same on android. So wrote a utility class, that extracts the audio tracks from files, converts them into a PCM file format, and stores them in a TAP file. Then implemented manual sound mixer separately for desktop and android. In that way, audio works seamlessly as soon as it is loaded from the TAP file. Maybe there is a better way, but I found this is pretty enough. Btw, here is a good article about sound mixing.
Here I thought I am done. So started to code my first game. One more thing was missing. It was just too tough to do everything in code (it's possible to do, but I am lazy b**ch). So wanted to make it easier.
That was missing bit. So I created one that allows me to define prefabs, and then place them into scenes. Also, added there a way to reference the scripts so prefabs are connected with code. In order to do this, I had to significantly improve the UI. And tadaaaa.
Yai, now everything is ready for game creation. So go ahead and enjoy Snake. Hope I will be able to add more over time.
Happy I made this experiment. I learned a lot and used the knowledge in my commercial activities (even that has nothing to do with games).
In addition, now I have my own intellectual property that is ready to evolve and grow up. That's pretty cool!
Please put me a comment about what would you like to see as a next step.
Opinions expressed by DZone contributors are their own.