summaryrefslogtreecommitdiffstats
path: root/src/res2cc_cmd.py
blob: f6321f63035e64f32ee3cbc56a94e535ba677868 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/python
# Script that compiles a set of resources into a single C++ source file. The C++ file
# offers an initResources() function, which registers the resources with the resource
# manager (class ResourceMgr)
#
# Copyright (C) 1997-2015 by Dimitri van Heesch.
#
# Permission to use, copy, modify, and distribute this software and its
# documentation under the terms of the GNU General Public License is hereby
# granted. No representations are made about the suitability of this software
# for any purpose. It is provided "as is" without express or implied warranty.
# See the GNU General Public License for more details.
#
# Documents produced by Doxygen are derivative works derived from the
# input used in their production; they are not affected by this license.
#
from __future__ import print_function
from os         import listdir, stat, walk
from os.path    import isfile, join, splitext
import sys

class File(object):
	def __init__(self,directory,subdir,fileName,mode):
		self.directory = directory
		self.subdir    = subdir
		self.fileName  = fileName
		filePath  = join(directory,subdir,fileName)
		self.fileSize  = stat(filePath).st_size
		self.bareName  = fileName.replace('.','_')
		self.inputFile = open(filePath,mode)

	def formatByte(self,byte):
		if isinstance(byte,int):
			return "%02x" % byte
		else:
			return format(ord(byte),'02x')

	def writeBytes(self,data,outputFile):
		bytes_per_line=16
		print("static const unsigned char %s_data[] = " % self.bareName,file=outputFile)
		print("{",file=outputFile)
		lines = [data[x:x+bytes_per_line] for x in range(0,len(data),bytes_per_line)]
		linesAsString = ',\n  '.join([', '.join(['0x'+self.formatByte(byte) for byte in line]) for line in lines])
		print('  %s' % linesAsString,file=outputFile)
		print("};",file=outputFile)
		print("const int %s_len = %d;\n" % (self.bareName,len(data)),file=outputFile)

	def convertToBytes(self,outputFile):
		lines = [x for x in self.inputFile.readlines() if not x.startswith('#')]
		w,h = (int(x) for x in lines[0].split())
		data = "".join(map(chr,[int(w>>8),int(w&0xFF),int(h>>8),int(h&0xFF)]+
		                       [int(x) for line in lines[1:] for x in line.split()]))
		self.writeBytes(data,outputFile)

	@staticmethod
	def factory(directory,subdir,fname):
		ext = splitext(fname)[1]
		if ext=='.lum':  return LumFile(directory,subdir,fname)
		if ext=='.luma': return LumaFile(directory,subdir,fname)
		if ext=='.css':  return CSSFile(directory,subdir,fname)
		if ext=='.svg':  return SVGFile(directory,subdir,fname)
		return VerbatimFile(directory,subdir,fname)

class VerbatimFile(File):
	def __init__(self,directory,subdir,fileName):
		File.__init__(self,directory,subdir,fileName,"rb")
	def writeContents(self,outputFile):
		self.writeBytes(self.inputFile.read(),outputFile)
	def writeDirEntry(self,outputFile):
		print("  { \"%s\", \"%s\", %s_data, %s_len, Resource::Verbatim }," % (self.subdir,self.fileName,self.bareName,self.bareName), file=outputFile)

class CSSFile(File):
	def __init__(self,directory,subdir,fileName):
		File.__init__(self,directory,subdir,fileName,"r")
	def writeContents(self,outputFile):
		self.writeBytes(self.inputFile.read(),outputFile)
	def writeDirEntry(self,outputFile):
		print("  { \"%s\", \"%s\", %s_data, %s_len, Resource::CSS }," % (self.subdir,self.fileName,self.bareName,self.bareName), file=outputFile)

class SVGFile(File):
	def __init__(self,directory,subdir,fileName):
		File.__init__(self,directory,subdir,fileName,"r")
	def writeContents(self,outputFile):
		self.writeBytes(self.inputFile.read(),outputFile)
	def writeDirEntry(self,outputFile):
		print("  { \"%s\", \"%s\", %s_data, %s_len, Resource::SVG }," % (self.subdir,self.fileName,self.bareName,self.bareName), file=outputFile)

class LumFile(File):
	def __init__(self,directory,subdir,fileName):
		File.__init__(self,directory,subdir,fileName,"r")
	def writeContents(self,outputFile):
		self.convertToBytes(outputFile)
	def writeDirEntry(self,outputFile):
		print("  { \"%s\", \"%s\", %s_data, %s_len, Resource::Luminance }," % (self.subdir,self.fileName,self.bareName,self.bareName), file=outputFile)

class LumaFile(File):
	def __init__(self,directory,subdir,fileName):
		File.__init__(self,directory,subdir,fileName,"r")
	def writeContents(self,outputFile):
		self.convertToBytes(outputFile)
	def writeDirEntry(self,outputFile):
		print("  { \"%s\", \"%s\", %s_data, %s_len, Resource::LumAlpha }," % (self.subdir,self.fileName,self.bareName,self.bareName), file=outputFile)

def main():
	if len(sys.argv)<3:
		sys.exit('Usage: %s directory output_file.cpp' % sys.argv[0])
	directory = sys.argv[1]
	files = []
	for dirName, subdirList, fileList in walk(directory):
		for fname in fileList:
			subdir = dirName[len(directory)+1:] if dirName.startswith(directory) else dirName
			if subdir:
				files.append(File.factory(directory,subdir,fname))
	files.sort(key=lambda f: f.subdir + "/" + f.fileName)
	outputFile = open(sys.argv[2],"w")
	print("#include \"resourcemgr.h\"\n",file=outputFile)
	for f in files:
		f.writeContents(outputFile)
	print("void initResources() { ResourceMgr::instance().registerResources({",file=outputFile)
	for f in files:
		f.writeDirEntry(outputFile)
	print("});}",file=outputFile)

if __name__ == '__main__':
	main()