Loading custom images on sprites is tedious and unwieldy

I’m trying to make a sort of a trading card game thing for the perk selection screen of our game. Something like this:

I want those polaroids to have custom images and that’s proving to be quite difficult.

Problem #1: Just loading a texture from a PNG requires you to jump through at least one unnecessary conversion and allocation because resource.* functions work with buffers and image.load works with strings. This is slightly better when working with GUI nodes, because gui.new_texture accepts a string, but the inconsistency is still annoying.

Here’s what you have to do to get an image to load:

    local data = sys.load_resource("/episodes/perks/" .. polaroid.perk .. ".png")
    if data then
      local img = image.load(data, true)
      local texture_path = go.get(msg.url(url.socket, url.path, "image"), "texture0")
      local header = {
        width = img.width,
        height = img.height,
        type = resource.TEXTURE_TYPE_2D,
        format = resource.TEXTURE_FORMAT_RGBA,
      }
     
      -- There is no API to copy a string into a buffer stream
      local src_string = img.buffer
      local rgb_buffer = buffer.create(#src_string, {
        { name = hash("data"), type=buffer.VALUE_TYPE_UINT8, count = 1 }
      })
      local stream = buffer.get_stream(rgb_buffer, hash("data"))
      for i = 1, #src_string do
        stream[i] = string.byte(src_string, i)
      end

      resource.set_texture(texture_path, header, rgb_buffer)
    end

Problem #2: There is no way of creating textures at runtime as you can do in GUI. I can only replace an already existing texture, which means I have to create a different empty atlas for each of those polaroids (there can be max 14 of them on the screen at the same time), costing me memory, loading time and manual labor :laughing: , which would then just get replaced. Not to mention I have to juggle with a power of two empty image in those atlases just to get my texture coords to stick to [0, 1], and then scale that.

I find it great that you can even go this far, but is there any simpler way of doing this? If not, are there any plans to work on this API?

2 Likes

Yes, that’s not an optimal workflow for sure. Being able to create a texture at run-time for game objects would be very useful. @andreas.tadic, any plans to add this?

3 Likes

Any plan for this?

Is this what you want? ImageLoader - native extension for JPG and PNG

3 Likes