Skip to content

Commit

Permalink
Support custom modules inclusion
Browse files Browse the repository at this point in the history
Allow to choose which modules are compiled with raylib, if some modules are excluded from compilation, required functionality is not available but smaller builds are possible.
  • Loading branch information
raysan5 committed Dec 4, 2021
1 parent 48d4806 commit e637ad9
Show file tree
Hide file tree
Showing 9 changed files with 193 additions and 123 deletions.
7 changes: 7 additions & 0 deletions CMakeOptions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ endif()
option(INCLUDE_EVERYTHING "Include everything disabled by default (for CI usage" OFF)
set(OFF ${INCLUDE_EVERYTHING} CACHE INTERNAL "Replace any OFF by default with \${OFF} to have it covered by this option")

# raylib modules included
cmake_dependent_option(SUPPORT_MODULE_RSHAPES "Include module: rshapes" ON CUSTOMIZE_BUILD ON)
cmake_dependent_option(SUPPORT_MODULE_RTEXTURES "Include module: rtextures" ON CUSTOMIZE_BUILD ON)
cmake_dependent_option(SUPPORT_MODULE_RTEXT "Include module: rtext" ON CUSTOMIZE_BUILD ON)
cmake_dependent_option(SUPPORT_MODULE_RMODELS "Include module: rmodels" ON CUSTOMIZE_BUILD ON)
cmake_dependent_option(SUPPORT_MODULE_RAUDIO "Include module: raudio" ON CUSTOMIZE_BUILD ON)

# rcore.c
cmake_dependent_option(SUPPORT_CAMERA_SYSTEM "Provide camera module (rcamera.h) with multiple predefined cameras: free, 1st/3rd person, orbital" ON CUSTOMIZE_BUILD ON)
cmake_dependent_option(SUPPORT_GESTURES_SYSTEM "Gestures module is included (rgestures.h) to support gestures detection: tap, hold, swipe, drag" ON CUSTOMIZE_BUILD ON)
Expand Down
30 changes: 20 additions & 10 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,17 @@
**********************************************************************************************/

//------------------------------------------------------------------------------------
// Module: core - Configuration Flags
// Module selection - Some modules could be avoided
// Mandatory modules: rcore, rlgl, utils
//------------------------------------------------------------------------------------
#define SUPPORT_MODULE_RSHAPES 1
#define SUPPORT_MODULE_RTEXTURES 1
#define SUPPORT_MODULE_RTEXT 1 // WARNING: It requires SUPPORT_MODULE_RTEXTURES to load sprite font textures
#define SUPPORT_MODULE_RMODELS 1
#define SUPPORT_MODULE_RAUDIO 1

//------------------------------------------------------------------------------------
// Module: rcore - Configuration Flags
//------------------------------------------------------------------------------------
// Camera module is included (rcamera.h) and multiple predefined cameras are available: free, 1st/3rd person, orbital
#define SUPPORT_CAMERA_SYSTEM 1
Expand Down Expand Up @@ -62,7 +72,7 @@
// Enabling this flag allows manual control of the frame processes, use at your own risk
//#define SUPPORT_CUSTOM_FRAME_CONTROL 1

// core: Configuration values
// rcore: Configuration values
//------------------------------------------------------------------------------------
#if defined(__linux__)
#define MAX_FILEPATH_LENGTH 4096 // Maximum length for filepaths (Linux PATH_MAX default value)
Expand Down Expand Up @@ -127,15 +137,15 @@


//------------------------------------------------------------------------------------
// Module: shapes - Configuration Flags
// Module: rshapes - Configuration Flags
//------------------------------------------------------------------------------------
// Use QUADS instead of TRIANGLES for drawing when possible
// Some lines-based shapes could still use lines
#define SUPPORT_QUADS_DRAW_MODE 1


//------------------------------------------------------------------------------------
// Module: textures - Configuration Flags
// Module: rtextures - Configuration Flags
//------------------------------------------------------------------------------------
// Selecte desired fileformats to be supported for image data loading
#define SUPPORT_FILEFORMAT_PNG 1
Expand All @@ -162,7 +172,7 @@


