Drawing Simple Sprites


The goal of this tutorial is to show how to draw sprites using the graphics component of Revolution-FX. This tutorial will cover the basics of drawing sprite graphics with Revolution-FX. It is assumed that you have some knowledge of C programming and have already installed the CASIO fx-9860G SDK and Revolution-FX library. If you are new to C programming, it is recommended that you read a textbook or look up resources on the Internet. If you're ready let's continue.

Declaring A Sprite

Well, the first step to drawing a sprite is to declare one. A array is needed that contains the data that represents the sprite. Let's look at a example of a 8x8 sprite:
  char checker_pattern[8] = { 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55 };
The sprite is 8 bytes big and each bytes contains 8 bits. So the sprite is 64 bits large. If we looked at the array in bit level:
Each bit can either be 1 or 0. 0 could represent white and 1 representing black. Now that we have a sprite, let's draw it to our screen.

Drawing A Sprite

As of v0.2, Revolution-FX supports 8x8 and 8x16 pixel sprites. Future versions may support 16x16 and 32x32, but what will be described here can be applied to future releases of Revolution-FX. The function we will be using to draw the 8x8 sprite is DrawSprite8. Here's the prototype:
  void DrawSprite8(char x, char y, unsigned char *buffer, char *bitmap);
x and y are the coordinates for the starting point of the sprite. buffer is where we will draw our bitmap (sprite). Let's say we want to place our sprite in the middle of the screen:
  DrawSprite8(60, 28, buffer, checker_pattern);
That wasn't so hard. Now let's do a real program.

Putting It Together

Ok, let's do a real program. First step always when using the graphics component of Revolution-FX is to create a buffer that will represent our screen. The LCD is 128x64 pixels or 8192 total pixels. Since each pixel can be represented by a bit and 1 byte contains 8 bits, 8192/8 gives us 1024 bytes. So for our screen, we need to create a buffer 1024 bytes large:
unsigned char buffer[1024];
Let's also make sure to empty out our buffer:
memset(buffer, 0, 1024);
Ok, now let's place our sprite onto the buffer:
DrawSprite(60, 28, buffer, checker_pattern);
We've placed the sprite onto the buffer, now let's draw the buffer to the screen:
Awesome, now we have drawn a sprite to the screen. Let's see the actual program as a whole:
#include "fxlib.h"
#include "revolution.h"

int AddIn_main(int isAppli, unsigned short OptionNum)
	unsigned char buffer[1024];				// Declare our buffer
	char checker_pattern[8] = 
	{ 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55 };	// Declare our sprite
	memset(buffer, 0, 1024);				// Clear our buffer
	DrawSprite8(60, 28, buffer, checker_pattern);		// Place sprite onto buffer
	DrawAll(buffer);					// Draw the buffer to the screen

	while (IsKeyDown(KEY_CTRL_EXIT) != 1)			// Wait until the user presses EXIT

	return 1;

#pragma section _BR_Size
unsigned long BR_Size;
#pragma section

#pragma section _TOP
int InitializeSystem(int isAppli, unsigned short OptionNum)
    return INIT_ADDIN_APPLICATION(isAppli, OptionNum);
#pragma section


You've finished the tutorial! Congrats! Hopefully you learned something. Hope it wasn't too hard. If you were to do the same thing using the CASIO SDK libraries, you would find that Revolution-FX simplifies the process. If you are still confused or have any questions, please contact us at our forums. LogoContact Us | ©2007 Revolution-FX Development Team