Gb.h

From GbdevWiki
Revision as of 10:24, 3 June 2016 by Caseyweederman (Talk | contribs) (Added a couple of functions.)

Jump to: navigation, search

gb/gb.h handles basic inputs and drawing background and sprites to the screen.

Important definitions:

J_START, J_SELECT, J_B, J_A, J_DOWN, J_UP, J_LEFT, and J_RIGHT are keyed to these specific hardware inputs, and can be checked with the joypad() function.

Important functions:

delay(UINT16 d) waits for the specified number of milliseconds.

joypad(void) checks the input buttons of the Game Boy and returns the state of each button.

set_sprite_data(UINT8 first_tile, UINT8 nb_tiles, unsigned char *data) feeds a tile as described in array data into sprite data, starting at first_tile and writing for nb_tiles number of tiles.

set_sprite_tile(UINT8 nb, UINT8 tile) displays tile number tile of sprite number nb.

set_sprite_prop(UINT8 nb, UINT8 prop) sets various properties of the tile, like where the sprite is drawn relative to other layers and which way it is facing.

move_sprite( UINT8 nb, UINT8 x, UINT8 y) moves sprite number nb to position x, y on the screen. Note: Everything before 8, 16 is invisible.

scroll_sprite(INT8 nb, INT8 x, INT8 y) takes sprite number nb and moves it relative to where it is.

SHOW_SPRITES; turns on the sprite layer.

Example:

#include <gb/gb.h> //include the main GBDK library header
//This example successfully compiles under GBDK version 2.96a
//and draws a rudimentary check mark when the start button is pressed.

unsigned char Check[] = //A simple sprite to load, made in GBTD
{
  0x00,0x00,0x01,0x01,0x02,0x02,0x44,0x44,
  0x28,0x28,0x10,0x10,0x00,0x00,0x00,0x00
};

int main(){ //the main and required function
  while(1){ //loop forever
    if(joypad() & J_START){ //If joypad() indicates that Start is pressed...
    	set_sprite_data(0, 1, Check); //Turn the array into sprite number 0
    	set_sprite_tile(0, 0); //Draw tile 0 of sprite number 0 to the screen
    	move_sprite(0, 80, 80); //Move sprite number 0 to the middle of the screen
    	SHOW_SPRITES; //Turn on the sprite layer
    }
  }

} //end of main function