-const Attr* NeedAttr (const Collection* C, const char* Name, const char* Context)
+const Attr* NeedAttr (const Collection* C, const char* Name, const char* Op)
/* Search for an attribute with the given name and return it. If the attribute
- * is not found, the function terminates with an error using Context as
- * additional context in the error message.
+ * is not found, the function terminates with an error using Op as additional
+ * context in the error message.
*/
{
/* Search for the attribute and return it */
unsigned Index;
if (!FindAttr (C, Name, &Index)) {
- Error ("Found no attribute named `%s' in %s", Name, Context);
+ Error ("Found no attribute named `%s' for operation %s", Name, Op);
}
return CollConstAt (C, Index);
}
-const char* NeedAttrVal (const Collection* C, const char* Name, const char* Context)
+const char* NeedAttrVal (const Collection* C, const char* Name, const char* Op)
/* Search for an attribute with the given name and return its value. If the
* attribute wasn't not found, the function terminates with an error using
- * Context as additional context in the error message.
+ * Op as additional context in the error message.
*/
{
- const Attr* A = NeedAttr (C, Name, Context);
+ const Attr* A = NeedAttr (C, Name, Op);
return (A == 0)? 0 : A->Value;
}
* returns NULL if the attribute wasn't found.
*/
-const Attr* NeedAttr (const Collection* C, const char* Name, const char* Context);
+const Attr* NeedAttr (const Collection* C, const char* Name, const char* Op);
/* Search for an attribute with the given name and return it. If the attribute
- * is not found, the function terminates with an error using Context as
- * additional context in the error message.
+ * is not found, the function terminates with an error using Op as additional
+ * context in the error message.
*/
const char* GetAttrVal (const Collection* C, const char* Name);
* function returns NULL if the attribute wasn't found.
*/
-const char* NeedAttrVal (const Collection* C, const char* Name, const char* Context);
+const char* NeedAttrVal (const Collection* C, const char* Name, const char* Op);
/* Search for an attribute with the given name and return its value. If the
* attribute wasn't not found, the function terminates with an error using
- * Context as additional context in the error message.
+ * Op as additional context in the error message.
*/
void AddAttr (Collection* C, const char* Name, const char* Value);
+#include <stdlib.h>
+
/* sp65 */
+#include "attr.h"
#include "convert.h"
+#include "error.h"
#include "koala.h"
#include "vic2sprite.h"
+/*****************************************************************************/
+/* Data */
+/*****************************************************************************/
+
+
+
+/* Type of the entry in the converter table */
+typedef struct ConverterMapEntry ConverterMapEntry;
+struct ConverterMapEntry {
+ const char* Format;
+ StrBuf* (*ConvertFunc) (const Bitmap*, const Collection*);
+};
+
+/* Converter table, alphabetically sorted */
+static const ConverterMapEntry ConverterMap[] = {
+ { "koala", GenKoala },
+ { "vic2-sprite", GenVic2Sprite },
+};
+
+
+
/*****************************************************************************/
/* Code */
/*****************************************************************************/
+static int Compare (const void* Key, const void* MapEntry)
+/* Compare function for bsearch */
+{
+ return strcmp (Key, ((const ConverterMapEntry*) MapEntry)->Format);
+}
+
+
+
StrBuf* ConvertTo (const Bitmap* B, const Collection* A)
/* Convert the bitmap B into some sort of other binary format. The output is
* stored in a string buffer (which is actually a dynamic char array) and
* returned. The actual output format is taken from the "format" attribute
* in the attribute collection A.
- */
+ */
{
+ const ConverterMapEntry* E;
+
+ /* Get the format to convert to */
+ const char* Format = NeedAttrVal (A, "format", "convert");
+
+ /* Search for the matching converter */
+ E = bsearch (Format,
+ ConverterMap,
+ sizeof (ConverterMap) / sizeof (ConverterMap[0]),
+ sizeof (ConverterMap[0]),
+ Compare);
+ if (E == 0) {
+ Error ("No such target format: `%s'", Format);
+ }
+
+ /* Do the conversion */
+ return E->ConvertFunc (B, A);
}
}
/* Write the file */
- WriteOutputFile (FileName, 0, OF);
+ WriteOutputFile (FileName, D, OF);
/* Delete the attribute list */
FreeCollection (A);
GetBitmapColors (B) != 2 ||
GetBitmapHeight (B) != HEIGHT ||
GetBitmapWidth (B) != WIDTH) {
-
+
+ printf ("w = %u, h = %u\n", GetBitmapWidth (B), GetBitmapHeight (B));
Error ("Bitmaps converted to vic2 sprite format must be in indexed "
- "mode with 2 colors max and a size of %ux%u", WIDTH, HEIGHT);
+ "mode with a size of %ux%u and two colors", WIDTH, HEIGHT);
}
/* Create the output buffer and resize it to the required size. */