Determine the RLE format for .obj file sprite pixeldata
This commit is contained in:
@@ -52,60 +52,52 @@ var transparent = color.RGBA{0, 0, 0, 0}
|
||||
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)
|
||||
log.Printf("%v %v: width=%v height=%v", name, idx, sprite.Width, sprite.Height)
|
||||
|
||||
for y := 0; y < int(sprite.Height); y++ {
|
||||
encoded := sprite.Rows[y]
|
||||
decoded := make([]byte, 0, int(sprite.Width))
|
||||
|
||||
// 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)
|
||||
for i := 0; i < len(encoded); i++ {
|
||||
b := encoded[i]
|
||||
|
||||
// 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)]
|
||||
// This appears to be a kind of RLE
|
||||
if b == 0 {
|
||||
continue // finished
|
||||
} else if b < 0x80 {
|
||||
// repeat the next byte this many times
|
||||
for j := 0; j < int(b); j++ {
|
||||
decoded = append(decoded, encoded[i+1])
|
||||
}
|
||||
|
||||
// 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
|
||||
i++ // skip the repeat byte
|
||||
} else if b == 0x80 {
|
||||
// transparent value, skip forward *x+1 rows
|
||||
skip := int(encoded[i+1])
|
||||
for i := 0; i < skip; i++ {
|
||||
decoded = append(decoded, byte(0x00))
|
||||
}
|
||||
|
||||
// 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)]
|
||||
i++ // skip the count byte
|
||||
} else {
|
||||
// take the next b-0x80 bytes literally
|
||||
literals := int(b) - 0x80
|
||||
for j := i + 1; j <= i+literals; j++ {
|
||||
decoded = append(decoded, encoded[j])
|
||||
}
|
||||
|
||||
// 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]
|
||||
i = i + literals
|
||||
}
|
||||
}
|
||||
|
||||
//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 {
|
||||
// Update the picture
|
||||
for x, b := range decoded {
|
||||
if err := setPaletteColor(pic, x, y, b); err != nil {
|
||||
log.Printf("%s %d: %d,%d: %v", name, idx, x, y, err)
|
||||
}
|
||||
}
|
||||
@@ -114,8 +106,9 @@ func spriteToPic(name string, idx int, sprite *data.Sprite) *pixel.PictureData {
|
||||
return pic
|
||||
}
|
||||
|
||||
func setPaletteColor(pic *pixel.PictureData, point pixel.Vec, colorIdx byte) error {
|
||||
idx := pic.Index(point)
|
||||
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)
|
||||
|
Reference in New Issue
Block a user