Creating 2D Car Racing Game in Android: 2. Creating OpenGL Drawing Surface

In the presiding part we have created Start Menu and Game Play activity for the game. In this part you are going to learn how to create render surface for game play. For the 2D car racing game, the environment object will be Road track from top view, a car top view, controls graphics like break, accelarator and left right controls etc.

Creating 2D Car Racing Game in Android

So lets start creating render surface for all these.

Topics Covered

Rendering Graphics

In the previous part we have used ImageVIew component to display background graphics of the Main Menu screen. But it is not possible to process game graphics. To proccessing game graphics is overloading and complex process, and luckly we have tool which is impleated with Android, that is OpenGL ES which is lightwight to process graphics and flexible to use.

Creating Game View

You may used Android canvas in your past development to draw the graphics on screen. But we are going to use OpenGL ES too and it has its own type of canvas to draw the graphics on screen. The GLSurfaceView…. It allows ability to us to display game graphics on screen.

We have already created GameActivity class in previous part. Now we are going to create new class named “GameView” for it to display. So let’s create class “GameView” and extend this class to GLSurfaceView.

In the GameActivity class the setContentView() value is not yet set. Now you have created GLSurfaceView for it. So let’s set the setContentView() value of the GameActivity.

Open the and create instance of GameView GLSurfaceView which you have craeted.Then instantiate the GameView and set the setContentView() to the new instantce.

Sometime while playing the game, player may interrupt the game and turn his/her focus to another application or may be an incomming phone call interuppt the game. For handling these kind of situation, android has implemented onPause() and onResume() methods. So let’s override these methods in GameActivity class and add some code for gameView class to pause and resume it.

Creating Renderer

In presiding topic you have just ceated GameView GLSurfaceView for display OpenGL. To preccess havy graphics, GLSurfaceView need the contirubution of the Renderer. For that we need renderer for it.
Create renderer class named GameRenderer and implement it with GLSurfaceView’s Renderer class. Make sure to add unimplemented methods.

In the above code, notice the three methods we have implemented. The first one onDrawFrame() method called when renderer draws a frame on the screen. Second one is onSurfaceCreated() method called when surface is created and the third one is onSurfaceChanged() method called when view size has changed and initiate.
Let’s add some more code to render surface. You can find brief about each line of code, written in below class.

In the next part we will see, how we can add Road Texture on surface.

Download Part 2 Source

6 replies
  1. Osama
    Osama says:

    “In the next part we will see, how we can add Road Texture on surface.” but where is next part i e part3


Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *