/bpk

:construction_worker: bpk packages files as binary resources availables in your C++ project

Primary LanguageC++

bpk

bpk packages files as binary resources availables in your project

Requirements

Download & Build

git clone --recursive https://github.com/a-cordier/bpk.git
cd bpk
mkdir build
cd build
cmake ..
make

Package your files

bpk -d <src_dir> -o <file_name>.h -n <namespace>

Use in your project

  • Add the output file to your project
  • Use <namespace>::getResource(const char* key) to get access to your file

Files are named after their relative path inside their resource directory

Example

  • Assuming the following resource directory
resources
└── svg
    ├── next.svg
    ├── pause.svg
    ├── play.svg
    ├── previous.svg
    └── stop.svg

Running:

bpk -d ./resources -o resources.h -n resources
  • Will generate the following resources.h file
#include <iostream>
#include <vector>
#include <map>
#include <utility>

namespace resources {

	namespace {

		std::map<std::string, std::vector<char> > data = {
			{ "svg/previous.svg", { /* Data chunks */ } },
			{ "svg/pause.svg", { /* Data chunks */ } },
			{ "svg/play.svg", { /* Data chunks */ } },
			{ "svg/next.svg", { /* Data chunks */ } },
		};
	}

	inline char* get(const char* name) {
    		auto it = data.find(name);
    		return it == data.end() ? nullptr : it->second.data();
    	}
    
    inline std::vector<char>::size_type size(const char* name) {
        auto it = data.find(name);
        return it == data.end() ? 0 : it->second.size();
    }
}
  • Resources being accessed the following way
#include "resources.h"

auto data = resources::get("svg/play.svg")

Integrating with CMAKE

Integration with CMAKE for automating generation at build time may be achieved using the ExternalProject module

CMAKE integration sample

include(ExternalProject)

ExternalProject_Add(
        bpk
	GIT_REPOSITORY    "https://github.com/a-cordier/bpk.git"
        GIT_TAG           "v1.0.0"
        SOURCE_DIR        "${CMAKE_CURRENT_BINARY_DIR}/bpk-src"
        BINARY_DIR        "${CMAKE_CURRENT_BINARY_DIR}/bpk-build"
        INSTALL_COMMAND   ""
)

set(RESOURCES_DIR "${CMAKE_CURRENT_SOURCE_DIR}/resources")
set(RESOURCES_FILE "${CMAKE_CURRENT_SOURCE_DIR}/src/resources.h")

add_custom_command(
        TARGET bpk
        POST_BUILD
        WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
        COMMAND "${CMAKE_CURRENT_BINARY_DIR}/bpk-build/bin/bpk" 
	        "-o" "${RESOURCES_FILE}" 
		"-d" "${RESOURCES_DIR}" 
		"-n" "resources"
        COMMENT "Running bpk to generate resources file"
)