From 7563f4096ca61bcb7be32779b7519fa9db1492dc Mon Sep 17 00:00:00 2001 From: uz Date: Sat, 10 Mar 2012 13:50:53 +0000 Subject: [PATCH] Implemented main conversion module. git-svn-id: svn://svn.cc65.org/cc65/trunk@5587 b7a2c559-68d2-44c3-8de9-860c34a00d81 --- src/sp65/attr.c | 14 ++++++------ src/sp65/attr.h | 10 ++++----- src/sp65/convert.c | 52 ++++++++++++++++++++++++++++++++++++++++++- src/sp65/main.c | 2 +- src/sp65/vic2sprite.c | 5 +++-- 5 files changed, 67 insertions(+), 16 deletions(-) diff --git a/src/sp65/attr.c b/src/sp65/attr.c index d3449f0a9..891a2d6ef 100644 --- a/src/sp65/attr.c +++ b/src/sp65/attr.c @@ -156,16 +156,16 @@ const Attr* GetAttr (const Collection* C, const char* Name) -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); } @@ -183,13 +183,13 @@ const char* GetAttrVal (const Collection* C, const char* Name) -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; } diff --git a/src/sp65/attr.h b/src/sp65/attr.h index b1a6b92be..9379a8020 100644 --- a/src/sp65/attr.h +++ b/src/sp65/attr.h @@ -90,10 +90,10 @@ const Attr* GetAttr (const Collection* C, const char* Name); * 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); @@ -101,10 +101,10 @@ 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); diff --git a/src/sp65/convert.c b/src/sp65/convert.c index 05dfce461..f66d4e7df 100644 --- a/src/sp65/convert.c +++ b/src/sp65/convert.c @@ -33,26 +33,76 @@ +#include + /* 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); } diff --git a/src/sp65/main.c b/src/sp65/main.c index 10523c7cf..fa1d1d658 100644 --- a/src/sp65/main.c +++ b/src/sp65/main.c @@ -297,7 +297,7 @@ static void OptWrite (const char* Opt, const char* Arg) } /* Write the file */ - WriteOutputFile (FileName, 0, OF); + WriteOutputFile (FileName, D, OF); /* Delete the attribute list */ FreeCollection (A); diff --git a/src/sp65/vic2sprite.c b/src/sp65/vic2sprite.c index 9c4c36864..fa3a7359d 100644 --- a/src/sp65/vic2sprite.c +++ b/src/sp65/vic2sprite.c @@ -74,9 +74,10 @@ StrBuf* GenVic2Sprite (const Bitmap* B, const Collection* A attribute ((unused)) 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. */ -- 2.39.2