| 224 | } |
| 225 | |
| 226 | int LoadBitmapImage(FILE* f, surface_t* surface) { |
| 227 | fseek(f, 0, SEEK_SET); |
| 228 | |
| 229 | bitmap_file_header_t fileHeader; |
| 230 | fread(&fileHeader, sizeof(bitmap_file_header_t), 1, f); |
| 231 | |
| 232 | bitmap_info_header_t infoHeader; |
| 233 | fread(&infoHeader, sizeof(bitmap_info_header_t), 1, f); |
| 234 | |
| 235 | fseek(f, fileHeader.offset, SEEK_SET); |
| 236 | |
| 237 | uint8_t bpp = infoHeader.bpp; |
| 238 | int width = infoHeader.width; |
| 239 | int height = infoHeader.height; |
| 240 | *surface = (surface_t){.width = width, .height = height, .depth = 32, .buffer = (uint8_t*)malloc(width * height * 4)}; |
| 241 | |
| 242 | uint32_t rowSize = floor((bpp*width + 31) / 32) * 4; |
| 243 | uint8_t* row = new uint8_t[rowSize]; |
| 244 | |
| 245 | uint32_t* buffer = (uint32_t*)surface->buffer; |
| 246 | |
| 247 | for (int i = height; i > 0; i--) { |
| 248 | if(!fread(row, rowSize, 1, f)) break; // End of file |
| 249 | for (int j = 0; j < width; j++) { |
| 250 | int c1 = row[j * (bpp / 8)]; |
| 251 | int c2 = row[j * (bpp / 8) + 1]; |
| 252 | int c3 = row[j * (bpp / 8) + 2]; |
| 253 | buffer[(i - 1) * width + j] = (c3 << 16) | (c2 << 8) | c1; |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | return 0; |
| 258 | } |
| 259 | |
| 260 | int DrawBitmapImage(int x, int y, int w, int h, uint8_t *data, surface_t* surface, bool preserveAspectRatio) { |
| 261 | bitmap_file_header_t bmpHeader = *(bitmap_file_header_t*)data; |