]> git.sur5r.net Git - cc65/commitdiff
Move attribute lookup into the output functions. Allow a bytesperline
authoruz <uz@b7a2c559-68d2-44c3-8de9-860c34a00d81>
Sat, 10 Mar 2012 14:22:51 +0000 (14:22 +0000)
committeruz <uz@b7a2c559-68d2-44c3-8de9-860c34a00d81>
Sat, 10 Mar 2012 14:22:51 +0000 (14:22 +0000)
attribute for asm output files.

git-svn-id: svn://svn.cc65.org/cc65/trunk@5589 b7a2c559-68d2-44c3-8de9-860c34a00d81

src/sp65/asm.c
src/sp65/asm.h
src/sp65/bin.c
src/sp65/bin.h
src/sp65/main.c
src/sp65/output.c
src/sp65/output.h
src/sp65/vic2sprite.c

index 73721c17f7a2e1f8a9438a8ee53e71ba885ad89b..e95bfc664715a8e87d26dc745378e2e4f3b36200 100644 (file)
@@ -42,6 +42,7 @@
 #include "version.h"
 
 /* sp65 */
+#include "attr.h"
 #include "bin.h"
 #include "error.h"
 
 
 
 
-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) {
index 6fef885f7dc935eed8fd1e111a3fc13ea616b34d..03b9b2f8fd5377b82292657a6c3a196d6b7772b4 100644 (file)
@@ -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 */
 
 
 
index 6aded16e1608e4466f897776fb6c983d547508c1..d698ae4ba4d8f5bbd06ac7f9c989e44a1e1d4881 100644 (file)
@@ -38,6 +38,7 @@
 #include <string.h>
 
 /* sp65 */
+#include "attr.h"
 #include "bin.h"
 #include "error.h"
 
 
 
 
-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) {
index 3bfb5a440e239d00cf8a12a2b12bdc518b735fa1..363111889d91178da7d22e1fc8f4a0f2b83e90b8 100644 (file)
@@ -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 */
 
 
 
index fa1d1d6582c2c49e4c500ce7dee3b0952d6967d9..f243ce3fe83f7cb50c4a4acddbe037893b375935 100644 (file)
@@ -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);
index 0dbe5282507f06d2c29bffd149e43dba238f8e8f..4726a5c6f7f2212623e35b53c0a74baca05ef8ff 100644 (file)
@@ -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);
 }
 
 
index 9b564b59dbd76e8f15817ad93ec5c0852685c42b..abedd011eebdf1d7ee0b9e5c35c742c1dd56cf94 100644 (file)
@@ -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.
  */
 
 
index fa3a7359df9cd3bb1374a4b7684543181dec7fd5..371b9616bdeaffdbb6f06294fc3a05a706939921 100644 (file)
@@ -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);
     }