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
This commit is contained in:
raysan5 2016-12-27 17:37:35 +01:00
parent 3c91dc099d
commit e7464d5fc3
6 changed files with 32 additions and 73 deletions

View File

@ -985,7 +985,7 @@ static Wave LoadWAV(const char *fileName)
char chunkID[4]; char chunkID[4];
int chunkSize; int chunkSize;
char format[4]; char format[4];
} WavRiffHeader; } WAVRiffHeader;
typedef struct { typedef struct {
char subChunkID[4]; char subChunkID[4];
@ -996,16 +996,16 @@ static Wave LoadWAV(const char *fileName)
int byteRate; int byteRate;
short blockAlign; short blockAlign;
short bitsPerSample; short bitsPerSample;
} WavFormat; } WAVFormat;
typedef struct { typedef struct {
char subChunkID[4]; char subChunkID[4];
int subChunkSize; int subChunkSize;
} WavData; } WAVData;
WavRiffHeader wavRiffHeader; WAVRiffHeader wavRiffHeader;
WavFormat wavFormat; WAVFormat wavFormat;
WavData wavData; WAVData wavData;
Wave wave = { 0 }; Wave wave = { 0 };
FILE *wavFile; FILE *wavFile;
@ -1020,7 +1020,7 @@ static Wave LoadWAV(const char *fileName)
else else
{ {
// Read in the first chunk into the struct // 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 // Check for RIFF and WAVE tags
if (strncmp(wavRiffHeader.chunkID, "RIFF", 4) || if (strncmp(wavRiffHeader.chunkID, "RIFF", 4) ||
@ -1031,7 +1031,7 @@ static Wave LoadWAV(const char *fileName)
else else
{ {
// Read in the 2nd chunk for the wave info // 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 // Check for fmt tag
if ((wavFormat.subChunkID[0] != 'f') || (wavFormat.subChunkID[1] != 'm') || 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); if (wavFormat.subChunkSize > 16) fseek(wavFile, sizeof(short), SEEK_CUR);
// Read in the the last byte of data before the sound file // 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 // Check for data tag
if ((wavData.subChunkID[0] != 'd') || (wavData.subChunkID[1] != 'a') || if ((wavData.subChunkID[0] != 'd') || (wavData.subChunkID[1] != 'a') ||

View File

@ -2139,7 +2139,7 @@ static void TakeScreenshot(void)
sprintf(buffer, "screenshot%03i.png", shotNum); sprintf(buffer, "screenshot%03i.png", shotNum);
// Save image as PNG // Save image as PNG
WritePNG(buffer, imgData, renderWidth, renderHeight, 4); SavePNG(buffer, imgData, renderWidth, renderHeight, 4);
free(imgData); 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... // if too much time passes between reads, queue gets full and new events override older ones...
static void *MouseThread(void *arg) 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 { typedef struct {
char buttons; char buttons;

View File

@ -192,7 +192,7 @@
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
// Dynamic vertex buffers (position + texcoords + colors + indices arrays) // 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 vCounter; // vertex position counter to process (and draw) from full buffer
int tcCounter; // vertex texcoord 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 int cCounter; // vertex color counter to process (and draw) from full buffer
@ -211,7 +211,7 @@ typedef struct {
// Draw call type // Draw call type
// NOTE: Used to track required draw-calls, organized by texture // NOTE: Used to track required draw-calls, organized by texture
typedef struct { typedef struct DrawCall {
int vertexCount; int vertexCount;
GLuint vaoId; GLuint vaoId;
GLuint textureId; GLuint textureId;
@ -226,7 +226,7 @@ typedef struct {
} DrawCall; } DrawCall;
// Head-Mounted-Display device parameters // Head-Mounted-Display device parameters
typedef struct { typedef struct VrDeviceInfo {
int hResolution; // HMD horizontal resolution in pixels int hResolution; // HMD horizontal resolution in pixels
int vResolution; // HMD vertical resolution in pixels int vResolution; // HMD vertical resolution in pixels
float hScreenSize; // HMD horizontal size in meters float hScreenSize; // HMD horizontal size in meters
@ -240,7 +240,7 @@ typedef struct {
} VrDeviceInfo; } VrDeviceInfo;
// VR Stereo rendering configuration for simulator // VR Stereo rendering configuration for simulator
typedef struct { typedef struct VrStereoConfig {
RenderTexture2D stereoFbo; // VR stereo rendering framebuffer RenderTexture2D stereoFbo; // VR stereo rendering framebuffer
Shader distortionShader; // VR stereo rendering distortion shader Shader distortionShader; // VR stereo rendering distortion shader
//Rectangle eyesViewport[2]; // VR stereo rendering eyes viewports //Rectangle eyesViewport[2]; // VR stereo rendering eyes viewports

View File

@ -983,7 +983,7 @@ static SpriteFont LoadTTF(const char *fileName, int fontSize, int numChars, int
font.texture = LoadTextureFromImage(image); 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 UnloadImage(image); // Unloads dataGrayAlpha

View File

@ -42,13 +42,16 @@
#include <stdlib.h> // Required for: malloc(), free() #include <stdlib.h> // Required for: malloc(), free()
#include <stdio.h> // Required for: fopen(), fclose(), fputc(), fwrite(), printf(), fprintf(), funopen() #include <stdio.h> // Required for: fopen(), fclose(), fputc(), fwrite(), printf(), fprintf(), funopen()
#include <stdarg.h> // Required for: va_list, va_start(), vfprintf(), va_end() #include <stdarg.h> // Required for: va_list, va_start(), vfprintf(), va_end()
//#include <string.h> // Required for: strlen(), strrchr(), strcmp() #include <string.h> // Required for: strlen(), strrchr(), strcmp()
#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_RPI) #if defined(PLATFORM_DESKTOP) || defined(PLATFORM_RPI)
#define STB_IMAGE_WRITE_IMPLEMENTATION #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 #endif
#define RRES_IMPLEMENTATION
#include "rres.h"
#define DO_NOT_TRACE_DEBUG_MSGS // Avoid DEBUG messages tracing #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) #if defined(PLATFORM_DESKTOP) || defined(PLATFORM_RPI)
// Creates a bitmap (BMP) file from an array of pixel data // Creates a BMP image file from an array of pixel data
// NOTE: This function is not explicitly available to raylib users void SaveBMP(const char *fileName, unsigned char *imgData, int width, int height, int compSize)
void WriteBitmap(const char *fileName, unsigned char *imgData, int width, int height)
{ {
int filesize = 54 + 3*width*height; stbi_write_bmp(fileName, width, height, compSize, imgData);
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
} }
// Creates a PNG image file from an array of pixel data // Creates a PNG image file from an array of pixel data
// NOTE: Uses stb_image_write void SavePNG(const char *fileName, unsigned char *imgData, int width, int height, int compSize)
void WritePNG(const char *fileName, unsigned char *imgData, int width, int height, int compSize)
{ {
stbi_write_png(fileName, width, height, compSize, imgData, width*compSize); stbi_write_png(fileName, width, height, compSize, imgData, width*compSize);
} }

View File

@ -31,6 +31,8 @@
#include <android/asset_manager.h> // Required for: AAssetManager #include <android/asset_manager.h> // Required for: AAssetManager
#endif #endif
#include "rres.h"
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
// Some basic Defines // Some basic Defines
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
@ -66,11 +68,9 @@ extern "C" { // Prevents name mangling of functions
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
// Module Functions Declaration // Module Functions Declaration
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
unsigned char *DecompressData(const unsigned char *data, unsigned long compSize, int uncompSize);
#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_RPI) #if defined(PLATFORM_DESKTOP) || defined(PLATFORM_RPI)
void WriteBitmap(const char *fileName, unsigned char *imgData, int width, int height); void SaveBMP(const char *fileName, unsigned char *imgData, int width, int height, int compSize);
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);
#endif #endif
void TraceLog(int msgType, const char *text, ...); // Outputs a trace log message void TraceLog(int msgType, const char *text, ...); // Outputs a trace log message