Function to use different block image grids

Started by Ratty, August 21, 2008, 05:28:26 AM

Previous topic - Next topic

Ratty

Hi all, just got started with playbasic and this is one of the first functions I have written.

I Hate having to draw an image with a single long line of blocks, so I created this function to allow me to use a grid of images, with or without spacers in them. Hope this helps people, apologies if something similar has been posted before.

Simply load your image, pass the image ID to this along with the block parameters (see the comments in the code for info) and this returns a PB formatted image.

PlayBASIC Code: [Select]
Psub BuildPBBlockImage(   sourceimage,blockx, blocky,blockspacerx, blockspacery,offsetx, offsety)
; BuildPBBlockImage function By Ratty 20080821

; this will return a PB block image from any image grid of blocks with or without spacers

; blockx, blocky = size of each gfx block
; blockspacerx, blockspacery = if there is a space between each block
; offsetx, offsety = if there is a space at the top left of the image

; for instance, if you have a grid of 10x10 blocks, each block 64x64 pixels,
; with a 1 pixel space between them and a 1 pixel gap at the top and left of the image
; the call would be 'BuildPBBlockImage(myimageid, 64, 64, 1, 1, 1, 1)'

; calc the number of blocks across and down and the total number
blocksx = ((GetImageWidth(sourceimage) - offsetx) / (blockx + blockspacerx))
blocksy = ((GetImageHeight(sourceimage) - offsety) / (blocky + blockspacery))
blocks = blocksx * blocksy

imageout = GetFreeImage()
If blockspacerx = 0 And blockspacery = 0 And blocksy = 1
; if this is a standard PB block set with no spacers then do just copy this image
CopyImage sourceimage, imageout
Else
; otherwise build the image
CreateImage imageout, blockx * blocks, blocky
xout = 0
For y = offsety To (GetImageWidth(sourceimage) - (blocky + blockspacery)) Step (blocky + blockspacery)
For x = offsetx To (GetImageWidth(sourceimage) - (blockx + blockspacerx)) Step (blockx + blockspacerx)
CopyRect sourceimage, x, y, x + blockx, y + blocky, imageout, xout, 0
xout = xout + blockx
Next x
Next y
EndIf

EndPsub imageout



Any bugs best post them here.

Ratty