TGC Codebase Backup



Tile Grabber Function by darknut

28th Jul 2007 12:48
Summary

This function grabs tiles out of a bitmap and assigns a number to them according to the order they are arranged in the source bitmap. It loads them into memory by get image and the



Description

This function grabs tiles out of a bitmap and assigns a number to them according to the order they are arranged in the source bitmap. It loads them into memory by get image and then you can just draw the tiles wherever using paste image. You just pass the filename of the source bitmap and how many tiles you want to grab as parameters. The function is completly self contained except I use a global constant to determine the TILESIZE. You can change this by creating a tilesize parameter. Just thought I would post this in case it was handy to anyone else.



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    function loadTiles(fileName$ as string,total as integer)
`grabs tiles out of source bitmap consecutively
`parameters: source.bmp, total number of tiles to grab
   local SOURCEBITMAP = 5  `source bitmap number
   local tile = 1          `first tile number
   local count = 0         `number of tiles
   `load source bitmap for tiles
   load bitmap fileName$, SOURCEBITMAP
   set current bitmap SOURCEBITMAP
   `calculate rows
   r = (bitmap height()/TILESIZE)
   c = (bitmap width()/TILESIZE)
   `grab tile images from source bitmap
   ty = 0
   for y = 0 to r -1
      tx = 0
      for x = 0 to c -1
         `grab tile image for tile map
         get image tile,tx,ty,tx+TILESIZE,ty+TILESIZE
         inc count : inc tile
         `if last tile is grabbed break out of for x loop
         if count = total then x=c
         tx = tx + 16
      next x
   `if last tile is grabbed break out of for y loop
   if count = total then y=r
   ty = ty + 16
   next y
   `clear memory of source bitmap
   delete bitmap SOURCEBITMAP
   `redirect drawing commands back to screen
   set current bitmap 0
endfunction