This repository has been archived on 2021-12-06. You can view files and clone it, but cannot push or open issues or pull requests.
raylib-test/include/fileToCharArray.h

34 lines
1,002 B
C
Raw Normal View History

2021-11-24 14:36:30 +01:00
#include "raylib.h"
#include <string.h>
void fileToBytes(const char *inputFileName, const char *outputFileName, const char *arrayName)
{
unsigned int fileSize = 0;
unsigned char *const fileData = LoadFileData(inputFileName, &fileSize);
2021-11-24 14:36:30 +01:00
char *const content = (char *const)MemAlloc(sizeof(char) * fileSize * 10);
2021-11-24 14:36:30 +01:00
int cursor = 0;
for (unsigned int i = 0; i < fileSize; i++)
{
TextAppend(content, TextFormat(" 0x%02x,\n", fileData[i]), &cursor);
}
cursor = 0;
char *const final = (char *const)MemAlloc((sizeof("static const unsigned char ") + strlen(arrayName) + sizeof("[] = {\n") + (fileSize * sizeof(" 0x00,\n")) + sizeof("};")) * sizeof(char));
2021-11-24 14:36:30 +01:00
TextAppend(final, "static const unsigned char ", &cursor);
2021-11-24 14:36:30 +01:00
TextAppend(final, arrayName, &cursor);
TextAppend(final, TextFormat("[%d] = {\n", fileSize), &cursor);
TextAppend(final, content, &cursor);
TextAppend(final, " };", &cursor);
MemFree(content);
SaveFileText(outputFileName, final);
MemFree(final);
}