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/level.c

      1 #include <stdint.h>
      2 #include <gbdk/platform.h>
      3 #include "common.h"
      4 #include "World1Area1.h"
      5 #include "World1Area2.h"
      6 #include "World2Area1.h"
      7 #include "World1Tileset.h"
      8 #include "World2Tileset.h"
      9 
     10 #define WORLD1_SOLID_TILE_COUNT 17
     11 #define WORLD2_SOLID_TILE_COUNT 68
     12 
     13 BANKREF_EXTERN(World1Tileset)
     14 BANKREF_EXTERN(World2Tileset)
     15 BANKREF_EXTERN(World1Area1)
     16 BANKREF_EXTERN(World1Area2)
     17 BANKREF_EXTERN(World1Area1)
     18 
     19 
     20 // Instead of directly referencing our map constants, camera and physica code will reference these which can be changed between levels
     21 uint16_t currentLevelWidth;
     22 uint16_t currentLevelWidthInTiles;
     23 uint16_t currentLevelHeight;
     24 uint16_t currentLevelHeightInTiles;
     25 const uint8_t *currentLevelMap;
     26 uint8_t currentLevelNonSolidTileCount;
     27 uint8_t currentAreaBank;
     28 
     29 
     30 // What is the current and next level we are on
     31 // When they differ, we'll change levels
     32 uint8_t currentLevel = 255;
     33 uint8_t nextLevel = 0;
     34 
     35 /**
     36  * @param worldX a (non-scaled) x position in the world
     37  * @param worldY a (non-scaled) y position in the world
     38  * @return uint8_t FALSE (0) if the tile is not solid, TRUE (1) if the tile is solid
     39  */
     40 uint8_t IsTileSolid(uint16_t worldX,uint16_t worldY) NONBANKED{
     41 
     42     uint8_t _previous_bank = CURRENT_BANK;
     43 
     44     SWITCH_ROM(currentAreaBank);
     45 
     46     // Get convert the world position to a column and row by dividing by 8
     47     uint16_t column = worldX>>3;
     48     uint16_t row = worldY>>3;
     49 
     50     uint16_t worldMaxRow = currentLevelHeight>>3;
     51 
     52     // Our y coordinate and row are unsigned integers
     53     // We'll check if the row is larger than the maxium
     54     // if that is the case, the player's unsigned y position has wrapped around (instead of going into negative values)
     55     if(row>worldMaxRow||column>=currentLevelWidthInTiles){
     56 
     57         SWITCH_ROM(_previous_bank);
     58         return TRUE;
     59     }
     60 
     61     uint16_t index = column + row* currentLevelWidthInTiles;
     62 
     63     uint8_t tile = currentLevelMap[index];
     64 
     65     SWITCH_ROM(_previous_bank);
     66 
     67     return tile<currentLevelNonSolidTileCount;
     68 }
     69 
     70 
     71 
     72 void SetupCurrentLevel(void) NONBANKED{
     73 
     74     uint8_t _previous_bank = CURRENT_BANK;
     75 
     76     for(uint8_t i=0;i<DEVICE_SCREEN_BUFFER_WIDTH;i++){
     77         for(uint8_t j=0;j<DEVICE_SCREEN_BUFFER_HEIGHT;j++){
     78             set_attribute_xy(i,j,0);
     79             }
     80     }
     81 
     82 
     83     // We only have 3 levels
     84     // This will loop between them
     85     switch(currentLevel % 3){
     86         case 0:
     87 
     88 
     89 
     90             // Switch to whichever bank source tileset is in
     91             SWITCH_ROM(( BANK(World1Tileset)));
     92 
     93             set_native_tile_data(0,World1Tileset_TILE_COUNT,World1Tileset_tiles);
     94             setBKGPalettes(World1Tileset_PALETTE_COUNT,World1Tileset_palettes);
     95             
     96             // Switch to whichever the map data is in
     97             SWITCH_ROM((currentAreaBank = BANK(World1Area1)));
     98 
     99             currentLevelNonSolidTileCount=WORLD1_SOLID_TILE_COUNT;
    100             currentLevelWidth = World1Area1_WIDTH;
    101             currentLevelHeight = World1Area1_HEIGHT;
    102             currentLevelWidthInTiles = World1Area1_WIDTH>>3;
    103             currentLevelHeightInTiles = World1Area1_HEIGHT>>3;            
    104             currentLevelMap= World1Area1_map;
    105 
    106 
    107             break;
    108         case 1:
    109 
    110 
    111             // Switch to whichever bank source tileset is in
    112             SWITCH_ROM((currentAreaBank = BANK(World1Tileset)));
    113 
    114             set_native_tile_data(0,World1Tileset_TILE_COUNT,World1Tileset_tiles);
    115             setBKGPalettes(World1Tileset_PALETTE_COUNT,World1Tileset_palettes);
    116 
    117             
    118             // Switch to whichever the map data is in
    119             SWITCH_ROM((currentAreaBank = BANK(World1Area2)));
    120 
    121             currentLevelNonSolidTileCount=WORLD1_SOLID_TILE_COUNT;
    122             currentLevelWidth = World1Area2_WIDTH;
    123             currentLevelHeight = World1Area2_HEIGHT;
    124             currentLevelWidthInTiles = World1Area2_WIDTH>>3;
    125             currentLevelHeightInTiles = World1Area2_HEIGHT>>3;
    126             currentLevelMap= World1Area2_map;
    127 
    128             break;
    129         case 2:
    130 
    131 
    132             // Switch to whichever bank source tileset is in
    133             SWITCH_ROM((BANK(World2Tileset)));
    134 
    135             set_native_tile_data(0,World2Tileset_TILE_COUNT,World2Tileset_tiles);
    136             setBKGPalettes(World2Tileset_PALETTE_COUNT,World2Tileset_palettes);
    137 
    138             
    139             // Switch to whichever the map data is in
    140             SWITCH_ROM((currentAreaBank = BANK(World2Area1)));
    141 
    142             currentLevelNonSolidTileCount=WORLD2_SOLID_TILE_COUNT;
    143             currentLevelWidth = World2Area1_WIDTH;
    144             currentLevelHeight = World2Area1_HEIGHT;
    145             currentLevelWidthInTiles = World2Area1_WIDTH>>3;
    146             currentLevelHeightInTiles = World2Area1_HEIGHT>>3;
    147             currentLevelMap= World2Area1_map;
    148 
    149             break;
    150     }
    151 
    152     SWITCH_ROM(_previous_bank);
    153 }

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