cscg22-gearboy

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

large_map.c (2991B)


      1#include <gb/gb.h>
      2#include <stdint.h>
      3
      4#include "bigmap_map.h"
      5#include "bigmap_tiles.h"
      6
      7#define camera_max_y ((bigmap_mapHeight - 18) * 8) 
      8#define camera_max_x ((bigmap_mapWidth - 20) * 8) 
      9
     10#define MIN(A,B) ((A)<(B)?(A):(B))
     11
     12uint8_t joy;
     13
     14// current and old positions of the camera in pixels
     15uint16_t camera_x, camera_y, old_camera_x, old_camera_y;
     16// current and old position of the map in tiles
     17uint8_t map_pos_x, map_pos_y, old_map_pos_x, old_map_pos_y;
     18// redraw flag, indicates that camera position was changed
     19uint8_t redraw;
     20
     21void set_camera() {
     22    // update hardware scroll position
     23    SCY_REG = camera_y; SCX_REG = camera_x; 
     24    // up or down
     25    map_pos_y = (uint8_t)(camera_y >> 3u);
     26    if (map_pos_y != old_map_pos_y) { 
     27        if (camera_y < old_camera_y) {
     28            set_bkg_submap(map_pos_x, map_pos_y, MIN(21u, bigmap_mapWidth-map_pos_x), 1, bigmap_map, bigmap_mapWidth);
     29        } else {
     30            if ((bigmap_mapHeight - 18u) > map_pos_y) set_bkg_submap(map_pos_x, map_pos_y + 18u, MIN(21u, bigmap_mapWidth-map_pos_x), 1, bigmap_map, bigmap_mapWidth);     
     31        }
     32        old_map_pos_y = map_pos_y; 
     33    }
     34    // left or right
     35    map_pos_x = (uint8_t)(camera_x >> 3u);
     36    if (map_pos_x != old_map_pos_x) {
     37        if (camera_x < old_camera_x) {
     38            set_bkg_submap(map_pos_x, map_pos_y, 1, MIN(19u, bigmap_mapHeight - map_pos_y), bigmap_map, bigmap_mapWidth);     
     39        } else {
     40            if ((bigmap_mapWidth - 20u) > map_pos_x) set_bkg_submap(map_pos_x + 20u, map_pos_y, 1, MIN(19u, bigmap_mapHeight - map_pos_y), bigmap_map, bigmap_mapWidth);     
     41        }
     42        old_map_pos_x = map_pos_x;
     43    }
     44    // set old camera position to current camera position
     45    old_camera_x = camera_x, old_camera_y = camera_y;
     46}
     47
     48void main(){
     49    DISPLAY_OFF;
     50    SHOW_BKG;
     51    set_bkg_data(0, 241u, bigmap_tiles);
     52
     53    map_pos_x = map_pos_y = 0; 
     54    old_map_pos_x = old_map_pos_y = 255;
     55    set_bkg_submap(map_pos_x, map_pos_y, 20, 18, bigmap_map, bigmap_mapWidth);
     56    DISPLAY_ON;
     57    
     58    camera_x = camera_y = 0;
     59    old_camera_x = camera_x; old_camera_y = camera_y;
     60
     61    redraw = FALSE;
     62
     63    SCX_REG = camera_x; SCY_REG = camera_y; 
     64    while (TRUE) {
     65        joy = joypad();
     66        // up or down
     67        if (joy & J_UP) {
     68            if (camera_y) {
     69                camera_y--;
     70                redraw = TRUE;
     71            }
     72        } else if (joy & J_DOWN) {
     73            if (camera_y < camera_max_y) {
     74                camera_y++;
     75                redraw = TRUE;
     76            }
     77        } 
     78        // left or right
     79        if (joy & J_LEFT) {
     80            if (camera_x) {
     81                camera_x--;
     82                redraw = TRUE;
     83            }
     84        } else if (joy & J_RIGHT) {
     85            if (camera_x < camera_max_x) {
     86                camera_x++;
     87                redraw = TRUE;
     88            }
     89        } 
     90        if (redraw) {
     91            wait_vbl_done();
     92            set_camera();
     93            redraw = FALSE;
     94        } else wait_vbl_done();
     95    }
     96}