Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
"the next file in the folder" — Gideros Forum

"the next file in the folder"

PlatypusPlatypus Member
edited March 2013 in Game & application design
Hi, guys.

In my Gideros project, I made a folder called "images" and filled it with about 264 images. I then clicked "sort", so they are arranged in numerical order (of filename).

Their names are of the following style (but might change):

0000.png
0010.png
0020.png
0024.png
0029.png
0030.png
.
.
2700.png

All of them are images that fill the entire screen. The app is an e-book. When it starts, it will display 0000.png (the book's cover).


What is the best way to write the following three functions?

[Load the next image from the "/images/" folder into memory.]
[Now, display it and load the next image from the "/images/" folder into memory.]
[Display the previous image (which is, hopefully, still in memory) again.]


Thanks very much.
Platypus
Kate's Catalogue of Travelling Theatre Centres :
Meet Kate. Grey is her favourite colour. Maths is her favourite subject. Decency is her favourite type of behaviour.
She definitely does not like jewellery or modelling, but loves aeroplanes and other machines made of aluminium.

Comments

  • MellsMells Guru
    edited March 2013
    @Platypus
    Hello and welcome, have you checked The E-book Template?
    That would cut your dev time, the page management work is already done so you can focus on your art + texts.
    twitter@TheWindApps Artful applications : The Wind Forest. #art #japan #apps
  • Just the fact that you say the naming convention may change makes this a little problematic. You could try something like this to read the directory into a table and then select the filenames by iterating through the table,
  • Dear @Mells and @Scouser,

    Thanks, guys. Yeah, I have been analysing both of those (both the e-book and that webpage) during the past few days (plus numerous Gideros resources, posts on this forum and many other Lua resources), but I cannot decide upon (Well,.. I cannot even recognise) the simplest solution to use.

    In that e-book template (which is absolutely wonderful), each page is a separate scene and a separate Lua file, which is probably necessary for an interactive e-book.

    Mine has hundreds of pages, but it is not interactive at all. It is just a folder full of screen-sized images.

    I need to be able to reuse the code for all future books I release, and I always need to be able to insert pages easily. That's why I name them in multiples of ten.
    e.g. It's easy to insert "0725.png" between the following files:
    .
    .
    0710.png
    0720.png
    0730.png
    .
    .

    Isn't there some kind of one-line solution?
    (Attention Beginners - The following is not valid code.)
    Some kind of code like:

    current = 0720

    Function
    LoadFile "images/(>current).png"
    current = (>current)
    End

    ...or something equally short and sweet? I really hope there is. I want the computer to do the computing.

    Kate's Catalogue of Travelling Theatre Centres :
    Meet Kate. Grey is her favourite colour. Maths is her favourite subject. Decency is her favourite type of behaviour.
    She definitely does not like jewellery or modelling, but loves aeroplanes and other machines made of aluminium.
  • Could any of the following commands be applied in this case?

    loadfile?
    dofile?
    read?
    readfile?
    require?
    io.open?
    io.read?
    io.getElements?


    How about this:

    (From @atilim's code on
    http://www.giderosmobile.com/forum/discussion/comment/90#Comment_90 )

    local f = io.open("images/", "....(Something?)....")
    local t = f:read("*all")
    f:close()

    ?
    Kate's Catalogue of Travelling Theatre Centres :
    Meet Kate. Grey is her favourite colour. Maths is her favourite subject. Decency is her favourite type of behaviour.
    She definitely does not like jewellery or modelling, but loves aeroplanes and other machines made of aluminium.
  • ar2rsawseenar2rsawseen Maintainer
    edited March 2013
    Maybe I don't really understand the issue here, but would it not be easier to create for example a global variable with current image, and load each image in the separate scene, like this:
    Somewhere in main lua
    curImage = 0
    maxImage = 9000
    function imageFormat(num)
        if num < 10 then
            return "000"..num
        elseif num < 100 then
            return "00"..num
        elseif num < 1000 then
            return "0"..num
        end
        return num
    end
    And then in scene
    scene = Core.class(Sprite)
     
    --in the init
    function scene:init()
        --load current image
        local bitmap = Bitmap.new(Texture.new("images/"..imageFormat(curImage)..".png"))
    end
     
    --provide methods to switch images
    function scene:next()
        if(curImage < maxImage)
            curImage = curImage + 1
        end
    end
     
    function scene:prev()
        if(curImage > 0)
            curImage = curImage - 1
        end
    end
    Well I used scene manager, but this approach can be firstly be used without it, secondly used in combination of pre-loading next and previous image

    I don't think there is a need to store all of them in memory, it might not be feasible.
  • @Platypus: Somewhere along the lines, you are going to need a table containing the filenames (or at least the numeric part of the filenames) as they are not sequential so there is no easy way of knowing what the next page is going to be. As you said yourself, you may insert an image between 0160 & 0170 which would become 0165. How do you expect the software to know there is a new page after 0160 unless you specifically tell it so.

    That is why I suggested the directory method as it wouldn't matter how many images you inserted, the software would automatically find it during the directory search.
  • ar2rsawseenar2rsawseen Maintainer
    oh, not sequencial, thats what I missed :)
  • ar2rsawseenar2rsawseen Maintainer
    edited March 2013
    But then you can use Lua File System plugin:
    require"lfs"
     
    function listdir (path)
    	for file in lfs.dir(path) do
    		if file ~= "." and file ~= ".." then
    			local f = path..'/'..file
    			print (f)
    		end
    	end
    end
     
    listdir (".")
  • ScouserScouser Guru
    edited March 2013
    Something similar to what @ar2rsawseen wrote:
    require"lfs"
     
    function getdir (path)
    	local pages={}
    	for file in lfs.dir(path) do
    		if file ~= "." and file ~= ".." then
    			if string.sub(file,-4) == ".png" then pages[#pages+1] = file end
    		end
    	end
    	return pages
    end
     
    pages = getdir ("pages")
    should return a list of all the .png files in a directory.
  • Fantastic stuff, guys! Thank you very much for being so helpful. I sincerely appreciate it. I will study your suggested methods.

    Kate's Catalogue of Travelling Theatre Centres :
    Meet Kate. Grey is her favourite colour. Maths is her favourite subject. Decency is her favourite type of behaviour.
    She definitely does not like jewellery or modelling, but loves aeroplanes and other machines made of aluminium.
Sign In or Register to comment.