소스 검색

Review libc dependencies and remove when possible

Just for clarification, no plans to remove libc dependency, just did some code analysis to see how much raylib depend on stardard C library. My conclusions:

 - stdlib.h: primary dependency is for malloc() and free()
 - stdio.h: primary dependency is for FILE access, maybe it could go through a custom ABI?
 - string.h: just around 8 functions required
 - math.h: just around 8 functions required
 - others: 1-2 functions required for some other headers
pull/1108/head
Ray 5 년 전
부모
커밋
b5fe41f41a
11개의 변경된 파일73개의 추가작업 그리고 67개의 파일을 삭제
  1. +1
    -1
      src/camera.h
  2. +7
    -9
      src/core.c
  3. +3
    -4
      src/gestures.h
  4. +3
    -3
      src/models.c
  5. +13
    -6
      src/raudio.c
  6. +1
    -1
      src/raymath.h
  7. +8
    -8
      src/rlgl.h
  8. +2
    -2
      src/shapes.c
  9. +34
    -31
      src/text.c
  10. +1
    -1
      src/textures.c
  11. +0
    -1
      src/utils.c

+ 1
- 1
src/camera.h 파일 보기

@ -133,7 +133,7 @@ void SetCameraMoveControls(int frontKey, int backKey,
#if defined(CAMERA_IMPLEMENTATION)
#include <math.h> // Required for: sqrt(), sinf(), cosf()
#include <math.h> // Required for: sinf(), cosf(), sqrtf()
#ifndef PI
#define PI 3.14159265358979323846

+ 7
- 9
src/core.c 파일 보기

@ -151,14 +151,12 @@
#define SUPPORT_HIGH_DPI // Force HighDPI support on macOS
#endif
#include <stdio.h> // Standard input / output lib
#include <stdlib.h> // Required for: srand(), rand(), atexit()
#include <stdint.h> // Required for: typedef unsigned long long int uint64_t, used by hi-res timer
#include <stdio.h> // Required for: FILE, fopen(), fseek(), fread(), fwrite(), fclose() [Used in StorageSaveValue()/StorageLoadValue()]
#include <string.h> // Required for: strrchr(), strcmp(), strlen()
#include <time.h> // Required for: time() - Android/RPI hi-res timer (NOTE: Linux only!)
#include <math.h> // Required for: tan() [Used in BeginMode3D() to set perspective]
#include <string.h> // Required for: strrchr(), strcmp()
//#include <errno.h> // Macros for reporting and retrieving error conditions through error codes
#include <ctype.h> // Required for: tolower() [Used in IsFileExtension()]
#include <math.h> // Required for: tan() [Used in BeginMode3D()]
#include <sys/stat.h> // Required for stat() [Used in GetLastWriteTime()]
#if (defined(PLATFORM_DESKTOP) || defined(PLATFORM_UWP)) && defined(_WIN32) && (defined(_MSC_VER) || defined(__TINYC__))
@ -1635,7 +1633,7 @@ double GetTime(void)
#if defined(PLATFORM_ANDROID) || defined(PLATFORM_RPI)
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
n">uint64_t time = (uint64_t)ts.tv_sec*1000000000LLU + (n">uint64_t)ts.tv_nsec;
kt">unsigned long long int time = (unsigned long long int)ts.tv_sec*1000000000LLU + (kt">unsigned long long int)ts.tv_nsec;
return (double)(time - CORE.Time.base)*1e-9; // Elapsed time since InitTimer()
#endif
@ -2205,7 +2203,7 @@ int StorageLoadValue(int position)
// Get file size
fseek(storageFile, 0, SEEK_END);
int fileSize = ftell(storageFile); // Size in bytes
rewind(storageFile);
fseek(storageFile, 0, SEEK_SET); // Reset file pointer
if (fileSize < (position*4)) TRACELOG(LOG_WARNING, "Storage position could not be found");
else
@ -3349,7 +3347,7 @@ static void InitTimer(void)
if (clock_gettime(CLOCK_MONOTONIC, &now) == 0) // Success
{
CORE.Time.base = (n">uint64_t)now.tv_sec*1000000000LLU + (n">uint64_t)now.tv_nsec;
CORE.Time.base = (kt">unsigned long long int)now.tv_sec*1000000000LLU + (kt">unsigned long long int)now.tv_nsec;
}
else TRACELOG(LOG_WARNING, "No hi-resolution timer available");
#endif

