Friday, 26 September 2014

For Starters


This year, much like the last I will be recording what I learned in the process of this course.
For those of you who are here for graphics go here: www.bishopsgraphics.blogspot.ca

What is Game Engines


To start with we should ask what is game engines. A game engine is the basic software or a video game. It is what we start with and work from to get a game instead of working from scratch. There are a large variety of engines out there, two examples of these would be the Creation Engine which Bethesda made and used for Skyrim and Unity. The Creation Engine would be an in house engine, meaning that is is made by and used by the same company internally and they do not share it. The Unity engine on the other hand I developed specifically for others to use, coming with its own built in tools that allow people to pick up and use it with less experience than required for other engines. With game engines you can do special effects with the tools you already have, but, to stand out, it is better to know the inner workings of your engines in order to help make you own unique effects.


What do we Need

You may be thinking, what do we need in order to learn engines. Really all you need is your computer and to get a hold of the source code for an engine. It also help to have strong knowledge of C++, linear algebra, and computer graphics.


Pointers 

The first topic we are going to be starting with are pointers, specifically smart pointers. What is a smart pointer? A smart pointer is a data type that simulates a pointer while also filling in with other useful functions. Unlike normal pointers, smart pointers keep track of the memory they point to. This means you can use them to free up memory when deleting an object and prevent memory leak. This is done through returning an unique pointer.The unique pointer takes a copy of your data for you to use, then deletes itself when you are done with it so that you do not forget to delete your objects. The major benefit of a unique pointer is that if I will not work then the program will complain instead of continuing to run, forcing you to find the error yourself.

A virtual pointer is a type of smart pointer. Take for instance the code

        {int *i = new int(0); delete i;} 
This is a shared pointer and the code will not push itself off the stack instead we must use:
        {sharedPtr i(new int(0));}
This code will delete itself but we want to avoid using new so we use
        {sharedPtr i = MakeShared<int> (0);}
While this works it is still a shared pointer. Shared pointers have a innate problem where they permeate through your code and make them unable to be deleted as they get tangled up in to much code that ends up using it. To stop this we would use the Virtual pointer I talked about before.
        {VirtualPtr<obj> obj = ToVirtualPtr(GetObject());}

If you look at the old method, lets say there is a function called ThisFunction() later in your code and ThisFunction() requires a shared pointer. By using you pointer ThisFunction() has now altered it so you have to go back and make sure it does not mess up your old code. If we instead use the virtual pointer then we would not screw up our system, this is because if it gets deleted then you will be alerted by the system and you will not have to go back and search through all your code for the errors, saving you time and making your code easier to read.

For pointer remember to not used shared and try to avoid unique pointers unless the code is solely yours. There are other pointers called weak pointers which break cyclical dependencies but since those are frowned on we also try to avoid them. Always try to use virtual pointers and virtual stack objects (covered later) instead.



Entity Component System



The Entity Component System (or ECS) is the inner working of our program that helps us process information. Every library has a Component system in it. While we may use a scene graph in order to see our game scenes and how the objects in them interact the ECS exists for our computer. It is how we use objects in functions.


Think of the entity as a key and the components are the teeth on a key. A function would be a lock, in this example character movement. To open the lock we require a few things, the current velocity of the character and its position. 


If you look above you can see that our key has two of these teeth. meaning that it can open the lock to the character movement function. This prevents objects lacking attributes needed for a function to be used for a function.