70 lines
1.3 KiB
Go
70 lines
1.3 KiB
Go
package conv
|
|
|
|
import (
|
|
"github.com/hajimehoshi/ebiten"
|
|
|
|
"code.ur.gs/lupine/ordoor/internal/data"
|
|
)
|
|
|
|
// Important conversions:
|
|
//
|
|
// * Width & height now stored using int
|
|
// * Colour data is now 32-bit rather than using a palette
|
|
type Sprite struct {
|
|
Width int
|
|
Height int
|
|
|
|
Image *ebiten.Image
|
|
}
|
|
|
|
type Object struct {
|
|
Name string
|
|
Sprites []*Sprite
|
|
}
|
|
|
|
func MapByName(objects []*Object) map[string]*Object {
|
|
out := make(map[string]*Object, len(objects))
|
|
|
|
for _, obj := range objects {
|
|
out[obj.Name] = obj
|
|
}
|
|
|
|
return out
|
|
}
|
|
|
|
func ConvertObject(rawObj *data.Object, name string) (*Object, error) {
|
|
out := &Object{
|
|
Name: name,
|
|
Sprites: make([]*Sprite, len(rawObj.Sprites)),
|
|
}
|
|
|
|
for i, rawSpr := range rawObj.Sprites {
|
|
w := int(rawSpr.Width)
|
|
h := int(rawSpr.Height)
|
|
data := spriteToData(name, i, rawSpr)
|
|
|
|
image, err := ebiten.NewImage(w, h, ebiten.FilterDefault)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
_ = image.ReplacePixels(data)
|
|
|
|
out.Sprites[i] = &Sprite{Width: w, Height: h, Image: image}
|
|
}
|
|
|
|
return out, nil
|
|
}
|
|
|
|
func spriteToData(name string, idx int, sprite *data.Sprite) []byte {
|
|
width := int(sprite.Width)
|
|
height := int(sprite.Height)
|
|
out := make([]byte, 0, 4*width*height)
|
|
|
|
for _, palette := range sprite.Data {
|
|
color := data.ColorPalette[int(palette)]
|
|
out = append(out, color.R, color.G, color.B, color.A)
|
|
}
|
|
|
|
return out
|
|
}
|