summaryrefslogtreecommitdiffhomepage
path: root/src/utils.c
diff options
context:
space:
mode:
authorvictorfisac <[email protected]>2017-03-06 09:40:04 +0100
committervictorfisac <[email protected]>2017-03-06 09:40:04 +0100
commit9261c3b8dc03d093bff5246a18ad9310ae8eaeb3 (patch)
treeaf87165723ac563ee1a7e1c605c7a4df821d74ea /src/utils.c
parente8630c78d069a1cba50b1a78108663ebc19e5b9b (diff)
parentb734802743f2089c8d649b27aea48ab71fa653b3 (diff)
downloadraylib-9261c3b8dc03d093bff5246a18ad9310ae8eaeb3.tar.gz
raylib-9261c3b8dc03d093bff5246a18ad9310ae8eaeb3.zip
Merge remote-tracking branch 'refs/remotes/raysan5/develop' into develop
Diffstat (limited to 'src/utils.c')
-rw-r--r--src/utils.c208
1 files changed, 44 insertions, 164 deletions
diff --git a/src/utils.c b/src/utils.c
index 640c5720..9a2a723a 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -1,16 +1,23 @@
/**********************************************************************************************
*
-* raylib.utils
+* raylib.utils - Some common utility functions
*
-* Some utility functions
+* CONFIGURATION:
*
-* External libs:
-* tinfl - zlib DEFLATE algorithm decompression
+* #define SUPPORT_SAVE_PNG
+* Enable saving PNG fileformat
+* NOTE: Requires stb_image_write library
+*
+* #define SUPPORT_SAVE_BMP
+*
+* #define DO_NOT_TRACE_DEBUG_MSGS
+* Avoid showing DEBUG TraceLog() messages
+*
+* DEPENDENCIES:
* stb_image_write - PNG writting functions
*
-* Module Configuration Flags:
-* DO_NOT_TRACE_DEBUG_MSGS - Avoid showing DEBUG TraceLog() messages
*
+* LICENSE: zlib/libpng
*
* Copyright (c) 2014-2016 Ramon Santamaria (@raysan5)
*
@@ -42,16 +49,17 @@
#include <stdlib.h> // Required for: malloc(), free()
#include <stdio.h> // Required for: fopen(), fclose(), fputc(), fwrite(), printf(), fprintf(), funopen()
#include <stdarg.h> // Required for: va_list, va_start(), vfprintf(), va_end()
-//#include <string.h> // Required for: strlen(), strrchr(), strcmp()
+#include <string.h> // Required for: strlen(), strrchr(), strcmp()
#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_RPI)
#define STB_IMAGE_WRITE_IMPLEMENTATION
- #include "external/stb_image_write.h" // Required for: stbi_write_png()
+ #include "external/stb_image_write.h" // Required for: stbi_write_bmp(), stbi_write_png()
#endif
-#include "external/tinfl.c" // Required for: tinfl_decompress_mem_to_mem()
- // NOTE: DEFLATE algorythm data decompression
+#define RRES_IMPLEMENTATION
+#include "rres.h"
+//#define NO_TRACELOG // Avoid TraceLog() output (any type)
#define DO_NOT_TRACE_DEBUG_MSGS // Avoid DEBUG messages tracing
//----------------------------------------------------------------------------------
@@ -74,143 +82,11 @@ static int android_close(void *cookie);
//----------------------------------------------------------------------------------
// Module Functions Definition - Utilities
//----------------------------------------------------------------------------------
-
-// Data decompression function
-// NOTE: Allocated data MUST be freed!
-unsigned char *DecompressData(const unsigned char *data, unsigned long compSize, int uncompSize)
-{
- int tempUncompSize;
- unsigned char *pUncomp;
-
- // Allocate buffer to hold decompressed data
- pUncomp = (mz_uint8 *)malloc((size_t)uncompSize);
-
- // Check correct memory allocation
- if (pUncomp == NULL)
- {
- TraceLog(WARNING, "Out of memory while decompressing data");
- }
- else
- {
- // Decompress data
- tempUncompSize = tinfl_decompress_mem_to_mem(pUncomp, (size_t)uncompSize, data, compSize, 1);
-
- if (tempUncompSize == -1)
- {
- TraceLog(WARNING, "Data decompression failed");
- free(pUncomp);
- }
-
- if (uncompSize != (int)tempUncompSize)
- {
- TraceLog(WARNING, "Expected uncompressed size do not match, data may be corrupted");
- TraceLog(WARNING, " -- Expected uncompressed size: %i", uncompSize);
- TraceLog(WARNING, " -- Returned uncompressed size: %i", tempUncompSize);
- }
-
- TraceLog(INFO, "Data decompressed successfully from %u bytes to %u bytes", (mz_uint32)compSize, (mz_uint32)tempUncompSize);
- }
-
- return pUncomp;
-}
-
-#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_RPI)
-// Creates a bitmap (BMP) file from an array of pixel data
-// NOTE: This function is not explicitly available to raylib users
-void WriteBitmap(const char *fileName, unsigned char *imgData, int width, int height)
-{
- int filesize = 54 + 3*width*height;
-
- unsigned char bmpFileHeader[14] = {'B','M', 0,0,0,0, 0,0, 0,0, 54,0,0,0}; // Standard BMP file header
- unsigned char bmpInfoHeader[40] = {40,0,0,0, 0,0,0,0, 0,0,0,0, 1,0, 24,0}; // Standard BMP info header
-
- bmpFileHeader[2] = (unsigned char)(filesize);
- bmpFileHeader[3] = (unsigned char)(filesize>>8);
- bmpFileHeader[4] = (unsigned char)(filesize>>16);
- bmpFileHeader[5] = (unsigned char)(filesize>>24);
-
- bmpInfoHeader[4] = (unsigned char)(width);
- bmpInfoHeader[5] = (unsigned char)(width>>8);
- bmpInfoHeader[6] = (unsigned char)(width>>16);
- bmpInfoHeader[7] = (unsigned char)(width>>24);
- bmpInfoHeader[8] = (unsigned char)(height);
- bmpInfoHeader[9] = (unsigned char)(height>>8);
- bmpInfoHeader[10] = (unsigned char)(height>>16);
- bmpInfoHeader[11] = (unsigned char)(height>>24);
-
- FILE *bmpFile = fopen(fileName, "wb"); // Define a pointer to bitmap file and open it in write-binary mode
-
- if (bmpFile == NULL)
- {
- TraceLog(WARNING, "[%s] BMP file could not be created", fileName);
- }
- else
- {
- // NOTE: fwrite parameters are: data pointer, size in bytes of each element to be written, number of elements, file-to-write pointer
- fwrite(bmpFileHeader, sizeof(unsigned char), 14, bmpFile); // Write BMP file header data
- fwrite(bmpInfoHeader, sizeof(unsigned char), 40, bmpFile); // Write BMP info header data
-
- // Write pixel data to file
- for (int y = 0; y < height ; y++)
- {
- for (int x = 0; x < width; x++)
- {
- fputc(imgData[(x*4)+2 + (y*width*4)], bmpFile);
- fputc(imgData[(x*4)+1 + (y*width*4)], bmpFile);
- fputc(imgData[(x*4) + (y*width*4)], bmpFile);
- }
- }
- }
-
- fclose(bmpFile); // Close bitmap file
-}
-
-// Creates a PNG image file from an array of pixel data
-// NOTE: Uses stb_image_write
-void WritePNG(const char *fileName, unsigned char *imgData, int width, int height, int compSize)
-{
- stbi_write_png(fileName, width, height, compSize, imgData, width*compSize);
-}
-#endif
-
-#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_RPI) || defined(PLATFORM_WEB)
-// Outputs a trace log message (INFO, ERROR, WARNING)
-// NOTE: If a file has been init, output log is written there
-void TraceLog(int msgType, const char *text, ...)
-{
- va_list args;
- int traceDebugMsgs = 1;
-
-#ifdef DO_NOT_TRACE_DEBUG_MSGS
- traceDebugMsgs = 0;
-#endif
-
- switch(msgType)
- {
- case INFO: fprintf(stdout, "INFO: "); break;
- case ERROR: fprintf(stdout, "ERROR: "); break;
- case WARNING: fprintf(stdout, "WARNING: "); break;
- case DEBUG: if (traceDebugMsgs) fprintf(stdout, "DEBUG: "); break;
- default: break;
- }
-
- if ((msgType != DEBUG) || ((msgType == DEBUG) && (traceDebugMsgs)))
- {
- va_start(args, text);
- vfprintf(stdout, text, args);
- va_end(args);
-
- fprintf(stdout, "\n");
- }
-
- if (msgType == ERROR) exit(1); // If ERROR message, exit program
-}
-#endif
-
-#if defined(PLATFORM_ANDROID)
+// Outputs a trace log message
void TraceLog(int msgType, const char *text, ...)
{
- static char buffer[100];
+#if !defined(NO_TRACELOG)
+ static char buffer[128];
int traceDebugMsgs = 1;
#ifdef DO_NOT_TRACE_DEBUG_MSGS
@@ -230,8 +106,9 @@ void TraceLog(int msgType, const char *text, ...)
strcat(buffer, "\n");
va_list args;
- va_start(args, buffer);
+ va_start(args, text);
+#if defined(PLATFORM_ANDROID)
switch(msgType)
{
case INFO: __android_log_vprint(ANDROID_LOG_INFO, "raylib", buffer, args); break;
@@ -240,12 +117,32 @@ void TraceLog(int msgType, const char *text, ...)
case DEBUG: if (traceDebugMsgs) __android_log_vprint(ANDROID_LOG_DEBUG, "raylib", buffer, args); break;
default: break;
}
+#else
+ if ((msgType != DEBUG) || ((msgType == DEBUG) && (traceDebugMsgs))) vprintf(buffer, args);
+#endif
va_end(args);
- if (msgType == ERROR) exit(1);
+ if (msgType == ERROR) exit(1); // If ERROR message, exit program
+
+#endif // NO_TRACELOG
+}
+
+#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_RPI)
+// Creates a BMP image file from an array of pixel data
+void SaveBMP(const char *fileName, unsigned char *imgData, int width, int height, int compSize)
+{
+ stbi_write_bmp(fileName, width, height, compSize, imgData);
+}
+
+// Creates a PNG image file from an array of pixel data
+void SavePNG(const char *fileName, unsigned char *imgData, int width, int height, int compSize)
+{
+ stbi_write_png(fileName, width, height, compSize, imgData, width*compSize);
}
+#endif
+#if defined(PLATFORM_ANDROID)
// Initialize asset manager from android app
void InitAssetManager(AAssetManager *manager)
{
@@ -283,23 +180,6 @@ const char *GetExtension(const char *fileName)
return (dot + 1);
}
-// Calculate next power-of-two value for a given num
-int GetNextPOT(int num)
-{
- if (num != 0)
- {
- num--;
- num |= (num >> 1); // Or first 2 bits
- num |= (num >> 2); // Or next 2 bits
- num |= (num >> 4); // Or next 4 bits
- num |= (num >> 8); // Or next 8 bits
- num |= (num >> 16); // Or next 16 bits
- num++;
- }
-
- return num;
-}
-
//----------------------------------------------------------------------------------
// Module specific Functions Definition
//----------------------------------------------------------------------------------