Before reading this article, read SGDK. Create an image.
Preface.
In this tutorial, we will look at all 3 ways to scroll in SGDK. And since all 3 methods use the same function VDP_setScrollingMode,I decided to write about them in one lesson.
Before you begin, download the project files from MEGA.
Well, let’s get started.
Move the layer.
Open Planescroll/out/rom.bin.
This scene is made with the help of a scroll of one picture.
Now, let’s break down the code. To do this, open the main.c file in the PlaneScroll project.
s16 x_offset = 0;
Stores the x-axis displacement of a layer.
VDP_setPlaneSize(32,32, TRUE);
VDP_setPlaneSize – indicates the size of the layer in tiles. Its syntax is as follows.
VDP_setPlaneSize(width_in_tiles, height_in_tiles, optimization_VRAM);
- width_in_tiles – sets the width of the layer (available values 32, 64, 128)
- height_in_tiles – sets the height of the layer (available values 32, 64, 128)
- optimization_VRAM – if TRUE, then tile-maps will be automatically distributed over VRAM depending on the size of the layer.
Thus, we set the layer size to 256×256 px or 32×32 tile (1 tile = 8×8 px).
VDP_setScrollingMode(HSCROLL_PLANE, VSCROLL_PLANE);
Here, we set the scroll mode, according to the following syntax.
VDP_setScrollingMode(horizontal_scroll, vertical_scroll)
Horizontal scrolling has the following modes:
- HSCROLL_PLANE is a layer scroll.
- HSCROLL_TILE is a scroll of tiles.
- HSCROLL_LINE – scrolling lines.
While vertical:
- VSCROLL_PLANE is a layer scroll.
- VSCROLL_2TILE – scroll meta-tile (1 metatail = 4 tiles = 16×16 px)
VDP_setScrollingMode(HSCROLL_PLANE, VSCROLL_PLANE);
In this example, we have installed a horizontal and vertical scroll of the layer.
VDP_setHorizontalScroll(BG_A, x_offset);
Here, using the VDP_setHorizontalScroll function, a layer for scrolling(BG_A) and a displacement of the x_offset were installed.
Vertical scrolling is exactly the same as a horizontal, but instead of VDP_setHorizontalScroll is used VDP_setVerticalScroll
x_offset += 10;
if(x_offset > 256) {
x_offset -= 256;
}
The offset increases each frame by 10 pixels. Whatever the offset increases to infinity, so i limited it to 256 (the size of the layer along the x-axis).
When, the scroll of the layer is done, go to scroll tiles.
Move the tiles.
In the TileScroll project, open the rom.bin.
Here there is a vertical scroll of meta-tiles (tile 16×16 px).
Now, let’s analyze the code in main.c.
VDP_setScrollingMode(HSCROLL_TILE , VSCROLL_2TILE);
Established the method of scrolling, on tile. On the x-axis we scroll tiles,on the y-axis we scroll meta-tiles (it’s a limitation of Sega Genesis).
VDP_setVerticalScrollTile(BG_A, 3, offset_mask, 6, CPU);
VDP_setVerticalScrollTile – responsible for the vertical scrolling of tiles. The syntax here is as follows.
VDP_setVerticalScrollTile (layer, initial_tile, mask, end_tile, transmission_method);
- layer – is a layer that we will scroll (BG_A or BG_B)
- initial_tile – is a meta-tile on the x-axis,starting from which the maskwill be applied.
- Mask – is an array of values that stores the scroll speed of meta-tiles.
- end_tile – is the last meta-tile.
- transmission_method – takes values (CPU, DMA, DMA_QUEUE, DMA_QUEUE_COPY)
Look at the mask.
s16 offset_mask [6] = {0,5,10,15,20,25};
And now for the animation.
- In the 3rd meta-tile, the velocity is 0.
- In the 4th is equal to 5
- A, in the 5th is equal to 10
- In VDP_setVerticalScrollTile, we indicated that the mask will be applied from the 3rd meta-tile, which is what we see in the animation.
for(i = 0; i < 6; i++) offset_mask [i] = offset_mask [i] +i;
This cycle,moves the displacement of all tiles.
Vertical scroll tiles disassembled,now replace this line.
VDP_setVerticalScrollTile(BG_A, 3, offset_mask, 6, CPU);
On this one. The arguments are the same, only the name of the function is different.
VDP_setHorizontalScrollTile(BG_A, 3, offset_mask, 6, CPU);
And compile.
As you can see, the logic here is the same. Only instead of meta-tiles, tiles are used, and scroll horizontal.
Let’s move on to scrolling lines.
Moving lines.
Open rom file in the LineScroll project
Now, open main.c. Let’s break it down.
s16 offsetMaskLine [224] ;
As with scrolling tiles, we will need a mask, in this mask, will be stored shifts of all lines on the screen.
for(int i=0; i < 224; i++)
offsetMaskLine [i] = 0;
Fill the array with zeros.
VDP_setScrollingMode(HSCROLL_LINE, VSCROLL_PLANE);
Set the scroll mode, on the scroll of lines (HSCROLL_LINE).
VDP_setHorizontalScrollLine(BG_A, 0, offsetMaskLine, 224, CPU);
And scroll lines, the syntax is the same as with the tiles. Only instead of tiles, we using lines.
for(int i=0; i < 224; i++)
offsetMaskLine [i] += random() % 10;
In this cycle, I shifted each line to a random number, from 0 to 9.
Now, comment on the cycle above, and uncomment the other cycle.
for(int i=0; i <= 224; i+=2) {
offsetMaskLine [i] += 1;
offsetMaskLine [i-1] -= 1;
}
This cycle moves:
- even lines – right
- odd – left
As a result, we got this effect.
Conclusion.
So this lesson is over, if there are any suggestions for the release of articles, write them in the comments. Also, in this series of lessons, I smoothly lead you to the creation of a platformer, and from the next lesson, we will already begin to work on this, namely, we will learn how to scroll the map.