git.y1.nz

gbdk-2020

GameBoy Development Kit
download: https://git.y1.nz/archives/gbdk.tar.gz
README | Files | Log | Refs | LICENSE

gbdk-support/png2hicolorgb/src/c_source.c

      1 // See LICENSE  file for license details
      2 
      3 #include <stdio.h>
      4 #include <string.h>
      5 #include <stdlib.h>
      6 #include <stdbool.h>
      7 #include <stdint.h>
      8 
      9 #include "common.h"
     10 #include "image_info.h"
     11 #include "path_ops.h"
     12 #include "logging.h"
     13 #include "options.h"
     14 
     15 
     16 // Writes a buffer to a file in C source format
     17 // Adds a matching .h if possible
     18 //
     19 bool file_c_output_write(const char * fname_base, const char * varname_in, int bank_num, int tile_count, int height_in_tiles) {
     20 
     21     char varname_withpath[MAX_PATH * 2];
     22     char filename_c[MAX_PATH * 2];
     23     char filename_h[MAX_PATH * 2];
     24     strcpy(varname_withpath, varname_in);
     25     strcpy(filename_c, fname_base);
     26     strcpy(filename_h, fname_base);
     27     strcat(filename_c, ".c");
     28     strcat(filename_h, ".h");
     29 
     30     // Fix up varname if needed to be usable in the C source file
     31     //
     32     // If varname arg wasn't specified it will be derived from
     33     // filename out and may include path that needs to be stripped.
     34     char * varname = (char *)get_filename_from_path(varname_withpath);
     35     char * ch = varname;
     36     while (*ch != '\0') {
     37         if ((*ch == ' ') || (*ch == '-')) *ch = '_';
     38         ch++;
     39     }
     40 
     41     VERBOSE("Writing C format to: %s, %s\n", filename_c, filename_h);
     42 
     43     // === C Source output ===
     44     FILE * file_out_c = fopen(filename_c, "w");
     45     if (file_out_c) {
     46 
     47         fprintf(file_out_c,
     48             "#include <gbdk/platform.h>\n"
     49             "#include <gbdk/incbin.h>\n\n");
     50 
     51         // If Bank Num is set add pragma
     52         if (bank_num != BANK_NUM_UNSET) {
     53             fprintf(file_out_c, "#pragma bank %d\n", bank_num);
     54         }
     55         fprintf(file_out_c, "BANKREF(%s)\n\n", varname);
     56 
     57         fprintf(file_out_c,
     58             "#include <gbc_hicolor.h>\n"
     59             "\n"
     60             "// Generated by png2hicolorgb\n"
     61             "\n"
     62             "// Note: filepath is relative to the working directory of the tool that is calling it (often a makefile's working directory), NOT to the file it's being included into.\n"
     63             "\n"
     64             "INCBIN(%s_tiles,   \"%s.til\")\n"
     65             "INCBIN(%s_map,     \"%s.map\")\n"
     66             "INCBIN(%s_attr,    \"%s.atr\")\n"
     67             "INCBIN(%s_palette, \"%s.pal\")\n"
     68             "\n"
     69             "INCBIN_EXTERN(%s_tiles)\n"
     70             "INCBIN_EXTERN(%s_map)\n"
     71             "INCBIN_EXTERN(%s_attr)\n"
     72             "INCBIN_EXTERN(%s_palette)\n"
     73             "\n"
     74             "const hicolor_data %s_data = {\n"
     75             "    .tile_count      = %du,\n"
     76             "    .height_in_tiles = %du,\n"
     77             "    .p_tiles         = %s_tiles,\n"
     78             "    .p_map           = %s_map,\n"
     79             "    .p_attribute_map = %s_attr,\n"
     80             "    .p_palette       = %s_palette\n"
     81             "};\n"
     82             "\n"
     83         ,varname, fname_base,
     84          varname, fname_base,
     85          varname, fname_base,
     86          varname, fname_base,
     87          varname, varname, varname, varname,
     88          varname,
     89          tile_count, height_in_tiles,
     90          varname, varname, varname, varname
     91         );
     92         fclose(file_out_c);
     93     } else {
     94         printf("Error: Failed to open output file: %s\n", filename_c);
     95         return false;
     96     }
     97 
     98     // === Header output ===
     99     FILE * file_out_h = fopen(filename_h, "w");
    100     if (file_out_h) {
    101 
    102         fprintf(file_out_h, "#include <gbdk/platform.h>\n\n");
    103         fprintf(file_out_h, "BANKREF_EXTERN(%s)\n\n", varname);
    104 
    105         fprintf(file_out_h,
    106             "#ifndef __%s_h_\n"
    107             "#define __%s_h_\n"
    108             "\n"
    109             "#include <gbc_hicolor.h>\n"
    110             "\n"
    111             "// Generated by png2hicolorgb\n"
    112             "\n"
    113             "extern const hicolor_data %s_data;\n"
    114             "\n"
    115             "#endif\n"
    116             "\n"
    117             ,varname, varname,
    118              // varname, varname, varname, varname,
    119              varname
    120         );
    121         fclose(file_out_h);
    122     } else {
    123         printf("Error: Failed to open output file: %s\n", filename_h);
    124         return false;
    125     }
    126 
    127     return true;
    128 }

This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.