Пример #1
0
// NewImage will create a new texture for this image and return the *Image. If the
// file does not exist or cannot be decoded it will return an error.
func NewImage(path string) (*Image, error) {
	//we do this first time to check the image before volitile load
	imgFile, new_err := file.NewFile(path)
	defer imgFile.Close()
	if new_err != nil {
		return nil, new_err
	}

	decoded_img, _, img_err := image.Decode(imgFile)
	if img_err != nil {
		return nil, img_err
	}

	bounds := decoded_img.Bounds()
	new_image := &Image{
		filePath: path,
		mipmaps:  false,
		Texture: &Texture{
			Width:  int32(bounds.Dx()),
			Height: int32(bounds.Dy()),
		},
	}

	registerVolatile(new_image)
	return new_image, nil
}
Пример #2
0
// Decode will get the file at the path provided. It will then send it to the decoder
// that will handle its file type by the extention on the path. Supported formats
// are wav, ogg, and flac. If there is an error retrieving the file or decoding it,
// will return that error.
func Decode(filepath string) (Decoder, error) {
	src, err := file.NewFile(filepath)
	if err != nil {
		return nil, err
	}

	base := decoderBase{
		src:      src,
		src_path: filepath,
	}

	var decoder Decoder
	switch file.Ext(filepath) {
	case ".wav":
		decoder = &waveDecoder{decoderBase: base}
	case ".ogg":
		decoder = &vorbisDecoder{decoderBase: base}
	case ".flac":
		decoder = &flacDecoder{decoderBase: base}
	default:
		src.Close()
		return nil, errors.New("unsupported audio file extention")
	}

	if err = decoder.read(); err != nil {
		src.Close()
		return nil, err
	}

	return decoder, nil
}
Пример #3
0
func (rast *imageFontRasterizer) loadVolatile() bool {
	glyph_rune_hints := []rune(rast.glyph_hints)

	imgFile, err := file.NewFile(rast.filepath)
	if err != nil {
		return false
	}
	defer imgFile.Close()

	img, _, err := image.Decode(imgFile)
	if err != nil {
		return false
	}

	rast.glyphs = make(map[rune]glyphData)
	glyphsPerRow := int32(16)
	glyphsPerCol := (int32(len(glyph_rune_hints)) / glyphsPerRow) + 1
	rast.advance = img.Bounds().Dx() / len(glyph_rune_hints)
	rast.height = img.Bounds().Dy()
	rast.ascent = rast.height
	rast.descent = 0
	image_width := pow2(uint32(int32(rast.advance) * glyphsPerRow))
	image_height := pow2(uint32(int32(rast.height) * glyphsPerCol))

	rgba := image.NewRGBA(image.Rect(0, 0, int(image_width), int(image_height)))

	var gx, gy int
	for i := 0; i < len(glyph_rune_hints); i++ {
		dst_rec := image.Rect(gx, gy, gx+rast.advance, gy+rast.height)
		draw.Draw(rgba, dst_rec, img, image.Pt(i*rast.advance, 0), draw.Src)

		rast.glyphs[glyph_rune_hints[i]] = glyphData{
			rec:             NewQuad(int32(gx), int32(gy), int32(rast.advance), int32(rast.height), int32(image_width), int32(image_height)),
			leftSideBearing: 0,
			advanceWidth:    rast.advance,
			topSideBearing:  0,
			advanceHeight:   rast.height + 5,
			ascent:          rast.height,
			descent:         0,
		}

		if i%16 == 0 {
			gx = 0
			gy += rast.height
		} else {
			gx += rast.advance
		}
	}

	rast.Texture, err = newImageTexture(rgba, false)
	return err == nil
}
Пример #4
0
// loadConfig will find the config file (works with bundle) and load it into the
// struct and the return it. If the config does not exist it will return a default
// config. If there was an issue reading the file it will return the error, possibly
// permission errors.
func loadConfig() (*windowConfig, error) {
	var config windowConfig
	path := path.Join(".", config_file_name)

	if _, err := file.NewFileData(path); os.IsNotExist(err) {
		return &config, nil //no added config bail out early
	}

	config_file, file_err := file.NewFile(config_file_name)
	if file_err != nil {
		return nil, file_err
	}

	if _, err := toml.DecodeReader(config_file, &config); err != nil {
		return nil, err
	}

	return &config, nil
}
Пример #5
0
// loadVolatile will create the volatile objects
func (img *Image) loadVolatile() bool {
	imgFile, new_err := file.NewFile(img.filePath)
	defer imgFile.Close()
	if new_err != nil {
		return false
	}

	decoded_img, _, img_err := image.Decode(imgFile)
	if img_err != nil {
		return false
	}

	img.Texture, img_err = newImageTexture(decoded_img, img.mipmaps)
	if img_err != nil {
		return false
	}

	return true
}