Property definitions

gamnit $ Texture :: defaultinit
# Texture composed of pixels, loaded from the assets folder by default
#
# Most textures should be created with `App` (as attributes)
# for the method `create_scene` to load them.
#
# ~~~
# import gamnit::flat
#
# redef class App
#     # Create the texture object, it will be loaded automatically
#     var texture = new Texture("path/in/assets.png")
#
#     redef fun create_scene()
#     do
#         # Let `create_scene` load the texture
#         super
#
#         # Use the texture
#         var sprite = new Sprite(texture, new Point3d[Float](0.0, 0.0, 0.0))
#         app.sprites.add sprite
#     end
# end
# ~~~
#
# Otherwise, they can be loaded and error checked explicitly after `create_scene`.
#
# ~~~nitish
# var texture = new Texture("path/in/assets.png")
# texture.load
# var error = texture.error
# if error != null then print_error error
# ~~~
#
# A texture may also be created programmatically, like `CheckerTexture`,
# or derived from another texture, using `subtexture`.
# Textures with actual pixel data (not `Subtexture`) are `RootTexture`.
# Texture loaded from the assets folder may in the PNG or JPG formats.
abstract class Texture

	# Prepare a texture located at `path` within the `assets` folder
	new (path: Text) do return new TextureAsset(path.to_s)

	# Root texture from which `self` is derived
	fun root: RootTexture is abstract

	# Width in pixels of this texture
	var width = 0.0

	# Height in pixels of this texture
	var height = 0.0

	# Load this texture, force reloading it if `force`
	fun load(force: nullable Bool) do end

	# Last error on this texture
	var error: nullable Error = null

	# OpenGL handle to this texture
	fun gl_texture: Int do return root.gl_texture

	# Prepare a subtexture from this texture, from the given pixel offsets
	fun subtexture(left, top, width, height: Numeric): Subtexture
	do
		return new AbsoluteSubtexture(self, left.to_f, top.to_f, width.to_f, height.to_f)
	end

	# Offset of the left border on `root` from 0.0 to 1.0
	fun offset_left: Float do return 0.0

	# Offset of the top border on `root` from 0.0 to 1.0
	fun offset_top: Float do return 0.0

	# Offset of the right border on `root` from 0.0 to 1.0
	fun offset_right: Float do return 1.0

	# Offset of the bottom border on `root` from 0.0 to 1.0
	fun offset_bottom: Float do return 1.0

	# Should this texture be drawn pixelated when magnified? otherwise it is interpolated
	#
	# This setting affects all the textures based on the same pixel data, or `root`.
	#
	# Must be set after a successful call to `load`.
	fun pixelated=(pixelated: Bool)
	do
		if root.gl_texture == -1 then return

		# TODO do not modify `root` by using *sampler objects* in glesv3
		glBindTexture(gl_TEXTURE_2D, root.gl_texture)

		var param = if pixelated then gl_NEAREST else gl_LINEAR
		glTexParameteri(gl_TEXTURE_2D, gl_TEXTURE_MAG_FILTER, param)
	end
end
lib/gamnit/textures.nit:20,1--113,3