Browse Source

Review memory loading functions signesness

Ray 5 years ago
parent
commit
8cf0be4b6c
5 changed files with 47 additions and 38 deletions
  1. 10 2
      examples/text/text_font_sdf.c
  2. 10 10
      src/raudio.c
  3. 9 8
      src/raylib.h
  4. 3 3
      src/text.c
  5. 15 15
      src/textures.c

+ 10 - 2
examples/text/text_font_sdf.c

@@ -31,13 +31,19 @@ int main(void)
     // NOTE: Textures/Fonts MUST be loaded after Window initialization (OpenGL context is required)
 
     const char msg[50] = "Signed Distance Fields";
+    
+    // Loading file to memory
+    unsigned int fileSize = 0;
+    unsigned char *fileData = LoadFileData("resources/anonymous_pro_bold.ttf", &fileSize);
 
     // Default font generation from TTF font
     Font fontDefault = { 0 };
     fontDefault.baseSize = 16;
     fontDefault.charsCount = 95;
+    
+    // Loading font data from memory data
     // Parameters > font size: 16, no chars array provided (0), chars count: 95 (autogenerate chars array)
-    fontDefault.chars = LoadFontData("resources/anonymous_pro_bold.ttf", 16, 0, 95, FONT_DEFAULT);
+    fontDefault.chars = LoadFontData(fileData, fileSize, 16, 0, 95, FONT_DEFAULT);
     // Parameters > chars count: 95, font size: 16, chars padding in image: 4 px, pack method: 0 (default)
     Image atlas = GenImageFontAtlas(fontDefault.chars, &fontDefault.recs, 95, 16, 4, 0);
     fontDefault.texture = LoadTextureFromImage(atlas);
@@ -48,11 +54,13 @@ int main(void)
     fontSDF.baseSize = 16;
     fontSDF.charsCount = 95;
     // Parameters > font size: 16, no chars array provided (0), chars count: 0 (defaults to 95)
-    fontSDF.chars = LoadFontData("resources/anonymous_pro_bold.ttf", 16, 0, 0, FONT_SDF);
+    fontSDF.chars = LoadFontData(fileData, fileSize, 16, 0, 0, FONT_SDF);
     // Parameters > chars count: 95, font size: 16, chars padding in image: 0 px, pack method: 1 (Skyline algorythm)
     atlas = GenImageFontAtlas(fontSDF.chars, &fontSDF.recs, 95, 16, 0, 1);
     fontSDF.texture = LoadTextureFromImage(atlas);
     UnloadImage(atlas);
+    
+    RL_FREE(fileData);      // Free memory from loaded file
 
     // Load SDF required shader (we use default vertex shader)
     Shader shader = LoadShader(0, TextFormat("resources/shaders/glsl%i/sdf.fs", GLSL_VERSION));

+ 10 - 10
src/raudio.c

@@ -370,17 +370,17 @@ static void InitAudioBufferPool(void);                  // Initialise the multic
 static void CloseAudioBufferPool(void);                 // Close the audio buffers pool
 
 #if defined(SUPPORT_FILEFORMAT_WAV)
-static Wave LoadWAV(const char *fileData, unsigned int fileSize);   // Load WAV file
+static Wave LoadWAV(const unsigned char *fileData, unsigned int fileSize);   // Load WAV file
 static int SaveWAV(Wave wave, const char *fileName);    // Save wave data as WAV file
 #endif
 #if defined(SUPPORT_FILEFORMAT_OGG)
-static Wave LoadOGG(const char *fileData, unsigned int fileSize);   // Load OGG file
+static Wave LoadOGG(const unsigned char *fileData, unsigned int fileSize);   // Load OGG file
 #endif
 #if defined(SUPPORT_FILEFORMAT_FLAC)
-static Wave LoadFLAC(const char *fileData, unsigned int fileSize);  // Load FLAC file
+static Wave LoadFLAC(const unsigned char *fileData, unsigned int fileSize);  // Load FLAC file
 #endif
 #if defined(SUPPORT_FILEFORMAT_MP3)
