Megadrive Programming : Adding Controller

In this section I will be teaching you how to detect the console’s controller input.

Image result for mega drive control pad

The function which we need to call to initialise controller support is JOY_init(). We place this at the beginning of the main function. This function resets the controls and begins to check what buttons on the controller are being pressed.

There are two ways to handle the controls. One is synchronous (each frame it will be executed) and the other is asynchronous(it will be executed only when a button is pressed)

Generally choosing which one to use is related to the type of game we are going to make. For an arcade action title its probably best to poll the controller every frame, however, for a strategy game title polling as soon as a button is pressed would probably suffice.

Synchronous Control Method Example

Here’s a sample piece of code which will illustrate how the control function works when we poll for input each frame.

One of the disadvantages of this approach is that, as this is running in every frame, a lot of code in the handleInput function could cause serious slowdown.

Asynchronous Control Method Example

As mentioned before, the asynchronous function detects when there is a change in the status of the button. This is defined at the beginning of the program

Then just before the game loop in the main function we put:

This piece of code executes a callback. This is when a change in the status of the button is detected, it will be executed automatically in the function joyEvent.

With the asynchronous methodology we don’t need to put anything in the main method. It will be the SGDK which will execute the function when appropriate. When we define the joyEvent function we must check what button has been pressed and act accordingly.

This way of checking the changes in the controller state will not work if we constantly press the button down. It will not detect a second press until we release and press it again, since having a button pressed is not a change, until it is released. This control method would not, therefore work for a racing game.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s