Gb.h

From GbdevWiki
Revision as of 04:56, 19 June 2016 by Caseyweederman (Talk | contribs) (Important functions:: Link to the set_sprite_prop page)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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) sets active sprite tile number nb to a sprite set with set_sprite_data.

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. The properties are stored in binary, see the set_sprite_prop tutorial on how to combine and revert properties.

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 sprite number 0 into tile number 0
    	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