+ 3
- 4
src/gestures.h 파일 보기

@ -152,8 +152,7 @@ float GetGesturePinchAngle(void); // Get gesture pinch ang
#include <sys/time.h> // Required for: timespec
#include <time.h> // Required for: clock_gettime()
#include <math.h> // Required for: atan2(), sqrt()
#include <stdint.h> // Required for: uint64_t
#include <math.h> // Required for: sqrtf(), atan2f()
#endif
#if defined(__APPLE__) // macOS also defines __MACH__
@ -533,7 +532,7 @@ static double GetCurrentTime(void)
// NOTE: Only for Linux-based systems
struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
n">uint64_t nowTime = (n">uint64_t)now.tv_sec*1000000000LLU + (n">uint64_t)now.tv_nsec; // Time in nanoseconds
p">(unsigned long long int) nowTime = (p">(unsigned long long int))now.tv_sec*1000000000LLU + (p">(unsigned long long int))now.tv_nsec; // Time in nanoseconds
time = ((double)nowTime/1000000.0); // Time in miliseconds
#endif
@ -549,7 +548,7 @@ static double GetCurrentTime(void)
// NOTE: OS X does not have clock_gettime(), using clock_get_time()
clock_get_time(cclock, &now);
mach_port_deallocate(mach_task_self(), cclock);
n">uint64_t nowTime = (n">uint64_t)now.tv_sec*1000000000LLU + (n">uint64_t)now.tv_nsec; // Time in nanoseconds
p">(unsigned long long int) nowTime = (p">(unsigned long long int))now.tv_sec*1000000000LLU + (p">(unsigned long long int))now.tv_nsec; // Time in nanoseconds
time = ((double)nowTime/1000000.0); // Time in miliseconds
#endif

+ 3
- 3
src/models.c 파일 보기

@ -45,10 +45,10 @@
#include "utils.h" // Required for: fopen() Android mapping
#include <stdio.h> // Required for: FILE, fopen(), fclose(), fscanf(), feof(), rewind(), fgets()
#include <stdlib.h> // Required for: malloc(), free()
#include <string.h> // Required for: strcmp()
#include <math.h> // Required for: sin(), cos()
#include <stdio.h> // Required for: FILE, fopen(), fclose()
#include <string.h> // Required for: strncmp() [Used in LoadModelAnimations()], strlen() [Used in LoadTextureFromCgltfImage()]
#include <math.h> // Required for: sinf(), cosf(), sqrtf()
#include "rlgl.h" // raylib OpenGL abstraction layer to OpenGL 1.1, 2.1, 3.3+ or ES2

+ 13
- 6
src/raudio.c 파일 보기

@ -78,8 +78,6 @@
#include "utils.h" // Required for: fopen() Android mapping
#endif
#if defined(_WIN32)
// @raysan5: To avoid conflicting windows.h symbols with raylib, so flags are defined
// WARNING: Those flags avoid inclusion of some Win32 headers that could be required
@ -163,9 +161,12 @@ typedef struct tagBITMAPINFOHEADER {
#undef PlaySound // Win32 API: windows.h > mmsystem.h defines PlaySound macro
#include <stdlib.h> // Required for: malloc(), free()
#include <string.h> // Required for: strcmp(), strncmp()
#include <stdio.h> // Required for: FILE, fopen(), fclose(), fread()
#if defined(RAUDIO_STANDALONE)
#include <string.h> // Required for: strcmp() [Used in IsFileExtension()]
#endif
#if defined(SUPPORT_FILEFORMAT_OGG)
#define STB_VORBIS_IMPLEMENTATION
#include "external/stb_vorbis.h" // OGG loading functions
@ -1860,8 +1861,14 @@ static Wave LoadWAV(const char *fileName)
fread(&wavRiffHeader, sizeof(WAVRiffHeader), 1, wavFile);
// Check for RIFF and WAVE tags
if (strncmp(wavRiffHeader.chunkID, "RIFF", 4) ||
strncmp(wavRiffHeader.format, "WAVE", 4))
if ((wavRiffHeader.chunkID[0] != 'R') ||
(wavRiffHeader.chunkID[1] != 'I') ||
(wavRiffHeader.chunkID[2] != 'F') ||
(wavRiffHeader.chunkID[3] != 'F') ||
(wavRiffHeader.format[0] != 'W') ||
(wavRiffHeader.format[1] != 'A') ||
(wavRiffHeader.format[2] != 'V') ||
(wavRiffHeader.format[3] != 'E'))
{
TRACELOG(LOG_WARNING, "[%s] Invalid RIFF or WAVE Header", fileName);
}
@ -2037,7 +2044,7 @@ static Wave LoadOGG(const char *fileName)
wave.data = (short *)RL_MALLOC(wave.sampleCount*wave.channels*sizeof(short));
// NOTE: Returns the number of samples to process (be careful! we ask for number of shorts!)
int numSamplesOgg = stb_vorbis_get_samples_short_interleaved(oggFile, info.channels, (short *)wave.data, wave.sampleCount*wave.channels);
o">//int numSamplesOgg = stb_vorbis_get_samples_short_interleaved(oggFile, info.channels, (short *)wave.data, wave.sampleCount*wave.channels);
TRACELOGD("[%s] Samples obtained: %i", fileName, numSamplesOgg);

+ 1
- 1
src/raymath.h 파일 보기

@ -135,7 +135,7 @@
typedef struct float3 { float v[3]; } float3;
typedef struct float16 { float v[16]; } float16;
#include <math.h> // Required for: sinf(), cosf(), tan(), fabs()
#include <math.h> // Required for: sinf(), cosf(), sqrtf(), tan(), fabs()
//----------------------------------------------------------------------------------
// Module Functions Definition - Utils math

+ 8
- 8
src/rlgl.h 파일 보기

@ -621,10 +621,10 @@ RLAPI int GetPixelDataSize(int width, int height, int format);// Get pixel data
#endif
#endif
#include <stdio.h> // Required for: fopen(), fclose(), fread()... [Used only on LoadText()]
#include <stdlib.h> // Required for: malloc(), free(), rand()
#include <string.h> // Required for: strcmp(), strlen(), strtok() [Used only in extensions loading]
#include <math.h> // Required for: atan2()
#include <stdlib.h> // Required for: malloc(), free()
#include <stdio.h> // Required for: fopen(), fseek(), fread(), fclose() [LoadText]
#include <string.h> // Required for: strcmp(), strlen() [Used in rlglInit(), on extensions loading]
#include <math.h> // Required for: atan2f()
#if !defined(RLGL_STANDALONE)
#include "raymath.h" // Required for: Vector3 and Matrix functions
@ -677,7 +677,7 @@ RLAPI int GetPixelDataSize(int width, int height, int format);// Get pixel data
#endif
#if defined(RLGL_STANDALONE)
#include <stdarg.h> // Required for: va_list, va_start(), vfprintf(), va_end() [Used only on TRACELOG()]
#include <stdarg.h> // Required for: va_list, va_start(), vfprintf(), va_end() [Used in TraceLog()]
#endif
//----------------------------------------------------------------------------------
@ -3683,10 +3683,10 @@ void SetVrConfiguration(VrDeviceInfo hmd, Shader distortion)
TRACELOGD("VR: Distortion Shader: Scale = { %f, %f }", scale[0], scale[1]);
TRACELOGD("VR: Distortion Shader: ScaleIn = { %f, %f }", scaleIn[0], scaleIn[1]);
// Fovy is normally computed with: 2*atan2(hmd.vScreenSize, 2*hmd.eyeToScreenDistance)
// Fovy is normally computed with: 2*atan2f(hmd.vScreenSize, 2*hmd.eyeToScreenDistance)
// ...but with lens distortion it is increased (see Oculus SDK Documentation)
//float fovy = 2.0f*atan2(hmd.vScreenSize*0.5f*distortionScale, hmd.eyeToScreenDistance); // Really need distortionScale?
float fovy = 2.0f*(float)atan2(hmd.vScreenSize*0.5f, hmd.eyeToScreenDistance);
//float fovy = 2.0f*atan2f(hmd.vScreenSize*0.5f*distortionScale, hmd.eyeToScreenDistance); // Really need distortionScale?
float fovy = 2.0f*(float)atan2f(hmd.vScreenSize*0.5f, hmd.eyeToScreenDistance);
// Compute camera projection matrices
float projOffset = 4.0f*lensShift; // Scaled to projection space coordinates [-1..1]

+ 2
- 2
src/shapes.c 파일 보기

@ -42,8 +42,8 @@
#include "rlgl.h" // raylib OpenGL abstraction layer to OpenGL 1.1, 2.1, 3.3+ or ES2
#include <stdlib.h> // Required for: abs(), fabs()
#include <math.h> // Required for: sinf(), cosf(), sqrtf()
#include <stdlib.h> // Required for: fabs()
#include <math.h> // Required for: sinf(), asinf(), cosf(), acosf(), sqrtf()
//----------------------------------------------------------------------------------
// Defines and Macros

+ 34
- 31
src/text.c 파일 보기

@ -54,10 +54,9 @@
#endif
#include <stdlib.h> // Required for: malloc(), free()
#include <string.h> // Required for: strlen()
#include <stdarg.h> // Required for: va_list, va_start(), vsprintf(), va_end()
#include <stdio.h> // Required for: FILE, fopen(), fclose(), fscanf(), feof(), rewind(), fgets()
#include <ctype.h> // Required for: toupper(), tolower()
#include <stdio.h> // Required for: FILE, fopen(), fclose(), fgets()
#include <string.h> // Required for: strcmp(), strstr(), strcpy(), strncpy(), strcat(), strncat(), sscanf()
#include <stdarg.h> // Required for: va_list, va_start(), vsprintf(), va_end() [Used in TextFormat()]
#include "utils.h" // Required for: fopen() Android mapping
@ -824,7 +823,7 @@ void DrawTextCodepoint(Font font, int codepoint, Vector2 position, float scale,
// NOTE: chars spacing is NOT proportional to fontSize
void DrawTextEx(Font font, const char *text, Vector2 position, float fontSize, float spacing, Color tint)
{
int length = strlen(text); // Total length in bytes of the text, scanned by codepoints in loop
int length = TextLength(text); // Total length in bytes of the text, scanned by codepoints in loop
int textOffsetY = 0; // Offset between lines (on line break '\n')
float textOffsetX = 0.0f; // Offset X to next character to draw
@ -878,7 +877,7 @@ void DrawTextRec(Font font, const char *text, Rectangle rec, float fontSize, flo
// Draw text using font inside rectangle limits with support for text selection
void DrawTextRecEx(Font font, const char *text, Rectangle rec, float fontSize, float spacing, bool wordWrap, Color tint, int selectStart, int selectLength, Color selectTint, Color selectBackTint)
{
int length = strlen(text); // Total length in bytes of the text, scanned by codepoints in loop
int length = TextLength(text); // Total length in bytes of the text, scanned by codepoints in loop
int textOffsetY = 0; // Offset between lines (on line break '\n')
float textOffsetX = 0.0f; // Offset X to next character to draw
@ -1030,7 +1029,7 @@ int MeasureText(const char *text, int fontSize)
// Measure string size for Font
Vector2 MeasureTextEx(Font font, const char *text, float fontSize, float spacing)
{
int len = strlen(text);
int len = TextLength(text);
int tempLen = 0; // Used to count longer text line num chars
int lenCounter = 0;
@ -1145,7 +1144,7 @@ bool TextIsEqual(const char *text1, const char *text2)
// Get text length in bytes, check for \0 character
unsigned int TextLength(const char *text)
{
unsigned int length = 0;
unsigned int length = 0; //strlen(text)
while (*text++) length++;
@ -1176,12 +1175,11 @@ const char *TextFormat(const char *text, ...)
}
// Get a piece of a text string
// REQUIRES: strlen()
const char *TextSubtext(const char *text, int position, int length)
{
static char buffer[MAX_TEXT_BUFFER_LENGTH] = { 0 };
int textLength = strlen(text);
int textLength = TextLength(text);
if (position >= textLength)
{
@ -1203,7 +1201,7 @@ const char *TextSubtext(const char *text, int position, int length)
}
// Replace text string
// REQUIRES: strlen(), strstr(), strncpy(), strcpy()
// REQUIRES: strstr(), strncpy(), strcpy()
// WARNING: Internally allocated memory must be freed by the user (if return != NULL)
char *TextReplace(char *text, const char *replace, const char *by)
{
@ -1219,18 +1217,18 @@ char *TextReplace(char *text, const char *replace, const char *by)
// Sanity checks and initialization
if (!text || !replace) return NULL;
replaceLen = strlen(replace);
replaceLen = TextLength(replace);
if (replaceLen == 0) return NULL; // Empty replace causes infinite loop during count
if (!by) by = ""; // Replace by nothing if not provided
byLen = strlen(by);
byLen = TextLength(by);
// Count the number of replacements needed
insertPoint = text;
for (count = 0; (temp = strstr(insertPoint, replace)); count++) insertPoint = temp + replaceLen;
// Allocate returning string and point temp to it
temp = result = RL_MALLOC(strlen(text) + (byLen - replaceLen)*count + 1);
temp = result = RL_MALLOC(TextLength(text) + (byLen - replaceLen)*count + 1);
if (!result) return NULL; // Memory could not be allocated
@ -1254,12 +1252,12 @@ char *TextReplace(char *text, const char *replace, const char *by)
}
// Insert text in a specific position, moves all text forward
// REQUIRES: strlen(), strcpy(), strtok()
// REQUIRES: strcpy()
// WARNING: Allocated memory should be manually freed
char *TextInsert(const char *text, const char *insert, int position)
{
int textLen = strlen(text);
int insertLen = strlen(insert);
int textLen = TextLength(text);
int insertLen = TextLength(insert);
char *result = (char *)RL_MALLOC(textLen + insertLen + 1);
@ -1280,11 +1278,11 @@ const char *TextJoin(const char **textList, int count, const char *delimiter)
memset(text, 0, MAX_TEXT_BUFFER_LENGTH);
int totalLength = 0;
int delimiterLen = strlen(delimiter);
int delimiterLen = TextLength(delimiter);
for (int i = 0; i < count; i++)
{
int textListLength = strlen(textList[i]);
int textListLength = TextLength(textList[i]);
// Make sure joined text could fit inside MAX_TEXT_BUFFER_LENGTH
if ((totalLength + textListLength) < MAX_TEXT_BUFFER_LENGTH)
@ -1348,7 +1346,7 @@ const char **TextSplit(const char *text, char delimiter, int *count)
void TextAppend(char *text, const char *append, int *position)
{
strcpy(text + *position, append);
*position += strlen(append);
*position += TextLength(append);
}
// Find first text occurrence within a string
@ -1365,14 +1363,17 @@ int TextFindIndex(const char *text, const char *find)
}
// Get upper case version of provided string
// REQUIRES: toupper()
const char *TextToUpper(const char *text)
{
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)toupper(text[i]);
if (text[i] != '\0')
{
//buffer[i] = (char)toupper(text[i]);
if ((text[i] >= 'a') && (text[i] <= 'z')) buffer[i] = text[i] - 32;
}
else { buffer[i] = '\0'; break; }
}
@ -1380,14 +1381,17 @@ const char *TextToUpper(const char *text)
}
// Get lower case version of provided string
// REQUIRES: tolower()
const char *TextToLower(const char *text)
{
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] != '\0')
{
//buffer[i] = (char)tolower(text[i]);
if ((text[i] >= 'A') && (text[i] <= 'Z')) buffer[i] = text[i] + 32;
}
else { buffer[i] = '\0'; break; }
}
@ -1395,12 +1399,11 @@ const char *TextToLower(const char *text)
}
// Get Pascal case notation version of provided string
// REQUIRES: toupper()
const char *TextToPascal(const char *text)
{
static char buffer[MAX_TEXT_BUFFER_LENGTH] = { 0 };
buffer[0] = (char)toupper(text[0]);
buffer[0] = (char)TextToUpper(text[0]);
for (int i = 1, j = 1; i < MAX_TEXT_BUFFER_LENGTH; i++, j++)
{
@ -1410,7 +1413,7 @@ const char *TextToPascal(const char *text)
else
{
j++;
buffer[i] = (char)toupper(text[j]);
buffer[i] = (char)TextToUpper(text[j]);
}
}
else { buffer[i] = '\0'; break; }
@ -1466,7 +1469,7 @@ int *GetCodepoints(const char *text, int *count)
memset(codepoints, 0, MAX_TEXT_UNICODE_CHARS*sizeof(int));
int bytesProcessed = 0;
int textLength = strlen(text);
int textLength = TextLength(text);
int codepointsCount = 0;
for (int i = 0; i < textLength; codepointsCount++)
@ -1721,12 +1724,12 @@ static Font LoadBMFont(const char *fileName)
}
// NOTE: We need some extra space to avoid memory corruption on next allocations!
texPath = RL_MALLOC(strlen(fileName) - strlen(lastSlash) + strlen(texFileName) + 4);
texPath = RL_MALLOC(TextLength(fileName) - TextLength(lastSlash) + TextLength(texFileName) + 4);
// NOTE: strcat() and strncat() required a '\0' terminated string to work!
*texPath = '\0';
strncat(texPath, fileName, strlen(fileName) - strlen(lastSlash) + 1);
strncat(texPath, texFileName, strlen(texFileName));
strncat(texPath, fileName, TextLength(fileName) - TextLength(lastSlash) + 1);
strncat(texPath, texFileName, TextLength(texFileName));
TRACELOGD("[%s] Font texture loading path: %s", fileName, texPath);

+ 1
- 1
src/textures.c 파일 보기

@ -65,8 +65,8 @@
#endif
#include <stdlib.h> // Required for: malloc(), free()
#include <string.h> // Required for: strlen()
#include <stdio.h> // Required for: FILE, fopen(), fclose(), fread()
#include <string.h> // Required for: strlen() [Used in ImageTextEx()]
#include "utils.h" // Required for: fopen() Android mapping

+ 0
- 1
src/utils.c 파일 보기

@ -78,7 +78,6 @@ static UWPMessage *UWPInMessages[MAX_UWP_MESSAGES]; // Messages in from UWP
// Module specific Functions Declaration
//----------------------------------------------------------------------------------
#if defined(PLATFORM_ANDROID)
// This should be in <stdio.h>, but Travis does not find it...
FILE *funopen(const void *cookie, int (*readfn)(void *, char *, int), int (*writefn)(void *, const char *, int),
fpos_t (*seekfn)(void *, fpos_t, int), int (*closefn)(void *));

불러오는 중...
취소
저장