Update GPU data of sprite

Property definitions

gamnit $ SpriteContext :: update_sprite
	# Update GPU data of `sprite`
	fun update_sprite(sprite: Sprite)
	do
		var context = sprite.context
		if context != self then return

		var sprite_index = sprite.context_index
		assert sprite_index != -1

		# Vertices data

		var data = local_data_buffer
		var o = 0
		for v in [0..4[ do
			# vec4 translation
			data[o+ 0] = sprite.center.x
			data[o+ 1] = sprite.center.y
			data[o+ 2] = sprite.center.z
			data[o+ 3] = 0.0

			# vec4 color
			data[o+ 4] = sprite.tint[0]
			data[o+ 5] = sprite.tint[1]
			data[o+ 6] = sprite.tint[2]
			data[o+ 7] = sprite.tint[3]

			# float scale
			data[o+ 8] = sprite.scale

			# vec4 coord
			data[o+ 9] = sprite.texture.vertices[v*3+0]
			data[o+10] = sprite.texture.vertices[v*3+1]
			data[o+11] = sprite.texture.vertices[v*3+2]
			data[o+12] = 0.0

			# vec2 tex_coord
			var texture = texture
			if texture != null then
				var tc = if sprite.invert_x then
						sprite.texture.texture_coords_invert_x
					else sprite.texture.texture_coords
				data[o+13] = tc[v*2+0]
				data[o+14] = tc[v*2+1]
			end

			# mat4 rotation
			var rot
			if sprite.rotation == 0.0 then
				# Cache the matrix at no rotation
				rot = once new Matrix.identity(4)
			else
				rot = new Matrix.rotation(sprite.rotation, 0.0, 0.0, 1.0)
			end
			data.fill_from_matrix(rot, o+15)

			var animation = sprite.animation
			if animation == null then
				for i in [31..40] do data[o+i] = 0.0
			else
				# a_fps
				data[o+31] = animation.fps

				# a_n_frames
				data[o+32] = animation.frames.length.to_f

				# a_coord
				data[o+33] = animation.frames.first.vertices[v*3+0]
				data[o+34] = animation.frames.first.vertices[v*3+1]

				# a_tex_coord
				var tc = if sprite.invert_x then
						animation.frames.first.texture_coords_invert_x
					else animation.frames.first.texture_coords
				data[o+35] = tc[v*2]
				data[o+36] = tc[v*2+1]

				# a_tex_diff
				var dx = 0.0
				var dy = 0.0
				if animation.frames.length > 1 then
					dx = animation.frames[1].texture_coords[0] - animation.frames[0].texture_coords[0]
					dy = animation.frames[1].texture_coords[1] - animation.frames[0].texture_coords[1]
				end
				data[o+37] = dx
				data[o+38] = dy

				# a_start
				data[o+39] = sprite.animation_start

				# a_loops
				data[o+40] = sprite.animation_loops
			end

			o += float_per_vertex
		end

		glBindBuffer(gl_ARRAY_BUFFER, buffer_array)
		glBufferSubData(gl_ARRAY_BUFFER, sprite_index*bytes_per_sprite, bytes_per_sprite, data.native_array)

		var gl_error = glGetError
		assert gl_error == gl_NO_ERROR else print_error gl_error

		# Element / indices
		#
		# 0--1
		# | /|
		# |/ |
		# 2--3

		var indices = local_indices_buffer
		var io = sprite_index*4
		indices[0] = io+0
		indices[1] = io+2
		indices[2] = io+1
		indices[3] = io+1
		indices[4] = io+2
		indices[5] = io+3

		glBindBuffer(gl_ELEMENT_ARRAY_BUFFER, buffer_element)
		glBufferSubData(gl_ELEMENT_ARRAY_BUFFER, sprite_index*6*2, 6*2, indices.native_array)

		assert glGetError == gl_NO_ERROR
	end
lib/gamnit/flat/flat_core.nit:1168,2--1290,4