Saving a tile map array

BlitzMax Forums/BlitzMax Beginners Area/Saving a tile map array

abelian_grape(Posted 2010) [#1]
I tried doing something similar to what they were working on in this thread:

saving tile array maps

but I'm getting an error saying "Unhandled Exception:Error reading from stream" when I try to load a map. What I did was create a blank map.txt file in my application's directory. The two texture tiles I am working with have 5 letter names and a .bmp extension (water.bmp, grass.bmp) which is why I have the line:

map[x, y].my_floor.texture = ReadString(load_file, 9)	'Load the 5 letter name with .bmp file extension


in the LoadMap() function. Here are my two functions:



So if I make a 3x3 map with tileSize = 32, white tiles on every tile except the middle one which has a "grass" texture, my map.txt file looks like:
          ˙   ˙   ˙   ˙   ˙   ˙   ˙   ˙   ˙   ˙   ˙   ˙   ˙   ˙   ˙   grass.bmp˙   ˙   ˙   ˙   ˙   ˙   ˙   ˙   ˙   ˙   ˙   ˙  


So I'm guessing something is going wrong with when I write to the file, because I would expect that these should be numbers? It makes sense that I'm getting the y's with the umlauts because the character code for that is 255, which is what you need for R, G, B to get a white tile. I'm sure I'm making some stupid mistake. Can anyone see what I did wrong?

Thanks!
-muffins


_Skully(Posted 2010) [#2]
You might be running out of VRAM because you are loading the image for every tile rather than loading it once and referencing the same one. Running out of VRAM will cause white images I hear

What you can do is keep a record of loaded images and if the path is the same as a previously loaded one just set the image to the same one already loaded

example (untested)
Type Images
  global list:Tlist=new Tlist
  Field Image
  Field Path
End type

Function LoadImage:Timage(path:string)
   For local im:Images=eachin Images.list
      if im.path=path return im.image
   Next
   local img:TImage=loadimage(path,DYNAMICIMAGE|MASKEDIMAGE)
   if img
        local tim:images=new images
        tim.path=path
        tim.Image=img
        return img
   endif
   notify "Image "+path+" Failed to load"
End Function



abelian_grape(Posted 2010) [#3]
I can certainly see how that will turn into an issue extremely fast once my maps get bigger :) I'll have to just load the images into an array and reference their index rather than loading a new image every time I want to use it. Thanks for the tip :)

I think for this particular instance, though, being that my test map is only a 3x3, the VRAM shouldn't be an issue. I think it might have to do with my integers being converted to characters somewhere along the line, and when the program goes to read them back from that file and, for example, encounters a ˙ instead of 255, it freaks out.


_Skully(Posted 2010) [#4]
oops.. you posted too fast.. I added above

Do you use the RGB values somewhere?


_Skully(Posted 2010) [#5]
Try writeline instead of writestring


abelian_grape(Posted 2010) [#6]
Do you use the RGB values somewhere?


The RGB values aren't used for tiles that have a texture drawn on them. I did build RGB options into the editor so that the user could create a tile without an image and make it just a solid RGB color. For current games this wouldn't make sense, but frequently in the old NES games (which is the look I am going for) it was a big time-saver. So if a particular tile doesn't have a texture graphic on it, then it is a solid - pre-defined by user - RGB color.

Is your previous code just to check if the images are loading properly and only once?


_Skully(Posted 2010) [#7]
Ah... and yes!


abelian_grape(Posted 2010) [#8]
Thanks for the code, _Skully :D

Even after fixing the image loading, though, the saving/loading doesn't seem to be functioning properly. Regardless of what I save, sometimes the load will make all tiles have one texture on them, and sometimes the tiles will be all white.


_Skully(Posted 2010) [#9]
did you try writeline instead of writestring. I seem to recall an issue with writestring not liking \/'s


abelian_grape(Posted 2010) [#10]
I did switch to writeline, and the output looks much cleaner. Is the program converting the weird characters in the output to ints when using ReadInt? I'm wondering if that's what is messing this up.


_Skully(Posted 2010) [#11]
I beleive, but don't quote me that WriteString can fail on a read string if certain characters are contained within the string; whereas, writeline/readline didn't have the same problem.


abelian_grape(Posted 2010) [#12]
*EDIT*
I found the bugs in my code. Thank you all for the continual help :)

-muffins