Reading map from file and displaying

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
jondoe
Prole
Posts: 4
Joined: Sat Oct 22, 2011 2:07 pm

Reading map from file and displaying

Post by jondoe »

I've been trying to read a map from a file and display it as an hexagonal tile map for hours now and I'm starting to give up.

The file consists of linear string of bytes between 0x00 and 0xA0, mapWidth * mapHeight large. I'm trying to read it with

Code: Select all

  mapWidth = 3600
  mapHeight = 1800

  print("Loading map...")
  file = io.open("world.map", "rb")
  --local mapdata = love.filesystem.read( "world.map" )

  local mapdata = file:read("*all")

  i = 0
  map = {}
  for x=1,mapWidth do
    map[x] = {}
    for y=1,mapHeight do
      i = i+1
      map[x][y] = string.char(mapdata.byte(i)-1)
    end
  end
  print("Done")
But I get the wrong values! I get this result, when in reality it's supposed to be all blue. :( I find it unlikely that this pattern appears anywhere in the real map.

Advice?
Attachments
GykfK.png
GykfK.png (57.72 KiB) Viewed 358 times
jondoe
Prole
Posts: 4
Joined: Sat Oct 22, 2011 2:07 pm

Re: Reading map from file and displaying

Post by jondoe »

Here's the complete .love archive.
Attachments
maprenderer.love
(385.79 KiB) Downloaded 173 times
jondoe
Prole
Posts: 4
Joined: Sat Oct 22, 2011 2:07 pm

Re: Reading map from file and displaying

Post by jondoe »

Smaller map (5*5)
Attachments
smallmaprenderer.love
(5.34 KiB) Downloaded 165 times
User avatar
josefnpat
Inner party member
Posts: 955
Joined: Wed Oct 05, 2011 1:36 am
Location: your basement
Contact:

Re: Reading map from file and displaying

Post by josefnpat »

<josefnpat> jondoe, may i make a suggestion?
<jondoe> most definitely
<slime74> issue #325
<knoll> Issue #325 - Allow ImageData objects to take a 2D array of pixels - https://bitbucket.org/rude/love/issue/325/
<josefnpat> use 0-9, A-F for your characters
<josefnpat> and just put them into a file
* hagish__ (~hagish@pD9FBE0C0.dip.t-dialin.net) has joined #love
<josefnpat> I did random 3600*1800 characters into a file, and suprise: it was 6.2MB
<josefnpat> You might as well use ASCII instead of writing things bitwise, unless you can actually write small bits
<josefnpat> I guess what I'm saying is:
<josefnpat> you're outputting into byte notation anyway. That's why it's screwing up
<josefnpat> in your lua, you need to read an entire byte to get the right values.
<jondoe> yeah it'd probably save me some pain
<josefnpat> or you need to reduce it down to a nibble in your hex editor
<josefnpat> you have 16 sprites, therefore you need a nibble. if you can switch your hex editor to nibble mode, and read in your lua via nibbles, you can reduce your map size by 50%
<josefnpat> but then you will be limited to 16 sprites (plus your default)
<josefnpat> if you want up to 32 sprites, you need 5 bits
<josefnpat> and you'd have to rencode absolutely everything
<jondoe> There'll probably be more sprites later, but I'm not very concerned about map size as long as it's not completely insane
<josefnpat> so you might as well keep bytes. and if you're going to keep bytes, you might as well spit characters out to your database.
<josefnpat> or whatever you want to call you *.map file :P
<josefnpat> if the map file gets updated a lot, you can do what notch does, and hashmap it.
<jondoe> ok thanks
<jondoe> I'll do this and see what happens :)
<josefnpat> :) Good luck.
Missing Sentinel Software | Twitter

FORCIBLY IGNORED.
<leafo> when in doubt delete all of your code
<bartbes> git rm -r *
<bartbes> git commit -m "Fixed all bugs"
<bartbes> git push
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 2 guests