SuperSonic wrote:So anyways, if James doesn't mind me asking, what's the hardest funtion you've ever had to make?
Well, I'm not sure which code was the hardest to make, but here's one..
A function which loads bitmap file and divides it into frames by the value the user has given.
Currently only left to right pictures can be used with this, it's still under work.
- Code: Select all
const int B = 0, G = 1, R = 2;
unsigned char TRANSPARENT[3]; //Array for values of transparent color
unsigned char IMG[6][200][200][3]; //Array for animations frames
int iW, iH, lim;
int readBMP(char * filePath, int images)
{
int header[64], i, j, k, curImg = 0, value;
FILE * bmp = fopen(filePath, "r+b");
value = (fgetc(bmp) == 'B');
if (value) //Check if the file exist
{
fseek(bmp, 0, SEEK_SET);
for (i = 0; i < 64; i ++) //Get header info
{
header[i] = fgetc(bmp);
}
iW = header[18] + header[19] * 256; //Get width
iH = header[22] + header[23] * 256; //Get height
lim = iW/images; //Set lim equal to width / frames
fseek(bmp, header[10], SEEK_SET); //Move pointer to the start of the actual image data
for(i = iH - 1; i >= 0; i --) //Loop for y
{
curImg = 0;
for(j = 0; j < iW; j ++) //Loop for x
{
k ++;
IMG[curImg][i][k * (k < lim)][B] = fgetc(bmp); //Get b of current pixel
IMG[curImg][i][k * (k < lim)][G] = fgetc(bmp); //Get g of current pixel
IMG[curImg][i][k * (k < lim)][R] = fgetc(bmp); //Get r of current pixel
if (i == 0 && j == 0) //If we are in the top left corner of the image,
//get the transparent color
{
TRANSPARENT[R] = IMG[curImg][i][k * (k < lim)][R]; //Set transparent r
TRANSPARENT[G] = IMG[curImg][i][k * (k < lim)][G]; //Set transparent g
TRANSPARENT[B] = IMG[curImg][i][k * (k < lim)][B]; //Set transparent b
}
if (k == lim){curImg++; k = 0;}
}
}
}
fclose(bmp); //Close the bitmap file
return value;
}
And a function for drawing the frames for making an animation:
- Code: Select all
void drawImg(int num, float size)
{
int i, j;
unsigned char RR, GG, BB, tR = TRANSPARENT[R], tG = TRANSPARENT[G], tB = TRANSPARENT[B];
for (i = 0; i < iH; i ++)
{
for (j = 0; j < lim; j ++)
{
RR = IMG[num][i][j][R]; //Copy current pixels r to RR
GG = IMG[num][i][j][G]; //Copy current pixels g to GG
BB = IMG[num][i][j][B]; //Copy current pixels b to BB
if (RR != tR || GG != tG || BB != tB) //If some of the colours doesn't match with
//the transparent, draw the pixel
{
setpen(RR, GG, BB, 0, size);
putpixel(j*size, i*size);
}
}
}
}
Usage:
(time is global integer variable)
- Code: Select all
readBMP("myBMPFile.bmp", 6); //get the animation with 6 horizontal frames
time ++;
if (time >= 2)
{
img ++;
if (img == 6){img = 0;}
erase(0, 0, 0, 1);
drawImg(img, 1); //draw the current frame. as time variable changes, so changes the img and the frame to be drawn. and so we achieve an effect of animation :D
time = 0;
}
When I get this function ready, I will publish it here on forums with explanation of using it.
When this is ready, it will be possible to even make games have custom characters that everyone can make and then load them to the game.
It just requires the player actor to be canvas, but that won't be a problem, because canvas willl collide only with the parts of it where something is drawn.
One just has to remember to use erase();