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.

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.

Introduced properties

fun error: nullable Error

gamnit :: Texture :: error

Last error on this texture
protected fun error=(error: nullable Error)

gamnit :: Texture :: error=

Last error on this texture
fun gl_texture: Int

gamnit :: Texture :: gl_texture

OpenGL handle to this texture
fun height: Float

gamnit :: Texture :: height

Height in pixels of this texture
protected fun height=(height: Float)

gamnit :: Texture :: height=

Height in pixels of this texture
fun load(force: nullable Bool)

gamnit :: Texture :: load

Load this texture, force reloading it if force
init new(path: Text): Texture

gamnit :: Texture :: new

Prepare a texture located at path within the assets folder
fun offset_bottom: Float

gamnit :: Texture :: offset_bottom

Offset of the bottom border on root from 0.0 to 1.0
fun offset_left: Float

gamnit :: Texture :: offset_left

Offset of the left border on root from 0.0 to 1.0
fun offset_right: Float

gamnit :: Texture :: offset_right

Offset of the right border on root from 0.0 to 1.0
fun offset_top: Float

gamnit :: Texture :: offset_top

Offset of the top border on root from 0.0 to 1.0
fun pixelated=(pixelated: Bool)

gamnit :: Texture :: pixelated=

Should this texture be drawn pixelated when magnified? otherwise it is interpolated
abstract fun root: RootTexture

gamnit :: Texture :: root

Root texture from which self is derived
fun subtexture(left: Numeric, top: Numeric, width: Numeric, height: Numeric): Subtexture

gamnit :: Texture :: subtexture

Prepare a subtexture from this texture, from the given pixel offsets
fun to_animation(fps: Float, x: Int, y: Int): Animation

gamnit :: Texture :: to_animation

Convert to a sprite animation at fps speed with x or y frames
fun width: Float

gamnit :: Texture :: width

Width in pixels of this texture
protected fun width=(width: Float)

gamnit :: Texture :: width=

Width in pixels of this texture

Redefined properties

redef type SELF: Texture

gamnit $ Texture :: SELF

Type of this instance, automatically specialized in every class

All properties

fun !=(other: nullable Object): Bool

core :: Object :: !=

Have self and other different values?
fun ==(other: nullable Object): Bool

core :: Object :: ==

Have self and other the same value?
type CLASS: Class[SELF]

core :: Object :: CLASS

The type of the class of self.
type SELF: Object

core :: Object :: SELF

Type of this instance, automatically specialized in every class
protected fun class_factory(name: String): CLASS

core :: Object :: class_factory

Implementation used by get_class to create the specific class.
fun class_name: String

core :: Object :: class_name

The class name of the object.
fun error: nullable Error

gamnit :: Texture :: error

Last error on this texture
protected fun error=(error: nullable Error)

gamnit :: Texture :: error=

Last error on this texture
fun get_class: CLASS

core :: Object :: get_class

The meta-object representing the dynamic type of self.
fun gl_texture: Int

gamnit :: Texture :: gl_texture

OpenGL handle to this texture
fun hash: Int

core :: Object :: hash

The hash code of the object.
fun height: Float

gamnit :: Texture :: height

Height in pixels of this texture
protected fun height=(height: Float)

gamnit :: Texture :: height=

Height in pixels of this texture
init init

core :: Object :: init

fun inspect: String

core :: Object :: inspect

Developer readable representation of self.
protected fun inspect_head: String

core :: Object :: inspect_head

Return "CLASSNAME:#OBJECTID".
intern fun is_same_instance(other: nullable Object): Bool

core :: Object :: is_same_instance

Return true if self and other are the same instance (i.e. same identity).
fun is_same_serialized(other: nullable Object): Bool

core :: Object :: is_same_serialized

Is self the same as other in a serialization context?
intern fun is_same_type(other: Object): Bool

core :: Object :: is_same_type

Return true if self and other have the same dynamic type.
fun load(force: nullable Bool)

gamnit :: Texture :: load

Load this texture, force reloading it if force
init new(path: Text): Texture

gamnit :: Texture :: new

Prepare a texture located at path within the assets folder
intern fun object_id: Int

core :: Object :: object_id

An internal hash code for the object based on its identity.
fun offset_bottom: Float

gamnit :: Texture :: offset_bottom

Offset of the bottom border on root from 0.0 to 1.0
fun offset_left: Float

gamnit :: Texture :: offset_left

Offset of the left border on root from 0.0 to 1.0
fun offset_right: Float

gamnit :: Texture :: offset_right

Offset of the right border on root from 0.0 to 1.0
fun offset_top: Float

gamnit :: Texture :: offset_top

Offset of the top border on root from 0.0 to 1.0
fun output

core :: Object :: output

Display self on stdout (debug only).
intern fun output_class_name

core :: Object :: output_class_name

Display class name on stdout (debug only).
fun pixelated=(pixelated: Bool)

gamnit :: Texture :: pixelated=

Should this texture be drawn pixelated when magnified? otherwise it is interpolated
abstract fun root: RootTexture

gamnit :: Texture :: root

Root texture from which self is derived
fun serialization_hash: Int

core :: Object :: serialization_hash

