From 1cffacb72e5adb4a2903c99adc7baea8b84bf4da Mon Sep 17 00:00:00 2001 From: Clownacy Date: Mon, 27 Jan 2020 00:30:54 +0000 Subject: [PATCH] Added DecodeBitmapFromFile --- src/Bitmap.cpp | 19 +++++++++++++++++++ src/Bitmap.h | 1 + src/Draw.cpp | 27 ++------------------------- 3 files changed, 22 insertions(+), 25 deletions(-) diff --git a/src/Bitmap.cpp b/src/Bitmap.cpp index 589509c9..954f4dd3 100644 --- a/src/Bitmap.cpp +++ b/src/Bitmap.cpp @@ -8,11 +8,30 @@ #define STBI_NO_LINEAR #include "stb_image.h" +#include "File.h" + unsigned char* DecodeBitmap(const unsigned char *in_buffer, size_t in_buffer_size, unsigned int *width, unsigned int *height) { return stbi_load_from_memory(in_buffer, in_buffer_size, (int*)width, (int*)height, NULL, 3); } +unsigned char* DecodeBitmapFromFile(const char *path, unsigned int *width, unsigned int *height) +{ + unsigned char *image_buffer = NULL; + + size_t size; + unsigned char *data = LoadFileToMemory(path, &size); + + if (data != NULL) + { + image_buffer = DecodeBitmap(data, size, width, height); + + free(data); + } + + return image_buffer; +} + void FreeBitmap(unsigned char *buffer) { stbi_image_free(buffer); diff --git a/src/Bitmap.h b/src/Bitmap.h index c59c3d6e..48f12ea1 100644 --- a/src/Bitmap.h +++ b/src/Bitmap.h @@ -3,4 +3,5 @@ #include unsigned char* DecodeBitmap(const unsigned char *in_buffer, size_t in_buffer_size, unsigned int *width, unsigned int *height); +unsigned char* DecodeBitmapFromFile(const char *path, unsigned int *width, unsigned int *height); void FreeBitmap(unsigned char *buffer); diff --git a/src/Draw.cpp b/src/Draw.cpp index fb768a25..a9153995 100644 --- a/src/Draw.cpp +++ b/src/Draw.cpp @@ -13,7 +13,6 @@ #include "Bitmap.h" #include "CommonDefines.h" #include "Ending.h" -#include "File.h" #include "Font.h" #include "Generic.h" #include "Main.h" @@ -282,19 +281,8 @@ BOOL MakeSurface_File(const char *name, SurfaceID surf_no) return FALSE; } - size_t size; - unsigned char *data = LoadFileToMemory(path, &size); - - if (data == NULL) - { - PrintBitmapError(path, 1); - return FALSE; - } - unsigned int width, height; - unsigned char *image_buffer = DecodeBitmap(data, size, &width, &height); - - free(data); + unsigned char *image_buffer = DecodeBitmapFromFile(path, &width, &height); if (image_buffer == NULL) { @@ -378,19 +366,8 @@ BOOL ReloadBitmap_File(const char *name, SurfaceID surf_no) return FALSE; } - size_t size; - unsigned char *data = LoadFileToMemory(path, &size); - - if (data == NULL) - { - PrintBitmapError(path, 1); - return FALSE; - } - unsigned int width, height; - unsigned char *image_buffer = DecodeBitmap(data, size, &width, &height); - - free(data); + unsigned char *image_buffer = DecodeBitmapFromFile(path, &width, &height); if (image_buffer == NULL) {