From e7464d5fc376783912da9086a4bf49d2d5759135 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Tue, 27 Dec 2016 17:37:35 +0100 Subject: [PATCH] Review some formatting and naming - Renamed WritePNG() to SavePNG() for consistency with other file loading functions - Renamed WriteBitmap() to SaveBMP() for consistency with other file loading functions - Redesigned SaveBMP() to use stb_image_write --- src/audio.c | 18 ++++++++-------- src/core.c | 5 +++-- src/rlgl.c | 8 +++---- src/text.c | 6 +++--- src/utils.c | 60 ++++++++--------------------------------------------- src/utils.h | 8 +++---- 6 files changed, 32 insertions(+), 73 deletions(-) diff --git a/src/audio.c b/src/audio.c index 3a4ca3dfa..944d1b9db 100644 --- a/src/audio.c +++ b/src/audio.c @@ -985,7 +985,7 @@ static Wave LoadWAV(const char *fileName) char chunkID[4]; int chunkSize; char format[4]; - } WavRiffHeader; + } WAVRiffHeader; typedef struct { char subChunkID[4]; @@ -996,16 +996,16 @@ static Wave LoadWAV(const char *fileName) int byteRate; short blockAlign; short bitsPerSample; - } WavFormat; + } WAVFormat; typedef struct { char subChunkID[4]; int subChunkSize; - } WavData; + } WAVData; - WavRiffHeader wavRiffHeader; - WavFormat wavFormat; - WavData wavData; + WAVRiffHeader wavRiffHeader; + WAVFormat wavFormat; + WAVData wavData; Wave wave = { 0 }; FILE *wavFile; @@ -1020,7 +1020,7 @@ static Wave LoadWAV(const char *fileName) else { // Read in the first chunk into the struct - fread(&wavRiffHeader, sizeof(WavRiffHeader), 1, wavFile); + fread(&wavRiffHeader, sizeof(WAVRiffHeader), 1, wavFile); // Check for RIFF and WAVE tags if (strncmp(wavRiffHeader.chunkID, "RIFF", 4) || @@ -1031,7 +1031,7 @@ static Wave LoadWAV(const char *fileName) else { // Read in the 2nd chunk for the wave info - fread(&wavFormat, sizeof(WavFormat), 1, wavFile); + fread(&wavFormat, sizeof(WAVFormat), 1, wavFile); // Check for fmt tag if ((wavFormat.subChunkID[0] != 'f') || (wavFormat.subChunkID[1] != 'm') || @@ -1045,7 +1045,7 @@ static Wave LoadWAV(const char *fileName) if (wavFormat.subChunkSize > 16) fseek(wavFile, sizeof(short), SEEK_CUR); // Read in the the last byte of data before the sound file - fread(&wavData, sizeof(WavData), 1, wavFile); + fread(&wavData, sizeof(WAVData), 1, wavFile); // Check for data tag if ((wavData.subChunkID[0] != 'd') || (wavData.subChunkID[1] != 'a') || diff --git a/src/core.c b/src/core.c index a210df56e..147010f58 100644 --- a/src/core.c +++ b/src/core.c @@ -2139,7 +2139,7 @@ static void TakeScreenshot(void) sprintf(buffer, "screenshot%03i.png", shotNum); // Save image as PNG - WritePNG(buffer, imgData, renderWidth, renderHeight, 4); + SavePNG(buffer, imgData, renderWidth, renderHeight, 4); free(imgData); @@ -2818,7 +2818,8 @@ static void InitMouse(void) // if too much time passes between reads, queue gets full and new events override older ones... static void *MouseThread(void *arg) { - const unsigned char XSIGN = 1<<4, YSIGN = 1<<5; + const unsigned char XSIGN = (1 << 4); + const unsigned char YSIGN = (1 << 5); typedef struct { char buttons; diff --git a/src/rlgl.c b/src/rlgl.c index ae28d9b6d..cb1ac709b 100644 --- a/src/rlgl.c +++ b/src/rlgl.c @@ -192,7 +192,7 @@ //---------------------------------------------------------------------------------- // Dynamic vertex buffers (position + texcoords + colors + indices arrays) -typedef struct { +typedef struct DynamicBuffer { int vCounter; // vertex position counter to process (and draw) from full buffer int tcCounter; // vertex texcoord counter to process (and draw) from full buffer int cCounter; // vertex color counter to process (and draw) from full buffer @@ -211,7 +211,7 @@ typedef struct { // Draw call type // NOTE: Used to track required draw-calls, organized by texture -typedef struct { +typedef struct DrawCall { int vertexCount; GLuint vaoId; GLuint textureId; @@ -226,7 +226,7 @@ typedef struct { } DrawCall; // Head-Mounted-Display device parameters -typedef struct { +typedef struct VrDeviceInfo { int hResolution; // HMD horizontal resolution in pixels int vResolution; // HMD vertical resolution in pixels float hScreenSize; // HMD horizontal size in meters @@ -240,7 +240,7 @@ typedef struct { } VrDeviceInfo; // VR Stereo rendering configuration for simulator -typedef struct { +typedef struct VrStereoConfig { RenderTexture2D stereoFbo; // VR stereo rendering framebuffer Shader distortionShader; // VR stereo rendering distortion shader //Rectangle eyesViewport[2]; // VR stereo rendering eyes viewports diff --git a/src/text.c b/src/text.c index f97b581d9..74d5940b5 100644 --- a/src/text.c +++ b/src/text.c @@ -53,7 +53,7 @@ #define MAX_FORMATTEXT_LENGTH 64 #define MAX_SUBTEXT_LENGTH 64 -#define BIT_CHECK(a,b) ((a) & (1<<(b))) +#define BIT_CHECK(a,b) ((a) & (1 << (b))) //---------------------------------------------------------------------------------- // Types and Structures Definition @@ -948,7 +948,7 @@ static SpriteFont LoadTTF(const char *fileName, int fontSize, int numChars, int return font; } - fread(ttfBuffer, 1, 1<<25, ttfFile); + fread(ttfBuffer, 1, 1 << 25, ttfFile); if (fontChars[0] != 32) TraceLog(WARNING, "TTF spritefont loading: first character is not SPACE(32) character"); @@ -983,7 +983,7 @@ static SpriteFont LoadTTF(const char *fileName, int fontSize, int numChars, int font.texture = LoadTextureFromImage(image); - //WritePNG("generated_ttf_image.png", (unsigned char *)image.data, image.width, image.height, 2); + //SavePNG("generated_ttf_image.png", (unsigned char *)image.data, image.width, image.height, 2); UnloadImage(image); // Unloads dataGrayAlpha diff --git a/src/utils.c b/src/utils.c index 8fedcaadf..711ffab3f 100644 --- a/src/utils.c +++ b/src/utils.c @@ -42,13 +42,16 @@ #include // Required for: malloc(), free() #include // Required for: fopen(), fclose(), fputc(), fwrite(), printf(), fprintf(), funopen() #include // Required for: va_list, va_start(), vfprintf(), va_end() -//#include // Required for: strlen(), strrchr(), strcmp() +#include // Required for: strlen(), strrchr(), strcmp() #if defined(PLATFORM_DESKTOP) || defined(PLATFORM_RPI) #define STB_IMAGE_WRITE_IMPLEMENTATION - #include "external/stb_image_write.h" // Required for: stbi_write_png() + #include "external/stb_image_write.h" // Required for: stbi_write_bmp(), stbi_write_png() #endif +#define RRES_IMPLEMENTATION +#include "rres.h" + #define DO_NOT_TRACE_DEBUG_MSGS // Avoid DEBUG messages tracing //---------------------------------------------------------------------------------- @@ -73,59 +76,14 @@ static int android_close(void *cookie); //---------------------------------------------------------------------------------- #if defined(PLATFORM_DESKTOP) || defined(PLATFORM_RPI) -// 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) +// 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) { - int filesize = 54 + 3*width*height; - - 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 - - if (bmpFile == NULL) - { - TraceLog(WARNING, "[%s] BMP file could not be created", fileName); - } - else - { - // 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++) - { - 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); - } - } - } - - fclose(bmpFile); // Close bitmap file + stbi_write_bmp(fileName, width, height, compSize, imgData); } // 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, int compSize) +void SavePNG(const char *fileName, unsigned char *imgData, int width, int height, int compSize) { stbi_write_png(fileName, width, height, compSize, imgData, width*compSize); } diff --git a/src/utils.h b/src/utils.h index 045b0692c..e0db51fe6 100644 --- a/src/utils.h +++ b/src/utils.h @@ -31,6 +31,8 @@ #include // Required for: AAssetManager #endif +#include "rres.h" + //---------------------------------------------------------------------------------- // Some basic Defines //---------------------------------------------------------------------------------- @@ -66,11 +68,9 @@ extern "C" { // Prevents name mangling of functions //---------------------------------------------------------------------------------- // Module Functions Declaration //---------------------------------------------------------------------------------- -unsigned char *DecompressData(const unsigned char *data, unsigned long compSize, int uncompSize); - #if defined(PLATFORM_DESKTOP) || defined(PLATFORM_RPI) -void WriteBitmap(const char *fileName, unsigned char *imgData, int width, int height); -void WritePNG(const char *fileName, unsigned char *imgData, int width, int height, int compSize); +void SaveBMP(const char *fileName, unsigned char *imgData, int width, int height, int compSize); +void SavePNG(const char *fileName, unsigned char *imgData, int width, int height, int compSize); #endif void TraceLog(int msgType, const char *text, ...); // Outputs a trace log message