diff options
| author | Ray <[email protected]> | 2022-05-06 00:14:28 +0200 |
|---|---|---|
| committer | Ray <[email protected]> | 2022-05-06 00:14:28 +0200 |
| commit | d9a30b84800d8d354714624210be8888a89be99e (patch) | |
| tree | 3d11fb4fef8760ec602bae79d50d4b3c138e0d77 /src/utils.c | |
| parent | ed2ab55034078da1c09073a5cb1cd9de35d3afe8 (diff) | |
| download | raylib-d9a30b84800d8d354714624210be8888a89be99e.tar.gz raylib-d9a30b84800d8d354714624210be8888a89be99e.zip | |
ADDED: `ExportDataAsCode()`
Diffstat (limited to 'src/utils.c')
| -rw-r--r-- | src/utils.c | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/src/utils.c b/src/utils.c index 37b89a9a..bc7470a7 100644 --- a/src/utils.c +++ b/src/utils.c @@ -268,6 +268,51 @@ bool SaveFileData(const char *fileName, void *data, unsigned int bytesToWrite) return success; } +// Export data to code (.h), returns true on success +bool ExportDataAsCode(const char *data, unsigned int size, const char *fileName) +{ + bool success = false; + +#ifndef TEXT_BYTES_PER_LINE + #define TEXT_BYTES_PER_LINE 20 +#endif + + // NOTE: Text data buffer size is estimated considering raw data size in bytes + // and requiring 6 char bytes for every byte: "0x00, " + char *txtData = (char *)RL_CALLOC(size*6 + 2000, sizeof(char)); + + int byteCount = 0; + byteCount += sprintf(txtData + byteCount, "////////////////////////////////////////////////////////////////////////////////////////\n"); + byteCount += sprintf(txtData + byteCount, "// //\n"); + byteCount += sprintf(txtData + byteCount, "// DataAsCode exporter v1.0 - Raw data exported as an array of bytes //\n"); + byteCount += sprintf(txtData + byteCount, "// //\n"); + byteCount += sprintf(txtData + byteCount, "// more info and bugs-report: github.com/raysan5/raylib //\n"); + byteCount += sprintf(txtData + byteCount, "// feedback and support: ray[at]raylib.com //\n"); + byteCount += sprintf(txtData + byteCount, "// //\n"); + byteCount += sprintf(txtData + byteCount, "// Copyright (c) 2022 Ramon Santamaria (@raysan5) //\n"); + byteCount += sprintf(txtData + byteCount, "// //\n"); + byteCount += sprintf(txtData + byteCount, "////////////////////////////////////////////////////////////////////////////////////////\n\n"); + + // Get file name from path and convert variable name to uppercase + char varFileName[256] = { 0 }; + strcpy(varFileName, GetFileNameWithoutExt(fileName)); + for (int i = 0; varFileName[i] != '\0'; i++) if ((varFileName[i] >= 'a') && (varFileName[i] <= 'z')) { varFileName[i] = varFileName[i] - 32; } + + byteCount += sprintf(txtData + byteCount, "static unsigned char %s_DATA[%i] = { ", varFileName, size); + for (int i = 0; i < dataSize - 1; i++) byteCount += sprintf(txtData + byteCount, ((i%TEXT_BYTES_PER_LINE == 0)? "0x%x,\n" : "0x%x, "), data[i]); + byteCount += sprintf(txtData + byteCount, "0x%x };\n", data[size - 1]); + + // NOTE: Text data size exported is determined by '\0' (NULL) character + success = SaveFileText(fileName, txtData); + + RL_FREE(txtData); + + if (success != 0) TRACELOG(LOG_INFO, "FILEIO: [%s] Data as code exported successfully", fileName); + else TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to export data as code", fileName); + + return success; +} + // Load text data from file, returns a '\0' terminated string // NOTE: text chars array should be freed manually char *LoadFileText(const char *fileName) |
