User Tools

Site Tools


midres_library:tutorial:mctile:program
Translations of this page:


TUTORIAL: MULTICOLOR TILE

LET'S WRITE THE PROGRAM!

We now come to the code, to be inserted in the tutorial_ctile.c file:

#include "midres.h"

The first line is an inclusion, and the file to include is just midres.h, which contains all the definitions needed to use the methods and data structures of the library, and of the various target environments. For example, it contains the color definitions used by img2tile which are differentiated by reference computer.

  #include "tutorial_mctile.h"

The second file to include is tutorial_mctile.h, the content of which was generated by img2tile and which we have already described in this page.

  #define TILE_GHOST_MOVING TILE_START + TILE_COUNT + 1

In order to manage the fluid movement of a multicolored tile it is necessary to generate all the intermediate “frames” of this movement. However, this requires that you have free space. The first free space is right after the last defined tile, that is TILE_START + TILE_COUNT + 1

  void main () {

This is where the actual program begins.

  mr_tile_position x = 0;

We place the ghost at the first position of the screen. We use the type mr_tile_position because it is able to represent a position ranging from -32768 to +32768, and therefore it is able to handle the most common width resolutions, such as 320 pixels.

  mr_init_multicolor ();

Now let's initialize the graphics subsystem, indicating the preference for a multicolor system. Attention, however: if during compilation you choose a target that does not support this type of graphics, the system will automatically switch to monochrome mode.

  mr_clear_bitmap (MR_SCREEN_DEFAULT);

Let's clear the default screen.

  mr_tile_setcolor (MR_TILE_BACKGROUND, TILE_COLOR0);
  mr_tile_setcolor (MR_TILE_COLOR0, TILE_COLOR1);
  mr_tile_setcolor (MR_TILE_COLOR1, TILE_COLOR2);
  mr_tile_setcolor (MR_TILE_COLOR2, TILE_COLOR3);

Now we can set the colors that have been communicated to us by img2tile. This step is necessary as for each multicolor tile some colors are shared and others are in common.

mr_tileset_load (“mctile.bin”, MR_TILESET_0, TILE_START, TILE_COUNT);

Let's now load the tiles generated by img2tile. Note that the original file name has been shortened: this was necessary due to the limitation that many 8-bit computer file systems have.

  mr_tile_prepare_horizontal_extended (MR_TILESET_0, TILE_GHOST, TILE_GHOST_WIDTH, TILE_GHOST_HEIGHT, TILE_GHOST_MOVING);

This function prepares (ie: pre-calculates) all the frames in between for the ghost. To be able to recall it, it is necessary to indicate on which tileset you are working, the number of the macrotile, the dimensions (in tile) and, finally, the position from which to start to save the tiles thus recalculated. From now, it will be possible to use TILE_GHOST_MOVING instead of the original macrotile, in order to obtain a smooth movement.

  mr_tileset_visible (MR_TILESET_0);

Let's make the redesigned tiles active.

    while (1) {
      
      mr_start_frame ();

We now must execute an infinite loop and, at the entrance to the loop, we signal that we are starting to draw a frame.

      ++x;
      
      mr_tile_moveto_horizontal_extended (MR_SCREEN_DEFAULT, x, MR_SCREEN_HEIGHT >> 1, TILE_GHOST_MOVING, TILE_GHOST_WIDTH, TILE_GHOST_HEIGHT, MR_COLOR_LIGHT_BLUE);

At each round we move the ghost to the right by one pixel. By calling the mr_tile_moveto_horizontal_extended function we are actually redrawing TILE_GHOST_MOVING horizontally each time. The MR_SCREEN_HEIGHT symbol is valued at the number of tiles per column. It follows that we draw the ghost starting from the x position just recalculated, and halfway up the screen. The last parameter is the color but, as we explained above, it is not necessarily the color used to draw the tile. Let's say it is a “suggestion”, where the color setting was insufficient.

	if ((x >> MR_TILE_WIDTH_FACTOR) > MR_SCREEN_WIDTH) {
		x = -TILE_GHOST_WIDTH*MR_TILE_WIDTH_IN_PIXEL;
	}

This control is used to make the ghost, once exiting from the right side of the screen, re-enter from the left side. We calculate using che MR_TILE_WIDTH_FACTOR that depends on resolution. The abscissa is set equal to the width of the macrotile, so as to have the effect of a “pop-up” entry from the left.

     mr_end_frame (4);

At the end we report that the frame has ended. The number 4 indicates that, if less than 4/60th of a second has passed, the system will have to wait that time before starting to draw again. In this way, the drawing speed will be the same regardless of the platform used.

Return to tutorial.