Sprites in .obj files are composed of Y null-separated records with a probable type field

This commit is contained in:
2018-03-21 22:49:46 +00:00
parent e3a2096b00
commit 5ee1ceb532
4 changed files with 88 additions and 59 deletions

View File

@@ -1,8 +1,9 @@
package main
import (
"bytes"
"flag"
// "image/color"
"image/color"
"log"
"math"
"os"
@@ -96,7 +97,7 @@ func (e *env) run() {
"new state: numObj=%d object=%d (%s) numFrames=%d sprite=%d zoom=%.2f",
state.env.set.Count(),
state.objIdx,
state.env.set.Palette[state.objIdx],
state.env.set.Palette[state.objIdx], // FIXME: palette is a confusing name
state.curObject().NumSprites,
state.spriteIdx,
state.zoom,
@@ -115,22 +116,57 @@ func (s *state) runStep(pWin *pixelgl.Window) *state {
return &newState
}
// WIP. Try to convert the pixeldata into a picture.
func spriteToPic(sprite *data.Sprite) *pixel.PictureData {
pic := pixel.MakePictureData(pixel.R(float64(0), float64(0), float64(sprite.Width), float64(sprite.Height)))
buf := bytes.NewBuffer(sprite.PixelData)
// The pixeldata seems to be formed of Y null-terminated records, with
// varying numbers of bytes in each row. Probably [type, *data] but ignore
// type for now.
//
// Theory: perhaps the data in each X is centered around the origin?
for y := 0; y < int(sprite.Height); y++ {
rowData, err := buf.ReadBytes(0)
if err != nil {
log.Printf("Error at y=%d: %v", y, err)
continue
}
leftPad := (int(sprite.Width) - len(rowData)) / 2
for x, b := range rowData {
idx := pic.Index(pixel.V(float64(leftPad+x), float64(y)))
pic.Pix[idx] = color.RGBA{
R: b,
G: b,
B: b,
A: 255,
}
}
}
return pic
}
func (s *state) present(pWin *pixelgl.Window) {
// obj := s.curObject()
// sprite := obj.Sprites[s.spriteIdx]
obj := s.curObject()
sprite := obj.Sprites[s.spriteIdx]
pic := spriteToPic(sprite)
center := pWin.Bounds().Center()
cam := pixel.IM
// cam = cam.ScaledXY(pixel.ZV, pixel.Vec{1.0, -1.0}) // invert the Y axis
cam = cam.Scaled(center, s.zoom) // apply current zoom factor
cam = cam.ScaledXY(center, pixel.Vec{1.0, -1.0}) // invert the Y axis
cam = cam.Scaled(center, s.zoom) // apply current zoom factor
//cam = cam.Moved(center.Sub(s.camPos)) // Make it central
//cam = cam.Rotated(center, -0.785) // Apply isometric angle
s.cam = cam
pWin.SetMatrix(s.cam)
pWin.Clear(colornames.White)
// pixel.NewSprite(pic, pic.Bounds()).Draw(pWin, pixel.IM.Moved(center))
pWin.Clear(colornames.Black)
pixel.NewSprite(pic, pic.Bounds()).Draw(pWin, pixel.IM.Moved(center))
}
func (s *state) handleKeys(pWin *pixelgl.Window) {