Look Here First

Curious why I created this blog? The following two posts explain it all. Click on the titles below to read more.

What this Blog is about.

Project Description.

Sunday, April 10, 2011

Displaying Sprites: Game Characters

To draw sprites the student decided to write a function that would take an array of bytes (bits representing colors) and another array of integers (which store the actual color values, the color palette).

For example, consider the following two arrays.

unsigned char box[36] = {
0, 0, 0, 0, 0, 0,
0, 1, 1, 1, 1, 0, 
0, 1, 1, 1, 1, 0,
0, 1, 1, 1, 1, 0, 
0, 1, 1, 1, 1, 0, 
0, 0, 0, 0, 0, 0
};

int colors[2] = {BLACK, RED};

These two arrays, when passed into the function would produce a 4x4 pixel red box surrounded by a 1 pixel black border.

To change this to a green box with a white border, box[] would still be passed and a different color array would be passed in, i.e.  
int colors2[2] = {WHITE, GREEN};

The following code excerpt contains the function the student wrote to output sprites and a use of the function to output a simple sprite. (note, the following code isn't complete, and will not compile or run... for simplicity just the LCDDrawSprite() function from MyLCD_Driver.c/.h is shown)

//*******************************************************
// main.c
// Tests Screen and Accelerometer drivers
//*******************************************************

//Included Libraries for the LPC2148 ARM
#include "LPC214x.h"
#include "MyLCD_Driver.h"
#include "system.h"
#include "target.h"
#include "pacsprite.h"

int main (void)
{
LCDInit();
LCDClear(BLACK);
int colors[2] = {BLACK, YELLOW}; 
LCDDrawSprite(54, 31, 8, 8, FullCircle, colors);
return 0;
}

//*******************************************************
// MyLCD_Driver.h
// Setup and declarations for MyLCD_Driver.c
//*******************************************************

void LCDDrawSprite(unsigned char botRigX, unsigned char botRigY,
unsigned char width, unsigned char height,
unsigned char bitmap[], int colors[]);

//********************************************************************
//
// MyLCD_Driver.c: 
// Interface for Nokia LCD
//
//********************************************************************
void LCDDrawSprite(unsigned char botRigX, unsigned char botRigY,
unsigned char width, unsigned char height,
unsigned char bitmap[], int colors[])
{
// if the bitmap isn't as long as the area (length x width) this will overrun the array 
//  with unpredictable results,  so it had better be correct also if there are more colors
//  in the bitmap than indexes in the color array there will also be memory overrun...
//  USE THIS FUNCTION WITH CAUTION
LCDCommand(PASET);   // page start/end ram
LCDData(botRigY);
LCDData(botRigY + height-1);
  
LCDCommand(CASET);   // column start/end ram
LCDData(botRigX);
LCDData(botRigX + width-1);
  
LCDCommand(RAMWR);    // write
for(int count= 0; count < (width) * (height) / 2; count ++)
{
int pixel1 = count * 2;
int pixel2 = count * 2 + 1;
unsigned int color1 = colors[bitmap[pixel1]];
unsigned int color2 = colors[bitmap[pixel2]];
// use this information to output three data bytes
LCDData((color1 >> 4) & 0xFF);
LCDData(((color1 & 0xF) << 4) | ((color2 >> 8) & 0xF));
LCDData(color2 & 0xFF);
}
}

//*******************************************************
// pacsprite.h
// declarations for the bitmap arrays used to hold 
//  Pac-Man sprites
//*******************************************************
extern const unsigned char FullCircle[64];

//*******************************************************
// pacsprite.c
// definitions of the bitmap arrays used to hold 
//  Pac-Man sprites
//*******************************************************
#include "pacsprite.h"

extern const unsigned char FullCircle[64] = {
0, 0, 1, 1, 1, 1, 0, 0,
0, 1, 1, 1, 1, 1, 1, 0, 
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 
0, 1, 1, 1, 1, 1, 1, 0,
0, 0, 1, 1, 1, 1, 0, 0
};


The code extract above results in the following 
sprite



No comments:

Post a Comment