Difference between revisions of "Gb.h"

From GbdevWiki
Jump to: navigation, search
(gb.h function overview page started with brief descriptions and an example)
 
(Important functions:: Link to the set_sprite_prop page)
 
(4 intermediate revisions by the same user not shown)
Line 6: Line 6:
  
 
== Important functions: ==
 
== Important functions: ==
<code>delay(UINT16 d)()</code> waits for the specified number of milliseconds.
+
<code>delay(UINT16 d)</code> waits for the specified number of milliseconds.
  
 
<code>joypad(void)</code> checks the input buttons of the Game Boy and returns the state of each button.
 
<code>joypad(void)</code> checks the input buttons of the Game Boy and returns the state of each button.
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>move_sprite( UINT8 nb, UINT8 x, UINT8 y)</code> moves sprite number <code>nb</code> to position <code>x</code>, <code>y</code> on the screen.
+
<code>[[GBDK set sprite prop|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. The properties are stored in binary, see the [[GBDK set sprite prop|set_sprite_prop tutorial]] on how to combine and revert properties.
  
<code>SHOW_SPRITES</code> turns on the sprite layer.
+
<code>move_sprite( UINT8 nb, UINT8 x, UINT8 y)</code> moves sprite number <code>nb</code> to position <code>x</code>, <code>y</code> on the screen. Note: Everything before 8, 16 is invisible.
  
<!-- Honestly this code block could be a transclusion and stored elsewhere -->
+
<code>scroll_sprite(INT8 nb, INT8 x, INT8 y)</code> takes sprite number <code>nb</code> and moves it relative to where it is.
 +
 
 +
<code>SHOW_SPRITES;</code> turns on the sprite layer.
 +
 
 +
<!-- Honestly this code block could be stored elsewhere and transcluded -->
 
<div class="toccolours mw-collapsible mw-collapsed">
 
<div class="toccolours mw-collapsible mw-collapsed">
 
Example:
 
Example:
Line 37: 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 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
Line 48: Line 52:
 
</div>
 
</div>
 
</div>
 
</div>
 +
[[Category:GBDK]]

Latest revision as of 03:56, 19 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. 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