Implemented resource manager which can load textures and levels.
Implemented script to generate Resources.g.h file linking assets to code. Added assets from old project.
This commit is contained in:
142
build/prepareAssets.py
Executable file
142
build/prepareAssets.py
Executable file
@ -0,0 +1,142 @@
|
||||
#!/usr/bin/python3
|
||||
import os
|
||||
import sys
|
||||
|
||||
#
|
||||
# Constants
|
||||
#
|
||||
|
||||
ASSETS_DIR="assets"
|
||||
RESOURCES_FILE="src/resources/Resources.g.h"
|
||||
|
||||
RESOURCES_FILE_HEADER=("// WARNING: This file is auto generated by the build/prepareAssets.py script.\n"
|
||||
"#ifndef STORAGE_RESOURCES_G_H_\n"
|
||||
"#define STORAGE_RESOURCES_G_H_\n"
|
||||
"\n"
|
||||
"#include <resources/ResourceInfo.h>\n"
|
||||
"\n"
|
||||
"namespace farmlands {\n"
|
||||
"namespace resources {\n"
|
||||
"\n")
|
||||
|
||||
RESOURCES_FILE_FOOTER=("\n"
|
||||
"}\n"
|
||||
"}\n"
|
||||
"#endif /* STORAGE_RESOURCES_G_H_ */\n")
|
||||
|
||||
R_HEADER=(" /**\n"
|
||||
" * This namespace contains all the resource IDs, used by the resource manager.\n"
|
||||
" * The IDs are generated at build time by the 'prepareAssets.py' script.\n"
|
||||
" */\n"
|
||||
" namespace R {\n")
|
||||
|
||||
R_FOOTER=" }\n"
|
||||
|
||||
RINFO_HEADER=(" /**\n"
|
||||
" * This array contains the names of all the files, and the corresponding file type.\n"
|
||||
" */\n"
|
||||
" const ResourceInfo RInfo[] = {\n")
|
||||
|
||||
RINFO_FOOTER=" };\n"
|
||||
|
||||
|
||||
#
|
||||
# File types
|
||||
#
|
||||
FILE_TYPES = [
|
||||
([".png", ".bmp"], "Texture"),
|
||||
([".level"], "Level"),
|
||||
([".csv"], "LevelLayer"),
|
||||
([".ttf"], "Font"),
|
||||
]
|
||||
|
||||
|
||||
#
|
||||
# Capitalizes the given string by converting the first character to uppercase.
|
||||
# It does not convert the other characters to lowercase.
|
||||
#
|
||||
def capitalize(s):
|
||||
if len(s) == 0:
|
||||
return s
|
||||
|
||||
return s[0].upper() + s[1:]
|
||||
|
||||
#
|
||||
# Obtains the file type using FILE_TYPES constant
|
||||
#
|
||||
def getFileType(fileName):
|
||||
ext = os.path.splitext(fileName)[1]
|
||||
|
||||
for (extensions, type) in FILE_TYPES:
|
||||
if ext in extensions:
|
||||
return type
|
||||
|
||||
return "None"
|
||||
|
||||
|
||||
#
|
||||
# Main routine
|
||||
#
|
||||
if __name__ == '__main__':
|
||||
r = ""
|
||||
rInfo = ""
|
||||
rInfoBegin = ""
|
||||
|
||||
assetId = 0
|
||||
|
||||
print ("Preparing assets...")
|
||||
|
||||
# Build R and RInfo
|
||||
for assetDir in os.listdir(ASSETS_DIR):
|
||||
assetDirPath=os.path.join(ASSETS_DIR, assetDir)
|
||||
|
||||
# Warning for non-directories
|
||||
if not os.path.isdir(assetDirPath):
|
||||
sys.stderr.writeline("Warning: file {0} is in the root 'assets' folder, and it will not be imported.".format(assetDir))
|
||||
continue
|
||||
|
||||
# Create R enum
|
||||
assetIdBegin = assetId
|
||||
rInfoBegin += " const int RInfo_{0}_Begin = {1};\n".format(capitalize(assetDir), assetIdBegin)
|
||||
r += (" enum " + capitalize(assetDir) + "\n"
|
||||
" {\n")
|
||||
|
||||
# Get items in directory
|
||||
for (dir, dirs, files) in os.walk(assetDirPath):
|
||||
|
||||
# R prefix - remove assetDirPath from path, replace path separators with _
|
||||
RdirPrefix = os.path.relpath(dir, assetDirPath)
|
||||
RdirPrefix = RdirPrefix.replace("/", "_").replace("\\", "_")
|
||||
if (RdirPrefix == "."):
|
||||
RdirPrefix = ""
|
||||
else:
|
||||
RdirPrefix += "_"
|
||||
|
||||
for f in files:
|
||||
# Append to R
|
||||
fResName = RdirPrefix + capitalize(os.path.splitext(f)[0])
|
||||
r += " {0} = {1},\n".format(fResName, assetId)
|
||||
assetId += 1
|
||||
|
||||
# RInfo path should be relative to assets directory
|
||||
fPath = os.path.join(dir, f)
|
||||
fPath = os.path.relpath(fPath, ASSETS_DIR)
|
||||
fType = getFileType(f)
|
||||
|
||||
rInfo += " {{ \"{0}\", ResourceType::{1} }},\n".format(fPath, fType) # TODO: obtain resource type
|
||||
|
||||
# Finish R enum
|
||||
r += " };\n"
|
||||
|
||||
# Build resource header file
|
||||
resFile = RESOURCES_FILE_HEADER
|
||||
resFile += R_HEADER + r + R_FOOTER
|
||||
resFile += "\n" + rInfoBegin + "\n"
|
||||
resFile += RINFO_HEADER + rInfo + RINFO_FOOTER
|
||||
resFile += RESOURCES_FILE_FOOTER
|
||||
|
||||
# Write resource file
|
||||
f = open(RESOURCES_FILE, "w")
|
||||
f.write(resFile)
|
||||
f.close()
|
||||
|
Reference in New Issue
Block a user