cscg22-gearboy

CSCG 2022 Challenge 'Gearboy'
git clone https://git.sinitax.com/sinitax/cscg22-gearboy
Log | Files | Refs | sfeed.txt

phys.c (2342B)


      1#include <gb/gb.h>
      2#include <gb/sgb.h>
      3
      4#include <stdio.h>
      5#include <gbdk/console.h>
      6
      7// A simple sub-pixel / fixed point example
      8// Postion values are calculated as 16 bit numbers and their
      9// lower 4 bits are dropped when applying them to the sprite
     10
     11UINT8 sprite_data[] = { 
     12    0x3C,0x3C,0x42,0x7E,0x99,0xFF,0xA9,0xFF,0x89,0xFF,0x89,0xFF,0x42,0x7E,0x3C,0x3C,
     13    0x3C,0x3C,0x42,0x7E,0xB9,0xFF,0x89,0xFF,0x91,0xFF,0xB9,0xFF,0x42,0x7E,0x3C,0x3C,
     14    0x3C,0x3C,0x42,0x7E,0x99,0xFF,0x89,0xFF,0x99,0xFF,0x89,0xFF,0x5A,0x7E,0x3C,0x3C,
     15    0x3C,0x3C,0x42,0x7E,0xA9,0xFF,0xA9,0xFF,0xB9,0xFF,0x89,0xFF,0x42,0x7E,0x3C,0x3C
     16
     17};
     18
     19joypads_t joypads;
     20
     21// sprite coords
     22UINT16 PosX, PosY;
     23INT16 SpdX, SpdY;
     24UINT8 Jump;
     25
     26// main funxction
     27void main(void) {
     28    // init palettes
     29    BGP_REG = OBP0_REG = OBP1_REG = 0xE4;
     30
     31    // load tile data into VRAM
     32    set_sprite_data(0, 4, sprite_data);
     33    
     34    // set sprite tile
     35    set_sprite_tile(0, 0);
     36
     37    // show bkg and sprites
     38    SHOW_BKG; SHOW_SPRITES;
     39
     40    // init 2 joypads
     41    joypad_init(1, &joypads);
     42 
     43    PosX = PosY = 64 << 4;
     44    Jump = SpdX = SpdY = 0;
     45
     46    while(1) {        
     47        // poll joypads
     48        joypad_ex(&joypads);
     49        
     50        // game object
     51        if (joypads.joy0 & J_UP) {
     52            SpdY -= 2;
     53            if (SpdY < -64) SpdY = -64;
     54        } else if (joypads.joy0 & J_DOWN) {
     55            SpdY += 2;
     56            if (SpdY > 64) SpdY = 64;
     57        }
     58        if (joypads.joy0 & J_LEFT) {
     59            SpdX -= 2;
     60            if (SpdX < -64) SpdX = -64;
     61        } else if (joypads.joy0 & J_RIGHT) {
     62            SpdX += 2;
     63            if (SpdX > 64) SpdX = 64;
     64        }
     65        if ((joypads.joy0 & J_A) && (!Jump)) {
     66            Jump = 3;
     67        }
     68
     69        // jump
     70        if (Jump) {
     71            SpdY -= 8;
     72            if (SpdY < -32) SpdY = -32;
     73            Jump--;
     74        }
     75
     76        PosX += SpdX, PosY += SpdY; 
     77
     78        // Translate to pixels and move sprite
     79        // Downshift by 4 bits to use the whole number values
     80        move_sprite(0, PosX >> 4, PosY >> 4);
     81
     82        // decelerate 
     83        if (SpdY >= 0) {
     84            if (SpdY) SpdY--; 
     85        } else SpdY ++;
     86        if (SpdX >= 0) {
     87            if (SpdX) SpdX--; 
     88        } else SpdX ++;
     89
     90        // Done processing, yield CPU and wait for start of next frame (VBlank)
     91        wait_vbl_done();
     92    }
     93}