Introduction

Day 1
GBA Hardware

Day 2
"Hello, World!"

Day 3
Input

Day 4
Backgrounds -
  Bitmapped Modes

Day 5
Sprites

Day 6
Backgrounds -
  Tile Modes

Day 7
Project 1 -
  Tetris

Quiz - Week 1

Day 8
Sprites #2 -
  Animation

Day 9
Maps

Day 10
Sprites #3 -
  Animation #2

Day 11
Backgrounds -
  Rotation

Day 12
Sprites #4 -
  Mosaic

Version History

Downloads

Books

Links

Graphics FAQ

GFX2GBA Readme

Games

Projects

Credits

Support

HAM Tutorial :: Day 3 :: Input

 

Another easy thing to do with HAM is user input. Today we'll start with the code from the Day 2 tutorial and add some code to check for input.

 
// The Main HAM Library
#include <mygba.h>

// Function: main()
int main()
{
    // Variables
    bool key_pressed = 0;

    // Initialize HAMlib
    ham_Init();

    // Initialize the text display system in BGMODE 0
    ham_InitText(0);

    // Draw some text to the screen
    ham_DrawText(0,0,"Hello, World!");
    ham_DrawText(10,19,"Press D-Pad To Reset");

    // Loop until a direction on the D-Pad is pressed
    while(key_pressed == 0)
    {
        // Check for input
        if (F_CTRLINPUT_UP_PRESSED
            || F_CTRLINPUT_DOWN_PRESSED
            || F_CTRLINPUT_LEFT_PRESSED
            || F_CTRLINPUT_RIGHT_PRESSED)
        {
            // Change key_pressed to true
            key_pressed = 1;
        }
    }

    return 0;
} // End of main()
 

Code Explanation

if (F_CTRLINPUT_UP_PRESSED ...)
During the while loop it will do a check to see if up, down, left or right is pressed. After changing key_pressed to false, it will end the while() loop and 'reset' the game.

GBA Button

HAM Equivalent

UP

F_CTRLINPUT_UP_PRESSED

DOWN

F_CTRLINPUT_DOWN_PRESSED

LEFT

F_CTRLINPUT_LEFT_PRESSED

RIGHT

F_CTRLINPUT_RIGHT_PRESSED

START

F_CTRLINPUT_START_PRESSED

SELECT

F_CTRLINPUT_SELECT_PRESSED

B

F_CTRLINPUT_B_PRESSED

A

F_CTRLINPUT_A_PRESSED

L

F_CTRLINPUT_L_PRESSED

R

F_CTRLINPUT_R_PRESSED

NOTE: In order to do diagonal movement, you can use something like:
if (F_CTRLINPUT_UP_PRESSED && F_CTRLINPUT_LEFT_PRESSED)
There will be an example of this in a later project.

Well, another day, another tutorial. Again, nothing difficult here.

 

Download Code

NOTE: You may need to Right-click and choose Save As.

HAM Version 2.80 And Higher
Download All Files In One Zip: Day3_Input.zip

View Demo Now

Discuss Day 3

 

<<

HOME

>>