1
0
mirror of https://github.com/cc65/cc65.git synced 2025-01-12 02:30:44 +00:00

Implemented main conversion module.

git-svn-id: svn://svn.cc65.org/cc65/trunk@5587 b7a2c559-68d2-44c3-8de9-860c34a00d81
This commit is contained in:
uz 2012-03-10 13:50:53 +00:00
parent 1aaff0ce2f
commit 7563f4096c
5 changed files with 67 additions and 16 deletions

View File

@ -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;
}

View File

@ -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);

View File

@ -33,19 +33,52 @@
#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
@ -53,6 +86,23 @@ StrBuf* ConvertTo (const Bitmap* B, const Collection* A)
* 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);
}

View File

@ -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);

View File

@ -75,8 +75,9 @@ StrBuf* GenVic2Sprite (const Bitmap* B, const Collection* A attribute ((unused))
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. */