How to Make a Game Engine: Cleaning Up and Making a Game

Hey there, game enthusiasts! Welcome to Episode 24 of the Sparky Game Mansion series. In this exciting episode, we will be diving deep into creating a beautiful game engine from scratch using C++.

Last time, we had the fantastic experience of creating audio files. We had the ability to loop audio, control volume, and do all sorts of cool things with it. This episode, we will be focusing on cleaning up and preparing for the upcoming M scripting to compile Sparky into JavaScript.

But before we dive in, let me introduce myself. My name is Jonah, and I’ll be your guide through this exhilarating journey of game engine creation.

How to Make a Game Engine: Cleaning Up and Making a Game
How to Make a Game Engine: Cleaning Up and Making a Game

Cleaning Up the Code

In the previous episode, we encountered a few minor issues that need to be fixed before moving forward. Let’s address those first. We’ll start by removing unnecessary code and fixing some small bugs.

First, let’s get rid of some inconsistencies in the code. We had a “static friends” concept, which is not valid since friends cannot be static. Let’s remove that altogether. Additionally, we had a “Const mat” in the renderer, which is not allowed in a vector. Let’s fix that as well.

Next, let’s focus on optimizing our code. We noticed that when we exceeded the maximum number of textures (32), we experienced lag and performance drops. To fix this issue, we’ll define a new constant called “renderer max textures” and set it to a lower value, such as 2. We’ll also clear the textures every frame to prevent any flickering.

Further reading:  Understanding Normal Mapping in 3D Graphics

Making a Game

Now that we have cleaned up the code, it’s time to move on to the exciting part: making a game! But before we proceed, there is an important point to note. The tile layer we will be creating is not actually a part of the engine itself. It serves as an example of how you can implement different layers in your game. So don’t worry if you don’t have a tile layer in your game; it’s not a requirement.

Since we’re not using the tile layer, we can remove it from the engine and place it in our own game code. This way, we can have full control over our game and implement different layers, such as debug, text, UI, etc. Let’s move the tile layer code outside of the engine and into our game code.

Creating a Main Loop

Now, let’s create a main loop for our game. This is where all the magic happens. We’ll start by including the necessary libraries and creating a window for our game. We’ll also create a layer to render our sprites.

Next, we’ll set up our game loop, which will run as long as the window is open. Inside the loop, we’ll clear the window, update the game logic, and render the game objects. We’ll also handle user input, such as moving the sprites.

To ensure a smooth gameplay experience, we’ll control the updates per second and frames per second. We’ll use timers to achieve this. We’ll also create a label to display the frames per second on the screen.

Further reading:  Game Programming: Mastering Offsets and Tile Rendering

Putting It All Together

Now that we have our main loop in place, let’s create our game. We’ll create a new class that inherits from the Sparky engine. Inside this class, we’ll define our game logic and render our game objects.

We’ll start by creating a window for our game using the “createWindow” function. We’ll also create a layer and a batch renderer to render our sprites. We’ll then create a sprite and set its position. Finally, we’ll render the sprite and update its position based on user input.

Voila! We’ve just created a simple game using the Sparky game engine. It’s amazing how easy it is to create a game with Sparky. With just a few lines of code, we’re able to bring our ideas to life.

Conclusion

In this episode, we’ve learned how to clean up our code and make a game using the Sparky game engine. We’ve fixed some minor issues, created a main loop, and implemented our game logic.

Sparky is a powerful and versatile game engine that empowers developers to create stunning games. It’s designed to be user-friendly yet provide advanced functionalities for experienced developers. The possibilities are endless with Sparky, and I can’t wait to see what you create!

Next time, we’ll be diving into M scripting and compiling Sparky into JavaScript. This is a big step, and I’m excited to explore this topic with you.

Until then, keep coding and keep creating amazing games! And remember, with Sparky, the sky’s the limit.

YouTube video
How to Make a Game Engine: Cleaning Up and Making a Game