News : The level of daily SPAM has reached insane proportions, all registrations are now manual. I ask you to send me an e-mail (john (at) murga (dot) org), to confirm that you want me to create an account for you.


Post Reply  Post Thread 
slicing images
Author Message
mikshaw
Senior Member
****


Posts: 522
Group: Registered
Joined: Apr 2007
Status: Offline
Reputation: 5
Post: #1
RE: slicing images

I believe I found a solution to the alpha problem. I haven't looked into the last post by juergen, but the solution seems to be related. The Fl_RGB_Image() line was definitely incorrect. To fix it I obtained the color depth from the original unscaled and unsliced image data, and applied that to Fl_RGB_Image():

Code:
image2 = fltk:Fl_RGB_Image(test[1],ts*tc,ts*tc,image1:d(),test["ld"])

This will use 4 for RGBA images and 3 for RGB images. I haven't tested many images yet, but it works for the transparent PNGs that were crashing before.

There are also some changes to the puzzle behavior:
The preview/cheat is a button that overlays the puzzle instead of a separate window (right-click or middle-click to toggle it). It also behaves as an image-load button initially and when the puzzle has been solved.
I hopefully have fixed the most recent bug I found by returning all tiles to their original positions before scrambling.
The Escape key closes the program only if the preview is not displayed while the puzzle is scrambled. Otherwise it hides the preview.

Code:
#!/home/dsl/bin/murgaLua-0.6.4

-- slide puzzle for MurgaLua 0.6.4+
-- 2008 mikshaw

-- window and image size are determined by these variables
ts=80 -- tile size
tc=4  -- number of tiles in one line
fr=5 -- frame size
--img_dir="/mnt/hda4/shared/image/slide_puzzle"
--img_dir="/home/mik/image/slide_puzzle"

if not Fl_Image.getTiles then
err="This program requires murgaLua 0.6.4 or newer.\n\n"..
"http://www.murga-projects.com/murgaLua/index.html"
if fltk then fltk.fl_alert(err) else print(err) end
os.exit(1)
end

function load_callback(object)
-- mod of a function (and a piece) written by John Murga
fileName = fltk.fl_file_chooser("Choose an RGB image", "Image Files (*.{jpg,png,bmp})", img_dir, nil)
if fileName then
  start=1 -- used for controlling window callback and scramble behavior
  if image1 then image1:uncache() end -- don't know if this is needed
  image1 = Fl_Shared_Image.get(fileName)
  test = image1:copy(ts*tc,ts*tc):data()
  image2 = fltk:Fl_RGB_Image(test[1],ts*tc,ts*tc,image1:d(),test["ld"])
  myImages = image2:getTiles(ts,ts)
  for key,value in ipairs(myImages) do
    tile[key-1]:image(value)
    -- make sure all tile locations are reset correctly
    -- fixes a bug if image is changed while tiles are scrambled
    tile[key-1]:position(pos[key-1].col,pos[key-1].row)
  end
  preview:image(image2)  -- load the preview
  scramble()
  scrambutt:label("load an image")
end
end

function move_tile(t)
if Fl:event_button() >1 then preview:show() elseif image2 then
  local my_x,my_y,movex,movey=t:x(),t:y()
  -- tile must be adjacent to the missing piece
  if (my_x == tile[hidden]:x() and math.abs(my_y-tile[hidden]:y()) == ts)
  or (my_y == tile[hidden]:y() and math.abs(my_x-tile[hidden]:x()) == ts)
  then
    -- swap selected tile with the hidden one
    movex=tile[hidden]:x()
    movey=tile[hidden]:y()
    tile[hidden]:position(my_x,my_y)
    t:position(movex,movey)
    w:redraw()
  end
  -- check to see if puzzle is solved
  -- "ok" is incremented each time a tile is found in its proper place
  if start==0 then -- don't check puzzle during scramble
  local ok=0
  for i=0,tc*tc-1 do
    if tile[i]:x()==pos[i].col and tile[i]:y()==pos[i].row then
      ok=ok+1
    if ok==tc*tc then -- if ok == total number of tiles
      fltk.fl_beep()
      scrambutt:label(plize[math.random(1,table.getn(plize))])
      preview:show()
      start=1
      break
    end
    end
  end
  end
end
end

function scramble()
-- this picks a random tile to attempt to move and
-- repeats the process many times. Simply placing
-- tiles in random locations could potentially make
-- the puzzle impossible to solve
preview:hide()
if hidden then -- reset hidden tile
  tile[hidden]:box(fltk.FL_UP_BOX)
  tile[hidden]:labeltype(fltk.FL_NORMAL_LABEL)
end
-- turn a random tile into the missing piece
hidden=math.random(0,tc*tc-1)
tile[hidden]:labeltype(fltk.FL_NO_LABEL)
tile[hidden]:box(fltk.FL_DOWN_BOX)
local scram=0
while scram < 10000 do
  move_tile(tile[math.random(0,tc*tc-1)])
  scram=scram+1
end
start=0
w:redraw()
end

plize={"Great Job!", "Hooray for You!", "You don't suck!", "WIN!", "GODLIKE!", "CONGRATULATE YOU!"}

