Resource Management in Game

Introduction

This post is the next in the game development series. In this post we will learn about Resource Management in the Game. After writing the game engine, now we can add some background to our game. We will try to add our menu as well and try to add navigation in the menu. Once that is done we will be able to move through the menu and choose various options. Without wasting much time, lets dive in.

Objects in the Game

There are many different type of entities in our game. They can be categorized into Screens and Actors and few more. You have absolute freedom to make it more granular but this is a good categorization for simplicity.

Let us have an Entity interface which defines a contract for all the game objects.



This means that every game entity will have X and Y co-ordinates, width , height, a tick method to update its state, a render method to render this object on the game canvas and a getBounds method which will be used to detect collisions. We can debate on moving getBounds to a lower level in the hierarchy, but it doesn’t hurt here as well.

Let us implement this interface to get a GameObject class.

Resource Management in Game

You can create an image using any photo editing tool, one with some dark sky and few stars will work. Name it background.jpg. We need two things for managing all images in our game.

Loading Images
  • An image loader, which loads buffered images. Let us name it BufferedImageLoader
  • A resource manager which manages the images read by the BufferedImageLoader and other resources like sound etc

Here is the BufferedImageLoader class:

If you look at the class above, it just reads an image from the classpath and returns it. Here is the ResourceManager which will have methods specific to various resources. Resources are various images/sound resources like sprites or .wav files.


The Texture Class

This class is a wrapper over our ResourceManager class and provides back images in terms of games objects. For e.g this class can get you an array of enemies, player, bullets etc. Remember, the Resource Manager just stores the full images and it doesn’t know about the game entities. The Texture class will simplify our lives in that term.

Here is a simple Texture class.

Initializing the ResourceManager

In the initialize method of our FSM which we wrote in last post, instantiate the ResourceManager and initialize it as below:

We need to initialize the Texture as well _tx = new Texture(_rf);

The Start State

This is the first state of our game, it is visible when the game starts. The Start State needs to have the texture to access all the images in the game. So modify the constructor of the StartState to accept the Texture.

Let us add a flash screen to the Start State of the game. Create a Flash Screen game object and draw the background.



Rendering the Background

If you run the game class now, you will just see a black screen. Let us place the background.jpg in the src directory first. So that it is available to read.

In the start state, we can now create a FlashScreen instance and in the render method of StartState, we can invoke the render of FlashScreen. And we are done! we can now see our background on the game canvas.

This took some more time because we wrote lot of infrastructure classes following our design and now things will be easy to create and draw as the setup is complete.

Source Code

As always, I have synced the code in Github. You can grab it from @ Github Repo

Conclusion

In this post, we had setup resource manager and were able to draw the background and discuss a bit about game objects. This was pretty cool, and next I plan to show some state transitions from Start to the New Menu state and other menu states. Which will help us choose states and play or exit game.

Don’t forget to subscribe to TechieMe to get updates on latest posts.