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/cross-platform/simple_physics/src/phys.c

      1 
      2 // A simple sub-pixel / fixed point example
      3 // Position values are calculated as 16 bit numbers and their
      4 // lower 4 bits are dropped when applying them to the sprite
      5 
      6 #include <gbdk/platform.h>
      7 
      8 #include <stdint.h>
      9 
     10 const uint8_t sprite_data[] = {
     11     0x3C,0x3C,0x42,0x7E,0x99,0xFF,0xA9,0xFF,0x89,0xFF,0x89,0xFF,0x42,0x7E,0x3C,0x3C,
     12     0x3C,0x3C,0x42,0x7E,0xB9,0xFF,0x89,0xFF,0x91,0xFF,0xB9,0xFF,0x42,0x7E,0x3C,0x3C,
     13     0x3C,0x3C,0x42,0x7E,0x99,0xFF,0x89,0xFF,0x99,0xFF,0x89,0xFF,0x5A,0x7E,0x3C,0x3C,
     14     0x3C,0x3C,0x42,0x7E,0xA9,0xFF,0xA9,0xFF,0xB9,0xFF,0x89,0xFF,0x42,0x7E,0x3C,0x3C
     15 
     16 };
     17 
     18 // update user input macro
     19 #define INPUT_PROCESS (old_joy=joy,joy=joypad())
     20 // check button down
     21 #define INPUT_KEY(key) (joy&(key))
     22 // check button press
     23 #define INPUT_KEYPRESS(key) ((joy & ~old_joy) & (key))
     24 
     25 // coordinate translation macros
     26 #define SUBPIXELS_TO_PIXELS(v) (v >> 4)
     27 #define PIXELS_TO_SUBPIXELS(v) (v << 4)
     28 
     29 // physics constants
     30 #define MAX_X_SPEED_IN_SUBPIXELS 64
     31 #define X_ACCELERATION_IN_SUBPIXELS 2
     32 #define MAX_Y_SPEED_IN_SUBPIXELS 64
     33 #define Y_ACCELERATION_IN_SUBPIXELS 2
     34 #define JUMP_ACCELERATION_IN_SUBPIXELS 32
     35 
     36 // object coords
     37 int16_t PosX, PosY;
     38 // object speeds
     39 int16_t SpdX, SpdY;
     40 
     41 // new and previous values of the joypad input
     42 uint8_t joy  = 0, old_joy;
     43 
     44 // main function
     45 void main(void) {
     46 #ifdef NINTENDO
     47     // init palettes
     48     BGP_REG = OBP0_REG = OBP1_REG = DMG_PALETTE(DMG_WHITE, DMG_LITE_GRAY, DMG_DARK_GRAY, DMG_BLACK);
     49 #endif
     50     // load tile data into VRAM
     51     set_sprite_data(0, 4, sprite_data);
     52 
     53     // set sprite tile
     54     set_sprite_tile(0, 0);
     55 
     56     // show bkg and sprites
     57     SHOW_BKG; SHOW_SPRITES;
     58 
     59     PosX = PosY = PIXELS_TO_SUBPIXELS(64);
     60     SpdX = SpdY = PIXELS_TO_SUBPIXELS(0);
     61 
     62     while(TRUE) {
     63         // poll joypads
     64         INPUT_PROCESS;
     65 
     66         // check d-pad and change the object speed
     67         if (INPUT_KEY(J_UP)) {
     68             SpdY -= Y_ACCELERATION_IN_SUBPIXELS;
     69             if (SpdY < -MAX_Y_SPEED_IN_SUBPIXELS) SpdY = -MAX_Y_SPEED_IN_SUBPIXELS;
     70         } else if (INPUT_KEY(J_DOWN)) {
     71             SpdY += Y_ACCELERATION_IN_SUBPIXELS;
     72             if (SpdY > MAX_Y_SPEED_IN_SUBPIXELS) SpdY = MAX_Y_SPEED_IN_SUBPIXELS;
     73         }
     74         if (INPUT_KEY(J_LEFT)) {
     75             SpdX -= X_ACCELERATION_IN_SUBPIXELS;
     76             if (SpdX < -MAX_X_SPEED_IN_SUBPIXELS) SpdX = -MAX_X_SPEED_IN_SUBPIXELS;
     77         } else if (INPUT_KEY(J_RIGHT)) {
     78             SpdX += X_ACCELERATION_IN_SUBPIXELS;
     79             if (SpdX > MAX_X_SPEED_IN_SUBPIXELS) SpdX = MAX_X_SPEED_IN_SUBPIXELS;
     80         }
     81         // check button keypress
     82         if (INPUT_KEYPRESS(J_A)) {
     83             SpdY = -JUMP_ACCELERATION_IN_SUBPIXELS;
     84         }
     85 
     86         // change coordinates of the object
     87         PosX += SpdX, PosY += SpdY;
     88 
     89         // Translate to pixels and move sprite
     90         move_sprite(0, SUBPIXELS_TO_PIXELS(PosX), SUBPIXELS_TO_PIXELS(PosY));
     91 
     92         // decelerate Y and X
     93         if (SpdY < 0) SpdY++; else if (SpdY) SpdY--;
     94         if (SpdX < 0) SpdX++; else if (SpdX) SpdX--;
     95 
     96         // Done processing, yield CPU and wait for start of next frame (VBlank)
     97         vsync();
     98     }
     99 }

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