Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Problem with Image in Project

in Bugs Posts: 9

I'm putting images in project folder and access it with readImage("Project:img")

I have 2 projects: Main and Lib
In the Lib project: I'm using readImage("Project:img") inside a function.
In the Main project: I call the Lib function, and magically the image is available.

After a couple of edit->run->edit->run of Main, Codea crash.
At restart, sometimes the image is not available anymore until I relauch the Lib manually.

To avoid the crash, I decide to copy/paste the image into the Main project.
It works, and no more crashes.

But, the Lib is actually an image editor. When I'm using it, It will edit which image? The Project:img from the Main project or Project:img from the Lib project? Hum!

For me, it's seem Codea use a cache to store loaded images shared by all projects.
It would be great to have something to specify the project name, like: readImage("MyProject:img")
Or avoid to share the cache across all the projects, I don't know.

Any suggestions?

Comments

  • dave1707dave1707 Mod
    Posts: 7,533

    @moechofe2 It might be easier to figure out what’s happening if you could post some code if it’s not to big. Or could you post something small that shows what you’re having a problem with.

    1. Create Lib project

    2. In Lib project, main.lua file:

      function update()
        sprite("Project:img",WIDTH/2,HEIGHT/2)
      end
      
    3. In Lib project, lib.lua file:

      local img
      
      local function buildImg()
          img=readImage("Project:img")
          if not img then
              img=image(128,128)
              changeColor(color(255,0,0))
          end
      end
      
      function changeColor(c)
          setContext(img)
          background(c)
          setContext()
          saveImage("Project:img",img)
      end
      
      function setup()
          buildImg()
      end
      
      function draw()
          background(0)
          update()
      end
      
    4. Launch the project Lib

    5. Check in the Lib project folder, there is a red image

    6. Create Main project

    7. Add the Lib dependency

    8. In Main project, main.lua file:

      local img=readImage("Project:img")
      
      function update()
          sprite(img,WIDTH/2,HEIGHT/2)
      end
      
      function touched()
          if CurrentTouch.state==BEGAN then
              changeColor(color(0,255,0))
          end
      end
      
    9. Launch the project Main

    10. Observe that the image from the Lib project is accessible in the Main project

    11. Tap the screen, it will change the color of the image and save it in the Main project

    12. Check the image in the Main project folder, there is a green image

    13. Relaunch the Main, the displayed image is still the red one, even if a new one was create in the Main project folder, Codea still use the one from the Lib project

  • Posts: 1,186

    @moechofe2 - not sure what you are trying to achieve. Try this out two projects, the first below:


    -- Tester function setup() img=image(128,128) changeColor(color(255,0,0)) end function draw() background(0) sprite(img,WIDTH/2,HEIGHT/2) end function touched() if CurrentTouch.state==BEGAN then changeColor(color(math.random(255),math.random(255),math.random(255))) end end function changeColor(c) setContext(img) background(c) setContext() saveImage("Project:img",img) end

    The second below:


    -- Tester2 -- Use this function to perform your initial setup function setup() img=readImage("Project:img") end function draw() background(0) sprite(img,WIDTH/2,HEIGHT/2) end

    Run the first, stop it then run the second. You can see the saved image.

    Repeat run the first, touch to get different colours, stop it then run the second.

    Is that what you wanted?

  • Posts: 1,186

    @dave1707 - can’t see why the above should work, unless the created object (in this case an image) is retained in memory when the first program is stopped. Does this relate to memory administration in Codea?

    Also what is Project: as a resource?

  • Posts: 1,186

    @dave1707 - looked into this further and it appears the image is saved to the project folder ie Tester.codea for the first instance. But why does Tester2 read it. The first time you run Tester2 the sprite is not always there. But run it a second time and the sprite is there in the same colour as the sprite in Tester when it last closed????

  • dave1707dave1707 Mod
    Posts: 7,533

    @Bri_G I haven’t had a chance to try your version yet. I’ll play around with it later and see what I can figure out. I have nothing to do with the development of Codea so I don’t know it’s inner workings. I’m just a user and any info I give is usually what I find out thru trial and error. Actually, I’m half way around the world from where Codea is developed.

  • I want to have a dependencies that act as an image editor. But weird behavior appends about readImage and saveImage that I don't understand well.
    The workaround is to make sure the read and the save are called inside the main project. Also sometimes, I need to copy the texture used by the library into the main project and sometimes I need to remove it. Sometimes, I need to kill Codea. O_o

  • Also, there is a screen of the image editor I'm using.

    IMG_0309.PNG 182.7K
  • dave1707dave1707 Mod
    Posts: 7,533

    @moechofe2 I haven’t played around with your or Bri_G code yet to figure out what is happening. But one suggestion is that any read and save image functions are in only one project that you want the images to be in. The editing code could be in the other project. I haven’t tried working with that to see if the images go where you expect them to.

  • Posts: 1,186
    @moechofe2 - it looks like you are editing sprites in an external (to Codea) editor. If so I can think of a few options:

    1.get a Dropbox account and place your images/sprites there. You can access directly or download to documents and program accordingly.

    2. Use fileapp to copy your sprites from the editors storage to the Codea folder.

    Alternatively

    3. Use a sprite editor from within Codea to generate sprites and save to Documents. I believe @JMV38 wrote a simple sprite editor in the early days for Codea. I'll see if I can find a link for you or repost the code.

    Out of interest what is your sprite editor called?
  • dave1707dave1707 Mod
    Posts: 7,533

    I played around with the code above and here's what I came up with. We have 2 projects, project A and project B and they both use the name Project:img. project A has imageA in its Project Assets folder named img. project B has imageB in its Project Assets folder names img. imageA and imageB are different images. If you exit Codea and then restart Codea and run project A that reads Project:img, it will show imageA. If you run project B that reads Project:img, it will also show imageA. If you exit Codea and then restart Codea and then run project B, it will show imageB. If you run projectA, it will also show imageB. If you exit and restart Codea before running each projectA and projectB, then projectA will show imageA and projectB will show imageB. So it appears the Project:img retains a pointer to whichever image it pointed to first until Codea itself is restarted. At least that what I saw.

  • @dave1707 That's what I came up with too. Codea seams to have a cache of images loaded and it's shared along all the projects.

    @Bri_G I already have hundred of files in Dropbox, it's became a mess to find a files. That's why I'm using project folder now. Also because I'm using Pixaki to edit image and copy/paste using the clipboad.

    @Bri_G The sprite editor came from a bigger project that allow me to speed up my work by providing tools and fast API to reduce the numbers of lines of code I need to tap.

  • Posts: 1,186

    @dave1707 - I think you are right about the pointers retained in memory.

    @moechofe2 - I think the using Projects: as resource should only be used from a single project, so write and read in the same project. If you want to achieve what I think you need replace Project: in both applications with Documents: and that should work - an intermediate common storage.

  • Posts: 1,186
    @moechofe2 - thanks for the feedback, if you have Dropbox you can organise it into folders - I keep my sprites in project folders in Dropbox. You can access them directly from Codea by adding the path like "Dropbox:game1\sprite1".

    All folders within the Codea resource are synced to a copy in the Codea Folder when you sync it from the Dropbox resource window.

    A bit of tidying up can give you a very flexible system.
  • Posts: 1,186
    @moechofe2 - during development of a project you can load resources from your most convenient source. I think when you compile in Xcode it draws in the resources and organises them internally. You need to check on that, I've never done it.

    Pixaki looks a sophisticated package and claims saving to iCloud. It may be possible to directly save to Dropbox.

    When you've finished development you could write lua code in Codea to transfer your images etc to your Project to keep a single package.
Sign In or Register to comment.