2014-09-17 00:51:31 +04:00
|
|
|
/**********************************************************************************************
|
2014-01-23 15:36:18 +04:00
|
|
|
*
|
|
|
|
* raylib.utils
|
|
|
|
*
|
|
|
|
* Utils Functions Definitions
|
2014-09-03 18:51:28 +04:00
|
|
|
*
|
|
|
|
* Uses external libs:
|
2014-01-23 15:36:18 +04:00
|
|
|
* tinfl - zlib DEFLATE algorithm decompression lib
|
2014-04-19 18:36:49 +04:00
|
|
|
* stb_image_write - PNG writting functions
|
2014-09-03 18:51:28 +04:00
|
|
|
*
|
2014-09-17 00:51:31 +04:00
|
|
|
* Copyright (c) 2014 Ramon Santamaria (Ray San - raysan@raysanweb.com)
|
2014-09-03 18:51:28 +04:00
|
|
|
*
|
|
|
|
* This software is provided "as-is", without any express or implied warranty. In no event
|
2014-01-23 15:36:18 +04:00
|
|
|
* will the authors be held liable for any damages arising from the use of this software.
|
|
|
|
*
|
2014-09-03 18:51:28 +04:00
|
|
|
* Permission is granted to anyone to use this software for any purpose, including commercial
|
2014-01-23 15:36:18 +04:00
|
|
|
* applications, and to alter it and redistribute it freely, subject to the following restrictions:
|
|
|
|
*
|
2014-09-03 18:51:28 +04:00
|
|
|
* 1. The origin of this software must not be misrepresented; you must not claim that you
|
|
|
|
* wrote the original software. If you use this software in a product, an acknowledgment
|
2014-01-23 15:36:18 +04:00
|
|
|
* in the product documentation would be appreciated but is not required.
|
|
|
|
*
|
|
|
|
* 2. Altered source versions must be plainly marked as such, and must not be misrepresented
|
|
|
|
* as being the original software.
|
|
|
|
*
|
|
|
|
* 3. This notice may not be removed or altered from any source distribution.
|
|
|
|
*
|
|
|
|
**********************************************************************************************/
|
|
|
|
|
|
|
|
#include "utils.h"
|
|
|
|
|
2014-09-17 00:51:31 +04:00
|
|
|
#if defined(PLATFORM_ANDROID)
|
|
|
|
#include <errno.h>
|
|
|
|
#include <android/log.h>
|
|
|
|
#include <android/asset_manager.h>
|
|
|
|
#endif
|
|
|
|
|
2014-04-19 18:36:49 +04:00
|
|
|
#include <stdlib.h> // malloc(), free()
|
|
|
|
#include <stdio.h> // printf(), fprintf()
|
|
|
|
#include <stdarg.h> // Used for functions with variable number of parameters (TraceLog())
|
|
|
|
//#include <string.h> // String management functions: strlen(), strrchr(), strcmp()
|
2014-01-23 15:36:18 +04:00
|
|
|
|
2014-09-17 00:51:31 +04:00
|
|
|
#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_RPI)
|
|
|
|
#define STB_IMAGE_WRITE_IMPLEMENTATION
|
|
|
|
#include "stb_image_write.h" // Create PNG file
|
|
|
|
#endif
|
2014-04-19 18:36:49 +04:00
|
|
|
|
2014-01-23 15:36:18 +04:00
|
|
|
#include "tinfl.c"
|
|
|
|
|
2014-04-09 22:25:26 +04:00
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
// Global Variables Definition
|
|
|
|
//----------------------------------------------------------------------------------
|
2014-09-17 00:51:31 +04:00
|
|
|
#if defined(PLATFORM_ANDROID)
|
|
|
|
AAssetManager *assetManager;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
// Module specific Functions Declaration
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
#if defined(PLATFORM_ANDROID)
|
|
|
|
static int android_read(void *cookie, char *buf, int size);
|
|
|
|
static int android_write(void *cookie, const char *buf, int size);
|
|
|
|
static fpos_t android_seek(void *cookie, fpos_t offset, int whence);
|
|
|
|
static int android_close(void *cookie);
|
|
|
|
#endif
|
2014-04-09 22:25:26 +04:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
// Module Functions Definition - Utilities
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
|
2014-01-23 15:36:18 +04:00
|
|
|
// 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;
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2014-01-23 15:36:18 +04:00
|
|
|
// Allocate buffer to hold decompressed data
|
|
|
|
pUncomp = (mz_uint8 *)malloc((size_t)uncompSize);
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2014-01-23 15:36:18 +04:00
|
|
|
// Check correct memory allocation
|
2015-01-18 12:57:30 +03:00
|
|
|
if (pUncomp == NULL)
|
2014-01-23 15:36:18 +04:00
|
|
|
{
|
2014-04-09 22:25:26 +04:00
|
|
|
TraceLog(WARNING, "Out of memory while decompressing data");
|
2014-01-23 15:36:18 +04:00
|
|
|
}
|
2014-04-09 22:25:26 +04:00
|
|
|
else
|
2014-01-23 15:36:18 +04:00
|
|
|
{
|
2014-04-09 22:25:26 +04:00
|
|
|
// Decompress data
|
|
|
|
tempUncompSize = tinfl_decompress_mem_to_mem(pUncomp, (size_t)uncompSize, data, compSize, 1);
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2014-04-09 22:25:26 +04:00
|
|
|
if (tempUncompSize == -1)
|
|
|
|
{
|
|
|
|
TraceLog(WARNING, "Data decompression failed");
|
|
|
|
free(pUncomp);
|
|
|
|
}
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2014-04-09 22:25:26 +04:00
|
|
|
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);
|
|
|
|
}
|
2014-01-23 15:36:18 +04:00
|
|
|
|
2014-04-09 22:25:26 +04:00
|
|
|
TraceLog(INFO, "Data decompressed successfully from %u bytes to %u bytes", (mz_uint32)compSize, (mz_uint32)tempUncompSize);
|
|
|
|
}
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2014-01-23 15:36:18 +04:00
|
|
|
return pUncomp;
|
|
|
|
}
|
|
|
|
|
2014-09-17 00:51:31 +04:00
|
|
|
#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_RPI)
|
2014-01-23 15:36:18 +04:00
|
|
|
// 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;
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2014-01-23 15:36:18 +04:00
|
|
|
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
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2014-12-31 20:03:32 +03:00
|
|
|
if (bmpFile == NULL)
|
|
|
|
{
|
|
|
|
TraceLog(WARNING, "[%s] BMP file could not be created", fileName);
|
|
|
|
}
|
|
|
|
else
|
2014-01-23 15:36:18 +04:00
|
|
|
{
|
2014-12-31 20:03:32 +03:00
|
|
|
// 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++)
|
2014-01-23 15:36:18 +04:00
|
|
|
{
|
2014-12-31 20:03:32 +03:00
|
|
|
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);
|
|
|
|
}
|
2014-01-23 15:36:18 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
stbi_write_png(fileName, width, height, 4, imgData, width*4); // It WORKS!!!
|
2014-04-09 22:25:26 +04:00
|
|
|
}
|
2014-12-15 03:08:30 +03:00
|
|
|
#endif
|
2014-04-09 22:25:26 +04:00
|
|
|
|
2014-12-15 03:08:30 +03:00
|
|
|
#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_RPI) || defined(PLATFORM_WEB)
|
2014-04-09 22:25:26 +04:00
|
|
|
// 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;
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2014-04-09 22:25:26 +04:00
|
|
|
#ifdef DO_NOT_TRACE_DEBUG_MSGS
|
|
|
|
traceDebugMsgs = 0;
|
|
|
|
#endif
|
2014-04-19 18:36:49 +04:00
|
|
|
|
|
|
|
switch(msgType)
|
2014-04-09 22:25:26 +04:00
|
|
|
{
|
2014-09-17 00:51:31 +04:00
|
|
|
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;
|
2014-04-19 18:36:49 +04:00
|
|
|
default: break;
|
2014-04-09 22:25:26 +04:00
|
|
|
}
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2014-04-19 18:36:49 +04:00
|
|
|
if ((msgType != DEBUG) || ((msgType == DEBUG) && (traceDebugMsgs)))
|
|
|
|
{
|
|
|
|
va_start(args, text);
|
2014-09-17 00:51:31 +04:00
|
|
|
vfprintf(stdout, text, args);
|
2014-04-19 18:36:49 +04:00
|
|
|
va_end(args);
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2014-09-17 00:51:31 +04:00
|
|
|
fprintf(stdout, "\n");
|
2014-04-09 22:25:26 +04:00
|
|
|
}
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2014-04-19 18:36:49 +04:00
|
|
|
if (msgType == ERROR) exit(1); // If ERROR message, exit program
|
2014-04-09 22:25:26 +04:00
|
|
|
}
|
2014-09-17 00:51:31 +04:00
|
|
|
#endif
|
2014-04-09 22:25:26 +04:00
|
|
|
|
2014-09-17 00:51:31 +04:00
|
|
|
#if defined(PLATFORM_ANDROID)
|
|
|
|
void TraceLog(int msgType, const char *text, ...)
|
2014-04-09 22:25:26 +04:00
|
|
|
{
|
2014-09-17 00:51:31 +04:00
|
|
|
static char buffer[100];
|
|
|
|
|
|
|
|
switch(msgType)
|
|
|
|
{
|
|
|
|
case INFO: strcpy(buffer, "INFO: "); break;
|
|
|
|
case ERROR: strcpy(buffer, "ERROR: "); break;
|
|
|
|
case WARNING: strcpy(buffer, "WARNING: "); break;
|
|
|
|
case DEBUG: strcpy(buffer, "DEBUG: "); break;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
|
|
|
|
strcat(buffer, text);
|
|
|
|
strcat(buffer, "\n");
|
|
|
|
|
|
|
|
va_list args;
|
|
|
|
va_start(args, buffer);
|
|
|
|
|
|
|
|
switch(msgType)
|
|
|
|
{
|
|
|
|
case INFO: __android_log_vprint(ANDROID_LOG_INFO, "raylib", buffer, args); break;
|
|
|
|
case ERROR: __android_log_vprint(ANDROID_LOG_ERROR, "raylib", buffer, args); break;
|
|
|
|
case WARNING: __android_log_vprint(ANDROID_LOG_WARN, "raylib", buffer, args); break;
|
|
|
|
case DEBUG: __android_log_vprint(ANDROID_LOG_DEBUG, "raylib", buffer, args); break;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
if (msgType == ERROR) exit(1);
|
|
|
|
}
|
2014-04-09 22:25:26 +04:00
|
|
|
|
2014-09-17 00:51:31 +04:00
|
|
|
// Initialize asset manager from android app
|
2015-02-02 02:53:49 +03:00
|
|
|
void InitAssetManager(AAssetManager *manager)
|
2014-09-17 00:51:31 +04:00
|
|
|
{
|
|
|
|
assetManager = manager;
|
2014-04-09 22:25:26 +04:00
|
|
|
}
|
|
|
|
|
2014-09-17 00:51:31 +04:00
|
|
|
// Replacement for fopen
|
2015-02-02 02:53:49 +03:00
|
|
|
FILE *android_fopen(const char *fileName, const char *mode)
|
2014-04-09 22:25:26 +04:00
|
|
|
{
|
2014-09-17 00:51:31 +04:00
|
|
|
if (mode[0] == 'w') return NULL;
|
|
|
|
|
|
|
|
AAsset *asset = AAssetManager_open(assetManager, fileName, 0);
|
2015-02-02 02:53:49 +03:00
|
|
|
|
2014-09-17 00:51:31 +04:00
|
|
|
if(!asset) return NULL;
|
|
|
|
|
|
|
|
return funopen(asset, android_read, android_write, android_seek, android_close);
|
2014-04-09 22:25:26 +04:00
|
|
|
}
|
2014-09-17 00:51:31 +04:00
|
|
|
#endif
|
2014-04-09 22:25:26 +04:00
|
|
|
|
2014-04-19 18:36:49 +04:00
|
|
|
// Keep track of memory allocated
|
|
|
|
// NOTE: mallocType defines the type of data allocated
|
2014-09-17 00:51:31 +04:00
|
|
|
/*
|
2014-04-19 18:36:49 +04:00
|
|
|
void RecordMalloc(int mallocType, int mallocSize, const char *msg)
|
|
|
|
{
|
|
|
|
// TODO: Investigate how to record memory allocation data...
|
|
|
|
// Maybe creating my own malloc function...
|
|
|
|
}
|
2014-09-17 00:51:31 +04:00
|
|
|
*/
|
2014-04-19 18:36:49 +04:00
|
|
|
|
|
|
|
// Get the extension for a filename
|
2014-09-03 18:51:28 +04:00
|
|
|
const char *GetExtension(const char *fileName)
|
2014-04-19 18:36:49 +04:00
|
|
|
{
|
|
|
|
const char *dot = strrchr(fileName, '.');
|
|
|
|
if(!dot || dot == fileName) return "";
|
|
|
|
return (dot + 1);
|
|
|
|
}
|
|
|
|
|
2014-12-31 20:03:32 +03:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2014-09-17 00:51:31 +04:00
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
// Module specific Functions Definition
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
#if defined(PLATFORM_ANDROID)
|
2015-02-02 02:53:49 +03:00
|
|
|
static int android_read(void *cookie, char *buf, int size)
|
2014-09-17 00:51:31 +04:00
|
|
|
{
|
|
|
|
return AAsset_read((AAsset *)cookie, buf, size);
|
|
|
|
}
|
|
|
|
|
2015-02-02 02:53:49 +03:00
|
|
|
static int android_write(void *cookie, const char *buf, int size)
|
2014-09-17 00:51:31 +04:00
|
|
|
{
|
|
|
|
TraceLog(ERROR, "Can't provide write access to the APK");
|
|
|
|
|
|
|
|
return EACCES;
|
|
|
|
}
|
|
|
|
|
2015-02-02 02:53:49 +03:00
|
|
|
static fpos_t android_seek(void *cookie, fpos_t offset, int whence)
|
2014-09-17 00:51:31 +04:00
|
|
|
{
|
|
|
|
return AAsset_seek((AAsset *)cookie, offset, whence);
|
|
|
|
}
|
|
|
|
|
2015-02-02 02:53:49 +03:00
|
|
|
static int android_close(void *cookie)
|
2014-09-17 00:51:31 +04:00
|
|
|
{
|
|
|
|
AAsset_close((AAsset *)cookie);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|