class cTexture { public: int width, height; cColor * texture; cTexture() { width=height=0; texture=0; } ~cTexture() { if(texture) delete [] texture; } cColor GetColorAtPer(float x, float y) { return GetColorAt(x*width,y*height); } cColor GetColorAt(int x, int y) { if(x<0 || y<0) return texture[0]; if(x>=width || y>=height) return texture[0]; return texture[x+y*width]; } void LoadTexture (char *filename) { FILE *file; long offset, size, bitsize; short type, bitcount; unsigned char *bitmap_data = NULL; if (texture) { delete [] texture; texture = 0; } file = fopen(filename, "rb"); if (!file) { return; } fread(&type, sizeof(short), 1, file); // check the type if (type != 0x4D42) { fclose(file); return; } // read the header info fseek(file, 10, SEEK_SET); fread(&offset, sizeof(long), 1, file); fseek(file, 4, SEEK_CUR); fread(&width, sizeof(long), 1, file); fread(&height, sizeof(long), 1, file); fseek(file, 2, SEEK_CUR); fread(&bitcount, sizeof(short), 1, file); //we expect a 24 bit bitmap if (bitcount != 24) { fclose(file); return; } // get the size of the actual bitmap data fseek(file, 4, SEEK_CUR); fread(&size, sizeof(long), 1, file); // skip the rest of the header fseek(file, 16, SEEK_CUR); size = width * height * 3; texture = new cColor[width * height]; bitmap_data = new unsigned char[size]; if (texture == NULL) {//couldn't allocate memory fclose(file); return; } //read in all the image data bitsize = fread(bitmap_data, 1, size, file); if (bitsize != size || !bitsize) { fclose(file); return; } int i = 0; int x, y,q; for(y = height-1; y > -1; y--) { for(x = 0; x < width; x++) { q = x + y * width; texture[q].b = bitmap_data[i++]; texture[q].g = bitmap_data[i++]; texture[q].r = bitmap_data[i++]; } i+=width%4; } delete [] bitmap_data; fclose(file); return; } };