saving tile array maps

Blitz3D Forums/Blitz3D Beginners Area/saving tile array maps

AvestheFox(Posted 2009) [#1]
Okay, so I've been experimenting with making my own tile based level editor. the problem I have right now is actually saving these array maps

I'm just not sure how to record all the data of the tile types into a single file... here's what I have in my code so far:

tile types:
Type tile
Field img
Field x
Field y
Field frame 
Field count
End Type 


function for making the maps
Function create_map()
For y=0 To 10
For x=0 To 50
t.tile=New tile
t\img=CopyImage(wld_1) 
t\frame=0
t\x=x
t\y=y
For t\count=1 To 550
b=t\count+t\x+","+t\y+","+t\frame
Next 
Next
Next
End Function 


function for drawing the maps within the 'main program'
Function draw_map()
For t.tile=Each tile
DrawImage t\img,t\x*16+80,t\y*16,t\frame
Next
End Function 


And now the messy bit of code where I've been playing and dallying with diferent options trying to figure out the save command. I've had several results, but none were what I was looking for... =P
Function Save_file()
If KeyHit(28)
For t.tile=Each tile
fileout=WriteFile("test.lvl")
For t\count=1 To 550
WriteString (fileout,b)
Next
CloseFile (fileout)
Next 
End If 
End Function


I apologies if my coding seems a bit out of whack here. I'm still learning.

Any help would be wonderful. Thanks :)

Edit: I wish Blitz Coder was still up and running.. that site had many awesome bits of code and what not. its a shame what I've missed over the years of being absent from the Blitz community... and here it is I have to learn to code all over again xD


Matty(Posted 2009) [#2]
type tile

field img
field imgfilename$
field x
field y
field frame
field count

end type

type tileimg

field imgfilename$
field img

end type

function savetiles(filename$)
outfile=writefile(filename)
if outfile<>0 then 
	tcount=0
	for t.tile=each tile
		tcount=tcount+1	;get the number of tiles otherwise when loading won't know when to stop..alternatively could just go by the 'end of file' marker
	next
	writeint outfile,tcount
	for t.tile=each tile
		;you will need to have a tile "image" filename stored otherwise
		;you won't be able to load back in simply from a handle which is
		;just an integer which pointed to the memory address the image was
		;stored at the last time it was run
		writestring outfile,t\imgfilename
		writeint outfile,t\x
		writeint outfile,t\y
		writeint outfile,t\frame
		writeint outfile,t\count	
	next
	closefile outfile
else
	; message an error occurred when creating the file
endif 



end function

function loadtiles(filename$)
infile=readfile(filename$)
if infile<>0 then 
	;first empty the current tiles...and free all references to images
	delete each tile ;<---NOTE THIS WILL CAUSE A MEMORY LEAK UNLESS YOU DO THE FOLLOWING:

	;TO PREVENT ABOVE MEMORY LEAK FREE THE IMAGE...KEEP A SEPARATE TYPE TO STORE THE UNIQUE IMAGES IN USE
	for ti.tileimg = each tileimg
		freeimage ti\img
		delete ti
	next
	tcount=readint(infile)
	for index=1 to tcount
		t.tile=new tile
		t\imgfilename=readstring(infile)
		t\x=readint(infile)
		t\y=readint(infile)
		t\frame=readint(infile)
		t\count=readint(infile)
	next	
	closefile infile
	;next load the images that are required..
	for t.tile=each tile
		found=false
		for ti.tileimg=each tileimg
			if ti\imgfilename = t\imgfilename then
				found=true
				t\img=ti\img
				exit
			endif
		next
		if found=false then 
			ti.tileimg=new tileimg
			ti\imgfilename=t\imgfilename
			ti\img=loadimage(t\imgfilename)
			t\img=ti\img
		endif 
	next
else
	;message an error occured when loading the file
endif 


end function



EDIT sorry about the lack of code box I forgot.


GIB3D(Posted 2009) [#3]
Use [codebox]


AvestheFox(Posted 2009) [#4]
Thanks, Matty. your a life saver here... I was laboring away all day on this little problem, now I can go to bed with something to look forward to tomorrow :)

that being said, I'll be looking over that code tomorrow and making corrections to my own code accordingly.

thanks again!


Ross C(Posted 2009) [#5]
I dunno about codebox. When it's a shortish piece of code, it's nice to see the whole thing without having to cut and paste, or scroll a wee tiny box.


GIB3D(Posted 2009) [#6]
But that wasn't a tiny piece of code.. Matty's code ;)


AvestheFox(Posted 2009) [#7]
Okay... so I copied and pasted the code that Matty supplied directly into my code, replacing certain functions and types. Changed certain values accordingly. And did everything but add the bathroom sink.

No errors...

It just wont draw the map from the load up...

here's the complete code for the editor and the program for loadup:

The Editor:


Seperate Code for loading:


What am I doing wrong here?


Matty(Posted 2009) [#8]
Okay - first thing.. you pass the variable filename$ to the load and save level function without specifying a value for filename$. See below I've changed it so that your level is saved as "My Tile Map.dat" or something similar. Otherwise it is not going to create a file which has an empty string for a name.

Secondly you should not run the save and load routines every frame of the while/wend loop - simply load before entering the loop and save after exiting the loop..or at least not every frame.

Thirdly you would be better off not using 'copyimage' and just referencing the image value that you are copying. There is no need to copy an image unless you are going to modify that image. If you are just drawing it you only need 1 instance in memory, copying the image and assigning a new handle simply wastes memory if the image is not going to be modified (only drawn).








AvestheFox(Posted 2009) [#9]
Thanks Matty! :)

I got it to work. Had to play around with the changed code. But now its loading perfectly!

Many a thanks to ya!

also... do you suppose that my use of copyimage is what's causing my editor to delay? When I added the draw_map() function and applied it into the main program it caused the program itself to have a long delay before opening or closing...


Matty(Posted 2009) [#10]
Only if the images are large and there are many of them. Define large: over 1024x1024 and more than say 10 of these...although on a modern system that shouldn't be a problem...just a guess though.


AvestheFox(Posted 2009) [#11]
the tiles are each 16 x 16 (as you could guess from the code)

I brought the editor grid x value down from 50tiles to 10tiles, and now its running faster... But I'd like to be able to edit much larger game maps than a pathetic 10 x 10 grid map :(

the image file for the test itself is 256 x 256 in pixels and is in png format.. not sure if that has anything to do with it, really


AvestheFox(Posted 2009) [#12]
also, I'm only editing with 16 tiles for this test


Guy Fawkes(Posted 2009) [#13]
ever tried rpgmaker xp? :)

MUCH easier than coding.

Infact, it requires little to no knowledge of coding :)

~DS~


Ross C(Posted 2009) [#14]
You don't look like your using triple buffering here. Using triple buffering, especially with smaller tile sizes, will speed you map drawing up no end. The basic idea is to draw all your tiles onto a huge master image, and only draw that image, whilst the tiles in view remain in view.

You will also want a tile buffer in parts that aren't seen. What you do is create an image that is the size of the screen, plus tile size x2 in each direction. So you will have an image that contains tiles that can't be seen at the top/bottom/left/right. This means when you scroll, you simply move the master image, without having to update the screen, till you reach the outer edge.

When this happens, you simply draw the master image, back onto itself, but offset, allowing the new tiles to be drawn in that direction. Then draw the new tiles. At the same move the master image's co-ords back a full tile size. This should happen all in the one loop, and the player should not notice a thing, but he'll have a new set of tiles in the direction he's moving.

At most in one loop, you'll have to draw a new row or column of tiles, speeding things up.


abelian_grape(Posted 2010) [#15]
Whoops, posted in wrong forum :[


pc_tek(Posted 2010) [#16]
Blitzcoder went the way of the Dodo quite some time ago. However, a 'clone' site is available which has code sections and the like. Well, at least it looks like the old Blitzcoder did...

http://socoder.net/index.php


AvestheFox(Posted 2010) [#17]
woah, this thread is old! :[

However I never did see those last few posts from over a year ago, so for the one who bumped this thread, its much appreciated actually :)

since then, I've greatly improved on my skills in tile mapping, learned a few tricks from research and experimentation. Indeed, this thread is older than the dinosaurs ;)

Rez: RPG Maker? Meh, I'd rather code. I feel more accomplished when I create something the hard way (granted, I still use click products such as The Games Factory every now and then) but thanks!

pc_tek: Yeah, I found that site a while back, but lost the link to it.. so thanks for posting that ;)