Code gardening
- Review formatting - Improve readability for some functions result return - Minimize early returns - Align LoadFileData() to UnloadFileData()
This commit is contained in:
parent
2754c80596
commit
29ce13b777
6 changed files with 160 additions and 80 deletions
36
src/raudio.c
36
src/raudio.c
|
@ -752,7 +752,7 @@ Wave LoadWave(const char *fileName)
|
||||||
// Loading wave from memory data
|
// Loading wave from memory data
|
||||||
if (fileData != NULL) wave = LoadWaveFromMemory(GetFileExtension(fileName), fileData, dataSize);
|
if (fileData != NULL) wave = LoadWaveFromMemory(GetFileExtension(fileName), fileData, dataSize);
|
||||||
|
|
||||||
RL_FREE(fileData);
|
UnloadFileData(fileData);
|
||||||
|
|
||||||
return wave;
|
return wave;
|
||||||
}
|
}
|
||||||
|
@ -870,11 +870,15 @@ Wave LoadWaveFromMemory(const char *fileType, const unsigned char *fileData, int
|
||||||
// Checks if wave data is ready
|
// Checks if wave data is ready
|
||||||
bool IsWaveReady(Wave wave)
|
bool IsWaveReady(Wave wave)
|
||||||
{
|
{
|
||||||
return ((wave.data != NULL) && // Validate wave data available
|
bool result = false;
|
||||||
(wave.frameCount > 0) && // Validate frame count
|
|
||||||
(wave.sampleRate > 0) && // Validate sample rate is supported
|
if ((wave.data != NULL) && // Validate wave data available
|
||||||
(wave.sampleSize > 0) && // Validate sample size is supported
|
(wave.frameCount > 0) && // Validate frame count
|
||||||
(wave.channels > 0)); // Validate number of channels supported
|
(wave.sampleRate > 0) && // Validate sample rate is supported
|
||||||
|
(wave.sampleSize > 0) && // Validate sample size is supported
|
||||||
|
(wave.channels > 0)) result = true; // Validate number of channels supported
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load sound from file
|
// Load sound from file
|
||||||
|
@ -967,11 +971,15 @@ Sound LoadSoundAlias(Sound source)
|
||||||
// Checks if a sound is ready
|
// Checks if a sound is ready
|
||||||
bool IsSoundReady(Sound sound)
|
bool IsSoundReady(Sound sound)
|
||||||
{
|
{
|
||||||
return ((sound.frameCount > 0) && // Validate frame count
|
bool result = false;
|
||||||
(sound.stream.buffer != NULL) && // Validate stream buffer
|
|
||||||
(sound.stream.sampleRate > 0) && // Validate sample rate is supported
|
if ((sound.frameCount > 0) && // Validate frame count
|
||||||
(sound.stream.sampleSize > 0) && // Validate sample size is supported
|
(sound.stream.buffer != NULL) && // Validate stream buffer
|
||||||
(sound.stream.channels > 0)); // Validate number of channels supported
|
(sound.stream.sampleRate > 0) && // Validate sample rate is supported
|
||||||
|
(sound.stream.sampleSize > 0) && // Validate sample size is supported
|
||||||
|
(sound.stream.channels > 0)) result = true; // Validate number of channels supported
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unload wave data
|
// Unload wave data
|
||||||
|
@ -1163,7 +1171,11 @@ void StopSound(Sound sound)
|
||||||
// Check if a sound is playing
|
// Check if a sound is playing
|
||||||
bool IsSoundPlaying(Sound sound)
|
bool IsSoundPlaying(Sound sound)
|
||||||
{
|
{
|
||||||
return IsAudioBufferPlaying(sound.stream.buffer);
|
bool result = false;
|
||||||
|
|
||||||
|
if (IsAudioBufferPlaying(sound.stream.buffer)) result = true;
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set volume for a sound
|
// Set volume for a sound
|
||||||
|
|
43
src/rcore.c
43
src/rcore.c
|
@ -1419,7 +1419,9 @@ void SetShaderValueTexture(Shader shader, int locIndex, Texture2D texture)
|
||||||
// Get a ray trace from screen position (i.e mouse)
|
// Get a ray trace from screen position (i.e mouse)
|
||||||
Ray GetScreenToWorldRay(Vector2 position, Camera camera)
|
Ray GetScreenToWorldRay(Vector2 position, Camera camera)
|
||||||
{
|
{
|
||||||
return GetScreenToWorldRayEx(position, camera, GetScreenWidth(), GetScreenHeight());
|
Ray ray = GetScreenToWorldRayEx(position, camera, GetScreenWidth(), GetScreenHeight());
|
||||||
|
|
||||||
|
return ray;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get a ray trace from the screen position (i.e mouse) within a specific section of the screen
|
// Get a ray trace from the screen position (i.e mouse) within a specific section of the screen
|
||||||
|
@ -1480,7 +1482,9 @@ Ray GetScreenToWorldRayEx(Vector2 position, Camera camera, int width, int height
|
||||||
// Get transform matrix for camera
|
// Get transform matrix for camera
|
||||||
Matrix GetCameraMatrix(Camera camera)
|
Matrix GetCameraMatrix(Camera camera)
|
||||||
{
|
{
|
||||||
return MatrixLookAt(camera.position, camera.target, camera.up);
|
Matrix mat = MatrixLookAt(camera.position, camera.target, camera.up);
|
||||||
|
|
||||||
|
return mat;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get camera 2d transform matrix
|
// Get camera 2d transform matrix
|
||||||
|
@ -1661,7 +1665,7 @@ float GetFrameTime(void)
|
||||||
// Ref: http://www.geisswerks.com/ryan/FAQS/timing.html --> All about timing on Win32!
|
// Ref: http://www.geisswerks.com/ryan/FAQS/timing.html --> All about timing on Win32!
|
||||||
void WaitTime(double seconds)
|
void WaitTime(double seconds)
|
||||||
{
|
{
|
||||||
if (seconds < 0) return;
|
if (seconds < 0) return; // Security check
|
||||||
|
|
||||||
#if defined(SUPPORT_BUSY_WAIT_LOOP) || defined(SUPPORT_PARTIALBUSY_WAIT_LOOP)
|
#if defined(SUPPORT_BUSY_WAIT_LOOP) || defined(SUPPORT_PARTIALBUSY_WAIT_LOOP)
|
||||||
double destinationTime = GetTime() + seconds;
|
double destinationTime = GetTime() + seconds;
|
||||||
|
@ -1754,7 +1758,7 @@ int *LoadRandomSequence(unsigned int count, int min, int max)
|
||||||
#if defined(SUPPORT_RPRAND_GENERATOR)
|
#if defined(SUPPORT_RPRAND_GENERATOR)
|
||||||
values = rprand_load_sequence(count, min, max);
|
values = rprand_load_sequence(count, min, max);
|
||||||
#else
|
#else
|
||||||
if (count > ((unsigned int)abs(max - min) + 1)) return values;
|
if (count > ((unsigned int)abs(max - min) + 1)) return values; // Security check
|
||||||
|
|
||||||
values = (int *)RL_CALLOC(count, sizeof(int));
|
values = (int *)RL_CALLOC(count, sizeof(int));
|
||||||
|
|
||||||
|
@ -1946,7 +1950,9 @@ const char *GetFileExtension(const char *fileName)
|
||||||
static const char *strprbrk(const char *s, const char *charset)
|
static const char *strprbrk(const char *s, const char *charset)
|
||||||
{
|
{
|
||||||
const char *latestMatch = NULL;
|
const char *latestMatch = NULL;
|
||||||
|
|
||||||
for (; s = strpbrk(s, charset), s != NULL; latestMatch = s++) { }
|
for (; s = strpbrk(s, charset), s != NULL; latestMatch = s++) { }
|
||||||
|
|
||||||
return latestMatch;
|
return latestMatch;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1954,9 +1960,10 @@ static const char *strprbrk(const char *s, const char *charset)
|
||||||
const char *GetFileName(const char *filePath)
|
const char *GetFileName(const char *filePath)
|
||||||
{
|
{
|
||||||
const char *fileName = NULL;
|
const char *fileName = NULL;
|
||||||
|
|
||||||
if (filePath != NULL) fileName = strprbrk(filePath, "\\/");
|
if (filePath != NULL) fileName = strprbrk(filePath, "\\/");
|
||||||
|
|
||||||
if (!fileName) return filePath;
|
if (fileName != NULL) return filePath;
|
||||||
|
|
||||||
return fileName + 1;
|
return fileName + 1;
|
||||||
}
|
}
|
||||||
|
@ -2235,8 +2242,11 @@ bool IsPathFile(const char *path)
|
||||||
// Check if a file has been dropped into window
|
// Check if a file has been dropped into window
|
||||||
bool IsFileDropped(void)
|
bool IsFileDropped(void)
|
||||||
{
|
{
|
||||||
if (CORE.Window.dropFileCount > 0) return true;
|
bool result = false;
|
||||||
else return false;
|
|
||||||
|
if (CORE.Window.dropFileCount > 0) result = true;
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load dropped filepaths
|
// Load dropped filepaths
|
||||||
|
@ -2270,15 +2280,16 @@ void UnloadDroppedFiles(FilePathList files)
|
||||||
long GetFileModTime(const char *fileName)
|
long GetFileModTime(const char *fileName)
|
||||||
{
|
{
|
||||||
struct stat result = { 0 };
|
struct stat result = { 0 };
|
||||||
|
long modTime = 0;
|
||||||
|
|
||||||
if (stat(fileName, &result) == 0)
|
if (stat(fileName, &result) == 0)
|
||||||
{
|
{
|
||||||
time_t mod = result.st_mtime;
|
time_t mod = result.st_mtime;
|
||||||
|
|
||||||
return (long)mod;
|
modTime = (long)mod;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return modTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
|
@ -2347,7 +2358,7 @@ char *EncodeDataBase64(const unsigned char *data, int dataSize, int *outputSize)
|
||||||
|
|
||||||
char *encodedData = (char *)RL_MALLOC(*outputSize);
|
char *encodedData = (char *)RL_MALLOC(*outputSize);
|
||||||
|
|
||||||
if (encodedData == NULL) return NULL;
|
if (encodedData == NULL) return NULL; // Security check
|
||||||
|
|
||||||
for (int i = 0, j = 0; i < dataSize;)
|
for (int i = 0, j = 0; i < dataSize;)
|
||||||
{
|
{
|
||||||
|
@ -2949,13 +2960,15 @@ bool IsMouseButtonUp(int button)
|
||||||
// Get mouse position X
|
// Get mouse position X
|
||||||
int GetMouseX(void)
|
int GetMouseX(void)
|
||||||
{
|
{
|
||||||
return (int)((CORE.Input.Mouse.currentPosition.x + CORE.Input.Mouse.offset.x)*CORE.Input.Mouse.scale.x);
|
int mouseX = (int)((CORE.Input.Mouse.currentPosition.x + CORE.Input.Mouse.offset.x)*CORE.Input.Mouse.scale.x);
|
||||||
|
return mouseX;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get mouse position Y
|
// Get mouse position Y
|
||||||
int GetMouseY(void)
|
int GetMouseY(void)
|
||||||
{
|
{
|
||||||
return (int)((CORE.Input.Mouse.currentPosition.y + CORE.Input.Mouse.offset.y)*CORE.Input.Mouse.scale.y);
|
int mouseY = (int)((CORE.Input.Mouse.currentPosition.y + CORE.Input.Mouse.offset.y)*CORE.Input.Mouse.scale.y);
|
||||||
|
return mouseY;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get mouse position XY
|
// Get mouse position XY
|
||||||
|
@ -3022,13 +3035,15 @@ Vector2 GetMouseWheelMoveV(void)
|
||||||
// Get touch position X for touch point 0 (relative to screen size)
|
// Get touch position X for touch point 0 (relative to screen size)
|
||||||
int GetTouchX(void)
|
int GetTouchX(void)
|
||||||
{
|
{
|
||||||
return (int)CORE.Input.Touch.position[0].x;
|
int touchX = (int)CORE.Input.Touch.position[0].x;
|
||||||
|
return touchX;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get touch position Y for touch point 0 (relative to screen size)
|
// Get touch position Y for touch point 0 (relative to screen size)
|
||||||
int GetTouchY(void)
|
int GetTouchY(void)
|
||||||
{
|
{
|
||||||
return (int)CORE.Input.Touch.position[0].y;
|
int touchY = (int)CORE.Input.Touch.position[0].y;
|
||||||
|
return touchY;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get touch position XY for a touch point index (relative to screen size)
|
// Get touch position XY for a touch point index (relative to screen size)
|
||||||
|
|
|
@ -228,7 +228,7 @@ void DrawTriangle3D(Vector3 v1, Vector3 v2, Vector3 v3, Color color)
|
||||||
// Draw a triangle strip defined by points
|
// Draw a triangle strip defined by points
|
||||||
void DrawTriangleStrip3D(Vector3 *points, int pointCount, Color color)
|
void DrawTriangleStrip3D(Vector3 *points, int pointCount, Color color)
|
||||||
{
|
{
|
||||||
if (pointCount < 3) return;
|
if (pointCount < 3) return; // Security check
|
||||||
|
|
||||||
rlBegin(RL_TRIANGLES);
|
rlBegin(RL_TRIANGLES);
|
||||||
rlColor4ub(color.r, color.g, color.b, color.a);
|
rlColor4ub(color.r, color.g, color.b, color.a);
|
||||||
|
@ -559,7 +559,7 @@ void DrawCylinderEx(Vector3 startPos, Vector3 endPos, float startRadius, float e
|
||||||
if (sides < 3) sides = 3;
|
if (sides < 3) sides = 3;
|
||||||
|
|
||||||
Vector3 direction = { endPos.x - startPos.x, endPos.y - startPos.y, endPos.z - startPos.z };
|
Vector3 direction = { endPos.x - startPos.x, endPos.y - startPos.y, endPos.z - startPos.z };
|
||||||
if ((direction.x == 0) && (direction.y == 0) && (direction.z == 0)) return;
|
if ((direction.x == 0) && (direction.y == 0) && (direction.z == 0)) return; // Security check
|
||||||
|
|
||||||
// Construct a basis of the base and the top face:
|
// Construct a basis of the base and the top face:
|
||||||
Vector3 b1 = Vector3Normalize(Vector3Perpendicular(direction));
|
Vector3 b1 = Vector3Normalize(Vector3Perpendicular(direction));
|
||||||
|
@ -649,7 +649,7 @@ void DrawCylinderWiresEx(Vector3 startPos, Vector3 endPos, float startRadius, fl
|
||||||
if (sides < 3) sides = 3;
|
if (sides < 3) sides = 3;
|
||||||
|
|
||||||
Vector3 direction = { endPos.x - startPos.x, endPos.y - startPos.y, endPos.z - startPos.z };
|
Vector3 direction = { endPos.x - startPos.x, endPos.y - startPos.y, endPos.z - startPos.z };
|
||||||
if ((direction.x == 0) && (direction.y == 0) && (direction.z == 0))return;
|
if ((direction.x == 0) && (direction.y == 0) && (direction.z == 0)) return; // Security check
|
||||||
|
|
||||||
// Construct a basis of the base and the top face:
|
// Construct a basis of the base and the top face:
|
||||||
Vector3 b1 = Vector3Normalize(Vector3Perpendicular(direction));
|
Vector3 b1 = Vector3Normalize(Vector3Perpendicular(direction));
|
||||||
|
@ -755,8 +755,8 @@ void DrawCapsule(Vector3 startPos, Vector3 endPos, float radius, int slices, int
|
||||||
capCenter.z + (sinf(baseRingAngle * ( i + 1 ))*b0.z + ringSin4*b1.z + ringCos4*b2.z) * radius
|
capCenter.z + (sinf(baseRingAngle * ( i + 1 ))*b0.z + ringSin4*b1.z + ringCos4*b2.z) * radius
|
||||||
};
|
};
|
||||||
|
|
||||||
// make sure cap triangle normals are facing outwards
|
// Make sure cap triangle normals are facing outwards
|
||||||
if(c == 0)
|
if (c == 0)
|
||||||
{
|
{
|
||||||
rlVertex3f(w1.x, w1.y, w1.z);
|
rlVertex3f(w1.x, w1.y, w1.z);
|
||||||
rlVertex3f(w2.x, w2.y, w2.z);
|
rlVertex3f(w2.x, w2.y, w2.z);
|
||||||
|
@ -1104,13 +1104,17 @@ Model LoadModelFromMesh(Mesh mesh)
|
||||||
// Check if a model is ready
|
// Check if a model is ready
|
||||||
bool IsModelReady(Model model)
|
bool IsModelReady(Model model)
|
||||||
{
|
{
|
||||||
return ((model.meshes != NULL) && // Validate model contains some mesh
|
bool result = false;
|
||||||
(model.materials != NULL) && // Validate model contains some material (at least default one)
|
|
||||||
(model.meshMaterial != NULL) && // Validate mesh-material linkage
|
if ((model.meshes != NULL) && // Validate model contains some mesh
|
||||||
(model.meshCount > 0) && // Validate mesh count
|
(model.materials != NULL) && // Validate model contains some material (at least default one)
|
||||||
(model.materialCount > 0)); // Validate material count
|
(model.meshMaterial != NULL) && // Validate mesh-material linkage
|
||||||
|
(model.meshCount > 0) && // Validate mesh count
|
||||||
|
(model.materialCount > 0)) result = true; // Validate material count
|
||||||
|
|
||||||
// NOTE: This is a very general model validation, many elements could be validated from a model...
|
// NOTE: This is a very general model validation, many elements could be validated from a model...
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unload model (meshes/materials) from memory (RAM and/or VRAM)
|
// Unload model (meshes/materials) from memory (RAM and/or VRAM)
|
||||||
|
@ -2033,8 +2037,12 @@ Material LoadMaterialDefault(void)
|
||||||
// Check if a material is ready
|
// Check if a material is ready
|
||||||
bool IsMaterialReady(Material material)
|
bool IsMaterialReady(Material material)
|
||||||
{
|
{
|
||||||
return ((material.maps != NULL) && // Validate material contain some map
|
bool result = false;
|
||||||
(material.shader.id > 0)); // Validate material shader is valid
|
|
||||||
|
if ((material.maps != NULL) && // Validate material contain some map
|
||||||
|
(material.shader.id > 0)) result = true; // Validate material shader is valid
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unload material from memory
|
// Unload material from memory
|
||||||
|
@ -2230,7 +2238,7 @@ Mesh GenMeshPoly(int sides, float radius)
|
||||||
{
|
{
|
||||||
Mesh mesh = { 0 };
|
Mesh mesh = { 0 };
|
||||||
|
|
||||||
if (sides < 3) return mesh;
|
if (sides < 3) return mesh; // Security check
|
||||||
|
|
||||||
int vertexCount = sides*3;
|
int vertexCount = sides*3;
|
||||||
|
|
||||||
|
@ -4493,7 +4501,7 @@ static Model LoadIQM(const char *fileName)
|
||||||
|
|
||||||
BuildPoseFromParentJoints(model.bones, model.boneCount, model.bindPose);
|
BuildPoseFromParentJoints(model.bones, model.boneCount, model.bindPose);
|
||||||
|
|
||||||
RL_FREE(fileData);
|
UnloadFileData(fileData);
|
||||||
|
|
||||||
RL_FREE(imesh);
|
RL_FREE(imesh);
|
||||||
RL_FREE(tri);
|
RL_FREE(tri);
|
||||||
|
@ -4725,7 +4733,7 @@ static ModelAnimation *LoadModelAnimationsIQM(const char *fileName, int *animCou
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
RL_FREE(fileData);
|
UnloadFileData(fileData);
|
||||||
|
|
||||||
RL_FREE(joints);
|
RL_FREE(joints);
|
||||||
RL_FREE(framedata);
|
RL_FREE(framedata);
|
||||||
|
@ -6102,7 +6110,7 @@ static Model LoadM3D(const char *fileName)
|
||||||
// called, but not before, however DrawMesh uses these if they exist (so not good if they are left empty).
|
// called, but not before, however DrawMesh uses these if they exist (so not good if they are left empty).
|
||||||
if (m3d->numbone && m3d->numskin)
|
if (m3d->numbone && m3d->numskin)
|
||||||
{
|
{
|
||||||
for(i = 0; i < model.meshCount; i++)
|
for (i = 0; i < model.meshCount; i++)
|
||||||
{
|
{
|
||||||
memcpy(model.meshes[i].animVertices, model.meshes[i].vertices, model.meshes[i].vertexCount*3*sizeof(float));
|
memcpy(model.meshes[i].animVertices, model.meshes[i].vertices, model.meshes[i].vertexCount*3*sizeof(float));
|
||||||
memcpy(model.meshes[i].animNormals, model.meshes[i].normals, model.meshes[i].vertexCount*3*sizeof(float));
|
memcpy(model.meshes[i].animNormals, model.meshes[i].normals, model.meshes[i].vertexCount*3*sizeof(float));
|
||||||
|
|
|
@ -2345,11 +2345,16 @@ Rectangle GetCollisionRec(Rectangle rec1, Rectangle rec2)
|
||||||
// NOTE: Used by DrawLineBezier() only
|
// NOTE: Used by DrawLineBezier() only
|
||||||
static float EaseCubicInOut(float t, float b, float c, float d)
|
static float EaseCubicInOut(float t, float b, float c, float d)
|
||||||
{
|
{
|
||||||
if ((t /= 0.5f*d) < 1) return 0.5f*c*t*t*t + b;
|
float result = 0.0f;
|
||||||
|
|
||||||
t -= 2;
|
if ((t /= 0.5f*d) < 1) result = 0.5f*c*t*t*t + b;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
t -= 2;
|
||||||
|
result = 0.5f*c*(t*t*t + 2.0f) + b;
|
||||||
|
}
|
||||||
|
|
||||||
return 0.5f*c*(t*t*t + 2.0f) + b;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // SUPPORT_MODULE_RSHAPES
|
#endif // SUPPORT_MODULE_RSHAPES
|
||||||
|
|
12
src/rtext.c
12
src/rtext.c
|
@ -434,7 +434,7 @@ Font LoadFontFromImage(Image image, Color key, int firstChar)
|
||||||
if (!COLOR_EQUAL(pixels[y*image.width + x], key)) break;
|
if (!COLOR_EQUAL(pixels[y*image.width + x], key)) break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((x == 0) || (y == 0)) return font;
|
if ((x == 0) || (y == 0)) return font; // Security check
|
||||||
|
|
||||||
charSpacing = x;
|
charSpacing = x;
|
||||||
lineSpacing = y;
|
lineSpacing = y;
|
||||||
|
@ -823,7 +823,7 @@ Image GenImageFontAtlas(const GlyphInfo *glyphs, Rectangle **glyphRecs, int glyp
|
||||||
|
|
||||||
if (offsetY > (atlas.height - fontSize - padding))
|
if (offsetY > (atlas.height - fontSize - padding))
|
||||||
{
|
{
|
||||||
for(int j = i + 1; j < glyphCount; j++)
|
for (int j = i + 1; j < glyphCount; j++)
|
||||||
{
|
{
|
||||||
TRACELOG(LOG_WARNING, "FONT: Failed to package character (%i)", j);
|
TRACELOG(LOG_WARNING, "FONT: Failed to package character (%i)", j);
|
||||||
// Make sure remaining recs contain valid data
|
// Make sure remaining recs contain valid data
|
||||||
|
@ -1282,7 +1282,7 @@ Vector2 MeasureTextEx(Font font, const char *text, float fontSize, float spacing
|
||||||
{
|
{
|
||||||
Vector2 textSize = { 0 };
|
Vector2 textSize = { 0 };
|
||||||
|
|
||||||
if ((font.texture.id == 0) || (text == NULL)) return textSize;
|
if ((font.texture.id == 0) || (text == NULL)) return textSize; // Security check
|
||||||
|
|
||||||
int size = TextLength(text); // Get size in bytes of text
|
int size = TextLength(text); // Get size in bytes of text
|
||||||
int tempByteCounter = 0; // Used to count longer text line num chars
|
int tempByteCounter = 0; // Used to count longer text line num chars
|
||||||
|
@ -2029,21 +2029,21 @@ int GetCodepointNext(const char *text, int *codepointSize)
|
||||||
if (0xf0 == (0xf8 & ptr[0]))
|
if (0xf0 == (0xf8 & ptr[0]))
|
||||||
{
|
{
|
||||||
// 4 byte UTF-8 codepoint
|
// 4 byte UTF-8 codepoint
|
||||||
if(((ptr[1] & 0xC0) ^ 0x80) || ((ptr[2] & 0xC0) ^ 0x80) || ((ptr[3] & 0xC0) ^ 0x80)) { return codepoint; } // 10xxxxxx checks
|
if (((ptr[1] & 0xC0) ^ 0x80) || ((ptr[2] & 0xC0) ^ 0x80) || ((ptr[3] & 0xC0) ^ 0x80)) { return codepoint; } // 10xxxxxx checks
|
||||||
codepoint = ((0x07 & ptr[0]) << 18) | ((0x3f & ptr[1]) << 12) | ((0x3f & ptr[2]) << 6) | (0x3f & ptr[3]);
|
codepoint = ((0x07 & ptr[0]) << 18) | ((0x3f & ptr[1]) << 12) | ((0x3f & ptr[2]) << 6) | (0x3f & ptr[3]);
|
||||||
*codepointSize = 4;
|
*codepointSize = 4;
|
||||||
}
|
}
|
||||||
else if (0xe0 == (0xf0 & ptr[0]))
|
else if (0xe0 == (0xf0 & ptr[0]))
|
||||||
{
|
{
|
||||||
// 3 byte UTF-8 codepoint */
|
// 3 byte UTF-8 codepoint */
|
||||||
if(((ptr[1] & 0xC0) ^ 0x80) || ((ptr[2] & 0xC0) ^ 0x80)) { return codepoint; } // 10xxxxxx checks
|
if (((ptr[1] & 0xC0) ^ 0x80) || ((ptr[2] & 0xC0) ^ 0x80)) { return codepoint; } // 10xxxxxx checks
|
||||||
codepoint = ((0x0f & ptr[0]) << 12) | ((0x3f & ptr[1]) << 6) | (0x3f & ptr[2]);
|
codepoint = ((0x0f & ptr[0]) << 12) | ((0x3f & ptr[1]) << 6) | (0x3f & ptr[2]);
|
||||||
*codepointSize = 3;
|
*codepointSize = 3;
|
||||||
}
|
}
|
||||||
else if (0xc0 == (0xe0 & ptr[0]))
|
else if (0xc0 == (0xe0 & ptr[0]))
|
||||||
{
|
{
|
||||||
// 2 byte UTF-8 codepoint
|
// 2 byte UTF-8 codepoint
|
||||||
if((ptr[1] & 0xC0) ^ 0x80) { return codepoint; } // 10xxxxxx checks
|
if ((ptr[1] & 0xC0) ^ 0x80) { return codepoint; } // 10xxxxxx checks
|
||||||
codepoint = ((0x1f & ptr[0]) << 6) | (0x3f & ptr[1]);
|
codepoint = ((0x1f & ptr[0]) << 6) | (0x3f & ptr[1]);
|
||||||
*codepointSize = 2;
|
*codepointSize = 2;
|
||||||
}
|
}
|
||||||
|
|
|
@ -71,10 +71,10 @@
|
||||||
#if defined(SUPPORT_MODULE_RTEXTURES)
|
#if defined(SUPPORT_MODULE_RTEXTURES)
|
||||||
|
|
||||||
#include "utils.h" // Required for: TRACELOG()
|
#include "utils.h" // Required for: TRACELOG()
|
||||||
#include "rlgl.h" // OpenGL abstraction layer to OpenGL 1.1, 3.3 or ES2
|
#include "rlgl.h" // OpenGL abstraction layer to multiple versions
|
||||||
|
|
||||||
#include <stdlib.h> // Required for: malloc(), free()
|
#include <stdlib.h> // Required for: malloc(), calloc(), free()
|
||||||
#include <string.h> // Required for: strlen() [Used in ImageTextEx()], strcmp() [Used in LoadImageFromMemory()]
|
#include <string.h> // Required for: strlen() [Used in ImageTextEx()], strcmp() [Used in LoadImageFromMemory()/LoadImageAnimFromMemory()/ExportImageToMemory()]
|
||||||
#include <math.h> // Required for: fabsf() [Used in DrawTextureRec()]
|
#include <math.h> // Required for: fabsf() [Used in DrawTextureRec()]
|
||||||
#include <stdio.h> // Required for: sprintf() [Used in ExportImageAsCode()]
|
#include <stdio.h> // Required for: sprintf() [Used in ExportImageAsCode()]
|
||||||
|
|
||||||
|
@ -293,9 +293,12 @@ Image LoadImage(const char *fileName)
|
||||||
unsigned char *fileData = LoadFileData(fileName, &dataSize);
|
unsigned char *fileData = LoadFileData(fileName, &dataSize);
|
||||||
|
|
||||||
// Loading image from memory data
|
// Loading image from memory data
|
||||||
if (fileData != NULL) image = LoadImageFromMemory(GetFileExtension(fileName), fileData, dataSize);
|
if (fileData != NULL)
|
||||||
|
{
|
||||||
|
image = LoadImageFromMemory(GetFileExtension(fileName), fileData, dataSize);
|
||||||
|
|
||||||
RL_FREE(fileData);
|
UnloadFileData(fileData);
|
||||||
|
}
|
||||||
|
|
||||||
return image;
|
return image;
|
||||||
}
|
}
|
||||||
|
@ -322,7 +325,7 @@ Image LoadImageRaw(const char *fileName, int width, int height, int format, int
|
||||||
image.mipmaps = 1;
|
image.mipmaps = 1;
|
||||||
image.format = format;
|
image.format = format;
|
||||||
|
|
||||||
RL_FREE(fileData);
|
UnloadFileData(fileData);
|
||||||
}
|
}
|
||||||
|
|
||||||
return image;
|
return image;
|
||||||
|
@ -431,7 +434,7 @@ Image LoadImageAnim(const char *fileName, int *frames)
|
||||||
image.mipmaps = 1;
|
image.mipmaps = 1;
|
||||||
image.format = PIXELFORMAT_UNCOMPRESSED_R8G8B8A8;
|
image.format = PIXELFORMAT_UNCOMPRESSED_R8G8B8A8;
|
||||||
|
|
||||||
RL_FREE(fileData);
|
UnloadFileData(fileData);
|
||||||
RL_FREE(delays); // NOTE: Frames delays are discarded
|
RL_FREE(delays); // NOTE: Frames delays are discarded
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -495,6 +498,9 @@ Image LoadImageFromMemory(const char *fileType, const unsigned char *fileData, i
|
||||||
{
|
{
|
||||||
Image image = { 0 };
|
Image image = { 0 };
|
||||||
|
|
||||||
|
// Security check for input data
|
||||||
|
if ((fileType == NULL) || (fileData == NULL) || (dataSize == 0)) return image;
|
||||||
|
|
||||||
if ((false)
|
if ((false)
|
||||||
#if defined(SUPPORT_FILEFORMAT_PNG)
|
#if defined(SUPPORT_FILEFORMAT_PNG)
|
||||||
|| (strcmp(fileType, ".png") == 0) || (strcmp(fileType, ".PNG") == 0)
|
|| (strcmp(fileType, ".png") == 0) || (strcmp(fileType, ".PNG") == 0)
|
||||||
|
@ -699,11 +705,15 @@ Image LoadImageFromScreen(void)
|
||||||
// Check if an image is ready
|
// Check if an image is ready
|
||||||
bool IsImageReady(Image image)
|
bool IsImageReady(Image image)
|
||||||
{
|
{
|
||||||
return ((image.data != NULL) && // Validate pixel data available
|
bool result = false;
|
||||||
(image.width > 0) &&
|
|
||||||
(image.height > 0) && // Validate image size
|
if ((image.data != NULL) && // Validate pixel data available
|
||||||
(image.format > 0) && // Validate image format
|
(image.width > 0) &&
|
||||||
(image.mipmaps > 0)); // Validate image mipmaps (at least 1 for basic mipmap level)
|
(image.height > 0) && // Validate image size
|
||||||
|
(image.format > 0) && // Validate image format
|
||||||
|
(image.mipmaps > 0)) result = true; // Validate image mipmaps (at least 1 for basic mipmap level)
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unload image from CPU memory (RAM)
|
// Unload image from CPU memory (RAM)
|
||||||
|
@ -718,6 +728,7 @@ bool ExportImage(Image image, const char *fileName)
|
||||||
{
|
{
|
||||||
int result = 0;
|
int result = 0;
|
||||||
|
|
||||||
|
// Security check for input data
|
||||||
if ((image.width == 0) || (image.height == 0) || (image.data == NULL)) return result;
|
if ((image.width == 0) || (image.height == 0) || (image.data == NULL)) return result;
|
||||||
|
|
||||||
#if defined(SUPPORT_IMAGE_EXPORT)
|
#if defined(SUPPORT_IMAGE_EXPORT)
|
||||||
|
@ -805,6 +816,7 @@ unsigned char *ExportImageToMemory(Image image, const char *fileType, int *dataS
|
||||||
unsigned char *fileData = NULL;
|
unsigned char *fileData = NULL;
|
||||||
*dataSize = 0;
|
*dataSize = 0;
|
||||||
|
|
||||||
|
// Security check for input data
|
||||||
if ((image.width == 0) || (image.height == 0) || (image.data == NULL)) return NULL;
|
if ((image.width == 0) || (image.height == 0) || (image.data == NULL)) return NULL;
|
||||||
|
|
||||||
#if defined(SUPPORT_IMAGE_EXPORT)
|
#if defined(SUPPORT_IMAGE_EXPORT)
|
||||||
|
@ -2184,7 +2196,7 @@ void ImageKernelConvolution(Image *image, float* kernel, int kernelSize)
|
||||||
endRange = kernelWidth/2 + 1;
|
endRange = kernelWidth/2 + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
for(int x = 0; x < image->height; x++)
|
for (int x = 0; x < image->height; x++)
|
||||||
{
|
{
|
||||||
for (int y = 0; y < image->width; y++)
|
for (int y = 0; y < image->width; y++)
|
||||||
{
|
{
|
||||||
|
@ -3940,13 +3952,17 @@ RenderTexture2D LoadRenderTexture(int width, int height)
|
||||||
// Check if a texture is ready
|
// Check if a texture is ready
|
||||||
bool IsTextureReady(Texture2D texture)
|
bool IsTextureReady(Texture2D texture)
|
||||||
{
|
{
|
||||||
|
bool result = false;
|
||||||
|
|
||||||
// TODO: Validate maximum texture size supported by GPU?
|
// TODO: Validate maximum texture size supported by GPU?
|
||||||
|
|
||||||
return ((texture.id > 0) && // Validate OpenGL id
|
if ((texture.id > 0) && // Validate OpenGL id
|
||||||
(texture.width > 0) &&
|
(texture.width > 0) &&
|
||||||
(texture.height > 0) && // Validate texture size
|
(texture.height > 0) && // Validate texture size
|
||||||
(texture.format > 0) && // Validate texture pixel format
|
(texture.format > 0) && // Validate texture pixel format
|
||||||
(texture.mipmaps > 0)); // Validate texture mipmaps (at least 1 for basic mipmap level)
|
(texture.mipmaps > 0)) result = true; // Validate texture mipmaps (at least 1 for basic mipmap level)
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unload texture from GPU memory (VRAM)
|
// Unload texture from GPU memory (VRAM)
|
||||||
|
@ -3963,9 +3979,13 @@ void UnloadTexture(Texture2D texture)
|
||||||
// Check if a render texture is ready
|
// Check if a render texture is ready
|
||||||
bool IsRenderTextureReady(RenderTexture2D target)
|
bool IsRenderTextureReady(RenderTexture2D target)
|
||||||
{
|
{
|
||||||
return ((target.id > 0) && // Validate OpenGL id
|
bool result = false;
|
||||||
IsTextureReady(target.depth) && // Validate FBO depth texture/renderbuffer
|
|
||||||
IsTextureReady(target.texture)); // Validate FBO texture
|
if ((target.id > 0) && // Validate OpenGL id
|
||||||
|
IsTextureReady(target.depth) && // Validate FBO depth texture/renderbuffer
|
||||||
|
IsTextureReady(target.texture)) result = true; // Validate FBO texture
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unload render texture from GPU memory (VRAM)
|
// Unload render texture from GPU memory (VRAM)
|
||||||
|
@ -4473,16 +4493,22 @@ bool ColorIsEqual(Color col1, Color col2)
|
||||||
// Get color with alpha applied, alpha goes from 0.0f to 1.0f
|
// Get color with alpha applied, alpha goes from 0.0f to 1.0f
|
||||||
Color Fade(Color color, float alpha)
|
Color Fade(Color color, float alpha)
|
||||||
{
|
{
|
||||||
|
Color result = color;
|
||||||
|
|
||||||
if (alpha < 0.0f) alpha = 0.0f;
|
if (alpha < 0.0f) alpha = 0.0f;
|
||||||
else if (alpha > 1.0f) alpha = 1.0f;
|
else if (alpha > 1.0f) alpha = 1.0f;
|
||||||
|
|
||||||
return (Color){ color.r, color.g, color.b, (unsigned char)(255.0f*alpha) };
|
result.a = (unsigned char)(255.0f*alpha);
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get hexadecimal value for a Color
|
// Get hexadecimal value for a Color
|
||||||
int ColorToInt(Color color)
|
int ColorToInt(Color color)
|
||||||
{
|
{
|
||||||
return (((int)color.r << 24) | ((int)color.g << 16) | ((int)color.b << 8) | (int)color.a);
|
int result = (((int)color.r << 24) | ((int)color.g << 16) | ((int)color.b << 8) | (int)color.a);
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get color normalized as float [0..1]
|
// Get color normalized as float [0..1]
|
||||||
|
@ -4701,10 +4727,14 @@ Color ColorContrast(Color color, float contrast)
|
||||||
// Get color with alpha applied, alpha goes from 0.0f to 1.0f
|
// Get color with alpha applied, alpha goes from 0.0f to 1.0f
|
||||||
Color ColorAlpha(Color color, float alpha)
|
Color ColorAlpha(Color color, float alpha)
|
||||||
{
|
{
|
||||||
|
Color result = color;
|
||||||
|
|
||||||
if (alpha < 0.0f) alpha = 0.0f;
|
if (alpha < 0.0f) alpha = 0.0f;
|
||||||
else if (alpha > 1.0f) alpha = 1.0f;
|
else if (alpha > 1.0f) alpha = 1.0f;
|
||||||
|
|
||||||
return (Color){color.r, color.g, color.b, (unsigned char)(255.0f*alpha)};
|
result.a = (unsigned char)(255.0f*alpha);
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get src alpha-blended into dst color with tint
|
// Get src alpha-blended into dst color with tint
|
||||||
|
@ -5007,21 +5037,31 @@ int GetPixelDataSize(int width, int height, int format)
|
||||||
// REF: https://stackoverflow.com/questions/1659440/32-bit-to-16-bit-floating-point-conversion/60047308#60047308
|
// REF: https://stackoverflow.com/questions/1659440/32-bit-to-16-bit-floating-point-conversion/60047308#60047308
|
||||||
static float HalfToFloat(unsigned short x)
|
static float HalfToFloat(unsigned short x)
|
||||||
{
|
{
|
||||||
|
float result = 0.0f;
|
||||||
|
|
||||||
const unsigned int e = (x & 0x7C00) >> 10; // Exponent
|
const unsigned int e = (x & 0x7C00) >> 10; // Exponent
|
||||||
const unsigned int m = (x & 0x03FF) << 13; // Mantissa
|
const unsigned int m = (x & 0x03FF) << 13; // Mantissa
|
||||||
const float fm = (float)m;
|
const float fm = (float)m;
|
||||||
const unsigned int v = (*(unsigned int*)&fm) >> 23; // Evil log2 bit hack to count leading zeros in denormalized format
|
const unsigned int v = (*(unsigned int*)&fm) >> 23; // Evil log2 bit hack to count leading zeros in denormalized format
|
||||||
const unsigned int r = (x & 0x8000) << 16 | (e != 0)*((e + 112) << 23 | m) | ((e == 0)&(m != 0))*((v - 37) << 23 | ((m << (150 - v)) & 0x007FE000)); // sign : normalized : denormalized
|
const unsigned int r = (x & 0x8000) << 16 | (e != 0)*((e + 112) << 23 | m) | ((e == 0)&(m != 0))*((v - 37) << 23 | ((m << (150 - v)) & 0x007FE000)); // sign : normalized : denormalized
|
||||||
return *(float*)&r;
|
|
||||||
|
result = *(float *)&r;
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert float to half-float (stored as unsigned short)
|
// Convert float to half-float (stored as unsigned short)
|
||||||
static unsigned short FloatToHalf(float x)
|
static unsigned short FloatToHalf(float x)
|
||||||
{
|
{
|
||||||
|
unsigned short result = 0;
|
||||||
|
|
||||||
const unsigned int b = (*(unsigned int*) & x) + 0x00001000; // Round-to-nearest-even: add last bit after truncated mantissa
|
const unsigned int b = (*(unsigned int*) & x) + 0x00001000; // Round-to-nearest-even: add last bit after truncated mantissa
|
||||||
const unsigned int e = (b & 0x7F800000) >> 23; // Exponent
|
const unsigned int e = (b & 0x7F800000) >> 23; // Exponent
|
||||||
const unsigned int m = b & 0x007FFFFF; // Mantissa; in line below: 0x007FF000 = 0x00800000-0x00001000 = decimal indicator flag - initial rounding
|
const unsigned int m = b & 0x007FFFFF; // Mantissa; in line below: 0x007FF000 = 0x00800000-0x00001000 = decimal indicator flag - initial rounding
|
||||||
return (b & 0x80000000) >> 16 | (e > 112)*((((e - 112) << 10) & 0x7C00) | m >> 13) | ((e < 113) & (e > 101))*((((0x007FF000 + m) >> (125 - e)) + 1) >> 1) | (e > 143)*0x7FFF; // sign : normalized : denormalized : saturate
|
|
||||||
|
result = (b & 0x80000000) >> 16 | (e > 112)*((((e - 112) << 10) & 0x7C00) | m >> 13) | ((e < 113) & (e > 101))*((((0x007FF000 + m) >> (125 - e)) + 1) >> 1) | (e > 143)*0x7FFF; // sign : normalized : denormalized : saturate
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get pixel data from image as Vector4 array (float normalized)
|
// Get pixel data from image as Vector4 array (float normalized)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue