|
@@ -0,0 +1,76 @@
|
|
|
+-- load image from custom resources
|
|
|
+-- read pixels and write them to a buffer
|
|
|
+local function create_buffer_from_image(filename)
|
|
|
+ local png = assert(sys.load_resource(filename))
|
|
|
+ local loaded_image = image.load(png)
|
|
|
+ local width = loaded_image.width
|
|
|
+ local height = loaded_image.height
|
|
|
+ local pixels = loaded_image.buffer
|
|
|
+
|
|
|
+ local buffer_declaration = {
|
|
|
+ {
|
|
|
+ name = hash("rgba"),
|
|
|
+ type = buffer.VALUE_TYPE_UINT8,
|
|
|
+ count = 4
|
|
|
+ }
|
|
|
+ }
|
|
|
+ local pixel_buffer = buffer.create(width * height, buffer_declaration)
|
|
|
+ local pixel_stream = buffer.get_stream(pixel_buffer, hash("rgba"))
|
|
|
+ for y = 1, height do
|
|
|
+ for x = 1, width do
|
|
|
+ -- flip image
|
|
|
+ local pixels_index = ((height - y) * width * 4) + ((x - 1) * 4) + 1
|
|
|
+ local r = pixels:byte(pixels_index + 0)
|
|
|
+ local g = pixels:byte(pixels_index + 1)
|
|
|
+ local b = pixels:byte(pixels_index + 2)
|
|
|
+ local a = pixels:byte(pixels_index + 3)
|
|
|
+
|
|
|
+ -- write to buffer stream
|
|
|
+ local stream_index = ((y - 1) * width * 4) + ((x - 1) * 4) + 1
|
|
|
+ pixel_stream[stream_index + 0] = r
|
|
|
+ pixel_stream[stream_index + 1] = g
|
|
|
+ pixel_stream[stream_index + 2] = b
|
|
|
+ pixel_stream[stream_index + 3] = a
|
|
|
+ end
|
|
|
+ end
|
|
|
+
|
|
|
+ return pixel_buffer, width, height
|
|
|
+end
|
|
|
+
|
|
|
+local function replace_atlas_image()
|
|
|
+ local atlas = resource.get_atlas("/examples/resource/modify_atlas/modify_atlas.a.texturesetc")
|
|
|
+ local texture = resource.get_texture_info(atlas.texture)
|
|
|
+ pprint(atlas)
|
|
|
+ pprint(texture)
|
|
|
+
|
|
|
+ local pixel_buffer, width, height = create_buffer_from_image("/examples/resource/modify_atlas/resources/shipYellow_manned.png")
|
|
|
+
|
|
|
+ local first_uvs = atlas.geometries[1].uvs
|
|
|
+ local x = first_uvs[1] - 0
|
|
|
+ local y = first_uvs[2] - 6
|
|
|
+ print(x, y)
|
|
|
+ print(width, height)
|
|
|
+ local texture_info = {
|
|
|
+ type = texture.type, -- resource.TEXTURE_TYPE_2D,
|
|
|
+ width = width,
|
|
|
+ height = height,
|
|
|
+ format = resource.TEXTURE_FORMAT_RGBA, -- why is there no texture.format?
|
|
|
+ x = x,
|
|
|
+ y = y,
|
|
|
+ compression_type = resource.COMPRESSION_TYPE_DEFAULT,
|
|
|
+ num_mip_maps = texture.mipmaps,
|
|
|
+ }
|
|
|
+ resource.set_texture(atlas.texture, texture_info, pixel_buffer)
|
|
|
+end
|
|
|
+
|
|
|
+
|
|
|
+function init(self)
|
|
|
+ msg.post(".", "acquire_input_focus")
|
|
|
+ replace_atlas_image()
|
|
|
+end
|
|
|
+
|
|
|
+function on_input(self, action_id, action)
|
|
|
+ if action.pressed then
|
|
|
+ replace_atlas_image()
|
|
|
+ end
|
|
|
+end
|