assetloader

package module
v0.3.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 24, 2023 License: MIT Imports: 14 Imported by: 0

README

Asset Loader

Version GoDoc Go

Note: This library is currently in prototype stage. It is not recommended to use this library in production.

Go Asset Loader is a straightforward asset loading library for the Go programming language. It is built on top of the Pixel and Beep packages.

This library has been developed as part of the 2D Game Engine project called Raindrop.

Features

Asset Loader supports the following features:

  • Textures: Load textures from PNG and JPEG files.
  • Sounds: Load sounds from MP3 files.
  • Fonts: Load fonts from TTF files.

Getting Started

  1. Installation: Get the package using go get:
   go get github.com/up1-io/assetloader
  1. Usage: Import the package in your code and start using it:
   import "github.com/up1-io/assetloader"
  1. Simple Example:
package main

import "github.com/up1-io/assetloader"

func main() {
	loader := assetloader.NewLoader()

	asset, err := loader.LoadTexture("test", "test/test-image.png")
	if err != nil {
		panic(err)
	}

	println(asset.Name, asset.Path, asset.Type)
}

License

This project is licensed under the MIT License - see the LICENSE file for details

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AssetData

type AssetData interface {
	FontAsset | AudioClipAsset | AudioStreamAsset | TextureAsset
}

type AssetResource

type AssetResource[T AssetData] struct {
	Type AssetType
	Name string
	Path string
	Data T
}

AssetResource is a type that defines an asset resource. An asset resource is a resource that is loaded by the asset loader. It holds the data of the asset.

type AssetType

type AssetType string

AssetType is a type that defines the type of an asset.

const (
	PngImageAssetType       AssetType = "image/png"
	JpegImageAssetType      AssetType = "image/jpeg"
	Mp3AudioAssetType       AssetType = "audio/mp3"
	WavAudioAssetType       AssetType = "audio/wav"
	Mp3AudioStreamAssetType AssetType = "audio/mp3-stream"
	WavAudioStreamAssetType AssetType = "audio/wav-stream"
	TFFAssetType            AssetType = "font/ttf"
)

func GetAssetTypeByPath added in v0.3.3

func GetAssetTypeByPath(path string) (AssetType, error)

GetAssetTypeByPath gets the asset type by the path.

type AudioClipAsset added in v0.3.3

type AudioClipAsset struct {
	Buffer *beep.Buffer
	Format beep.Format
}

AudioClipAsset is a type that defines a audio clip asset. It holds the buffer and format. A audio clip asset is a audio asset that is loaded into memory.

type AudioLoader

type AudioLoader struct {
	// contains filtered or unexported fields
}

func NewAudioLoader

func NewAudioLoader() AudioLoader

func (*AudioLoader) EachAudioClip

func (al *AudioLoader) EachAudioClip(fn func(name string, asset AssetResource[AudioClipAsset]))

EachAudioClip iterates over all audio clip assets in the asset loader.

func (*AudioLoader) EachAudioStream

func (al *AudioLoader) EachAudioStream(fn func(name string, asset AssetResource[AudioStreamAsset]))

EachAudioStream iterates over all audio stream assets in the asset loader.

func (*AudioLoader) GetAudioClip

func (al *AudioLoader) GetAudioClip(name string) (AssetResource[AudioClipAsset], bool)

GetAudioClip gets a audio clip asset from the asset loader.

func (*AudioLoader) GetAudioClips

func (al *AudioLoader) GetAudioClips() map[string]AssetResource[AudioClipAsset]

GetAudioClips gets all audio clip assets from the asset loader.

func (*AudioLoader) GetAudioStream

func (al *AudioLoader) GetAudioStream(name string) (AssetResource[AudioStreamAsset], bool)

GetAudioStream gets a audio stream asset from the asset loader.

func (*AudioLoader) GetAudioStreams

func (al *AudioLoader) GetAudioStreams() map[string]AssetResource[AudioStreamAsset]

GetAudioStreams gets all audio stream assets from the asset loader.

func (*AudioLoader) LoadAudioClip

func (al *AudioLoader) LoadAudioClip(name, path string) (AssetResource[AudioClipAsset], error)

LoadAudioClip loads a audio clip asset into the asset loader.

func (*AudioLoader) LoadAudioStream

func (al *AudioLoader) LoadAudioStream(name, path string) (AssetResource[AudioStreamAsset], error)

