Topic : Tiling in DirectX: Part 2
Author : Martin Estevao
Page : << Previous 2  
Go to page :


tiles at. These two variables are the heart of the "smooth" in Smooth Scrolling.


  for (y = 0; y < SCREEN_SIZEY; y++)
  {
    for (x = 0; x < SCREEN_SIZEX; x++)
    {
      // (NEW)
      scroll_x = x + (world_camerax / TILE_SIZE);  
      scroll_y = y + (world_cameray / TILE_SIZE);

      // (NEW)
      offset_x = world_camerax & (TILE_SIZE - 1);
      offset_y = world_cameray & (TILE_SIZE - 1);

      tile = map[scroll_y][scroll_x];



Ok, some new stuff here. First, a double-nested loop is established in order to loop through our map and capture the ID# of each tile.

Next, scroll_x and scroll_x are defined as the values of x and y, respectively, added to the amount of scrolling (in tiles) that occurred in the world so far (the world_camera variables).

Then the offset variables are defined, which will be used later in blitting. What we will do is subtract the offset variables (the amount of "smooth scroll") from the final tile location.


      tile_src.left    =  (tile - 1) * TILE_SIZE;
      tile_src.top     =  0;
      tile_src.right   =  tile * TILE_SIZE;
      tile_src.bottom  =  TILE_SIZE;



The tile_src RECT is now set up, depending on the tile ID stored in tile for the tile at map[scroll_y][scroll_x];


      // (MODIFIED)
      BltFast((x * TILE_SIZE) - offset_x, (y * TILE_SIZE) - offset_y, lpddsoffscreen, &tile_src, NULL);   

    }
  }
}


Now we blit the tile at the correct screen coordinates, which would be the x and y variable positions * 32 pixels - the "smooth scroll" stored in the offset variables. I used lpddsoffscreen as the name of the DirectX surface where my bitmap is loaded to. Yours might be loaded to a differently named surface.

Final Thoughts
   1. Now that we have a small scrolling tile engine set up, how do I implement it into a game?

It's relatively simple. To get the world scrolling you'd have to do something like this:


if (keypress_right)
  {
    world_camerax += 8;
  }

if (keypress_up)
  {
    world_cameray += 8;
  }



Now the player can press a key and have the tiled world scroll. Of course, a lot more goes into the workings of the world/player movement in a game, but I'm sure you can all figure it out : )

Note: I just made up keypress_right, etc. You'd have to set up DirectInput or use the Win32 API input functions to get player input.

   2. What is that ugly stuff that's happening around the edges of the tiles when I scroll the world?

That "ugly stuff" is BltFast() not being able to blit objects past the edges of the screen. What you are actually seeing is the drawing of a tile until the top/right/bottom/left of it hits the edge of the screen, and then nothing will be drawn in its place.

You can fix it by:


     a) Converting all this stuff to use Blt() instead of BltFast().
     b) Doing some fancy calculations with the tile_src RECTs for the appropriate rows or columns.
     c) Cheating and clearing the area around the ugly part with a black section so that it all looks fine and dandy : )

   3. What about all that other stuff like collision detection, multi-layers, etc.?

I'll really try to get to it. I promise.

   4. "I thought this article was pretty cool!" OR "I hated this @%*^%!$ article!"

Email me. lpSoftware@gdnmail.net || lpSoftware@home.com

Visit my website at http://lpsoftware.cfxweb.net

Also, if you find any mistakes or bugs in this tutorial, please let me know.

This article is 2000 Martin Estevao. This article may not be re-printed without the author's consent.

Page : << Previous 2