1 /*****************************************************************************/
5 /* C output for the sp65 sprite and bitmap utility */
9 /* (C) 2012, Ullrich von Bassewitz */
10 /* Roemerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
15 /* This software is provided 'as-is', without any expressed or implied */
16 /* warranty. In no event will the authors be held liable for any damages */
17 /* arising from the use of this software. */
19 /* Permission is granted to anyone to use this software for any purpose, */
20 /* including commercial applications, and to alter it and redistribute it */
21 /* freely, subject to the following restrictions: */
23 /* 1. The origin of this software must not be misrepresented; you must not */
24 /* claim that you wrote the original software. If you use this software */
25 /* in a product, an acknowledgment in the product documentation would be */
26 /* appreciated but is not required. */
27 /* 2. Altered source versions must be plainly marked as such, and must not */
28 /* be misrepresented as being the original software. */
29 /* 3. This notice may not be removed or altered from any source */
32 /*****************************************************************************/
53 /*****************************************************************************/
55 /*****************************************************************************/
59 static int ValidIdentifier (const char* L)
60 /* Check a C identifier for validity */
62 /* Must begin with underscore or alphabetic character */
63 if (*L != '_' && !IsAlpha (*L)) {
68 /* Remainder must be as above plus digits */
70 if (*L != '_' && !IsAlNum (*L)) {
82 static unsigned GetBytesPerLine (const Collection* A)
83 /* Return the number of bytes per line from the attribute collection A */
86 unsigned BytesPerLine = 16;
88 /* Check for a bytesperline attribute */
89 const char* V = GetAttrVal (A, "bytesperline");
90 if ((V && sscanf (V, "%u%c", &BytesPerLine, &C) != 1) ||
91 (BytesPerLine < 1 || BytesPerLine > 64)) {
92 Error ("Invalid value for attribute `bytesperline'");
99 static unsigned GetBase (const Collection* A)
100 /* Return the number base from the attribute collection A */
105 /* Check for a base attribute */
106 const char* V = GetAttrVal (A, "base");
107 if ((V && sscanf (V, "%u%c", &Base, &C) != 1) || (Base != 10 && Base != 16)) {
108 Error ("Invalid value for attribute `base'");
115 static const char* GetIdentifier (const Collection* A)
116 /* Return the variable identifier from the attribute collection A */
118 /* Check for a ident attribute */
119 const char* Ident = GetAttrVal (A, "ident");
120 if (Ident && !ValidIdentifier (Ident)) {
121 Error ("Invalid value for attribute `ident'");
128 void WriteCFile (const StrBuf* Data, const Collection* A, const Bitmap* B)
129 /* Write the contents of Data to a file in C format */
136 /* Get the name of the image */
137 const StrBuf* S = GetBitmapName (B);
139 /* Get the file name */
140 const char* Name = NeedAttrVal (A, "name", "write");
142 /* Check the number of bytes per line */
143 unsigned BytesPerLine = GetBytesPerLine (A);
145 /* Get the number base */
146 unsigned Base = GetBase (A);
148 /* Get the identifier */
149 const char* Ident = GetIdentifier (A);
151 /* Open the output file */
152 F = fopen (Name, "w");
154 Error ("Cannot open output file `%s': %s", Name, strerror (errno));
157 /* Write a readable header */
160 " * This file was generated by %s %s from\n"
161 " * %.*s (%ux%u, %u colors%s)\n"
165 GetVersionAsString (),
166 SB_GetLen (S), SB_GetConstBuf (S),
167 GetBitmapWidth (B), GetBitmapHeight (B),
169 BitmapIsIndexed (B)? ", indexed" : "");
171 /* If an identifier was given, output #defines for width, height, the
172 * number of colors and declare a variable for the data.
176 "#define %s_COLORS %u\n"
177 "#define %s_WIDTH %u\n"
178 "#define %s_HEIGHT %u\n"
179 "const unsigned char %s[] = {\n",
180 Ident, GetBitmapColors (B),
181 Ident, GetBitmapWidth (B),
182 Ident, GetBitmapHeight (B),
187 D = SB_GetConstBuf (Data);
188 Size = SB_GetLen (Data);
193 /* Output one line */
194 unsigned Chunk = Size;
195 if (Chunk > BytesPerLine) {
196 Chunk = BytesPerLine;
199 for (I = 0; I < Chunk; ++I) {
202 fprintf (F, "%u,", *D++ & 0xFF);
205 fprintf (F, "0x%02X,", *D++ & 0xFF);
212 /* Bump the counters */
216 /* Terminate the array if we had an identifier */
222 if (fclose (F) != 0) {
223 Error ("Error closing output file `%s': %s", Name, strerror (errno));