Difference between revisions of "Gb.h"

From GbdevWiki
Jump to: navigation, search
m (Adding to category)
(set_sprite_data correction)
Line 12: Line 12:
 
<code>set_sprite_data(UINT8 first_tile, UINT8 nb_tiles, unsigned char *data)</code> feeds a tile as described in array <code>data</code> into sprite data, starting at <code>first_tile</code> and writing for <code>nb_tiles</code> number of tiles.
 
<code>set_sprite_data(UINT8 first_tile, UINT8 nb_tiles, unsigned char *data)</code> feeds a tile as described in array <code>data</code> into sprite data, starting at <code>first_tile</code> and writing for <code>nb_tiles</code> number of tiles.
  
<code>set_sprite_tile(UINT8 nb, UINT8 tile)</code> displays tile number <code>tile</code> of sprite number <code>nb</code>.
+
<code>set_sprite_tile(UINT8 nb, UINT8 tile)</code> sets active sprite tile number <code>nb</code> to a sprite set with <code>set_sprite_data</code>.
  
 
<code>set_sprite_prop(UINT8 nb, UINT8 prop)</code> sets various properties of the tile, like where the sprite is drawn relative to other layers and which way it is facing.
 
<code>set_sprite_prop(UINT8 nb, UINT8 prop)</code> sets various properties of the tile, like where the sprite is drawn relative to other layers and which way it is facing.
Line 41: Line 41:
 
     if(joypad() & J_START){ //If joypad() indicates that Start is pressed...
 
     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_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
+
     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
 
     move_sprite(0, 80, 80); //Move sprite number 0 to the middle of the screen
 
     SHOW_SPRITES; //Turn on the sprite layer
 
     SHOW_SPRITES; //Turn on the sprite layer

Revision as of 01:43, 11 June 2016

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.

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