fltk.fl_register_images()
math.randomseed(os.time())
Fl:visible_focus(0) -- get rid of the ants
Fl:set_boxtype(fltk.FL_UP_BOX,fltk.FL_THIN_UP_BOX) -- looks a little more like tiles
Fl:set_boxtype(fltk.FL_DOWN_BOX,fltk.FL_THIN_DOWN_BOX)
w=fltk:Fl_Double_Window(ts*tc+fr*2,ts*(tc+0.5)+fr*3,"slide puzzle")

scrambutt=fltk:Fl_Button(fr,ts*tc+fr*2,ts*tc,ts/2,"load an image")
scrambutt:callback(load_callback)
scrambutt:tooltip([[
Click this button at any time to load a new image.

** Puzzle Controls **
Left click:  Move a tile.
Right or Middle click:  Toggle image display.
Esc:  Quit program, or close image display if it is visible.
]])

-- array of tiles, top left to right, then move down
tile={}; pos={}
-- allow space for the frame
row=fr; col=fr
-- subtract one is used because the number of tiles starts at one
-- but the table starts at zero (easier to position them from zero)
for i=0,tc*tc-1 do
  tile[i]=fltk:Fl_Button(col,row,ts,ts)
  tile[i]:align(80)
  tile[i]:callback(move_tile)
  pos[i]={col=col,row=row}
  -- next piece is ts pixels to the right
  col=col+ts
  -- start the next row
  if col == ts*tc+fr then col=fr;row=row+ts end
end

-- image preview (cheat)
preview=fltk:Fl_Button(fr,fr,ts*tc,ts*tc)
preview:callback(function()
if start==1 then load_callback()
else preview:hide() end
end)

--preview:hide()
w:callback(function()
  -- Esc just hides the preview if it's visible
  if preview:visible()==1 and start==0 then preview:hide()
  else os.exit() end
end)

start=1
w:show()
Fl:run()

This post was last modified: 02-28-2008 05:04 AM by mikshaw.

02-28-2008 04:55 AM
Find all posts by this user Quote this message in a reply
Post Reply  Post Thread 

Messages In This Thread
slicing images - mikshaw - 01-26-2008, 06:28 AM
RE: slicing images - JohnMurga - 01-26-2008, 08:48 AM
RE: slicing images - mikshaw - 01-26-2008, 08:51 AM
RE: slicing images - JohnMurga - 01-27-2008, 06:38 AM
RE: slicing images - mikshaw - 01-27-2008, 07:26 AM
RE: slicing images - mikshaw - 02-25-2008, 05:49 AM
RE: slicing images - iGame3D - 02-25-2008, 08:08 AM
RE: slicing images - mikshaw - 02-25-2008, 12:22 PM
RE: slicing images - iGame3D - 02-25-2008, 07:15 PM
RE: slicing images - iGame3D - 02-25-2008, 07:31 PM
RE: slicing images - iGame3D - 02-25-2008, 08:15 PM
RE: slicing images - mikshaw - 02-25-2008, 10:26 PM
RE: slicing images - mikshaw - 02-26-2008, 04:47 AM
RE: slicing images - iGame3D - 02-26-2008, 06:22 AM
RE: slicing images - mikshaw - 02-26-2008, 10:41 PM
RE: slicing images - Juergen - 02-27-2008, 04:42 AM
RE: slicing images - iGame3D - 02-27-2008, 05:53 AM
RE: slicing images - iGame3D - 02-27-2008, 06:30 AM
RE: slicing images - mikshaw - 02-27-2008, 07:08 AM
RE: slicing images - mikshaw - 02-27-2008, 07:18 AM
RE: slicing images - iGame3D - 02-27-2008, 07:52 AM
RE: slicing images - JohnMurga - 02-27-2008, 08:04 AM
RE: slicing images - JohnMurga - 02-27-2008, 08:28 AM
RE: slicing images - mikshaw - 02-27-2008, 08:31 AM
RE: slicing images - Juergen - 02-27-2008, 09:41 AM
RE: slicing images - mikshaw - 02-27-2008, 12:40 PM
RE: slicing images - iGame3D - 02-27-2008, 03:04 PM
RE: slicing images - mikshaw - 02-27-2008, 11:51 PM
RE: slicing images - Juergen - 02-28-2008, 03:50 AM
RE: slicing images - iGame3D - 02-28-2008, 04:22 AM
RE: slicing images - Juergen - 02-28-2008, 04:30 AM
RE: slicing images - mikshaw - 02-28-2008 04:55 AM
RE: slicing images - iGame3D - 02-28-2008, 07:23 AM
RE: slicing images - mikshaw - 02-28-2008, 08:04 AM
RE: slicing images - JohnMurga - 02-28-2008, 08:43 AM
RE: slicing images - iGame3D - 02-28-2008, 03:41 PM
RE: slicing images - mikshaw - 02-28-2008, 07:29 PM
RE: slicing images - iGame3D - 02-29-2008, 05:29 AM
RE: slicing images - mikshaw - 02-29-2008, 05:42 AM
RE: slicing images - iGame3D - 02-29-2008, 04:16 PM
RE: slicing images - mikshaw - 02-29-2008, 11:36 PM
RE: slicing images - Juergen - 03-01-2008, 04:27 AM
RE: slicing images - mikshaw - 03-01-2008, 06:57 AM
RE: slicing images - mikshaw - 03-01-2008, 09:20 AM

View a Printable Version
Send this Thread to a Friend
Subscribe to this Thread | Add Thread to Favorites

Forum Jump: