Custom texture with pixel values filled programmatically

At creation, the texture is composed of width by height (rounded down) transparent pixels. The pixels value can be set using []=.

var tex = new CustomTexture(2.0, 2.0)
tex[0, 0] = [1.0, 0.0, 0.0] # Red
tex[0, 1] = [0.0, 1.0, 0.0] # Green
tex[1, 0] = [0.0, 0.0, 1.0] # Blue
tex[1, 1] = [1.0, 1.0, 1.0, 0.5] # Transparent white
tex.load

Introduced properties

fun []=(x: Int, y: Int, color: Array[Float])

gamnit :: CustomTexture :: []=

Set the color of the pixel at x, y (from the top-left corner)
init defaultinit(width: Float, height: Float)

gamnit :: CustomTexture :: defaultinit

fun fill(color: Array[Float]): SELF

gamnit :: CustomTexture :: fill

Overwrite all pixels with color, return self

Redefined properties

redef type SELF: CustomTexture

gamnit $ CustomTexture :: SELF

Type of this instance, automatically specialized in every class
redef fun height: Float

gamnit $ CustomTexture :: height

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

gamnit $ CustomTexture :: height=

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

gamnit $ CustomTexture :: load

Load this texture, force reloading it if force
redef fun width: Float

gamnit $ CustomTexture :: width

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

gamnit $ CustomTexture :: width=

Width in pixels of this texture

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
fun []=(x: Int, y: Int, color: Array[Float])

gamnit :: CustomTexture :: []=

Set the color of the pixel at x, y (from the top-left corner)
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.
init defaultinit(width: Float, height: Float)

gamnit :: CustomTexture :: defaultinit

fun delete

gamnit :: RootTexture :: delete

Delete this texture and free all its resources
fun deleted: Bool

gamnit :: RootTexture :: deleted

Has this resource been deleted?
protected fun deleted=(deleted: Bool)

gamnit :: RootTexture :: deleted=

Has this resource been deleted?
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 fill(color: Array[Float]): SELF

gamnit :: CustomTexture :: fill

Overwrite all pixels with color, return self
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
protected fun gl_texture=(gl_texture: Int)

gamnit :: RootTexture :: gl_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
fun loaded: Bool

gamnit :: RootTexture :: loaded

Has this texture been loaded yet?
protected fun loaded=(loaded: Bool)

gamnit :: RootTexture :: loaded=

Has this texture been loaded yet?
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
fun premultiply_alpha: Bool

gamnit :: RootTexture :: premultiply_alpha

Should the pixels RGB values be premultiplied by their alpha value at loading?
fun premultiply_alpha=(premultiply_alpha: Bool)

gamnit :: RootTexture :: premultiply_alpha=

Should the pixels RGB values be premultiplied by their alpha value at loading?
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::CustomTexture CustomTexture gamnit::RootTexture RootTexture gamnit::CustomTexture->gamnit::RootTexture gamnit::Texture Texture gamnit::RootTexture->gamnit::Texture ...gamnit::Texture ... ...gamnit::Texture->gamnit::Texture

Ancestors

interface Object

core :: Object

The root of the class hierarchy.
abstract class Texture

gamnit :: Texture

Texture composed of pixels, loaded from the assets folder by default

Parents

class RootTexture

gamnit :: RootTexture

Texture with its own pixel data

Class definitions

gamnit $ CustomTexture
# Custom texture with pixel values filled programmatically
#
# At creation, the texture is composed of `width` by `height` (rounded down)
# transparent pixels. The pixels value can be set using `[]=`.
#
# ~~~
# # Build a texture with 4 colors
# var tex = new CustomTexture(2.0, 2.0)
# tex[0, 0] = [1.0, 0.0, 0.0] # Red
# tex[0, 1] = [0.0, 1.0, 0.0] # Green
# tex[1, 0] = [0.0, 0.0, 1.0] # Blue
# tex[1, 1] = [1.0, 1.0, 1.0, 0.5] # Transparent white
# tex.load
# ~~~
class CustomTexture
	super RootTexture

	redef var width
	redef var height

	private var cpixels = new CByteArray(4*width.to_i*height.to_i) is lazy

	# Set the `color` of the pixel at `x`, `y` (from the top-left corner)
	#
	# The argument `color` should be an array of up to 4 floats (RGBA).
	# If `color` has less than 4 items, the missing items are replaced by 1.0.
	#
	# Require: `x < width.to_i and y < height.to_i`
	fun []=(x, y: Int, color: Array[Float])
	do
		assert x < width.to_i and y < height.to_i else print_error "{class_name}::[] out of bounds"

		# Simple conversion from [0.0..1.0] to [0..255]
		var bytes = [for c in color do (c*255.0).round.to_i.clamp(0, 255).to_bytes.last]
		while bytes.length < 4 do bytes.add 255

		var offset = 4*(x + y*width.to_i)
		for i in [0..4[ do cpixels[offset+i] = bytes[i].to_b

		loaded = false
	end

	# Overwrite all pixels with `color`, return `self`
	#
	# The argument `color` should be an array of up to 4 floats (RGBA).
	# If `color` has less than 4 items, the missing items are replaced by 1.0.
	fun fill(color: Array[Float]): SELF
	do
		# Simple conversion from [0.0..1.0] to [0..255]
		var bytes = [for c in color do (c*255.0).round.to_i.clamp(0, 255).to_bytes.last]
		while bytes.length < 4 do bytes.add 255

		var i = 0
		for x in [0..width.to_i[ do
			for y in [0..height.to_i[ do
				for j in [0..4[ do cpixels[i+j] = bytes[j].to_b
				i += 4
			end
		end

		loaded = false
		return self
	end

	redef fun load(force)
	do
		force = force or else false
		if loaded and not force then return

		if force and glIsTexture(gl_texture) then
			# Was already loaded, free the previous GL name
			glDeleteTextures([gl_texture])
		end
		gl_texture = -1

		# Round down the desired dimension
		var width = width.to_i
		var height = height.to_i
		self.width = width.to_f
		self.height = height.to_f

		load_from_pixels(cpixels.native_array, width, height, gl_RGBA)

		loaded = true
	end
end
lib/gamnit/textures.nit:130,1--215,3