Developing RPG (Tile-based graphics with transparency)

Dan Ingalls Dan at SqueakLand.org
Sat Jul 28 23:46:29 UTC 2001


>I know what you are talking about but I don't know what this has to do with what I'm trying to achieve. Once more. Let's say I have tiles.png graphic file which is 20 pixels high and 50 pixels wide. It consists of 10 "tiles" images (A-J) with dimensions of 10x10 pixels like this:
>
>ABCDE
>FGHIJ
>
>Now, I want to import this file into Squeak and use these tiles to construct totally new image, for example this one, which will have dimensions of 50 (height) x 100 (width) pixels.
>
>AAAAAAAAAA
>ABBBBBBBBA
>ABCCCCCCBA
>ABBBBBBBBA
>AAAAAAAAAA
>
>So, I only need to 1) create some sort of empty "canvas"; 2) BitBlt (or something like that) tiles from tiles.png to this canvas. The result should be a single bitmap (Form?), not a collection of small morphs!

How about something like this:

"Read in the tiles..."
tileForm := PNGReadWriter formFromFileNamed: 'tiles.png'.  "your file"
	"tileForm _ Form fromUserWithExtent: 50 at 20. -- my quick test"
tileWidth := 10.
tileHeight := 10.
tileSpec :=
'ABCDE
FGHIJ'.
tilesWidth := (tileSpec indexOf: Character cr) - 1.

"Define the desired mosaic..."
resultSpec :=
'AAAAAAAAAA
ABBBBBBBBA
ABCCCCCCBA
ABBBBBBBBA
AAAAAAAAAA'.
resultWidth := (resultSpec indexOf: Character cr) - 1.
resultHeight := (resultSpec size + 1) // (resultWidth + 1).
resultForm := Form extent: (resultWidth*tileWidth) @ (resultHeight*tileHeight) depth: tileForm depth.

"Now run through the spec, and BLT from tileForm to resultForm..."
1 to: resultHeight do:
	[:y |
	1 to: resultWidth do:
		[:x |
		spec := resultSpec at: (y-1)*(resultWidth+1) + x.
		tileIndex := tileSpec indexOf: spec.
		sourceRow := tileIndex // (tilesWidth + 1).
		sourceCol := tileIndex \\ (tilesWidth + 1).
		resultForm copy: ((x-1*tileWidth) @ (y-1*tileHeight) extent: tileWidth at tileHeight)
			from: (sourceCol*tileWidth) @ (sourceRow*tileHeight)
			in: tileForm rule: Form over]].
resultForm display  "or whatever you want to do with it"

NOTE:  I did this on a Mac.  Be careful the the spec strings only have CR's, and no LF's in them if you try it out.  I'm assuming your real application won't have any such strings in it -- I just wanted to give you an exact solution.

	- Dan
-- 




More information about the Squeak-dev mailing list