Change texture at runtime: issue with the size (SOLVED)

Hello,

I’m trying to download an image from a server and display it in a sprite.
Here is my code:

local url = "https://blablabla.ams3.digitaloceanspaces.com/image.png"
http.request(url, "GET", function(self, id, res)
	if res.status ~= 200 and res.status ~= 304 then
		print("Unable to get image", res.response)
		return
	end

	local img = image.load(res.response)
	
	if not img then
		print("Unable to load image")
	else
		local img_resource = imageloader.load{data = res.response}
		pprint(img_resource)			
		resource.set_texture( go.get("test_card#sprite", "texture0"), img_resource.header, img_resource.buffer )
		
		
	end
end)

Problem is, the image doesn’t “fit” into the sprite. It appears much bigger.

Here is the sprite, with the image loaded from its atlas (as part of the game resource):

Here is when I download and change the texture,with the same image:

Can anyone help me about how what’s going on?

(I added the “Question” category for you)

1 Like

Remember that you are changing the entire texture, not the image within the texture. You must make sure that the downloaded image has the same size as the texture. Note that the texture gets padded to the nearest power of two in both dimensions.

Thanks for the reminder about the power of 2, that was my mistake!