Write a python script to generate byte arrays.

This is preparing for textures for the Flappy Bird clone.
This commit is contained in:
Fries 2024-04-21 21:12:49 -07:00
parent aaa6c15a9a
commit dd801873de
4 changed files with 52 additions and 1 deletions

7
src/flappyBird/Assets.hh Normal file
View file

@ -0,0 +1,7 @@
#ifndef ASSETS_HH
#define ASSETS_HH
#include <vector>
namespace assets {
std::vector<unsigned char> bird();
}
#endif

Binary file not shown.

After

Width:  |  Height:  |  Size: 426 B

View file

@ -0,0 +1,41 @@
import argparse
from pathlib import Path
from PIL import Image
parser = argparse.ArgumentParser()
parser.add_argument("function")
parser.add_argument("input")
parser.add_argument("output")
args = parser.parse_args()
function = args.function
input = Path(args.input)
output = Path(args.output)
if not input.exists():
print("Input " + str(path.absolute()) + " not found.")
raise SystemExit(1)
hexList = list()
with Image.open(input) as image:
imageBytes = bytes(image.getdata())
byteList = list(imageBytes)
for byte in byteList:
hexList.append(f'0x{byte:02x}')
code = f"""#include <Assets.hh>
#include <vector>
namespace assets {{
std::vector<unsigned char> {function}() {{
return {{ {','.join(hexList)} }};
}}
}}
"""
with open(output, 'w') as f:
f.write(code)

View file

@ -5,5 +5,8 @@ flappy_sources = [
'MainMenu.cc'
]
flappy_bird = static_library('flappyBird', flappy_sources, dependencies: deps, include_directories: incdirs)
python3 = find_program('python3')
bird = custom_target('bird', output: 'birdSprite.cc', input: ['generateAssets.py', 'assets/yellowbird-downflap.png'], command: [python3, '@INPUT0@', 'bird', '@INPUT1@', '@OUTPUT@'])
flappy_bird = static_library('flappyBird', [flappy_sources, bird], dependencies: deps, include_directories: incdirs)
flappy_dep = declare_dependency(link_with: [flappy_bird], include_directories: incdirs)