Code:
#include "stdio.h"
#include "stdlib.h"
#include "SDL.h"
#include "bmp_load.h"
//open file
BMP::BMP(char *filename, SDL_Surface *scr)
{
FILE *f = fopen(filename, "rb");
if(!f)
{
printf("Couldn't open file %s\n", filename);
exit(1);
}
fread(&bF, sizeof(BITMAPFILEHEADER), 1, f);
fread(&bI, sizeof(BITMAPINFOHEADER), 1, f);
if(bF.bfType != BITMAP_ID) //it's not a bitmap file
{
printf("Wrong file! %s\n", filename);
exit(1);
}
fseek(f, bF.bfOffBits, SEEK_SET);
imgData = (unsigned char*)malloc(bF.bfSize);
fread(imgData, bF.bfSize, 1, f);
screen = scr;
isT = 0;
fclose(f);
}
BMP::BMP()
{
}
BMP::~BMP()
{
delete imgData;
}
int BMP::load(char *filename, SDL_Surface *scr)
{
FILE *f = fopen(filename, "rb");
if(!f)
{
printf("Couldn't open file %s\n", filename);
return 0;
}
fread(&bF, sizeof(BITMAPFILEHEADER), 1, f);
fread(&bI, sizeof(BITMAPINFOHEADER), 1, f);
if(bF.bfType != BITMAP_ID) //it's not a bitmap file
{
printf("Wrong file! %s\n", filename);
return 0;
}
fseek(f, bF.bfOffBits, SEEK_SET);
imgData = (unsigned char*)malloc(bF.bfSize);
fread(imgData, bF.bfSize, 1, f);
screen = scr;
printf("Width: %d Height: %d\n", bI.biWidth, bI.biHeight);
fclose(f);
return 1;
}
inline void BMP::draw_px(int x, int y, int r, int g, int b)
{
color = SDL_MapRGB(screen->format, r, g, b);
bufp = (int*)screen->pixels+y*screen->pitch/4+x;
*bufp = color;
}
void BMP::draw(int posX, int posY)
{
if(!isT)
{
posY += bI.biHeight;
for(int j = 0; j < bI.biHeight; j++)
for(int i = 0; i < bI.biWidth; i++)
draw_px(posX+i, posY-j, imgData[j*bI.biWidth*3+i*3+2], imgData[j*bI.biWidth*3 + i*3+1], imgData[j*bI.biWidth*3 + i*3]);
} else {
posY += bI.biHeight;
for(int j = 0; j < bI.biHeight; j++)
for(int i = 0; i < bI.biWidth; i++)
if((imgData[j*bI.biWidth*3+i*3+2] != R) || (imgData[j*bI.biWidth*3+i*3+1] != G) || (imgData[j*bI.biWidth*3+i*3] != B))
draw_px(posX+i, posY-j, imgData[j*bI.biWidth*3+i*3+2], imgData[j*bI.biWidth*3 + i*3+1], imgData[j*bI.biWidth*3 + i*3]);
}
}
void BMP::setTransparent(int cR, int cG, int cB)
{
isT = 1;
R = cR;
G = cG;
B = cB;
}
In my opinion the drawing function is wrong, but it could be also the loading function...