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/platformer_template/src/camera.c

      1 #pragma bank 255
      2 
      3 #include <stdint.h>
      4 #include <gbdk/platform.h>
      5 #include "level.h"
      6 
      7 #define WRAP_SCROLL_Y(y) ((y) % DEVICE_SCREEN_PX_HEIGHT)
      8 
      9 #if defined(SEGA)
     10   // For SMS, artifacts are already invisible as screen buffer size is larger than screen size
     11   #define SCROLL_Y_OFFSET 0
     12 #elif defined(NINTENDO)
     13   // For GB, artifacts are already invisible as screen buffer size is larger than screen size
     14   #define SCROLL_Y_OFFSET 0
     15 #else
     16   // For other systems assume height of 240 and adjust Y-scroll 4 pixels down to partly hide artifacts in NTSC overscan
     17   #define SCROLL_Y_OFFSET 4
     18 #endif
     19 
     20 
     21 #define MIN(A,B) ((A)<(B)?(A):(B))
     22 
     23 // current and old positions of the camera in pixels
     24 uint16_t camera_x, old_camera_x;
     25 // current and old position of the map in tiles
     26 uint8_t map_pos_x, old_map_pos_x;
     27 // redraw flag, indicates that camera position was changed
     28 uint8_t redraw;
     29 
     30 void SetCurrentLevelSubmap(uint8_t x, uint8_t y, uint8_t w, uint8_t h) NONBANKED{
     31     
     32     uint8_t _previous_bank = CURRENT_BANK;
     33 
     34     SWITCH_ROM(currentAreaBank);
     35 
     36     set_bkg_submap(x,y,w, h, currentLevelMap, currentLevelWidthInTiles);
     37 
     38     SWITCH_ROM(_previous_bank);
     39     
     40 }
     41 
     42 inline uint8_t update_column_left(uint8_t map_pos_x)
     43 {
     44 #if (DEVICE_SCREEN_BUFFER_WIDTH == DEVICE_SCREEN_WIDTH)
     45     return map_pos_x + 1;
     46 #else
     47     return map_pos_x;
     48 #endif
     49 }
     50 
     51 inline uint8_t update_column_right(uint8_t map_pos_x)
     52 {
     53     return map_pos_x + DEVICE_SCREEN_WIDTH;
     54 }
     55 
     56 void UpdateCamera(void) BANKED {
     57 
     58     // update hardware scroll position
     59     move_bkg(camera_x, 0);
     60     
     61     // left or right
     62     map_pos_x = (uint8_t)(camera_x >> 3u);
     63     if (map_pos_x != old_map_pos_x) {
     64         if (camera_x < old_camera_x) {
     65             SetCurrentLevelSubmap(
     66                     update_column_left(map_pos_x), 
     67                     0, 
     68                     1, 
     69                     MIN(DEVICE_SCREEN_HEIGHT, currentLevelHeightInTiles ));     
     70         } else {
     71             if ((currentLevelWidthInTiles - DEVICE_SCREEN_WIDTH) > map_pos_x) {
     72                 SetCurrentLevelSubmap(
     73                     update_column_right(map_pos_x), 
     74                     0, 
     75                     1, 
     76                     MIN(DEVICE_SCREEN_HEIGHT, currentLevelHeightInTiles));   
     77             }  
     78         }
     79         old_map_pos_x = map_pos_x;
     80     }
     81     // set old camera position to current camera position
     82     old_camera_x = camera_x;
     83 }

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