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/isr_vector/lcd_isr_wobble.c

      1 #include <gbdk/platform.h>
      2 #include <gbdk/incbin.h>
      3 #include <stdint.h>
      4 
      5 
      6 // Include for using ISR_VECTOR()
      7 #include <gb/isr.h>
      8 
      9 void init_gfx(void);
     10 
     11 // Wobble Look up Table
     12 const uint8_t scanline_offsets_tbl[] = {0, 1, 2, 3, 3, 2, 1, 0, 0, 1, 2, 3, 3, 2, 1, 0};
     13 const uint8_t * scanline_offsets = scanline_offsets_tbl;
     14 
     15 // Create the ISR Function
     16 void scanline_isr(void) CRITICAL INTERRUPT {
     17     SCX_REG = scanline_offsets[LY_REG & (uint8_t)7];
     18 }
     19 // Then set the LCD Status (scanline) interrupt to call the ISR function
     20 // directly without using the GBDK interrupt dispatcher
     21 ISR_VECTOR(VECTOR_STAT, scanline_isr)
     22 
     23 // Note:
     24 //    The LCD STAT vector (@ref VECTOR_STAT) cannot be used
     25 //    in the same program as `stdio.h` since they install
     26 //    an ISR vector to the same location.
     27 
     28 
     29 void main(void) {
     30 
     31     // Load the background image
     32     init_gfx();
     33 
     34     // Set up the interrupt and enable it
     35     STAT_REG = STATF_MODE00;
     36     set_interrupts(VBL_IFLAG | LCD_IFLAG);
     37 
     38     // Show the background
     39     SHOW_BKG;
     40 
     41     while (TRUE) {
     42         vsync();        
     43         scanline_offsets = &scanline_offsets_tbl[(uint8_t)(sys_time >> 2) & 0x07u];
     44     }
     45 }
     46 
     47 
     48 
     49 // ==== Below just loads the background graphics ====
     50 
     51 // Some graphics of a game boy
     52 INCBIN(logo_tiles_data, "res/gbdk2020.bin") // Variable name to use, Path to file
     53 INCBIN_EXTERN(logo_tiles_data)              // Extern declarations for binary data
     54 
     55 INCBIN(logo_map, "res/gbdk2020_map.bin")
     56 INCBIN_EXTERN(logo_map)
     57 
     58 INCBIN(blank_tile_data, "res/blanktile.bin")
     59 INCBIN_EXTERN(blank_tile_data)
     60 
     61 
     62 #define TILE_BYTES 16 // 16 bytes per background tile
     63 
     64 // Since incbin just includes a binary file (the logo tiles)
     65 // a map needs to be created for them. For this example
     66 // the tiles are non-deduplciated, so a array of incrementing
     67 // values can be used. There are 84 tiles.
     68 #define LOGO_MAP_WIDTH  7u
     69 #define LOGO_MAP_HEIGHT 12u
     70 #define LOGO_MAP_X      (20u - LOGO_MAP_WIDTH) / 2u  // Center on X axis
     71 #define LOGO_MAP_Y      (18u - LOGO_MAP_HEIGHT) / 2u // Center on Y axis
     72 
     73 void init_gfx(void) {
     74     // Load a single clear background tile at location 0x80 and clear/fill the map with it
     75     set_bkg_data(0x80u, 1u, blank_tile_data); // The first 0x80u here is the tile ID
     76     fill_bkg_rect(0u, 0u, DEVICE_SCREEN_BUFFER_WIDTH, DEVICE_SCREEN_BUFFER_HEIGHT, 0x80u);   // The last 0x80u here is the tile ID 
     77 
     78     // Load logo background tiles and map
     79     // They start at 0u
     80     set_bkg_data(0u, INCBIN_SIZE(logo_tiles_data) / TILE_BYTES, logo_tiles_data);
     81     set_bkg_tiles(LOGO_MAP_X, LOGO_MAP_Y,
     82                   LOGO_MAP_WIDTH, LOGO_MAP_HEIGHT,
     83                   logo_map);
     84 }
     85 

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