LoadAudioStream loads a audio stream asset into the asset loader.

func (*AudioLoader) RemoveAudioClip

func (al *AudioLoader) RemoveAudioClip(name string)

RemoveAudioClip removes a audio clip asset from the asset loader.

func (*AudioLoader) RemoveAudioStream

func (al *AudioLoader) RemoveAudioStream(name string)

RemoveAudioStream removes a audio stream asset from the asset loader.

type AudioStreamAsset

type AudioStreamAsset struct {
	Stream beep.StreamSeekCloser
	Format beep.Format
}

AudioStreamAsset is a type that defines a audio stream asset. It holds the stream and format. A audio stream asset is a audio asset that is streamed from the disk.

type ErrAssetAlreadyExists

type ErrAssetAlreadyExists struct {
	// contains filtered or unexported fields
}

ErrAssetAlreadyExists is an error that is returned when the asset already exists.

func (ErrAssetAlreadyExists) Error

func (e ErrAssetAlreadyExists) Error() string

type ErrInvalidFileFormat

type ErrInvalidFileFormat struct {
	// contains filtered or unexported fields
}

ErrInvalidFileFormat is an error that is returned when the file format is invalid.

func (ErrInvalidFileFormat) Error

func (e ErrInvalidFileFormat) Error() string

type ErrUnsupportedAssetType added in v0.3.3

type ErrUnsupportedAssetType struct {
	// contains filtered or unexported fields
}

ErrUnsupportedAssetType is an error that is returned when the asset type is unsupported.

func (ErrUnsupportedAssetType) Error added in v0.3.3

func (e ErrUnsupportedAssetType) Error() string

type FontAsset

type FontAsset struct {
	Face  font.Face
	Atlas *text.Atlas
}

FontAsset is a type that defines a font asset. It holds the font face and atlas.

type FontLoader

type FontLoader struct {
	// contains filtered or unexported fields
}

func NewFontLoader

func NewFontLoader() FontLoader

func (*FontLoader) EachFont

func (fl *FontLoader) EachFont(fn func(name string, asset AssetResource[FontAsset]))

EachFont iterates over all font assets in the asset loader.

func (*FontLoader) GetFont

func (fl *FontLoader) GetFont(name string) (AssetResource[FontAsset], bool)

GetFont gets a font asset from the asset loader.

func (*FontLoader) GetFonts

func (fl *FontLoader) GetFonts() map[string]AssetResource[FontAsset]

GetFonts gets all font assets from the asset loader.

func (*FontLoader) LoadFont

func (fl *FontLoader) LoadFont(name, path string, options *truetype.Options, runeSets ...[]rune) (AssetResource[FontAsset], error)

LoadFont loads a font asset into the asset loader.

func (*FontLoader) RemoveFont

func (fl *FontLoader) RemoveFont(name string)

RemoveFont removes a font asset from the asset loader.

type Loader

type Loader struct {
	FontLoader
	TextureLoader
	AudioLoader
}

func NewLoader

func NewLoader() *Loader

NewLoader creates a new asset loader.

type TextureAsset

type TextureAsset struct {
	Picture pixel.Picture
}

TextureAsset is a type that defines a texture asset. It holds the picture.

type TextureLoader

type TextureLoader struct {
	// contains filtered or unexported fields
}

func NewTextureLoader

func NewTextureLoader() TextureLoader

func (*TextureLoader) EachTexture

func (tl *TextureLoader) EachTexture(fn func(name string, asset AssetResource[TextureAsset]))

EachTexture iterates over all texture assets in the asset loader.

func (*TextureLoader) GetTexture

func (tl *TextureLoader) GetTexture(name string) (AssetResource[TextureAsset], bool)

GetTexture gets a texture asset from the asset loader.

func (*TextureLoader) GetTextures

func (tl *TextureLoader) GetTextures() map[string]AssetResource[TextureAsset]

GetTextures gets all texture assets from the asset loader.

func (*TextureLoader) LoadTexture

func (tl *TextureLoader) LoadTexture(name string, path string) (AssetResource[TextureAsset], error)

LoadTexture loads a texture asset into the asset loader.

func (*TextureLoader) RemoveTexture

func (tl *TextureLoader) RemoveTexture(name string)

RemoveTexture removes a texture asset from the asset loader.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL