Code Monkey home page Code Monkey logo

embed-resource's Introduction

Overview

Embed binary files and resources (such as GLSL shader source files) into C++ projects. Uses C++11 features but no other library/dependencies.

Usage

Use CPM to add this to your CMakeLists.txt:

include(cmake/CPM.cmake)
CPMAddPackage("gh:CodeFinder2/embed-resource#master")

Now you can add your resources, by calling the provided embed_resources() function in your CMakeLists.txt file:

embed_resources(MyResources shaders/vertex.glsl shaders/frag.glsl)

Then add the generated source files, containing the resources, to your binary by adding the created variable to your add_executable() statement and linking against embed_resource:

add_executable(your_target_name ${SOURCE_FILES} ${MyResources})
target_link_libraries(your_target_name embed_resource)

In your C++ project, you can access your embed resources using the Resource class provided in embed_resource/loader.h. Here's an example:

#include <iostream>
using namespace std;

#include <embed_resource/loader.h>

int main()
{
  Resource text = LOAD_RESOURCE(shaders_frag_glsl);
  cout << text.toString() << endl;

  return EXIT_SUCCESS;
}

Note: to reference the file, replace the . in frag.glsl with an underscore _. So, in this example, the symbol name is shaders_frag_glsl.

An example is provided in the example/ directory. It can be compiled and started as follows:

mkdir build && cd build/
cmake -DEMBED_RESOURCE_BUILD_EXAMPLE=ON .. && make -j$(nproc)
./embed_resource_example

Limitations

  • embed_resources() can only be called once.
  • Resource names cannot be modified and must match the provided (and properly modified) path to the resource file.

Credits

Forked and improved from here.

License

Public Domain / WTFPL

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.