REDESIGNED: ExportImageAsCode() to use memory buffer #1232
This commit is contained in:
parent
c1e0978555
commit
d3dece3343
@ -418,40 +418,40 @@ void ExportImageAsCode(Image image, const char *fileName)
|
|||||||
#define TEXT_BYTES_PER_LINE 20
|
#define TEXT_BYTES_PER_LINE 20
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
FILE *txtFile = fopen(fileName, "wt");
|
|
||||||
|
|
||||||
if (txtFile != NULL)
|
|
||||||
{
|
|
||||||
char varFileName[256] = { 0 };
|
|
||||||
int dataSize = GetPixelDataSize(image.width, image.height, image.format);
|
int dataSize = GetPixelDataSize(image.width, image.height, image.format);
|
||||||
|
|
||||||
fprintf(txtFile, "////////////////////////////////////////////////////////////////////////////////////////\n");
|
// NOTE: Text data buffer size is estimated considering image data size
|
||||||
fprintf(txtFile, "// //\n");
|
char *txtData = (char *)RL_CALLOC(6*dataSize + 2000, sizeof(char));
|
||||||
fprintf(txtFile, "// ImageAsCode exporter v1.0 - Image pixel data exported as an array of bytes //\n");
|
|
||||||
fprintf(txtFile, "// //\n");
|
int bytesCount = 0;
|
||||||
fprintf(txtFile, "// more info and bugs-report: github.com/raysan5/raylib //\n");
|
bytesCount += sprintf(txtData + bytesCount, "////////////////////////////////////////////////////////////////////////////////////////\n");
|
||||||
fprintf(txtFile, "// feedback and support: ray[at]raylib.com //\n");
|
bytesCount += sprintf(txtData + bytesCount, "// //\n");
|
||||||
fprintf(txtFile, "// //\n");
|
bytesCount += sprintf(txtData + bytesCount, "// ImageAsCode exporter v1.0 - Image pixel data exported as an array of bytes //\n");
|
||||||
fprintf(txtFile, "// Copyright (c) 2020 Ramon Santamaria (@raysan5) //\n");
|
bytesCount += sprintf(txtData + bytesCount, "// //\n");
|
||||||
fprintf(txtFile, "// //\n");
|
bytesCount += sprintf(txtData + bytesCount, "// more info and bugs-report: github.com/raysan5/raylib //\n");
|
||||||
fprintf(txtFile, "////////////////////////////////////////////////////////////////////////////////////////\n\n");
|
bytesCount += sprintf(txtData + bytesCount, "// feedback and support: ray[at]raylib.com //\n");
|
||||||
|
bytesCount += sprintf(txtData + bytesCount, "// //\n");
|
||||||
|
bytesCount += sprintf(txtData + bytesCount, "// Copyright (c) 2020 Ramon Santamaria (@raysan5) //\n");
|
||||||
|
bytesCount += sprintf(txtData + bytesCount, "// //\n");
|
||||||
|
bytesCount += sprintf(txtData + bytesCount, "////////////////////////////////////////////////////////////////////////////////////////\n\n");
|
||||||
|
|
||||||
// Get file name from path and convert variable name to uppercase
|
// Get file name from path and convert variable name to uppercase
|
||||||
|
char varFileName[256] = { 0 };
|
||||||
strcpy(varFileName, GetFileNameWithoutExt(fileName));
|
strcpy(varFileName, GetFileNameWithoutExt(fileName));
|
||||||
for (int i = 0; varFileName[i] != '\0'; i++) if ((varFileName[i] >= 'a') && (varFileName[i] <= 'z')) { varFileName[i] = varFileName[i] - 32; }
|
for (int i = 0; varFileName[i] != '\0'; i++) if ((varFileName[i] >= 'a') && (varFileName[i] <= 'z')) { varFileName[i] = varFileName[i] - 32; }
|
||||||
|
|
||||||
// Add image information
|
// Add image information
|
||||||
fprintf(txtFile, "// Image data information\n");
|
bytesCount += sprintf(txtData + bytesCount, "// Image data information\n");
|
||||||
fprintf(txtFile, "#define %s_WIDTH %i\n", varFileName, image.width);
|
bytesCount += sprintf(txtData + bytesCount, "#define %s_WIDTH %i\n", varFileName, image.width);
|
||||||
fprintf(txtFile, "#define %s_HEIGHT %i\n", varFileName, image.height);
|
bytesCount += sprintf(txtData + bytesCount, "#define %s_HEIGHT %i\n", varFileName, image.height);
|
||||||
fprintf(txtFile, "#define %s_FORMAT %i // raylib internal pixel format\n\n", varFileName, image.format);
|
bytesCount += sprintf(txtData + bytesCount, "#define %s_FORMAT %i // raylib internal pixel format\n\n", varFileName, image.format);
|
||||||
|
|
||||||
fprintf(txtFile, "static unsigned char %s_DATA[%i] = { ", varFileName, dataSize);
|
bytesCount += sprintf(txtData + bytesCount, "static unsigned char %s_DATA[%i] = { ", varFileName, dataSize);
|
||||||
for (int i = 0; i < dataSize - 1; i++) fprintf(txtFile, ((i%TEXT_BYTES_PER_LINE == 0)? "0x%x,\n" : "0x%x, "), ((unsigned char *)image.data)[i]);
|
for (int i = 0; i < dataSize - 1; i++) bytesCount += sprintf(txtData + bytesCount, ((i%TEXT_BYTES_PER_LINE == 0)? "0x%x,\n" : "0x%x, "), ((unsigned char *)image.data)[i]);
|
||||||
fprintf(txtFile, "0x%x };\n", ((unsigned char *)image.data)[dataSize - 1]);
|
bytesCount += sprintf(txtData + bytesCount, "0x%x };\n", ((unsigned char *)image.data)[dataSize - 1]);
|
||||||
|
|
||||||
fclose(txtFile);
|
SaveFileText(fileName, txtData);
|
||||||
}
|
free(txtData);
|
||||||
}
|
}
|
||||||
|
|
||||||
//------------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------------
|
||||||
|
Loading…
x
Reference in New Issue
Block a user