?login_element?

Subversion Repositories NedoOS

Rev

Blame | Last modification | View Log | Download

  1. -- Export Tiles as BMPs
  2.  
  3. function num_to_char ( number )
  4.  return ( string.char ( math.mod ( math.mod ( number, 256 ) + 256, 256 ) ) )
  5. end
  6.  
  7. function writeIntLSB ( file, number )
  8.  file:write ( num_to_char( number )) -- x>>0
  9.  file:write ( num_to_char( number / 256 )) -- x>>8
  10.  file:write ( num_to_char( number / 65536 )) -- x>>16
  11.  file:write ( num_to_char( number / 16777216 )) -- x>>24
  12. end
  13.  
  14. function main ()
  15.  if mappy.msgBox ("Export Tiles as BMPs", "This saves the graphics tiles as individual BMP files. Enter the filename WITHOUT the .BMP extension (T???.BMP is added).\n\nRun the script (you will be prompted for a filename to save as)?", mappy.MMB_OKCANCEL, mappy.MMB_ICONQUESTION) == mappy.MMB_OK then
  16.  
  17.   local w = mappy.getValue(mappy.BLOCKWIDTH)
  18.   local h = mappy.getValue(mappy.BLOCKHEIGHT)
  19.   local wp = mappy.andVal(w,-4)
  20.  
  21.   if (mappy.getValue(mappy.MAPWIDTH) == 0) then
  22.    mappy.msgBox ("Export Tiles as BMPs", "You need to load or create a map first", mappy.MMB_OK, mappy.MMB_ICONINFO)
  23.   else
  24.  
  25.    local isok,asname = mappy.fileRequester (".", "BMP files (*.bmp)", "*.bmp", mappy.MMB_SAVE)
  26.    if isok == mappy.MMB_OK then
  27.  
  28.     local isok,tstrt,tend = mappy.doDialogue ("Export Tiles as BMPs", "Range:", "0,"..(mappy.getValue(mappy.NUMBLOCKGFX)-1), mappy.MMB_DIALOGUE2)
  29.     if isok == mappy.MMB_OK then
  30.  
  31.     tstrt = tonumber (tstrt)
  32.     tend = tonumber (tend)
  33.     local tnum = tstrt
  34.     while (tnum <= tend) do
  35. -- open file as binary
  36.      outas = io.open (asname..string.format("T%03d",tnum)..".BMP", "wb")
  37.  
  38. -- BITMAPFILEHEADER
  39.      outas:write ('B')
  40.      outas:write ('M')
  41.      writeIntLSB (outas, 0)
  42.      writeIntLSB (outas, 0)
  43.      if (mappy.getValue(mappy.BLOCKDEPTH) == 8) then
  44.       writeIntLSB (outas, 54+1024)
  45.      else
  46.       writeIntLSB (outas, 54)
  47.      end
  48.  
  49. -- BITMAPINFOHEADER
  50.      writeIntLSB (outas, 40)
  51.      writeIntLSB (outas, mappy.getValue(mappy.BLOCKWIDTH))
  52.      writeIntLSB (outas, mappy.getValue(mappy.BLOCKHEIGHT))
  53.      if (mappy.getValue(mappy.BLOCKDEPTH) == 8) then
  54.       writeIntLSB (outas, 8*65536+1)
  55.      else
  56.       writeIntLSB (outas, 24*65536+1)
  57.      end
  58.      writeIntLSB (outas, 0)
  59.      writeIntLSB (outas, 0)
  60.      writeIntLSB (outas, 1000)
  61.      writeIntLSB (outas, 1000)
  62.      writeIntLSB (outas, 0)
  63.      writeIntLSB (outas, 0)
  64.  
  65. -- Palette for 8bit
  66.      if (mappy.getValue(mappy.BLOCKDEPTH) == 8) then
  67.       local x = 0
  68.       while x < 256 do
  69.        local a,r,g,b = mappy.getValue(mappy.PALETTEARGB+x)
  70.        outas:write (num_to_char(b))
  71.        outas:write (num_to_char(g))
  72.        outas:write (num_to_char(r))
  73.        outas:write (num_to_char(0))
  74.        x = x + 1
  75.       end
  76.      end
  77.  
  78.      local y = 0
  79.      while y < h do
  80.       local x = 0
  81.       while x < w do
  82. -- getPixel returns an index for 8bit, or a,r,g,b for other depths
  83.        local i,r,g,b = mappy.getPixel (x, mappy.getValue(mappy.BLOCKHEIGHT)-(y+1), tnum)
  84.        if (mappy.getValue(mappy.BLOCKDEPTH) == 8) then
  85.         outas:write (num_to_char(i))
  86.        else
  87.         outas:write (num_to_char(b))
  88.         outas:write (num_to_char(g))
  89.         outas:write (num_to_char(r))
  90.        end
  91.        x = x + 1
  92.       end
  93. -- end of row BMP padding
  94.       if (mappy.getValue(mappy.BLOCKDEPTH) > 8) then
  95.        x = x * 3
  96.       end
  97.       if (mappy.andVal (x, 3) > 0) then
  98.        outas:write (0)
  99.        x = x + 1
  100.       end
  101.       if (mappy.andVal (x, 3) > 0) then
  102.        outas:write (0)
  103.        x = x + 1
  104.       end
  105.       if (mappy.andVal (x, 3) > 0) then
  106.        outas:write (0)
  107.       end
  108.       y = y + 1
  109.      end
  110.      outas:close ()
  111.      tnum = tnum + 1
  112.  
  113.     end
  114.     end
  115.    end
  116.   end
  117.  end
  118. end
  119.  
  120. test, errormsg = pcall( main )
  121. if not test then
  122.     mappy.msgBox("Error ...", errormsg, mappy.MMB_OK, mappy.MMB_ICONEXCLAMATION)
  123. end
  124.