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/mbc5_rumble/src/main.c

      1 #include <gbdk/platform.h>
      2 #include <stdint.h>
      3 #include <stdio.h>
      4 #include <gbdk/console.h>
      5 
      6 #include "mbc5_rumble.h"
      7 
      8 // Note about SRAM Banks
      9 //
     10 // If rumble is used in a program which does cart SRAM bank switching then the
     11 // SRAM bank should be kept track of in a variable. The MBC5_RUMBLE_ON and
     12 // MBC5_RUMBLE_OFF macros should be changed to include that SRAM bank variable
     13 // (replacing MBC_RAM_BANK_0) so that rumble updates do not unexpectedly alter
     14 // the cart SRAM bank.
     15 // 
     16 // Likewise when switching cart SRAM banks to access data, the rumble state
     17 // should be included when calling SWITCH_RAM() or SWITCH_RAM_MBC5() so
     18 // that rumble status is not interrupted when active.
     19 
     20 
     21 // New and previous values of the joypad input
     22 uint8_t joy  = 0, old_joy;
     23 // User input macros
     24 #define INPUT_UPDATE               (old_joy=joy,joy=joypad())
     25 #define INPUT_BUTTON(key)          (joy&(key))
     26 #define INPUT_BUTTONPRESS(key)     ((joy & ~old_joy) & (key))
     27 #define INPUT_BUTTONS_PRESSED      (joy & ~old_joy)
     28 
     29 
     30 #define INTENS_PRNT_X  18u
     31 #define INTENS_PRNT_Y  12u
     32 
     33 static void display_rumble_intensity(uint8_t value) {
     34     gotoxy(INTENS_PRNT_X, INTENS_PRNT_Y);
     35     printf("%hu", (uint8_t)value);
     36 }
     37 
     38 
     39 void main(void)
     40 {
     41     // Install Rumble update function as aV-Blank handler
     42     CRITICAL {
     43         add_VBL(rumble_update);
     44     }
     45 
     46     printf(
     47         "MBC5 Rumble Example\n"
     48         "\n"
     49         "Press:\n"
     50         "-ST for 1/2 second\n"
     51         "-A  for 1/4 second\n"
     52         "-B  for 1/8 second\n"
     53         "\n"
     54         "-SEL to stop early\n"
     55         "\n"
     56         "-Up more intensity\n"
     57         "-Dn less intensity\n"
     58         "\n"
     59         "Rumble intensity:");
     60 
     61     // Set the rumble intensity to MAX
     62     rumble_intensity_set(RUMBLE_INTENSITY_MAX);
     63     display_rumble_intensity(RUMBLE_INTENSITY_MAX);
     64 
     65     INPUT_UPDATE;
     66     while(1) {
     67 
     68         INPUT_UPDATE;
     69 
     70         switch(INPUT_BUTTONS_PRESSED) {
     71             case J_START:  rumble_start(RUMBLE_COUNT_HALF_SEC); break;
     72             case J_A:      rumble_start(RUMBLE_COUNT_QUARTER_SEC); break;
     73             case J_B:      rumble_start(RUMBLE_COUNT_EIGHTH_SEC); break;
     74 
     75             case J_UP:     display_rumble_intensity( rumble_intensity_increase() );
     76                            break;
     77             case J_DOWN:   display_rumble_intensity( rumble_intensity_decrease() );
     78                            break;
     79 
     80             case J_SELECT: rumble_cancel(); break;
     81         }
     82 
     83         vsync();
     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.