Skip to content

Commit

Permalink
Added MAP patching example (press button Y to enable / disable it)
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephane-D committed Sep 28, 2024
1 parent 5dd98a4 commit 43a7e93
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 4 deletions.
2 changes: 1 addition & 1 deletion sample/game/sonic/inc/level.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ void LEVEL_updateMapAlternate(VDPPlane plane, Map* map, s16 xmt, s16 ymt);
void LEVEL_updateVDPScroll();

void LEVEL_onVBlank(void);
void LEVEL_handleInput(u16 value);
void LEVEL_doJoyAction(u16 joy, u16 changed, u16 state);


#endif // _LEVEL_H_
Binary file modified sample/game/sonic/out/rom.bin
Binary file not shown.
40 changes: 40 additions & 0 deletions sample/game/sonic/src/level.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ Map *bga;
u16 bgBaseTileIndex[2];


// forward
static void PatchDataCallback(Map *map, u16 *buf, u16 x, u16 y, MapUpdateType updateType, u16 size);


u16 LEVEL_init(u16 vramIndex)
{
u16 ind;
Expand Down Expand Up @@ -167,9 +171,45 @@ void LEVEL_updateMapAlternate(VDPPlane plane, Map* map, s16 xmt, s16 ymt)
}


void LEVEL_doJoyAction(u16 joy, u16 changed, u16 state)
{
if (changed & state & BUTTON_X)
{
if (bga->mapDataPatchCB != NULL) MAP_setDataPatchCallback(bga, NULL);
else MAP_setDataPatchCallback(bga, PatchDataCallback);
}
}


void LEVEL_onVBlank(void)
{
// reset tilemap buffer position after update
bufOffset = 0;
}


static void PatchDataCallback(Map *map, u16 *buf, u16 x, u16 y, MapUpdateType updateType, u16 size)
{
u16* dst = buf;
u16 xt = x;
u16 yt = y;
u16 i = size;

while(i--)
{
u16 tileData = *dst;

// remove palette info
tileData &= ~TILE_ATTR_PALETTE_MASK;
// set palette depending tile position (just for fun)
tileData |= ((xt ^ yt) & 3) << TILE_ATTR_PALETTE_SFT;

// set back tile data
*dst++ = tileData;

// just to keep track of current tile position
if (updateType == ROW_UPDATE) xt++;
else yt++;
}
}

3 changes: 2 additions & 1 deletion sample/game/sonic/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,10 @@ static void joyEvent(u16 joy, u16 changed, u16 state)
// can't do more in paused state
if (paused) return;

// handle player / camera joy actions
// handle player / camera / level joy actions
PLAYER_doJoyAction(joy, changed, state);
CAMERA_doJoyAction(joy, changed, state);
LEVEL_doJoyAction(joy, changed, state);
}

static void vblank()
Expand Down
2 changes: 1 addition & 1 deletion sample/game/sonic/src/player.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ void PLAYER_handleInput(u16 value)

void PLAYER_doJoyAction(u16 joy, u16 changed, u16 state)
{
if (changed & state & (BUTTON_A | BUTTON_B | BUTTON_C | BUTTON_X | BUTTON_Y | BUTTON_Z))
if (changed & state & (BUTTON_A | BUTTON_B | BUTTON_C))
{
if (movY == 0)
{
Expand Down
1 change: 0 additions & 1 deletion sample/game/sonic/src/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,3 @@ void setSpritePosition(Sprite* sprite, s16 x, s16 y)
SPR_setPosition(sprite, x, y);
}
}

0 comments on commit 43a7e93

Please sign in to comment.