BINTOC

Convert any file into a binary representation in C

A Python script to convert any file into a binary representation in C source (the generated files are valid C++, too). This is useful when you want to embed a resource in a game (such as a font to be used as a fallback when no other files can be found, as in Athena).

This script is intended to be used in combination with the SCons build system, but it can be used directly from the Python command line, too.

To use it from SCons:

# Import the script from a folder called "tools".
# Be sure you have a __init__.py in that folder, or import won't work!
from tools import athena_bin_to_c
import os

# Create an environment
environment = Environment(ENV = os.environ)

# We need to wrap the script so that SCons' command builder can call it...
def CreateBinaryCFiles(env, target, source):
    athena_bin_to_c.BinToC(source[0], target)

# Call from the command builder, where "some_embedded_resource" is the name of the
#   source you want to generate, and "resources/some_resource.bin" is the file you
#   want to embed.
some_resource_gen = environment.Command(
    ["some_embedded_resource.c", "some_embedded_resource.h"],
    "resources/some_resource.bin",
    CreateBinaryCFiles
)
# SCons is smart enough to then regenerate the sources if your resource changes,
#   and to generate sources when a Program or Library builder needs those sources.


You can also simply call the script from the Python command line, if you want a oneshot deal (or SCons isn't your build system of choice):


$ python
>>> from tools import athena_bin_to_c
>>> athena_bin_to_c.BinToC("resources/some_resource.bin,["some_embedded_resource.c", "some_embedded_resource.h"])
>>> quit()
$

Details

  • 2.7 KB
  • 21
  • 12/24/2021 11:20 AM

Actions