Skip to content

Commit

Permalink
added loadedspritesheet and loadedfont functions (#786)
Browse files Browse the repository at this point in the history
* added loadedspritesheet and loadedfont functions to access preloaded spritesheets and fonts
  • Loading branch information
Noofbiz authored Mar 11, 2022
1 parent e1c4b2e commit 14b1e79
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
22 changes: 22 additions & 0 deletions common/font.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package common

import (
"errors"
"fmt"
"image"
"image/color"
Expand All @@ -20,6 +21,8 @@ var (
dpi = float64(72)
)

var fontCache []*Font

// Font keeps track of a specific Font. Fonts are explicit instances of a font file,
// including the Size and Color. A separate font will have to be generated to get
// different sizes and colors of the same font file.
Expand All @@ -32,6 +35,24 @@ type Font struct {
face font.Face
}

// LoadedFont returns a Font that was previously loaded via CreatePreloaded
func LoadedFont(url string, size float64, bg, fg color.Color) (*Font, error) {
idx := -1
for i, fnt := range fontCache {
r, g, b, a := bg.RGBA()
r2, g2, b2, a2 := fnt.BG.RGBA()
r3, g3, b3, a3 := fg.RGBA()
r4, g4, b4, a4 := fnt.FG.RGBA()
if fnt.URL == url && fnt.Size == size && r == r2 && g == g2 && b == b2 && a == a2 && r3 == r4 && g3 == g4 && b3 == b4 && a3 == a4 {
idx = i
}
}
if idx < 0 {
return nil, errors.New("No font matching that description was found.")
}
return fontCache[idx], nil
}

// Create is for loading fonts from the disk, given a location
func (f *Font) Create() error {
// Read and parse the font
Expand Down Expand Up @@ -72,6 +93,7 @@ func (f *Font) CreatePreloaded() error {
DPI: dpi,
Hinting: font.HintingFull,
})
fontCache = append(fontCache, f)
return nil
}

Expand Down
16 changes: 15 additions & 1 deletion common/spritesheet.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package common

import (
"errors"
"log"

"github.com/EngoEngine/engo"
"github.com/EngoEngine/engo/math"
"github.com/EngoEngine/gl"
)

var spritesheetCache = make(map[string]*Spritesheet)

// Spritesheet is a class that stores a set of tiles from a file, used by tilemaps and animations
type Spritesheet struct {
texture *gl.Texture // The original texture
Expand All @@ -22,16 +25,27 @@ type SpriteRegion struct {
Width, Height int
}

// LoadedSpritesheet returns a Spritesheet that has already been created by New*
func LoadedSpritesheet(url string) (*Spritesheet, error) {
sheet, ok := spritesheetCache[url]
if !ok {
return nil, errors.New("Unable to find Spritesheet at URL " + url)
}
return sheet, nil
}

// NewAsymmetricSpritesheetFromTexture creates a new AsymmetricSpriteSheet from a
// TextureResource. The data provided is the location and size of the sprites
func NewAsymmetricSpritesheetFromTexture(tr *TextureResource, spriteRegions []SpriteRegion) *Spritesheet {
return &Spritesheet{
sheet := &Spritesheet{
texture: tr.Texture,
width: tr.Width,
height: tr.Height,
cells: spriteRegions,
cache: make(map[int]Texture),
}
spritesheetCache[tr.URL()] = sheet
return sheet
}

// NewAsymmetricSpritesheetFromFile creates a new AsymmetricSpriteSheet from a
Expand Down

0 comments on commit 14b1e79

Please sign in to comment.