Let's start building the Game Framework

Let’s start building the Game Framework

Posted on 28 Sep 2019

This is the eleventh article of the Android Game Programming series. Starting from this article we will begin to add very significant classes to our Game Framework.

Graphics interface

First, we will add the Graphics interface that will manage the graphics services such as drawing bitmaps, points, lines, rectangles, and more. The bitmaps are managed by the Pixmap interface created from a file using the newPixmap API. This API also requires you to specify the bitmap format (PixmapFormat):: ARGB8888, ARGB4444, RGB565.

public interface Graphics {
    enum PixmapFormat {
        ARGB8888, ARGB4444, RGB565
    Pixmap newPixmap(String fileName, PixmapFormat format);
    void drawPixmap(Pixmap pixmap, int x, int y, int srcX, int srcY, int srcWidth, int srcHeight);
    void drawPixmap(Pixmap pixmap, int x, int y);

The newPixmap and drawPixmap methods will take care of loading the bitmap into memory and draw it on video. The first method, we will encapsulate the code of loading the bitmap discussed here, in the second one we will support the drawBitmap method of class Android Canvas.

The Screen interface

In the graphics management, a basic role is played by the Screen interface which represents the single screen of the video game.

interface Screen {
    void update();
    void draw();
    void pause();
    void resume();
    void dispose();

Creating a video game is a bit like making a movie. First, there is the storyline, that is the basic idea of what the video game will do. Afterward, it is necessary to write the script. In our case, it is a sheet where we draw the video game screenshots and events to switch from one to another.

The Loading and Start screen

The following image shows, for example, the screens that we will implement in this article.

Start and Loading Screen

As you can see the first screen is Loading Screen which is a dummy screen in which we only worry about loading all the video game assets into memory. In this case, there is just the bitmap startscreen.png. In the future, this screen could be made visible showing a progress bar that will report the loading progress. As this processing is very fast today we will avoid complicating unnecessarily our interface. Once all the assets have been loaded, the control will switch to the Start Screen on which we will show the bitmap already discussed here.

We define the package org.androidforfun.droids.view where we will put the two screens mentioned above. In the package, we add the Assets class which will contain the pointers to all the assets of our video game:

public class Assets {
    public static Pixmap startscreen;

We will then define the LoadingScreen class in which we will implement the update method:

public class LoadingScreen implements Screen {
    public void update() {
        Assets.startscreen = Gdx.graphics.newPixmap("startscreen.png", PixmapFormat.RGB565);
        Gdx.game.setScreen(new StartScreen());

The method loads the assets and the scores saved on external memory, then it passes the control to StartScreen class. The StartScreen class we will implement only the draw method in which we will draw the bitmap on screen:

public class StartScreen implements Screen {
    public void draw() {
        Gdx.graphics.drawPixmap(Assets.startscreen, 0, 0);

To execute the code you can perform the procedure discussed here. The source code of this article can be found here.