-static Wave LoadMP3(const char *fileData, unsigned int fileSize);   // Load MP3 file
+static Wave LoadMP3(const unsigned char *fileData, unsigned int fileSize);   // Load MP3 file
 #endif
 
 #if defined(RAUDIO_STANDALONE)
@@ -695,7 +695,7 @@ Wave LoadWave(const char *fileName)
     unsigned char *fileData = LoadFileData(fileName, &fileSize);
 
     // Loading wave from memory data
-    wave = LoadWaveFromMemory(GetFileExtension(fileName), (char *)fileData, fileSize);
+    wave = LoadWaveFromMemory(GetFileExtension(fileName), fileData, fileSize);
 
     RL_FREE(fileData);
 
@@ -703,7 +703,7 @@ Wave LoadWave(const char *fileName)
 }
 
 // Load wave from memory buffer, fileType refers to extension: i.e. "wav"
-Wave LoadWaveFromMemory(const char *fileType, const char *fileData, int dataSize)
+Wave LoadWaveFromMemory(const char *fileType, const unsigned char *fileData, int dataSize)
 {
     Wave wave = { 0 };
     
@@ -1911,7 +1911,7 @@ static void CloseAudioBufferPool(void)
 #if defined(SUPPORT_FILEFORMAT_WAV)
 // Load WAV file data into Wave structure
 // NOTE: Using dr_wav library
-static Wave LoadWAV(const char *fileData, unsigned int fileSize)
+static Wave LoadWAV(const unsigned char *fileData, unsigned int fileSize)
 {
     Wave wave = { 0 };
     drwav wav = { 0 };
@@ -1962,7 +1962,7 @@ static int SaveWAV(Wave wave, const char *fileName)
 #if defined(SUPPORT_FILEFORMAT_OGG)
 // Load OGG file data into Wave structure
 // NOTE: Using stb_vorbis library
-static Wave LoadOGG(const char *fileData, unsigned int fileSize)
+static Wave LoadOGG(const unsigned char *fileData, unsigned int fileSize)
 {
     Wave wave = { 0 };
 
@@ -1997,7 +1997,7 @@ static Wave LoadOGG(const char *fileData, unsigned int fileSize)
 #if defined(SUPPORT_FILEFORMAT_FLAC)
 // Load FLAC file data into Wave structure
 // NOTE: Using dr_flac library
-static Wave LoadFLAC(const char *fileData, unsigned int fileSize)
+static Wave LoadFLAC(const unsigned char *fileData, unsigned int fileSize)
 {
     Wave wave = { 0 };
 
@@ -2021,7 +2021,7 @@ static Wave LoadFLAC(const char *fileData, unsigned int fileSize)
 #if defined(SUPPORT_FILEFORMAT_MP3)
 // Load MP3 file data into Wave structure
 // NOTE: Using dr_mp3 library
-static Wave LoadMP3(const char *fileData, unsigned int fileSize)
+static Wave LoadMP3(const unsigned char *fileData, unsigned int fileSize)
 {
     Wave wave = { 0 };
     drmp3_config config = { 0 };

+ 9 - 8
src/raylib.h

@@ -153,10 +153,11 @@
 
 // Temporal hack to avoid breaking old codebases using
 // deprecated raylib implementation of these functions
-#define FormatText  TextFormat
-#define SubText     TextSubtext
-#define ShowWindow  UnhideWindow
-#define LoadText    LoadFileText
+#define FormatText      TextFormat
+#define SubText         TextSubtext
+#define ShowWindow      UnhideWindow
+#define LoadText        LoadFileText
+#define GetExtension    GetFileExtension
 //#define Fade(c, a)  ColorAlpha(c, a)
 
 //----------------------------------------------------------------------------------
@@ -1103,7 +1104,7 @@ RLAPI bool CheckCollisionPointTriangle(Vector2 point, Vector2 p1, Vector2 p2, Ve
 RLAPI Image LoadImage(const char *fileName);                                                             // Load image from file into CPU memory (RAM)
 RLAPI Image LoadImageRaw(const char *fileName, int width, int height, int format, int headerSize);       // Load image from RAW file data
 RLAPI Image LoadImageAnim(const char *fileName, int *frames);                                            // Load image sequence from file (frames appended to image.data)
-RLAPI Image LoadImageFromMemory(const char *fileType, const char *fileData, int dataSize);               // Load image from memory buffer, fileType refers to extension: i.e. "png"
+RLAPI Image LoadImageFromMemory(const char *fileType, const unsigned char *fileData, int dataSize);      // Load image from memory buffer, fileType refers to extension: i.e. "png"
 RLAPI void UnloadImage(Image image);                                                                     // Unload image from CPU memory (RAM)
 RLAPI void ExportImage(Image image, const char *fileName);                                               // Export image data to file
 RLAPI void ExportImageAsCode(Image image, const char *fileName);                                         // Export image as code file defining an array of bytes
@@ -1219,8 +1220,8 @@ RLAPI Font GetFontDefault(void);
 RLAPI Font LoadFont(const char *fileName);                                                  // Load font from file into GPU memory (VRAM)
 RLAPI Font LoadFontEx(const char *fileName, int fontSize, int *fontChars, int charsCount);  // Load font from file with extended parameters
 RLAPI Font LoadFontFromImage(Image image, Color key, int firstChar);                        // Load font from Image (XNA style)
-RLAPI Font LoadFontFromMemory(const char *fileType,  const char *fileData, int dataSize, int fontSize, int *fontChars, int charsCount); // Load font from memory buffer, fileType refers to extension: i.e. "ttf"
-RLAPI CharInfo *LoadFontData(const char *fileData, int dataSize, int fontSize, int *fontChars, int charsCount, int type);               // Load font data for further use
+RLAPI Font LoadFontFromMemory(const char *fileType, const unsigned char *fileData, int dataSize, int fontSize, int *fontChars, int charsCount); // Load font from memory buffer, fileType refers to extension: i.e. "ttf"
+RLAPI CharInfo *LoadFontData(const unsigned char *fileData, int dataSize, int fontSize, int *fontChars, int charsCount, int type);      // Load font data for further use
 RLAPI Image GenImageFontAtlas(const CharInfo *chars, Rectangle **recs, int charsCount, int fontSize, int padding, int packMethod);      // Generate image font atlas using chars info
 RLAPI void UnloadFont(Font font);                                                           // Unload Font from GPU memory (VRAM)
 
@@ -1415,7 +1416,7 @@ RLAPI void SetMasterVolume(float volume);                             // Set mas
 
 // Wave/Sound loading/unloading functions
 RLAPI Wave LoadWave(const char *fileName);                            // Load wave data from file
-RLAPI Wave LoadWaveFromMemory(const char *fileType, const char *fileData, int dataSize); // Load wave from memory buffer, fileType refers to extension: i.e. "wav"
+RLAPI Wave LoadWaveFromMemory(const char *fileType, const unsigned char *fileData, int dataSize); // Load wave from memory buffer, fileType refers to extension: i.e. "wav"
 RLAPI Sound LoadSound(const char *fileName);                          // Load sound from file
 RLAPI Sound LoadSoundFromWave(Wave wave);                             // Load sound from wave data
 RLAPI void UpdateSound(Sound sound, const void *data, int samplesCount);// Update sound buffer with new data

+ 3 - 3
src/text.c

@@ -341,7 +341,7 @@ Font LoadFontEx(const char *fileName, int fontSize, int *fontChars, int charsCou
     unsigned char *fileData = LoadFileData(fileName, &fileSize);
     
     // Loading font from memory data
-    font = LoadFontFromMemory(GetFileExtension(fileName), (char *)fileData, fileSize, fontSize, fontChars, charsCount);
+    font = LoadFontFromMemory(GetFileExtension(fileName), fileData, fileSize, fontSize, fontChars, charsCount);
     
     RL_FREE(fileData);
 
@@ -471,7 +471,7 @@ Font LoadFontFromImage(Image image, Color key, int firstChar)
 }
 
 // Load font from memory buffer, fileType refers to extension: i.e. "ttf"
-Font LoadFontFromMemory(const char *fileType, const char *fileData, int dataSize, int fontSize, int *fontChars, int charsCount)
+Font LoadFontFromMemory(const char *fileType, const unsigned char *fileData, int dataSize, int fontSize, int *fontChars, int charsCount)
 {
     Font font = { 0 };
     
@@ -511,7 +511,7 @@ Font LoadFontFromMemory(const char *fileType, const char *fileData, int dataSize
 
 // Load font data for further use
 // NOTE: Requires TTF font memory data and can generate SDF data
-CharInfo *LoadFontData(const char *fileData, int dataSize, int fontSize, int *fontChars, int charsCount, int type)
+CharInfo *LoadFontData(const unsigned char *fileData, int dataSize, int fontSize, int *fontChars, int charsCount, int type)
 {
     // NOTE: Using some SDF generation default values,
     // trades off precision with ability to handle *smaller* sizes

+ 15 - 15
src/textures.c

@@ -171,20 +171,20 @@
 // Module specific Functions Declaration
 //----------------------------------------------------------------------------------
 #if defined(SUPPORT_FILEFORMAT_DDS)
-static Image LoadDDS(const char *fileData, unsigned int fileSize);   // Load DDS file data
+static Image LoadDDS(const unsigned char *fileData, unsigned int fileSize);   // Load DDS file data
 #endif
 #if defined(SUPPORT_FILEFORMAT_PKM)
-static Image LoadPKM(const char *fileData, unsigned int fileSize);   // Load PKM file data
+static Image LoadPKM(const unsigned char *fileData, unsigned int fileSize);   // Load PKM file data
 #endif
 #if defined(SUPPORT_FILEFORMAT_KTX)
-static Image LoadKTX(const char *fileData, unsigned int fileSize);   // Load KTX file data
+static Image LoadKTX(const unsigned char *fileData, unsigned int fileSize);   // Load KTX file data
 static int SaveKTX(Image image, const char *fileName);  // Save image data as KTX file
 #endif
 #if defined(SUPPORT_FILEFORMAT_PVR)
-static Image LoadPVR(const char *fileData, unsigned int fileSize);   // Load PVR file data
+static Image LoadPVR(const unsigned char *fileData, unsigned int fileSize);   // Load PVR file data
 #endif
 #if defined(SUPPORT_FILEFORMAT_ASTC)
-static Image LoadASTC(const char *fileData, unsigned int fileSize);  // Load ASTC file data
+static Image LoadASTC(const unsigned char *fileData, unsigned int fileSize);  // Load ASTC file data
 #endif
 
 //----------------------------------------------------------------------------------
@@ -212,7 +212,7 @@ Image LoadImage(const char *fileName)
     unsigned char *fileData = LoadFileData(fileName, &fileSize);
     
     // Loading image from memory data
-    image = LoadImageFromMemory(GetFileExtension(fileName), (char *)fileData, fileSize);
+    image = LoadImageFromMemory(GetFileExtension(fileName), fileData, fileSize);
 
     if (image.data != NULL) TRACELOG(LOG_INFO, "IMAGE: [%s] Data loaded successfully (%ix%i)", fileName, image.width, image.height);
     else TRACELOG(LOG_WARNING, "IMAGE: [%s] Failed to load data", fileName);
@@ -291,7 +291,7 @@ Image LoadImageAnim(const char *fileName, int *frames)
 }
 
 // Load image from memory buffer, fileType refers to extension: i.e. "png"
-Image LoadImageFromMemory(const char *fileType, const char *fileData, int dataSize)
+Image LoadImageFromMemory(const char *fileType, const unsigned char *fileData, int dataSize)
 {
     Image image = { 0 };
     
@@ -330,7 +330,7 @@ Image LoadImageFromMemory(const char *fileType, const char *fileData, int dataSi
         if (fileData != NULL)
         {
             int comp = 0;
-            image.data = stbi_load_from_memory((unsigned char *)fileData, dataSize, &image.width, &image.height, &comp, 0);
+            image.data = stbi_load_from_memory(fileData, dataSize, &image.width, &image.height, &comp, 0);
 
             image.mipmaps = 1;
 
@@ -348,7 +348,7 @@ Image LoadImageFromMemory(const char *fileType, const char *fileData, int dataSi
         if (fileData != NULL)
         {
             int comp = 0;
-            image.data = stbi_loadf_from_memory((unsigned char *)fileData, dataSize, &image.width, &image.height, &comp, 0);
+            image.data = stbi_loadf_from_memory(fileData, dataSize, &image.width, &image.height, &comp, 0);
 
             image.mipmaps = 1;
 
@@ -3730,7 +3730,7 @@ int GetPixelDataSize(int width, int height, int format)
 //----------------------------------------------------------------------------------
 #if defined(SUPPORT_FILEFORMAT_DDS)
 // Loading DDS image data (compressed or uncompressed)
-static Image LoadDDS(const char *fileData, unsigned int fileSize)
+static Image LoadDDS(const unsigned char *fileData, unsigned int fileSize)
 {
     unsigned char *fileDataPtr = (unsigned char *)fileData;
     
@@ -3926,9 +3926,9 @@ static Image LoadDDS(const char *fileData, unsigned int fileSize)
 // Loading PKM image data (ETC1/ETC2 compression)
 // NOTE: KTX is the standard Khronos Group compression format (ETC1/ETC2, mipmaps)
 // PKM is a much simpler file format used mainly to contain a single ETC1/ETC2 compressed image (no mipmaps)
-static Image LoadPKM(const char *fileData, unsigned int fileSize)
+static Image LoadPKM(const unsigned char *fileData, unsigned int fileSize)
 {
-    unsigned char *fileDataPtr = fileData;
+    unsigned char *fileDataPtr = (unsigned char *)fileData;
     
     // Required extensions:
     // GL_OES_compressed_ETC1_RGB8_texture  (ETC1) (OpenGL ES 2.0)
@@ -4006,7 +4006,7 @@ static Image LoadPKM(const char *fileData, unsigned int fileSize)
 
 #if defined(SUPPORT_FILEFORMAT_KTX)
 // Load KTX compressed image data (ETC1/ETC2 compression)
-static Image LoadKTX(const char *fileData, unsigned int fileSize)
+static Image LoadKTX(const unsigned char *fileData, unsigned int fileSize)
 {
     unsigned char *fileDataPtr = (unsigned char *)fileData;
     
@@ -4190,7 +4190,7 @@ static int SaveKTX(Image image, const char *fileName)
 #if defined(SUPPORT_FILEFORMAT_PVR)
 // Loading PVR image data (uncompressed or PVRT compression)
 // NOTE: PVR v2 not supported, use PVR v3 instead
-static Image LoadPVR(const char *fileData, unsigned int fileSize)
+static Image LoadPVR(const unsigned char *fileData, unsigned int fileSize)
 {
     unsigned char *fileDataPtr = (unsigned char *)fileData;
     
@@ -4325,7 +4325,7 @@ static Image LoadPVR(const char *fileData, unsigned int fileSize)
 
 #if defined(SUPPORT_FILEFORMAT_ASTC)
 // Load ASTC compressed image data (ASTC compression)
-static Image LoadASTC(const char *fileData, unsigned int fileSize)
+static Image LoadASTC(const unsigned char *fileData, unsigned int fileSize)
 {
     unsigned char *fileDataPtr = (unsigned char *)fileData;