64 lines
1.7 KiB
Crystal
64 lines
1.7 KiB
Crystal
module Funfriend::TextureMan
|
|
extend self
|
|
|
|
DEFAULT_TEXTURE_PARAMS = {
|
|
Texture::ParameterName::TextureWrapS => Texture::ParameterValue::ClampToBorder,
|
|
Texture::ParameterName::TextureWrapT => Texture::ParameterValue::ClampToBorder,
|
|
Texture::ParameterName::TextureMinFilter => Texture::ParameterValue::Nearest,
|
|
Texture::ParameterName::TextureMagFilter => Texture::ParameterValue::Nearest
|
|
}
|
|
|
|
alias SizedTexture = NamedTuple(tex: Texture, width: Int32, height: Int32)
|
|
|
|
def load_texture(filepath : String, texture_params : Texture::ParameterHash = DEFAULT_TEXTURE_PARAMS) : SizedTexture
|
|
# Create a texture
|
|
texture = Texture.new
|
|
width, height = 0, 0
|
|
|
|
# Bind the texture the 2D target
|
|
texture.bind(Texture::Target::Texture2D) do |tex, target|
|
|
|
|
# Apply the previously declared configurations.
|
|
target.set_parameters(texture_params)
|
|
|
|
# Open up container image
|
|
CrystImage.open(filepath) do |image|
|
|
width, height = image.width, image.height
|
|
|
|
# Send the image data to the GPU
|
|
target.image_2d(0, BaseInternalFormat::RGBA, image.width, image.height, Format::RGBA, DataType::UnsignedByte, image.data)
|
|
end
|
|
end
|
|
|
|
return {
|
|
tex: texture,
|
|
width: width,
|
|
height: height
|
|
}
|
|
end
|
|
|
|
class TextureBasket
|
|
property textures : Array(SizedTexture)
|
|
property fps : Float64
|
|
property t : Float64
|
|
|
|
def initialize(textures : Array(SizedTexture), fps : Float64)
|
|
@textures = textures
|
|
@fps = fps
|
|
@t = 0
|
|
end
|
|
|
|
def frame : Int32
|
|
(t * fps).floor().to_i % textures.size
|
|
end
|
|
|
|
def texture : SizedTexture
|
|
textures[frame]
|
|
end
|
|
|
|
def update(delta : Float64)
|
|
@t += delta
|
|
end
|
|
end
|
|
end
|