git.y1.nz

gbdk-2020

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

gbdk-support/romusage/src/banks_color.c

      1 // This is free and unencumbered software released into the public domain.
      2 // For more information, please refer to <https://unlicense.org>
      3 // bbbbbr 2022
      4 
      5 #include <stdio.h>
      6 #include <string.h>
      7 #include <stdlib.h>
      8 #include <stdbool.h>
      9 #include <stdint.h>
     10 
     11 #include "common.h"
     12 #include "banks_color.h"
     13 
     14 
     15 #ifdef _WIN32
     16   #ifndef _WIN32
     17      #define __WIN32__
     18   #endif
     19 #endif
     20 
     21 // For GetConsoleMode()/etc..
     22 #ifdef __WIN32__
     23     #include <windows.h>
     24 #endif
     25 
     26 #ifdef __WIN32__
     27 // Enables Windows virtual terminal sequences for handling VT escape codes
     28 // MS recommends this over SetConsoleTextAttribute()
     29 bool colors_try_windows_enable_virtual_term_for_vt_codes(void) {
     30 
     31     // Set output mode to handle virtual terminal sequences
     32     HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
     33     if (hOut != INVALID_HANDLE_VALUE) {
     34 
     35         DWORD dwMode = 0;
     36         if (GetConsoleMode(hOut, &dwMode)) {
     37 
     38             dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
     39             if (SetConsoleMode(hOut, dwMode)) {
     40                 return true; // success
     41             }
     42         }
     43     }
     44     return false; // failure
     45 }
     46 #endif
     47 
     48 
     49 // Default colots
     50 color_pal_t bank_colors = {
     51     .default_color = PRINT_COLOR_DEFAULT,
     52     .rom     = PRINT_COLOR_ROM_DEFAULT,
     53     .vram    = PRINT_COLOR_VRAM_DEFAULT,
     54     .sram    = PRINT_COLOR_SRAM_DEFAULT,
     55     .wram    = PRINT_COLOR_WRAM_DEFAULT,
     56     .hram    = PRINT_COLOR_HRAM_DEFAULT,
     57 };
     58 
     59 
     60 static uint8_t bank_get_color(bank_item * p_bank) {
     61 
     62     uint8_t color_esc_code = bank_colors.default_color;
     63 
     64     if (get_option_percentage_based_color()) {
     65 
     66         int perc_free = bank_calc_percent_free(p_bank);
     67 
     68         // Color ramp from Default -> Green -> Yellow -> Red
     69         if      (perc_free == 0)  color_esc_code = VT_COLOR_RED_LIGHT;
     70         else if (perc_free <= 10) color_esc_code = VT_COLOR_YELLOW_LIGHT;
     71         else if (perc_free <= 25) color_esc_code = VT_COLOR_GREEN_LIGHT;
     72         // implied: else if (> 25) PRINT_COLOR_DEFAULT
     73     }
     74     else {
     75         switch (p_bank->bank_mem_type) {
     76             case BANK_MEM_TYPE_ROM:  color_esc_code = bank_colors.rom;  break;
     77             case BANK_MEM_TYPE_VRAM: color_esc_code = bank_colors.vram; break;
     78             case BANK_MEM_TYPE_SRAM: color_esc_code = bank_colors.sram; break;
     79             case BANK_MEM_TYPE_WRAM: color_esc_code = bank_colors.wram; break;
     80             case BANK_MEM_TYPE_HRAM: color_esc_code = bank_colors.hram; break;
     81             default: break;
     82         }
     83     }
     84 
     85     return color_esc_code;
     86 }
     87 
     88 
     89 // Write out the actual color/etc escape codes
     90 static void write_out_color_code(uint8_t esc_num) {
     91 
     92     #ifdef __WIN32__
     93 
     94         // Instead of Dim/Dim Off VT100 codes Windows virtual terminal uses
     95         // Bright/Bright Off with basically inverted meaning for escape code 22.
     96         // So flip them:
     97         //   Dim ON  (2)  -> Bright OFF (22)
     98         //   Dim OFF (22) -> Bright ON  (1)
     99         if      (esc_num == VT_ATTR_DIM)       esc_num = WINCON_ATTR_BRIGHT_RESET;
    100         else if (esc_num == VT_ATTR_DIM_RESET) esc_num = WINCON_ATTR_BRIGHT;
    101 
    102         printf("\x1b[%dm", esc_num);
    103     #else // VT100 compatible
    104         printf("\x1b[%dm", esc_num);
    105     #endif
    106 }
    107 
    108 
    109 // Writes out a color escape code based on current printing region and bank type
    110 void bank_render_color(bank_item * p_bank, int mode) {
    111 
    112     if (get_option_color_mode() != OPT_PRINT_COLOR_OFF) {
    113 
    114         switch(mode) {
    115             // Start row colorizing
    116             case PRINT_REGION_ROW_START:
    117                 write_out_color_code(bank_get_color(p_bank));
    118                 break;
    119 
    120             // Based on mode, either set to dim-color, turn off coloring, or continue coloring (do nothing)
    121             case PRINT_REGION_ROW_MIDDLE_START:
    122                 if (get_option_color_mode() == OPT_PRINT_COLOR_WHOLE_ROW_DIMMED) write_out_color_code(VT_ATTR_DIM);
    123                 else if (get_option_color_mode() == OPT_PRINT_COLOR_ROW_ENDS)    write_out_color_code(VT_ATTR_ALL_RESET);
    124                 // implied: else ( == OPT_PRINT_COLOR_WHOLE_ROW) : Don't change attributes in middle
    125                 break;
    126 
    127             // Based on mode, either turn off dim-color, turn coloring back on, or continue coloring (do nothing)
    128             case PRINT_REGION_ROW_MIDDLE_END:
    129                 if (get_option_color_mode() == OPT_PRINT_COLOR_WHOLE_ROW_DIMMED) write_out_color_code(VT_ATTR_DIM_RESET);
    130                 else if (get_option_color_mode() == OPT_PRINT_COLOR_ROW_ENDS)    write_out_color_code(bank_get_color(p_bank));
    131                 // implied: else ( == OPT_PRINT_COLOR_WHOLE_ROW) : Don't change attributes in middle
    132                 break;
    133 
    134             // Start end colorizing
    135             case PRINT_REGION_ROW_END:
    136                 write_out_color_code(VT_ATTR_ALL_RESET);
    137                 break;
    138         }
    139     }
    140 }
    141 
    142 
    143 
    144 #define MAX_SPLIT_WORDS 8
    145 #define ARG_COLORS_CUSTOM_PAL 7u
    146 
    147 // -sP : Custom Color Palette. Each colon separated entry is decimal VT100 color code
    148 //       -sP:DEFAULT:ROM:VRAM:SRAM:WRAM:HRAM
    149 //
    150 // Custom color scheme for output
    151 bool set_option_custom_bank_colors(char * arg_str) {
    152 
    153     int cols;
    154     char * p_str;
    155     char * p_words[MAX_SPLIT_WORDS];
    156     area_item area;
    157 
    158     // Split string into words separated by - and : chars
    159     cols = 0;
    160     p_str = strtok(arg_str,"-:");
    161     while (p_str != NULL)
    162     {
    163         p_words[cols++] = p_str;
    164         p_str = strtok(NULL, "-:");
    165         if (cols >= MAX_SPLIT_WORDS) break;
    166     }
    167 
    168     if (cols == ARG_COLORS_CUSTOM_PAL) {
    169         bank_colors.default_color = strtol(p_words[1], NULL, 10);
    170         bank_colors.rom     = strtol(p_words[2], NULL, 10);
    171         bank_colors.vram    = strtol(p_words[3], NULL, 10);
    172         bank_colors.sram    = strtol(p_words[4], NULL, 10);
    173         bank_colors.wram    = strtol(p_words[5], NULL, 10);
    174         bank_colors.hram    = strtol(p_words[6], NULL, 10);
    175 
    176         return true;
    177     } else
    178         return false; // Signal failure
    179 }

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