86 lines
1.8 KiB
Go
86 lines
1.8 KiB
Go
package conv
|
|
|
|
import (
|
|
"fmt"
|
|
"image/color"
|
|
"log"
|
|
|
|
"github.com/faiface/pixel"
|
|
|
|
"ur.gs/ordoor/internal/data"
|
|
)
|
|
|
|
var transparent = color.RGBA{0, 0, 0, 0}
|
|
|
|
// 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
|
|
|
|
Pic *pixel.PictureData
|
|
Spr *pixel.Sprite
|
|
}
|
|
|
|
type Object struct {
|
|
Name string
|
|
Sprites []Sprite
|
|
}
|
|
|
|
func ConvertObject(rawObj *data.Object, name string) *Object {
|
|
out := &Object{
|
|
Name: name,
|
|
Sprites: make([]Sprite, len(rawObj.Sprites)),
|
|
}
|
|
|
|
for i, rawSpr := range rawObj.Sprites {
|
|
pic := spriteToPic(name, i, rawSpr)
|
|
out.Sprites[i] = Sprite{
|
|
Width: int(rawSpr.Width),
|
|
Height: int(rawSpr.Height),
|
|
Pic: pic,
|
|
Spr: pixel.NewSprite(pic, pic.Bounds()),
|
|
}
|
|
}
|
|
|
|
return out
|
|
}
|
|
|
|
func spriteToPic(name string, idx int, sprite *data.Sprite) *pixel.PictureData {
|
|
pic := pixel.MakePictureData(pixel.R(float64(0), float64(0), float64(sprite.Width), float64(sprite.Height)))
|
|
width := int(sprite.Width)
|
|
height := int(sprite.Height)
|
|
|
|
log.Printf("%v %v: width=%v height=%v", name, idx, width, height)
|
|
|
|
for y := 0; y < height; y++ {
|
|
for x := 0; x < width; x++ {
|
|
b := sprite.Data[y*width+x]
|
|
|
|
// Update the picture
|
|
if err := setPaletteColor(pic, x, y, b); err != nil {
|
|
log.Printf("%s %d: %d,%d: %v", name, idx, x, y, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
return pic
|
|
}
|
|
|
|
func setPaletteColor(pic *pixel.PictureData, x int, y int, colorIdx byte) error {
|
|
vec := pixel.V(float64(x), float64(y))
|
|
idx := pic.Index(vec)
|
|
|
|
if idx > len(pic.Pix)-1 {
|
|
return fmt.Errorf("Got index %v which exceeds bounds", idx)
|
|
}
|
|
|
|
r, g, b, a := data.ColorPalette[int(colorIdx)].RGBA()
|
|
color := color.RGBA{uint8(r), uint8(g), uint8(b), uint8(a)}
|
|
|
|
pic.Pix[idx] = color
|
|
return nil
|
|
}
|