130 lines
3.2 KiB
Go
130 lines
3.2 KiB
Go
package conv
|
|
|
|
import (
|
|
"fmt"
|
|
"image/color"
|
|
"log"
|
|
|
|
"github.com/faiface/pixel"
|
|
|
|
"ur.gs/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
|
|
|
|
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
|
|
}
|
|
|
|
var transparent = color.RGBA{0, 0, 0, 0}
|
|
|
|
// WIP. Try to convert the pixeldata into a picture.
|
|
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)))
|
|
|
|
log.Printf("%v %v: width=%v height=%v", name, idx, sprite.Width, sprite.Height)
|
|
|
|
for y := 0; y < int(sprite.Height); y++ {
|
|
// Start with all bytes transparent
|
|
for x := 0; x < int(sprite.Width); x++ {
|
|
pic.Pix[pic.Index(pixel.V(float64(x), float64(y)))] = transparent
|
|
}
|
|
|
|
row := sprite.Rows[y]
|
|
log.Printf("%#v", row)
|
|
pixels := row[0 : len(row)-1] // Strip off the record separator (0x00)
|
|
|
|
// Not really clear on what this does yet. Aligned with sprite width in
|
|
// many cases but can also vary above and below that value.
|
|
u0 := int(pixels[0])
|
|
pixels = pixels[1:len(pixels)]
|
|
|
|
// In some cases, the column data is indented relative to the start of
|
|
// the row. Certainly true when u0 == 0x80, perhaps in other cases
|
|
// too.
|
|
//
|
|
// Definitely not the case when u0 == 0x01 - there aren't enough bytes
|
|
// in that case for it to be anything but pixeldata
|
|
xOffset := 0
|
|
|
|
// Do nothing if we're out of pixels
|
|
if u0 == 0x80 {
|
|
log.Printf("Handling 0x80: %#v", pixels)
|
|
xOffset = int(pixels[0])
|
|
pixels = pixels[1:len(pixels)]
|
|
|
|
// Sometimes, pixels is now empty. e.g. l_ivy02 sprite 6
|
|
if len(pixels) > 3 {
|
|
|
|
// For tiles, this has an inverse relationship with u0. Seems to add
|
|
// up to 0x42 in all cases, which matches byte 3 of the header?
|
|
//_ = int(pixels[0])
|
|
|
|
pixels = pixels[1:len(pixels)]
|
|
|
|
// On tiles, this removes some junk around the edge, but doesn't
|
|
// seem to be reasonable in-general?
|
|
pixels = pixels[0 : len(pixels)-2]
|
|
}
|
|
}
|
|
|
|
log.Printf(
|
|
"%v %d: len(row)=%v, len(pixels)=%v sprWidth=%v u0=%v xOffset=%v",
|
|
name, idx, len(row), len(pixels), sprite.Width, u0, xOffset,
|
|
)
|
|
|
|
for x, b := range pixels {
|
|
vec := pixel.V(float64(xOffset+x), float64(y))
|
|
if err := setPaletteColor(pic, vec, b); err != nil {
|
|
log.Printf("%s %d: %d,%d: %v", name, idx, x, y, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
return pic
|
|
}
|
|
|
|
func setPaletteColor(pic *pixel.PictureData, point pixel.Vec, colorIdx byte) error {
|
|
idx := pic.Index(point)
|
|
|
|
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
|
|
}
|