cscg22-gearboy

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

large_map.c (3175B)


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