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

#1

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?

0 Likes

#2

(I added the “Question” category for you)

1 Like

#3

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.

0 Likes

#4

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

0 Likes