//------------------------------------------------------------------------------------
// Module: text - Configuration Flags
// Module: rtext - Configuration Flags
//------------------------------------------------------------------------------------
// Default font is loaded on window initialization to be available for the user to render simple text
// NOTE: If enabled, uses external module functions to load default raylib font
Expand All @@ -175,15 +185,15 @@
// If not defined, still some functions are supported: TextLength(), TextFormat()
#define SUPPORT_TEXT_MANIPULATION 1

// text: Configuration values
// rtext: Configuration values
//------------------------------------------------------------------------------------
#define MAX_TEXT_BUFFER_LENGTH 1024 // Size of internal static buffers used on some functions:
// TextFormat(), TextSubtext(), TextToUpper(), TextToLower(), TextToPascal(), TextSplit()
#define MAX_TEXTSPLIT_COUNT 128 // Maximum number of substrings to split: TextSplit()


//------------------------------------------------------------------------------------
// Module: models - Configuration Flags
// Module: rmodels - Configuration Flags
//------------------------------------------------------------------------------------
// Selected desired model fileformats to be supported for loading
#define SUPPORT_FILEFORMAT_OBJ 1
Expand All @@ -195,13 +205,13 @@
// NOTE: Some generated meshes DO NOT include generated texture coordinates
#define SUPPORT_MESH_GENERATION 1

// models: Configuration values
// rmodels: Configuration values
//------------------------------------------------------------------------------------
#define MAX_MATERIAL_MAPS 12 // Maximum number of shader maps supported
#define MAX_MESH_VERTEX_BUFFERS 7 // Maximum vertex buffers (VBO) per mesh

//------------------------------------------------------------------------------------
// Module: audio - Configuration Flags
// Module: raudio - Configuration Flags
//------------------------------------------------------------------------------------
// Desired audio fileformats to be supported for loading
#define SUPPORT_FILEFORMAT_WAV 1
Expand All @@ -211,7 +221,7 @@
#define SUPPORT_FILEFORMAT_MP3 1
//#define SUPPORT_FILEFORMAT_FLAC 1

// audio: Configuration values
// raudio: Configuration values
//------------------------------------------------------------------------------------
#define AUDIO_DEVICE_FORMAT ma_format_f32 // Device output format (miniaudio: float-32bit)
#define AUDIO_DEVICE_CHANNELS 2 // Device output channels: stereo
Expand Down
74 changes: 21 additions & 53 deletions src/raudio.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
*
* CONFIGURATION:
*
* #define SUPPORT_MODULE_RAUDIO
* raudio module is included in the build
*
* #define RAUDIO_STANDALONE
* Define to use the module as standalone library (independently of raylib).
* Required types and functions are defined in the same module.
Expand Down Expand Up @@ -78,6 +81,8 @@
#include "utils.h" // Required for: fopen() Android mapping
#endif

#if defined(SUPPORT_MODULE_RAUDIO)

