UnderwareDESIGN

PlayBASIC => Resources => Source Codes => Topic started by: Big C. on July 08, 2006, 07:36:47 AM

Title: Universal TileLoader
Post by: Big C. on July 08, 2006, 07:36:47 AM

This is a universal Tile Loader Routine. You can use BlockImages with or without a Space around the Tiles. These Tiles will be uniform width and height.

Example BlockImages are included.


Download Version #3 of the loader here (http://www.underwaredesign.com/pbct/pbct_dataview.php?key=30)
Title: Universal TileLoader
Post by: thaaks on July 08, 2006, 03:47:22 PM
Nice one, Big C.  ;)

Unfortunately it's a teenie weenie little bit error prone.

You calculate the number of rows and columns by dividing the image width/height by tile width/height. But you forgot the tspacer!
If you have 32 images in one row and a tspace of 1 you'll try to read a non existant image after the right most one...

So you need to do something like

TColumns = GetImageWidth(Temp_Image)/(TWidth+TSpacer)
TRows = GetImageHeight(Temp_Image)/(THeight+TSpacer)

in your calculation.

Now try to get it updated in the PBCT  :P
I just read the post where Kevin says that entries can't be updated or deleted  :rolleyes:

Cheers,
Tommy
Title: Universal TileLoader
Post by: Big C. on July 08, 2006, 06:38:47 PM
In short words... you are right, Tommy  :( ... thx  :D

Updated Version above (third entry...)
Title: Universal TileLoader
Post by: Big C. on July 09, 2006, 06:08:48 AM
Ok, its currently a little bit tricky to hold my contribution at PBCT updated...

But, I enlarged the Loader Routine with a scaling parameter...

This is the sourcecode of V4... You find the graphics under the above-mentioned link.

[pbcode]
; PROJECT : TilesLoader V4
; AUTHOR  : Big C.
; CREATED : 08.07.2006
; EDITED  : 09.07.2006
; ---------------------------------------------------------------------

; ****************************************************************************
; * Global Definitions
; ****************************************************************************

Dim MyTiles(-1)

Global TotalTiles = 0

;Example Image#1 without spacer
MyImage$  = "MyTiles_Without_Spacer2.bmp"

;Example Image#2 with space around the Tiles of 1 Pixel
;MyImage$  = "MyTiles_With_Spacer2.bmp"

TileWidth     = 32
TileHeight     = 32

; Set TileSpacer to the Width of your Spacer in Pixel
; For example Image#2 I use 1 Pixel
TileSpacer     = 0

; TileScaleFlag scales your Tiles down or up or not
; -2 = 50%
; -1 = 70%
;  0 = Original Size = no scaling
;  1 = 125%
;  2 = 150%
TileScaleFlag  = 0

; Colour of Transparency
TransparentColour = RGB(255,0,255)


; ****************************************************************************
; * Subroutine: LoadTiles
; ****************************************************************************

Psub LoadTiles(TArray(), ImageFile$, TWidth, THeight, TSpacer, TScaleFlag, TColour)

If TScaleFlag < -2 Or TScaleFlag > 2 Or TScaleFlag = 0
   Scale = False
Else
   TScaleFactor# = 1 + (0.25*TScaleFlag)
   Scale = True
EndIf

Temp_Image = LoadNewImage(ImageFile$)

TColumns = GetImageWidth(Temp_Image)/(TWidth + TSpacer)
TRows = GetImageHeight(Temp_Image)/(THeight + TSpacer)

ReDim TArray(TColumns*TRows)

PosX = TSpacer
PosY = TSpacer

ImageNumber = 0

RenderToImage Temp_Image

For Rows = 0 To TRows-1
   For Columns = 0 To TColumns-1
 TArray(ImageNumber) = GetFreeImage()
 GetImage TArray(ImageNumber), PosX, PosY, PosX + TWidth, PosY + THeight
 If Scale
    ScaleImage TArray(ImageNumber), TScaleFactor#, TScaleFactor#, 0
 EndIf
 ImageMaskColour TArray(ImageNumber), TColour
 PosX = PosX + TWidth + TSpacer
 Inc ImageNumber
   Next Columns
   PosX = TSpacer
   PosY = PosY + THeight + TSpacer
Next Rows

RenderToScreen

DeleteImage Temp_Image

TotalTiles = ImageNumber

EndPsub

; ****************************************************************************
; * Initialize
; ****************************************************************************

OpenScreen 1024, 768, 16, 1

BeforeReDim = GetArrayElements(MyTiles(),1)

LoadTiles(MyTiles(), MyImage$, TileWidth, TileHeight, TileSpacer, TileScaleFlag, TransparentColour)

AfterReDim = GetArrayElements(MyTiles(),1)

; ****************************************************************************
; * Main Loop
; ****************************************************************************

Repeat
   
   Cls RGB(0,0,0)

   Print "Array-Elements before grabbing Tiles: " + Str$(BeforeReDim)
   Print "Number of Tiles loaded: " + Str$(TotalTiles)
   Print "Array-Elements after grabbing Tiles: " + Str$(AfterReDim)
   
   For i = 0 To TotalTiles-1
; Draw the Images in solid mode...
 DrawImage MyTiles(i), 100, 50+(i*40), 0
; ... and in transparency mode
 DrawImage MyTiles(i), 200, 50+(i*40), 1
   Next i

   Sync

Until EscKey()

DeleteAllImages

UnDimALL

CloseScreen

End
[/pbcode]