git.y1.nz

gbdk-2020

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

gbdk-lib/examples/gb/hicolor/src/main.c

      1 #include <gbdk/platform.h>
      2 #include <stdint.h>
      3 #include <stdbool.h>
      4 
      5 #include <gbc_hicolor.h>
      6 
      7 // GBC HiColor images; header file names align with png file names
      8 #include "example_image.h"
      9 #include "test_pattern_tall.h"
     10 #include "test_pattern_short.h"
     11 
     12 
     13 #define BG_LAST_TILE  255u
     14 const uint8_t blank_tile[] = {0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0};
     15 
     16 #define ARRAY_LEN(A)  sizeof(A) / sizeof(A[0])
     17 
     18 uint8_t buttons, buttons_prev;
     19 #define UPDATE_BUTTONS()            (buttons_prev = buttons, buttons = joypad())
     20 #define BUTTON_TOGGLED(BUTTON_MASK) ((buttons & (~buttons_prev)) & (BUTTON_MASK))
     21 #define BUTTON_PRESSED(BUTTON_MASK) (buttons & (BUTTON_MASK))
     22 
     23 typedef struct far_ptr_t {
     24     uint8_t bank;
     25     const void * ptr;
     26 } far_ptr_t;
     27 
     28 // Array of pointers to the generated hicolor data structures
     29 const far_ptr_t hicolors[] = {
     30     { BANK(test_pattern_tall),  &HICOLOR_VAR(test_pattern_tall) },
     31     { BANK(example_image),      &HICOLOR_VAR(example_image) },
     32     { BANK(test_pattern_short), &HICOLOR_VAR(test_pattern_short) }
     33 };
     34 
     35 
     36 void main(void) {
     37     // Image toggling variable, by default show the "example_image"
     38     uint8_t  img_select = 0;
     39     bool     first_pass = true;
     40     uint8_t  scroll_limit = 0;
     41     const    hicolor_data * p_hicolor;
     42     uint8_t  hicolor_bank;
     43 
     44     SHOW_BKG;
     45 
     46     // Require Game Boy Color
     47     if (_cpu == CGB_TYPE) {
     48         // CGB running in the double speed mode is required
     49         cpu_fast();
     50 
     51         while(true) {
     52 
     53             vsync();
     54             UPDATE_BUTTONS();
     55 
     56             // Change displayed Hi Color image when pressing A or B
     57             if (BUTTON_TOGGLED(J_A | J_B) || first_pass) {
     58 
     59                 vsync();
     60                 DISPLAY_OFF;
     61 
     62                 // Set current image to show
     63                 hicolor_bank = hicolors[img_select].bank;
     64                 p_hicolor = (const hicolor_data *)hicolors[img_select].ptr;
     65 
     66                 uint8_t bank_save = CURRENT_BANK;
     67                 if (hicolor_bank) SWITCH_ROM(hicolor_bank);
     68 
     69                 // Reset Y scroll and set scroll range based on converted image height
     70                 SCY_REG = 0u;
     71                 if ((p_hicolor->height_in_tiles * 8u) > DEVICE_SCREEN_PX_HEIGHT)
     72                     scroll_limit = ((p_hicolor->height_in_tiles * 8u) - DEVICE_SCREEN_PX_HEIGHT);
     73                 else scroll_limit = 0;
     74 
     75                 // Optional:
     76                 // If the Hi Color image is shorter than screen height
     77                 // then fill the remaining screen area with a tile.
     78                 //
     79                 // Put the tile at the end of CGB tile pattern vram since
     80                 // the short Hi Color image will be too small to use all of it.
     81                 if ((p_hicolor->height_in_tiles * 8u) < DEVICE_SCREEN_PX_HEIGHT) {
     82                     VBK_REG = VBK_BANK_1;
     83                     set_bkg_data(BG_LAST_TILE, 1u, blank_tile);
     84                     fill_bkg_rect(0u, (p_hicolor->height_in_tiles), DEVICE_SCREEN_WIDTH, DEVICE_SCREEN_HEIGHT, BKGF_BANK1);
     85                     VBK_REG = VBK_BANK_0;
     86                     fill_bkg_rect(0u, (p_hicolor->height_in_tiles), DEVICE_SCREEN_WIDTH, DEVICE_SCREEN_HEIGHT, BG_LAST_TILE);
     87                 }
     88 
     89                 SWITCH_ROM(bank_save);
     90 
     91                 // Load and display the HiColor image
     92                 hicolor_start(p_hicolor, hicolor_bank);
     93 
     94                 DISPLAY_ON;
     95 
     96                 // Cycle through which image to show next
     97                 img_select++;
     98                 if (img_select == ARRAY_LEN(hicolors)) img_select = 0;
     99 
    100                 first_pass = false;
    101             }
    102             // Scroll Up/Down if available
    103             else if (BUTTON_PRESSED(J_UP)) {
    104                 if (SCY_REG) SCY_REG--;
    105             } else if (BUTTON_PRESSED(J_DOWN)) {
    106                 if (SCY_REG < scroll_limit) SCY_REG++;
    107             }
    108         }
    109     }
    110 }
    111 

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