{"id":18823140,"url":"https://github.com/mobius3/c-rez","last_synced_at":"2025-08-26T03:02:39.887Z","repository":{"id":142742974,"uuid":"139493164","full_name":"mobius3/c-rez","owner":"mobius3","description":"A small tool to generate C arrays of data from a list of input files","archived":false,"fork":false,"pushed_at":"2020-05-18T20:08:19.000Z","size":18,"stargazers_count":47,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-14T01:36:00.561Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mobius3.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2018-07-02T20:49:12.000Z","updated_at":"2024-03-26T15:59:24.000Z","dependencies_parsed_at":null,"dependency_job_id":"52d958e1-58bb-4aba-8c7f-a5aa17bd7210","html_url":"https://github.com/mobius3/c-rez","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/mobius3/c-rez","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mobius3%2Fc-rez","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mobius3%2Fc-rez/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mobius3%2Fc-rez/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mobius3%2Fc-rez/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mobius3","download_url":"https://codeload.github.com/mobius3/c-rez/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mobius3%2Fc-rez/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264726769,"owners_count":23654494,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-08T00:52:52.812Z","updated_at":"2025-07-11T04:03:46.553Z","avatar_url":"https://github.com/mobius3.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# C-Rez\n\n**c-rez** is a small tool to generate `C` arrays of data from a list of\ninput files. You can then compile them in your project and reference\nthem just as regular variables.\n\nIt features:\n\n- An easy to use command-line tool\n- A neat CMake interface for CMake users\n- Allows you to reference assets both as strings\n(`c_rez_resource(\"sprites.png\")`) and as a variables (`sprites_png`)\n- Written in portable C89 code\n- Generates portable C89 code\n- Its MIT licensed\n\nYou might be wondering why would anyone ever need this tool since there\nare many ways of opening and reading a file, below are some of my reasons:\n\n- Cross-platform resource loading is a pain in the arse (specially when\ntargeting mobile devices (I'm looking at you, Android Assets))\n- Cross-platform resource bundling is even worse\n- No loading time, your assets are available as soon as `main` starts\n(even before!)\n- No loose files, you can distribute a single executable file\n- Asset bundling is now part of the compile and link process.\n\nOf course, there are downsides:\n\n- Makes your executable very very big depending on the size of your assets\n- Which in turn makes it slow to load\n- Depending on the final size of your app, the device might decide not to\nload it\n- You have no way of freeing up the loaded memory once you're done using\nthe asset\n- No way of deciding what *not* to load at runtime. It is always all or\nnothing (you can go around this by making your assets a shared library\nand then loading them with `dlopen`, but if you're doing this you might\nas well use plain old `fopen`)\n\n## Output\n\nA struct type is declared and used to represent each processed file.\nThis is how the struct looks:\n\n```cpp\ntypedef struct c_rez_resource {\n  unsigned char const * const data;\n  unsigned int const length;\n} c_rez_resource;\n```\n\nThe path of input files given to the **c-rez** tool will be used to generate\nan identifier. This identifier will be the variable name declared\nin the `.h` file and defined it in the `.c`file. **c-rez** also accepts a `key`\nthat will be part of the generated identifiers.\n\nFor instance, if you pass a resource file `img/sprites.png` and key\n`resources`, the `.h` file will have a variable called\n`resources_img_sprites_png` that will have a `data` member pointing to its\nbytes and a `length` member with the byte count.\n\nYou can also use the function `c_rez_locate_resources(\"img/sprites.png\")`, it\nwill return `\u0026resources_img_sprites_png`\n\nSuppose you generate an `img.h` header file, it would look like this:\n```cpp\n#ifndef c_rez_resources_img_h\n#define c_rez_resources_img_h\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n#ifndef c_rez_resource_struct\n#define c_rez_resource_struct\ntypedef struct c_rez_resource {\n  unsigned char const * const data;\n  unsigned int const length;\n} c_rez_resource;\n#endif /* c_rez_resource_struct */\n\nextern c_rez_resource const resources_img_sprites_png;\nstruct c_rez_resource const * c_rez_locate_resources(const char name[]);\n\n#ifdef __cplusplus\n}\n#endif\n\n#endif /* c_rez_resources_img_h */\n```\n\nAftwewards you can `#include` it in your project and use the generated structs\nto get to your data:\n\n```cpp\n#include \"resources/img.h\"\n\nint main() {\n    printf(\"sprites.png length: %u\\n\", resources_img_sprites_png.length);\n    printf(\"address of sprites.png: %p\\n\", c_rez_locate_resources(\"img/sprites.png\");\n    return 0;\n}\n```\n\nYou can then either commit these files to your project or make it part of your\nbuild process.\n\nRefer to either [CMake usage](#cmake-usage) or [Command-line\ntool](#command-line-tool) on how to generate and use these files in your\nproject.\n\n## CMake usage\n\n### As a subproject\n\n**c-rez** is ready to be used as a sub-project. Simply download the\nsource archive and use `add_subdirectory` before adding targets using\n**c-rez**\n\n```cmake\nadd_subdirectory(path/to/c-rez)\n```\n\n### As a CMake package\n\nYou can either compile from source or install a release archive.\n\n**Compile from source**\n\n1. Have a compiler environment ready (GCC, LLVM, MSVC, MinGW, etc);\n2. Have [CMake](http://cmake.org) 3.0 (minimum) installed;\n3. Download this repository;\n4. Create a `build` folder inside it;\n5. Run `cmake` (or `cmake-gui`), set the binary dir to the newly created\nbuild folder and the source dir to the repository folder;\n6. build it with `cmake --build . --target c-rez`\n    - Alternatively, you can open the generated project files and build them in\n    your IDE\n7. Install it with `cmake --build . --target install` (you might need to use\n **sudo** here or to adjust `CMAKE_INSTALL_PREFIX`)\n    - If using it inside an IDE, run the *INSTALL* target\n\n**Install a release**\n\n1. Download a suitable archive from the releases section\n2. Unzip it somewhere you can later find with CMake (you may need to\nadjust the `CMAKE_PREFIX_PATH` of your project)\n\n**Add package**\n\nUse `find_package` to add it to your project:\n\n```cmake\nlist(APPEND CMAKE_PREFIX_PATH /path/to/c-rez/prefix)\nfind_package(c-rez REQUIRED)\n```\n\n### Create a resource target\n\nAfter using either `find_package` or `add_subdirectory`, a new CMake\nfunction will be available: `add_c_rez`. This function will create a new\n`C` *STATIC* library under the `c-rez` namespace that you can link your\ntargets to:\n\n```cmake\nadd_executable(mygame mygame.c engine.c)\nadd_c_rez(images\n    assets/tileset.png\n    assets/sprites.png\n    assets/gui.png\n)\ntarget_link_libraries(mygame c-rez::images)\n```\n\nIf you place the word `TEXT` before a path, it will be treated as text and a\n`\\0` terminating byte will be appended to its data:\n\n```cmake\nadd_c_rez(texts\n    TEXT texts/chapter01.txt\n    TEXT texts/chapter02.txt\n    TEXT texts/chapter03.txt\n)\n```\n\nThe header files for your target will be under a `c-rez` folder in your build\npath (*different* targets might not be in the *same* path, though)\n\n```c\n#include \"c-rez/images.h\"\n#include \"c-rez/texts.h\"\n```\n\n## Command-line tool\nIf added to your `PATH`, you can call **c-rez** as a command line tool to\n generate a header\nand/or source files:\n\n```\nc-rez -k \u003cresource key\u003e [-h \u003coutput.h\u003e] [-c \u003coutput.c\u003e] [--text] \u003cinput_1\u003e [[--text] \u003cinput_2\u003e] [[--text] \u003cinput_n\u003e]*\n```\n\n- **-h \\\u003cfile.h\\\u003e**: specifies the header output file. If omitted, only\n source gets generated.\n- **-c \\\u003cfile.c\\\u003e**: specifies the source output file. If omitted, only\nheader gets generated.\n- **-k \\\u003cresource key\\\u003e**: specifies a key to identify this resource. It will\n be used in header guards and resource functions.\n- **--text**: appends `\\0` when processing the next *\\\u003cinput\\\u003e*\nfile. This helps when using its data as a string resource.\n- **\\\u003cinput\\\u003e**: space separated list of files to read from.\nDeclarations and definitions will be generated based on the file name. If\n`--text` is specified before the file name, `\\0` will be appended after\nprocessing.\n\n# License\n\n**c-rez** is MIT-licensed.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmobius3%2Fc-rez","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmobius3%2Fc-rez","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmobius3%2Fc-rez/lists"}