#if defined(_WIN32)
// To avoid conflicting windows.h symbols with raylib, some flags are defined
// WARNING: Those flags avoid inclusion of some Win32 headers that could be required
Expand Down Expand Up @@ -169,10 +174,9 @@ typedef struct tagBITMAPINFOHEADER {

#include <stdlib.h> // Required for: malloc(), free()
#include <stdio.h> // Required for: FILE, fopen(), fclose(), fread()
#include <string.h> // Required for: strcmp() [Used in IsFileExtension(), LoadWaveFromMemory(), LoadMusicStreamFromMemory()]

#if defined(RAUDIO_STANDALONE)
#include <string.h> // Required for: strcmp() [Used in IsFileExtension()]

#ifndef TRACELOG
#define TRACELOG(level, ...) (void)0
#endif
Expand Down Expand Up @@ -374,8 +378,6 @@ static void MixAudioFrames(float *framesOut, const float *framesIn, ma_uint32 fr
#if defined(RAUDIO_STANDALONE)
static bool IsFileExtension(const char *fileName, const char *ext); // Check file extension
static const char *GetFileExtension(const char *fileName); // Get pointer to extension for a filename string (includes the dot: .png)
static bool TextIsEqual(const char *text1, const char *text2); // Check if two text string are equal
static const char *TextToLower(const char *text); // Get lower case version of provided string

static unsigned char *LoadFileData(const char *fileName, unsigned int *bytesRead); // Load file data as byte array (read)
static bool SaveFileData(const char *fileName, void *data, unsigned int bytesToWrite); // Save data to file from byte array (write)
Expand Down Expand Up @@ -711,16 +713,14 @@ Wave LoadWave(const char *fileName)
}

// Load wave from memory buffer, fileType refers to extension: i.e. ".wav"
// WARNING: File extension must be provided in lower-case
Wave LoadWaveFromMemory(const char *fileType, const unsigned char *fileData, int dataSize)
{
Wave wave = { 0 };

char fileExtLower[16] = { 0 };
strcpy(fileExtLower, TextToLower(fileType));

if (false) { }
#if defined(SUPPORT_FILEFORMAT_WAV)
else if (TextIsEqual(fileExtLower, ".wav"))
else if (strcmp(fileType, ".wav") == 0)
{
drwav wav = { 0 };
bool success = drwav_init_memory(&wav, fileData, dataSize, NULL);
Expand All @@ -742,7 +742,7 @@ Wave LoadWaveFromMemory(const char *fileType, const unsigned char *fileData, int
}
#endif
#if defined(SUPPORT_FILEFORMAT_OGG)
else if (TextIsEqual(fileExtLower, ".ogg"))
else if (strcmp(fileType, ".ogg") == 0)
{
stb_vorbis *oggData = stb_vorbis_open_memory((unsigned char *)fileData, dataSize, NULL, NULL);

Expand All @@ -764,7 +764,7 @@ Wave LoadWaveFromMemory(const char *fileType, const unsigned char *fileData, int
}
#endif
#if defined(SUPPORT_FILEFORMAT_FLAC)
else if (TextIsEqual(fileExtLower, ".flac"))
else if (strcmp(fileType, ".flac") == 0)
{
unsigned long long int totalFrameCount = 0;

Expand All @@ -777,7 +777,7 @@ Wave LoadWaveFromMemory(const char *fileType, const unsigned char *fileData, int
}
#endif
#if defined(SUPPORT_FILEFORMAT_MP3)
else if (TextIsEqual(fileExtLower, ".mp3"))
else if (strcmp(fileType, ".mp3") == 0)
{
drmp3_config config = { 0 };
unsigned long long int totalFrameCount = 0;
Expand Down Expand Up @@ -1377,18 +1377,16 @@ Music LoadMusicStream(const char *fileName)
return music;
}

// extension including period ".mod"
// Load music stream from memory buffer, fileType refers to extension: i.e. ".wav"
// WARNING: File extension must be provided in lower-case
Music LoadMusicStreamFromMemory(const char *fileType, unsigned char *data, int dataSize)
{
Music music = { 0 };
bool musicLoaded = false;

char fileExtLower[16] = { 0 };
strcpy(fileExtLower, TextToLower(fileType));

if (false) { }
#if defined(SUPPORT_FILEFORMAT_WAV)
else if (TextIsEqual(fileExtLower, ".wav"))
else if (strcmp(fileType, ".wav") == 0)
{
drwav *ctxWav = RL_CALLOC(1, sizeof(drwav));

Expand All @@ -1410,7 +1408,7 @@ Music LoadMusicStreamFromMemory(const char *fileType, unsigned char *data, int d
}
#endif
#if defined(SUPPORT_FILEFORMAT_FLAC)
else if (TextIsEqual(fileExtLower, ".flac"))
else if (strcmp(fileType, ".flac") == 0)
{
music.ctxType = MUSIC_AUDIO_FLAC;
music.ctxData = drflac_open_memory((const void*)data, dataSize, NULL);
Expand All @@ -1427,7 +1425,7 @@ Music LoadMusicStreamFromMemory(const char *fileType, unsigned char *data, int d
}
#endif
#if defined(SUPPORT_FILEFORMAT_MP3)
else if (TextIsEqual(fileExtLower, ".mp3"))
else if (strcmp(fileType, ".mp3") == 0)
{
drmp3 *ctxMp3 = RL_CALLOC(1, sizeof(drmp3));
int success = drmp3_init_memory(ctxMp3, (const void*)data, dataSize, NULL);
Expand All @@ -1445,7 +1443,7 @@ Music LoadMusicStreamFromMemory(const char *fileType, unsigned char *data, int d
}
#endif
#if defined(SUPPORT_FILEFORMAT_OGG)
else if (TextIsEqual(fileExtLower, ".ogg"))
else if (strcmp(fileType, ".ogg") == 0)
{
// Open ogg audio stream
music.ctxType = MUSIC_AUDIO_OGG;
Expand All @@ -1467,7 +1465,7 @@ Music LoadMusicStreamFromMemory(const char *fileType, unsigned char *data, int d
}
#endif
#if defined(SUPPORT_FILEFORMAT_XM)
else if (TextIsEqual(fileExtLower, ".xm"))
else if (strcmp(fileType, ".xm") == 0)
{
jar_xm_context_t *ctxXm = NULL;
int result = jar_xm_create_context_safe(&ctxXm, (const char *)data, dataSize, AUDIO.System.device.sampleRate);
Expand All @@ -1494,7 +1492,7 @@ Music LoadMusicStreamFromMemory(const char *fileType, unsigned char *data, int d
}
#endif
#if defined(SUPPORT_FILEFORMAT_MOD)
else if (TextIsEqual(fileExtLower, ".mod"))
else if (strcmp(fileType, ".mod") == 0)
{
jar_mod_context_t *ctxMod = (jar_mod_context_t *)RL_MALLOC(sizeof(jar_mod_context_t));
int result = 0;
Expand Down Expand Up @@ -2261,38 +2259,6 @@ static const char *GetFileExtension(const char *fileName)
return dot;
}

// Check if two text string are equal
// REQUIRES: strcmp()
static bool TextIsEqual(const char *text1, const char *text2)
{
bool result = false;

if (strcmp(text1, text2) == 0) result = true;

return result;
}

// Get lower case version of provided string
// REQUIRES: tolower()
static const char *TextToLower(const char *text)
{
#define MAX_TEXT_BUFFER_LENGTH 1024

static char buffer[MAX_TEXT_BUFFER_LENGTH] = { 0 };

for (int i = 0; i < MAX_TEXT_BUFFER_LENGTH; i++)
{
if (text[i] != '\0')
{
buffer[i] = (char)tolower(text[i]);
//if ((text[i] >= 'A') && (text[i] <= 'Z')) buffer[i] = text[i] + 32;
}
else { buffer[i] = '\0'; break; }
}

return buffer;
}

// Load data from file into a buffer
static unsigned char *LoadFileData(const char *fileName, unsigned int *bytesRead)
{
Expand Down Expand Up @@ -2378,3 +2344,5 @@ static bool SaveFileText(const char *fileName, char *text)
#endif

#undef AudioBuffer

#endif // SUPPORT_MODULE_RAUDIO
4 changes: 2 additions & 2 deletions src/raylib.h
Original file line number Diff line number Diff line change
Expand Up @@ -1031,15 +1031,15 @@ RLAPI void SetLoadFileTextCallback(LoadFileTextCallback callback); // Set custom
RLAPI void SetSaveFileTextCallback(SaveFileTextCallback callback); // Set custom file text data saver

// Files management functions
RLAPI unsigned char *LoadFileData(const char *fileName, unsigned int *bytesRead); // Load file data as byte array (read)
RLAPI unsigned char *LoadFileData(const char *fileName, unsigned int *bytesRead); // Load file data as byte array (read)
RLAPI void UnloadFileData(unsigned char *data); // Unload file data allocated by LoadFileData()
RLAPI bool SaveFileData(const char *fileName, void *data, unsigned int bytesToWrite); // Save data to file from byte array (write), returns true on success
RLAPI char *LoadFileText(const char *fileName); // Load text data from file (read), returns a '\0' terminated string
RLAPI void UnloadFileText(char *text); // Unload file text data allocated by LoadFileText()
RLAPI bool SaveFileText(const char *fileName, char *text); // Save text data to file (write), string must be '\0' terminated, returns true on success
RLAPI bool FileExists(const char *fileName); // Check if file exists
RLAPI bool DirectoryExists(const char *dirPath); // Check if a directory path exists
RLAPI bool IsFileExtension(const char *fileName, const char *ext);// Check file extension (including point: .png, .wav)
RLAPI bool IsFileExtension(const char *fileName, const char *ext); // Check file extension (including point: .png, .wav)
RLAPI const char *GetFileExtension(const char *fileName); // Get pointer to extension for a filename string (includes dot: '.png')
RLAPI const char *GetFileName(const char *filePath); // Get pointer to filename for a path string
RLAPI const char *GetFileNameWithoutExt(const char *filePath); // Get filename string without extension (uses static string)
Expand Down
Loading

0 comments on commit e637ad9

Please sign in to comment.