Search my Blog

Saturday, 6 October 2018

Creating my own Game Engine in C#

While I was off college over summer I worked on putting my skills to the test. Creating a game engine from scratch is hard, this is because they are so complex.

What is a game engine?

In the 1980s creating a game was hard. This was because there were no game engines which means for every game you made you had to code it from rendering graphics to the screen, the player's actions. This is why it took so long and hard to create games back then. A game engine is simply a way to reuse code to make it easier to make games.

What does a game engine include?

  • A way to render graphics to the screen
  • Handle user input
  • Manage sounds
  • Show Effects
  • Collision detection
  • User Interface
  • Could include templates for objects like players
  • A way to handle objects
  • Could have physics
  • Could have artificial intelligence

How does a game engine work?

Game engines work by allowing the developer have access to tools which lets them develop games faster. Each of the topics I mentioned above are all functions or classes.

When a game starts it creates a thread (a second program) which runs the game loop. The game loop is responsible for rendering frames to your monitor over and over again and processing any necessary logic such as player movement and health systems.

The game loops

As this is a never-ending loop (apart from when you close the game) it is very important that you have built-in error handling that will close the loop if there is a problem as what you don't want is it to completely break and crash altogether.

Handling Logic

The way my game engine handles logic is that it loops through a list of all the defined objects in the game and runs any code that is in the "Tick" function every time the "gameloop" function runs.
Another think you can see in the code is that I start a timer when the gameloop start and stop it when it stops. I do this to measure how long it takes to complete the loop and process it in to frames per second (fps). The algorithm I use to convert the milliseconds to frames per seconds is 1000/milliseconds so for example if a frame took 1 second to process it would be 1 fps. (1s = 1000ms)


Objects and how they work

In my game engine objects use components like unreal engine. So a object with no components would be invisible and can act purely to serve functions.

I also have a box component which allows you to create squares, a sound component which is responsible for creating sounds and music, a sprite component for using images and lastly, a text component that handles creating text on the screen. Objects are also used for the heads-up-display (HUD).

Rendering graphics

The way you render graphics to the screen is that you create a virtual "canvas" the same size a your monitor and you render all the objects to that canvas and then render the canvas to the monitor. This is how "Vsync" works and it minimises screen tearing and flashing that can occur.

An Example game

This is an example game I have made with the newly created engine. It is currently bare but it has the potential to render more complex scenes.

No comments:

Post a Comment