Hash value use for serialization
fun subtexture(left: Numeric, top: Numeric, width: Numeric, height: Numeric): Subtexture

gamnit :: Texture :: subtexture

Prepare a subtexture from this texture, from the given pixel offsets
intern fun sys: Sys

core :: Object :: sys

Return the global sys object, the only instance of the Sys class.
fun to_animation(fps: Float, x: Int, y: Int): Animation

gamnit :: Texture :: to_animation

Convert to a sprite animation at fps speed with x or y frames
abstract fun to_jvalue(env: JniEnv): JValue

core :: Object :: to_jvalue

fun to_s: String

core :: Object :: to_s

User readable representation of self.
fun width: Float

gamnit :: Texture :: width

Width in pixels of this texture
protected fun width=(width: Float)

gamnit :: Texture :: width=

Width in pixels of this texture
package_diagram gamnit::Texture Texture core::Object Object gamnit::Texture->core::Object gamnit::RootTexture RootTexture gamnit::RootTexture->gamnit::Texture gamnit::Subtexture Subtexture gamnit::Subtexture->gamnit::Texture gamnit::CheckerTexture CheckerTexture gamnit::CheckerTexture->gamnit::RootTexture gamnit::CustomTexture CustomTexture gamnit::CustomTexture->gamnit::RootTexture gamnit::TextureAsset TextureAsset gamnit::TextureAsset->gamnit::RootTexture gamnit::CheckerTexture... ... gamnit::CheckerTexture...->gamnit::CheckerTexture gamnit::CustomTexture... ... gamnit::CustomTexture...->gamnit::CustomTexture gamnit::TextureAsset... ... gamnit::TextureAsset...->gamnit::TextureAsset gamnit::AbsoluteSubtexture AbsoluteSubtexture gamnit::AbsoluteSubtexture->gamnit::Subtexture gamnit::RelativeSubtexture RelativeSubtexture gamnit::RelativeSubtexture->gamnit::Subtexture gamnit::AbsoluteSubtexture... ... gamnit::AbsoluteSubtexture...->gamnit::AbsoluteSubtexture gamnit::RelativeSubtexture... ... gamnit::RelativeSubtexture...->gamnit::RelativeSubtexture

Parents

interface Object

core :: Object

The root of the class hierarchy.

Children

class RootTexture

gamnit :: RootTexture

Texture with its own pixel data
abstract class Subtexture

gamnit :: Subtexture

Texture derived from another texture, does not own its pixels

Descendants

class AbsoluteSubtexture

gamnit :: AbsoluteSubtexture

Subtexture created from pixel coordinates within parent
class CheckerTexture

gamnit :: CheckerTexture

Colorful small texture of 32x32 pixels by default
class CustomTexture

gamnit :: CustomTexture

Custom texture with pixel values filled programmatically
class RelativeSubtexture

gamnit :: RelativeSubtexture

Subtexture created from relative coordinates ([0..1]) out of the root texture
class TextureAsset

gamnit :: TextureAsset

Texture loaded from the assets folder

Class definitions

gamnit $ Texture
# 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

gamnit :: flat_core $ Texture
redef class Texture

	# Vertices coordinates of the base geometry
	#
	# Defines the default width and height of related sprites.
	private var vertices: Array[Float] is lazy do
		var w = width
		var h = height
		return [-0.5*w,  0.5*h, 0.0,
		         0.5*w,  0.5*h, 0.0,
		        -0.5*w, -0.5*h, 0.0,
		         0.5*w, -0.5*h, 0.0]
	end

	# Coordinates of this texture on the `root` texture, in `[0..1.0]`
	private var texture_coords: Array[Float] is lazy do
		var l = offset_left
		var r = offset_right
		var b = offset_bottom
		var t = offset_top
		return [l, t,
		        r, t,
		        l, b,
		        r, b]
	end

	# Coordinates of this texture on the `root` texture, inverting the X axis
	private var texture_coords_invert_x: Array[Float] is lazy do
		var l = offset_left
		var r = offset_right
		var b = offset_bottom
		var t = offset_top
		return [r, t,
		        l, t,
		        r, b,
		        l, b]
	end

	# Convert to a sprite animation at `fps` speed with `x` or `y` frames
	#
	# The arguments `x` and `y` set the number of frames in the texture.
	# Use `x` for an horizontal arrangement or `y` for vertical.
	# One and only one of the arguments must be different than 0,
	# as an animation can only be on a line and cannot wrap.
	fun to_animation(fps: Float, x, y: Int): Animation
	do
		assert (x == 0) != (y == 0)

		var n_frames = x.max(y)
		var frames = new Array[Texture]

		var dx = (x/n_frames).to_f/n_frames.to_f
		var dy = (y/n_frames).to_f/n_frames.to_f
		var w = if x == 0 then 1.0 else dx
		var h = if y == 0 then 1.0 else dy
		var left = 0.0
		var top = 0.0
		for i in n_frames.times do
			frames.add new RelativeSubtexture(root, left, top, left+w, top+h)
			left += dx
			top += dy
		end

		return new Animation(frames, fps)
	end
end
lib/gamnit/flat/flat_core.nit:567,1--632,3