2014-09-17 00:51:31 +04:00
|
|
|
/**********************************************************************************************
|
2014-01-23 15:36:18 +04:00
|
|
|
*
|
2017-02-16 02:50:02 +03:00
|
|
|
* raylib.utils - Some common utility functions
|
2014-01-23 15:36:18 +04:00
|
|
|
*
|
2017-02-16 02:50:02 +03:00
|
|
|
* CONFIGURATION:
|
2014-09-03 18:51:28 +04:00
|
|
|
*
|
2017-03-19 14:52:58 +03:00
|
|
|
* #define SUPPORT_SAVE_PNG (defined by default)
|
|
|
|
* Support saving image data as PNG fileformat
|
2017-02-16 02:50:02 +03:00
|
|
|
* NOTE: Requires stb_image_write library
|
|
|
|
*
|
|
|
|
* #define SUPPORT_SAVE_BMP
|
2017-03-19 14:52:58 +03:00
|
|
|
* Support saving image data as BMP fileformat
|
|
|
|
* NOTE: Requires stb_image_write library
|
|
|
|
*
|
|
|
|
* #define SUPPORT_TRACELOG
|
|
|
|
* Show TraceLog() output messages
|
|
|
|
* NOTE: By default DEBUG traces not shown
|
2017-02-16 02:50:02 +03:00
|
|
|
*
|
2017-03-19 14:52:58 +03:00
|
|
|
* #define SUPPORT_TRACELOG_DEBUG
|
|
|
|
* Show TraceLog() DEBUG messages
|
2017-02-16 02:50:02 +03:00
|
|
|
*
|
|
|
|
* DEPENDENCIES:
|
2017-03-25 14:01:01 +03:00
|
|
|
* stb_image_write - BMP/PNG writting functions
|
2014-09-03 18:51:28 +04:00
|
|
|
*
|
2016-11-16 20:46:13 +03:00
|
|
|
*
|
2017-02-16 02:50:02 +03:00
|
|
|
* LICENSE: zlib/libpng
|
2016-11-16 20:46:13 +03:00
|
|
|
*
|
2017-03-20 22:34:44 +03:00
|
|
|
* Copyright (c) 2014-2017 Ramon Santamaria (@raysan5)
|
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
|
|
|
|
|
2016-06-02 02:26:44 +03:00
|
|
|
#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()
|
2016-12-27 19:37:35 +03:00
|
|
|
#include <string.h> // Required for: 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
|
2016-12-27 19:37:35 +03:00
|
|
|
#include "external/stb_image_write.h" // Required for: stbi_write_bmp(), stbi_write_png()
|
2014-09-17 00:51:31 +04:00
|
|
|
#endif
|
2014-04-19 18:36:49 +04:00
|
|
|
|
2016-12-27 19:37:35 +03:00
|
|
|
#define RRES_IMPLEMENTATION
|
|
|
|
#include "rres.h"
|
|
|
|
|
2017-01-15 03:10:23 +03:00
|
|
|
//#define NO_TRACELOG // Avoid TraceLog() output (any type)
|
2016-11-16 20:46:13 +03:00
|
|
|
#define DO_NOT_TRACE_DEBUG_MSGS // Avoid DEBUG messages tracing
|
2014-01-23 15:36:18 +04:00
|
|
|
|
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
|
|
|
|
//----------------------------------------------------------------------------------
|
2017-01-15 03:10:23 +03:00
|
|
|
// Outputs a trace log message
|
2014-04-09 22:25:26 +04:00
|
|
|
void TraceLog(int msgType, const char *text, ...)
|
|
|
|
{
|
2017-01-15 03:10:23 +03:00
|
|
|
#if !defined(NO_TRACELOG)
|
|
|
|
static char buffer[128];
|
2016-07-18 18:06:33 +03:00
|
|
|
int traceDebugMsgs = 1;
|
|
|
|
|
|
|
|
#ifdef DO_NOT_TRACE_DEBUG_MSGS
|
|
|
|
traceDebugMsgs = 0;
|
|
|
|
#endif
|
2014-09-17 00:51:31 +04:00
|
|
|
|
|
|
|
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;
|
2017-01-15 03:10:23 +03:00
|
|
|
va_start(args, text);
|
2014-09-17 00:51:31 +04:00
|
|
|
|
2017-01-15 03:10:23 +03:00
|
|
|
#if defined(PLATFORM_ANDROID)
|
2014-09-17 00:51:31 +04:00
|
|
|
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;
|
2016-07-18 18:06:33 +03:00
|
|
|
case DEBUG: if (traceDebugMsgs) __android_log_vprint(ANDROID_LOG_DEBUG, "raylib", buffer, args); break;
|
2014-09-17 00:51:31 +04:00
|
|
|
default: break;
|
|
|
|
}
|
2017-01-15 03:10:23 +03:00
|
|
|
#else
|
|
|
|
if ((msgType != DEBUG) || ((msgType == DEBUG) && (traceDebugMsgs))) vprintf(buffer, args);
|
|
|
|
#endif
|
2014-09-17 00:51:31 +04:00
|
|
|
|
|
|
|
va_end(args);
|
|
|
|
|
2017-01-15 03:10:23 +03:00
|
|
|
if (msgType == ERROR) exit(1); // If ERROR message, exit program
|
|
|
|
|
|
|
|
#endif // NO_TRACELOG
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_RPI)
|
2017-03-19 14:52:58 +03:00
|
|
|
|
|
|
|
#if defined(SUPPORT_SAVE_BMP)
|
2017-01-15 03:10:23 +03:00
|
|
|
// 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);
|
|
|
|
}
|
2017-03-19 14:52:58 +03:00
|
|
|
#endif
|
2017-01-15 03:10:23 +03:00
|
|
|
|
2017-03-19 14:52:58 +03:00
|
|
|
#if defined(SUPPORT_SAVE_PNG)
|
2017-01-15 03:10:23 +03:00
|
|
|
// 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);
|
2014-09-17 00:51:31 +04:00
|
|
|
}
|
2017-01-15 03:10:23 +03:00
|
|
|
#endif
|
2017-03-19 14:52:58 +03:00
|
|
|
#endif
|
2014-04-09 22:25:26 +04:00
|
|
|
|
2017-01-15 03:10:23 +03:00
|
|
|
#if defined(PLATFORM_ANDROID)
|
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
|
|
|
|
2016-05-31 01:01:19 +03:00
|
|
|
if (!asset) return NULL;
|
2014-09-17 00:51:31 +04:00
|
|
|
|
|
|
|
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, '.');
|
2016-01-02 12:41:37 +03:00
|
|
|
if (!dot || dot == fileName) return "";
|
2014-04-19 18:36:49 +04:00
|
|
|
return (dot + 1);
|
|
|
|
}
|
|
|
|
|
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
|