From: uz Date: Sat, 10 Mar 2012 14:22:51 +0000 (+0000) Subject: Move attribute lookup into the output functions. Allow a bytesperline X-Git-Tag: V2.14~474 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=c140a15dac72a1b33893c65a06efaedf29784412;p=cc65 Move attribute lookup into the output functions. Allow a bytesperline attribute for asm output files. git-svn-id: svn://svn.cc65.org/cc65/trunk@5589 b7a2c559-68d2-44c3-8de9-860c34a00d81 --- diff --git a/src/sp65/asm.c b/src/sp65/asm.c index 73721c17f..e95bfc664 100644 --- a/src/sp65/asm.c +++ b/src/sp65/asm.c @@ -42,6 +42,7 @@ #include "version.h" /* sp65 */ +#include "attr.h" #include "bin.h" #include "error.h" @@ -53,14 +54,28 @@ -void WriteAsmFile (const char* Name, const StrBuf* Data) +void WriteAsmFile (const StrBuf* Data, const Collection* A) /* Write the contents of Data to the given file in assembler (ca65) format */ { + FILE* F; const char* D; unsigned Size; + unsigned BytesPerLine = 16; + char C; + + + /* Get the file name */ + const char* Name = NeedAttrVal (A, "name", "write"); + + /* Check for a bytesperline attribute */ + const char* V = GetAttrVal (A, "bytesperline"); + if ((V && sscanf (V, "%u%c", &BytesPerLine, &C) != 1) || + (BytesPerLine < 1 || BytesPerLine > 64)) { + Error ("Invalid value for attribute `bytesperline'"); + } /* Open the output file */ - FILE* F = fopen (Name, "w"); + F = fopen (Name, "w"); if (F == 0) { Error ("Cannot open output file `%s': %s", Name, strerror (errno)); } @@ -83,8 +98,8 @@ void WriteAsmFile (const char* Name, const StrBuf* Data) /* Output one line */ unsigned Chunk = Size; - if (Chunk > 16) { - Chunk = 16; + if (Chunk > BytesPerLine) { + Chunk = BytesPerLine; } fprintf (F, " .byte $%02X", (*D++ & 0xFF)); for (I = 1; I < Chunk; ++I) { diff --git a/src/sp65/asm.h b/src/sp65/asm.h index 6fef885f7..03b9b2f8f 100644 --- a/src/sp65/asm.h +++ b/src/sp65/asm.h @@ -39,6 +39,7 @@ /* common */ +#include "coll.h" #include "strbuf.h" @@ -49,8 +50,8 @@ -void WriteAsmFile (const char* Name, const StrBuf* Data); -/* Write the contents of Data to the given file in assembler (ca65) format */ +void WriteAsmFile (const StrBuf* Data, const Collection* A); +/* Write the contents of Data to a file in assembler (ca65) format */ diff --git a/src/sp65/bin.c b/src/sp65/bin.c index 6aded16e1..d698ae4ba 100644 --- a/src/sp65/bin.c +++ b/src/sp65/bin.c @@ -38,6 +38,7 @@ #include /* sp65 */ +#include "attr.h" #include "bin.h" #include "error.h" @@ -49,11 +50,14 @@ -void WriteBinFile (const char* Name, const StrBuf* Data) +void WriteBinFile (const StrBuf* Data, const Collection* A) /* Write the contents of Data to the given file in binary format */ { unsigned Size; + /* Get the file name */ + const char* Name = NeedAttrVal (A, "name", "write"); + /* Open the output file */ FILE* F = fopen (Name, "wb"); if (F == 0) { diff --git a/src/sp65/bin.h b/src/sp65/bin.h index 3bfb5a440..363111889 100644 --- a/src/sp65/bin.h +++ b/src/sp65/bin.h @@ -39,6 +39,7 @@ /* common */ +#include "coll.h" #include "strbuf.h" @@ -49,8 +50,8 @@ -void WriteBinFile (const char* Name, const StrBuf* Data); -/* Write the contents of Data to the given file in binary format */ +void WriteBinFile (const StrBuf* Data, const Collection* A); +/* Write the contents of Data to a file in binary format */ diff --git a/src/sp65/main.c b/src/sp65/main.c index fa1d1d658..f243ce3fe 100644 --- a/src/sp65/main.c +++ b/src/sp65/main.c @@ -272,7 +272,7 @@ static void OptVersion (const char* Opt attribute ((unused)), -static void OptWrite (const char* Opt, const char* Arg) +static void OptWrite (const char* Opt attribute ((unused)), const char* Arg) /* Write an output file */ { static const char* NameList[] = { @@ -283,21 +283,8 @@ static void OptWrite (const char* Opt, const char* Arg) /* Parse the argument */ Collection* A = ParseAttrList (Arg, NameList, 2); - /* Must have a file name given */ - const char* FileName = NeedAttrVal (A, "name", Opt); - - /* Determine the format of the input file */ - int OF = ofAuto; - const char* Format = GetAttrVal (A, "format"); - if (Format != 0) { - OF = FindOutputFormat (Format); - if (OF < 0) { - Error ("Unknown output format `%s'", Format); - } - } - /* Write the file */ - WriteOutputFile (FileName, D, OF); + WriteOutputFile (D, A); /* Delete the attribute list */ FreeCollection (A); diff --git a/src/sp65/output.c b/src/sp65/output.c index 0dbe52825..4726a5c6f 100644 --- a/src/sp65/output.c +++ b/src/sp65/output.c @@ -40,6 +40,7 @@ /* sp65 */ #include "asm.h" +#include "attr.h" #include "bin.h" #include "error.h" #include "output.h" @@ -56,7 +57,7 @@ typedef struct OutputFormatDesc OutputFormatDesc; struct OutputFormatDesc { /* Write routine */ - void (*Write) (const char* Name, const StrBuf* Data); + void (*Write) (const StrBuf*, const Collection*); }; @@ -90,46 +91,39 @@ static const FileId FormatTable[] = { -int FindOutputFormat (const char* Name) -/* Find an output format by name. The function returns a value less than zero - * if Name is not a known output format. +void WriteOutputFile (const StrBuf* Data, const Collection* A) +/* Write the contents of Data to a file. Format, file name etc. must be given + * as attributes in A. If no format is given, the function tries to autodetect + * it by using the extension of the file name. */ { - /* Search for the entry in the table. */ - const FileId* F = bsearch (Name, - FormatTable, - sizeof (FormatTable) / sizeof (FormatTable[0]), - sizeof (FormatTable[0]), - CompareFileId); - - /* Return the id or an error code */ - return (F == 0)? -1 : F->Id; -} - - - -void WriteOutputFile (const char* Name, const StrBuf* Data, OutputFormat Format) -/* Write the contents of Data to the given file in the format specified. If - * the format is ofAuto, it is determined by the file extension. - */ -{ - /* If the format is Auto, try to determine it from the file name */ - if (Format == ofAuto) { - /* Search for the entry in the table */ - const FileId* F = GetFileId (Name, FormatTable, - sizeof (FormatTable) / sizeof (FormatTable[0])); + const FileId* F; + + /* Get the file format from the command line */ + const char* Format = GetAttrVal (A, "format"); + if (Format != 0) { + /* Format is given, search for it in the table. */ + F = bsearch (Format, + FormatTable, + sizeof (FormatTable) / sizeof (FormatTable[0]), + sizeof (FormatTable[0]), + CompareFileId); + if (F == 0) { + Error ("Unknown output format `%s'", Format); + } + } else { + /* No format given, use file name extension */ + const char* Name = NeedAttrVal (A, "name", "write"); + F = GetFileId (Name, FormatTable, + sizeof (FormatTable) / sizeof (FormatTable[0])); /* Found? */ if (F == 0) { Error ("Cannot determine file format of output file `%s'", Name); } - Format = F->Id; } - /* Check the format just for safety */ - CHECK (Format >= 0 && Format < ofCount); - /* Call the format specific write */ - OutputFormatTable[Format].Write (Name, Data); + OutputFormatTable[F->Id].Write (Data, A); } diff --git a/src/sp65/output.h b/src/sp65/output.h index 9b564b59d..abedd011e 100644 --- a/src/sp65/output.h +++ b/src/sp65/output.h @@ -67,14 +67,10 @@ typedef enum OutputFormat OutputFormat; -int FindOutputFormat (const char* Name); -/* Find an output format by name. The function returns a value less than zero - * if Name is not a known output format. - */ - -void WriteOutputFile (const char* Name, const StrBuf* Data, OutputFormat Format); -/* Write the contents of Data to the given file in the format specified. If - * the format is ofAuto, it is determined by the file extension. +void WriteOutputFile (const StrBuf* Data, const Collection* A); +/* Write the contents of Data to a file. Format, file name etc. must be given + * as attributes in A. If no format is given, the function tries to autodetect + * it by using the extension of the file name. */ diff --git a/src/sp65/vic2sprite.c b/src/sp65/vic2sprite.c index fa3a7359d..371b9616b 100644 --- a/src/sp65/vic2sprite.c +++ b/src/sp65/vic2sprite.c @@ -35,6 +35,7 @@ /* common */ #include "attrib.h" +#include "print.h" /* sp65 */ #include "error.h" @@ -69,13 +70,18 @@ StrBuf* GenVic2Sprite (const Bitmap* B, const Collection* A attribute ((unused)) StrBuf* D; unsigned X, Y; + + /* Output the image properties */ + Print (stdout, 1, "Image is %ux%u with %u colors%s\n", + GetBitmapWidth (B), GetBitmapHeight (B), GetBitmapColors (B), + (GetBitmapType (B) == bmIndexed)? " (indexed)" : ""); + /* Sprites pictures are always 24x21 in size with 2 colors */ if (GetBitmapType (B) != bmIndexed || 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 a size of %ux%u and two colors", WIDTH, HEIGHT); }