In the April 1 screenshotsaturday I posted a short video in which the Last Crown Warriors scrolling and collision system could be seen in action.
Testing the #LastCrownWarriors scroll and collision system at MAXIMUM SPEED #GameBoy #ScreenshotSaturday #gamedev pic.twitter.com/Cj9iYMg0cd— Imanol Barriuso (@Imanolea) 3 de abril de 2017
In the video we can see Lilian moving at a speed of 7 pixels per frame along a stage of 512 by 512 pixels. In this post I will explain the logic behind the scroll system, which as you will see below, allows a scrolling of up to 8 pixels per frame.
The first thing to understand when it comes to setting up a scrolling system for Game Boy is that, in this console, any graphical update that is performed with the LCD switched on must occur during the VBlank interrupt (also during the HBlank interrupt, although is not considered for the scroll). And this interrupt marks the beginning of a period of time in which the system is not using the video ram, period in which therefore we have available that memory in order to modify it.
This interruption occurs 59.7 times per second (one for each frame of the game) in a Game Boy. This
graph represents the duration of the interrupt (4,560 clock cycles)
with respect to the total time of the frame (70,224 clock cycles).
Based on this graph we can conclude that the graphical update must be highly optimized.
And the second thing we need to understand is how the background scrolling works in this system. The scrolling of the background works at a hardware level, that is, changing the value of a register can alter the position of the background we have loaded into memory. But the size of this background is limited, 256 by 256 pixels, so for maps that exceed those dimensions it will need to be modified.
The base of the scroll system will then be the redrawing of the background. And
taking into account that the graphic modification should be as optimum
as possible, it only remains to decide which part of the background should be
updated during the interruption. In Last Crown Warriors, we update the visible column and row of the part of the background to which we are going. For example, if you go up, left, or up and left, we will update the background bar highlighted in the next image. The blue box represents the visible background.
For the rest of directions we will update the corresponding bar following the same
principle: update the row and column closest to being visible. This full modification of column and row allows us to scroll the background up to 8 pixels per iteration.
Once we are clear about what tiles need to be replaced, it remains to answer the question of how to do it. As we have seen, the time available to modify the video memory is very limited. So
in Last Crown Warriors we prepare a data structure with the following
information, in order to minimize the time it takes to replace the tiles
of the background:
- byte 0: low byte of the address of the tile to be replaced;
- byte 1: high byte of the address of the tile to be replaced;
- byte 2: new tile value.
This structure is repeated for each of the tiles that we have to update, 41 in total. Thus,
during the interruption of VBlank we must only execute the following
code, pointing to the beginning of the previously prepared data
structure.
We use REPT so that the indicated code is repeated for each of the tiles. Discarding a possible loop, which would consume additional processing.
Also it is important that in case we use other Game Boy interrupts we execute "ei" (enable interrupts) at the start of them. So we make sure that the VBlank interrupt is called at the right time and that no part of it will ever run outside the VBlank period.
Also it is important that in case we use other Game Boy interrupts we execute "ei" (enable interrupts) at the start of them. So we make sure that the VBlank interrupt is called at the right time and that no part of it will ever run outside the VBlank period.
And this would be a conceptual explanation of how the scrolling system works in this game. In
future posts I will try to cover other aspects of the development. For these posts it will not be necessary to have an advanced knowledge of the system in order to
understand the exposed ideas.