Difference between revisions of "C Library Commands and How to Use Them"

From Learn @ Cemetech
Jump to navigationJump to search
 
(5 intermediate revisions by the same user not shown)
Line 4: Line 4:
 
----
 
----
 
==Graphics Commands==
 
==Graphics Commands==
Here you will find all of the '''Graphx''' library commands
+
Here you will find all of the '''graphx''' library commands
 
----
 
----
 
===gfx_AllocSprite and gfx_GetSprite===
 
===gfx_AllocSprite and gfx_GetSprite===
Line 10: Line 10:
 
This command is useful to set room aside in the program for new sprites you will create with ''gfx_GetSprite'' and/or for sprites to need to decompress.
 
This command is useful to set room aside in the program for new sprites you will create with ''gfx_GetSprite'' and/or for sprites to need to decompress.
  
'''gfx_AllocSprite() In the graphx.h file:'''
+
'''gfx_MallocSprite() In the graphx.h file:'''
 
  /**
 
  /**
 
   * Allocates room on the heap for sprite data of given size
 
   * Allocates room on the heap for sprite data of given size
  * Takes a pointer to the routine used for malloc as well
 
 
   * Returns NULL upon failure to allocate space, otherwise a pointer to the sprite structure
 
   * Returns NULL upon failure to allocate space, otherwise a pointer to the sprite structure
 
   * Note that if you use the system malloc routine, it must be used elsewhere in your program
 
   * Note that if you use the system malloc routine, it must be used elsewhere in your program
 
   * otherwise it will not be linked correctly
 
   * otherwise it will not be linked correctly
 
   */
 
   */
  gfx_image_t *gfx_AllocSprite(uint8_t width, uint8_t height, void *malloc_routine);
+
  gfx_image_t *gfx_MallocSprite(uint8_t width, uint8_t height );
 
*width - The width of the sprite you want to decompress or create
 
*width - The width of the sprite you want to decompress or create
 
*height - The height of the sprite you want to decompress or create
 
*height - The height of the sprite you want to decompress or create
*malloc_routine - Not exactly sure what this does, just put ''malloc'' when writing the command
 
  
 
'''gfx_GetSprite() In the graphx.h file:'''
 
'''gfx_GetSprite() In the graphx.h file:'''
Line 30: Line 28:
 
   * sprite_buffer is updated with the screen coordinates given.
 
   * sprite_buffer is updated with the screen coordinates given.
 
   */
 
   */
  gfx_image_t *gfx_GetSprite_NoClip(gfx_image_t *sprite_buffer, uint24_t x, uint8_t y);
+
  gfx_image_t *gfx_GetSprite(gfx_image_t *sprite_buffer, int x, int y);
*sprite_buffer - The name of the sprite you want to create, the one defined with AllocSprite
+
*sprite_buffer - The name of the sprite you want to create, the one defined with MallocSprite
*x - The x position on teh screen to display the sprite
+
*x - The x position on the screen of where to grab the sprite
*y - The y position on the screen to display the sprite
+
*y - The y position on the screen of where to grab the sprite
  
 
Ok, so now we're going to show you how to use both commands. Check this example code out, to be explained afterwards.
 
Ok, so now we're going to show you how to use both commands. Check this example code out, to be explained afterwards.
//Make this a global:
+
 
  gfx_image_t *spriteName
+
  gfx_image_t *sprite;
  malloc(0);
+
 
  spriteName = gfx_AllocSprite(30, 40, malloc);
+
  malloc( 0 );
  gfx_GetSprite(spriteName, 20, 50);
+
  sprite = gfx_MallocSprite( 30, 40 );
*'''gfx_image_t spriteName''' defines the sprite variable
+
  gfx_GetSprite( sprite, 20, 50 );
*'''malloc(0);''' sets that ''malloc'' value to 0
+
 
*'''spriteName = gfx_AllocSprite(30, 40, malloc);''' sets spriteName to the width and height of the next few values
+
*'''gfx_image_t sprite;''' declares a sprite variable
*'''gfx_GetSprite(spriteName, 20, 50);''' grabs a sprite the dimensions of spriteName at x-20 and y-50, and sets it to spriteName, to be called later as a sprite
+
*'''malloc( 0 );''' makes sure malloc is linked correctly. You only need to do this once.
 +
*'''sprite = gfx_MallocSprite( 30, 40 );''' sets the width and height of sprite
 +
*'''gfx_GetSprite( sprite, 20, 50 );''' grabs a sprite at x = 20 and y = 50, and stores it in `sprite` for later use
 
----
 
----

Latest revision as of 12:19, 28 January 2017

Introduction


This guide will eventually list almost every C Library command in the SDK, and give a detailed explanation on how to use each one.


Graphics Commands

Here you will find all of the graphx library commands


gfx_AllocSprite and gfx_GetSprite


This command is useful to set room aside in the program for new sprites you will create with gfx_GetSprite and/or for sprites to need to decompress.

gfx_MallocSprite() In the graphx.h file:

/**
 * Allocates room on the heap for sprite data of given size
 * Returns NULL upon failure to allocate space, otherwise a pointer to the sprite structure
 * Note that if you use the system malloc routine, it must be used elsewhere in your program
 * otherwise it will not be linked correctly
 */
gfx_image_t *gfx_MallocSprite(uint8_t width, uint8_t height );
  • width - The width of the sprite you want to decompress or create
  • height - The height of the sprite you want to decompress or create

gfx_GetSprite() In the graphx.h file:

/**
 * Quickly grab the background behind a sprite (useful for transparency)
 * sprite_buffer must be pointing to a large enough buffer to hold width*height number of bytes
 * A pointer to sprite_buffer is also returned for ease of use.
 * sprite_buffer is updated with the screen coordinates given.
 */
gfx_image_t *gfx_GetSprite(gfx_image_t *sprite_buffer, int x, int y);
  • sprite_buffer - The name of the sprite you want to create, the one defined with MallocSprite
  • x - The x position on the screen of where to grab the sprite
  • y - The y position on the screen of where to grab the sprite

Ok, so now we're going to show you how to use both commands. Check this example code out, to be explained afterwards.

gfx_image_t *sprite;
malloc( 0 );
sprite = gfx_MallocSprite( 30, 40 );
gfx_GetSprite( sprite, 20, 50 );
  • gfx_image_t sprite; declares a sprite variable
  • malloc( 0 ); makes sure malloc is linked correctly. You only need to do this once.
  • sprite = gfx_MallocSprite( 30, 40 ); sets the width and height of sprite
  • gfx_GetSprite( sprite, 20, 50 ); grabs a sprite at x = 20 and y = 50, and stores it in `sprite` for later use