Log In  

Hi,

I noticed something strange while writing a function that dumps the content of a memory segment (in the case the spritesheet).

After importing it properly (and checking it in the p8 file through an external editor), I noticed that the last pixel of the spritesheet was always 0. I thought it was a bug (and maybe it is, I'm not 100% sure), but the code seems to be working right for other sections of memory. Here is what I am using :

function rle_comp(addr, ending)
 byte = 0x0
 pixel_index = 0
 local comp = ""

 function read_pixel()
  if pixel_index > 4 then
   addr += 0x0001
   pixel_index = 0
  end

  byte = peek(addr)
  print("addr : " .. tostr(addr, true) .. ' byte : ' .. tostr(byte, true))

  local pixel = band(byte, shl(0x000f, pixel_index))
  if pixel_index > 0 then
   pixel = lshr(pixel, pixel_index)
  end

  pixel_index += 4
  return pixel
 end

 while addr < ending do
  read_pixel()
 end

 return comp
end

function rle_decomp(ci, addr)
end

function _init()
 compressed_img = rle_comp(0x0000, 0x2000)
 rle_decomp(compressed_img, 0x6000)
 --printh(compressed_img, "compressed_img.txt", true)
end

Is it normal to have this trailing 0? Is it a bug in my code? It's late here and I can't tell anymore

P#56544 2018-09-11 18:10 ( Edited 2018-09-12 11:59)

I'm getting correct results for the actual spritesheet. However, you seem to be printing the byte at 0x2000 at the end. The spritesheet only goes up to 0x1fff. So the last pixel you're printing, which is 0, is actually the first tile of the sprite map.

You can see from the output that the last address is 0x2000, so you must have just misread the output? If you set the first tile of the sprite map to any other sprite than sprite #0 you'll quickly see what's happening.

Your while loop correctly terminates before 0x2000, but your read_pixel() function increments the address prematurely. Move the address increment to the end, and it seems to work correctly:

 function read_pixel()
  byte = peek(addr)
  print("addr : " .. tostr(addr, true) .. ' byte : ' .. tostr(byte, true))

  local pixel = band(byte, shl(0x000f, pixel_index))
  if pixel_index > 0 then
   pixel = lshr(pixel, pixel_index)
  end

  pixel_index += 4

  if pixel_index > 4 then
   addr += 0x0001
   pixel_index = 0
  end

  return pixel
 end
P#56552 2018-09-12 04:58 ( Edited 2018-09-12 09:00)

Thank you for taking the time to both answer and fix my code! I was suspecting an error of this kind but I couldn't find it.

P#56558 2018-09-12 07:59 ( Edited 2018-09-12 11:59)

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-04-18 23:31:21 | 0.006s | Q:9