ops.go raw

   1  // SPDX-License-Identifier: Unlicense OR MIT
   2  
   3  package ops
   4  
   5  import (
   6  	"encoding/binary"
   7  	"math"
   8  
   9  	"github.com/p9c/p9/pkg/gel/gio/f32"
  10  	"github.com/p9c/p9/pkg/gel/gio/internal/byteslice"
  11  	"github.com/p9c/p9/pkg/gel/gio/internal/opconst"
  12  	"github.com/p9c/p9/pkg/gel/gio/internal/scene"
  13  )
  14  
  15  func DecodeCommand(d []byte) scene.Command {
  16  	var cmd scene.Command
  17  	copy(byteslice.Uint32(cmd[:]), d)
  18  	return cmd
  19  }
  20  
  21  func EncodeCommand(out []byte, cmd scene.Command) {
  22  	copy(out, byteslice.Uint32(cmd[:]))
  23  }
  24  
  25  func DecodeTransform(data []byte) (t f32.Affine2D) {
  26  	if opconst.OpType(data[0]) != opconst.TypeTransform {
  27  		panic("invalid op")
  28  	}
  29  	data = data[1:]
  30  	data = data[:4*6]
  31  
  32  	bo := binary.LittleEndian
  33  	a := math.Float32frombits(bo.Uint32(data))
  34  	b := math.Float32frombits(bo.Uint32(data[4*1:]))
  35  	c := math.Float32frombits(bo.Uint32(data[4*2:]))
  36  	d := math.Float32frombits(bo.Uint32(data[4*3:]))
  37  	e := math.Float32frombits(bo.Uint32(data[4*4:]))
  38  	f := math.Float32frombits(bo.Uint32(data[4*5:]))
  39  	return f32.NewAffine2D(a, b, c, d, e, f)
  40  }
  41  
  42  // DecodeSave decodes the state id of a save op.
  43  func DecodeSave(data []byte) int {
  44  	if opconst.OpType(data[0]) != opconst.TypeSave {
  45  		panic("invalid op")
  46  	}
  47  	bo := binary.LittleEndian
  48  	return int(bo.Uint32(data[1:]))
  49  }
  50  
  51  // DecodeLoad decodes the state id and mask of a load op.
  52  func DecodeLoad(data []byte) (int, opconst.StateMask) {
  53  	if opconst.OpType(data[0]) != opconst.TypeLoad {
  54  		panic("invalid op")
  55  	}
  56  	bo := binary.LittleEndian
  57  	return int(bo.Uint32(data[2:])), opconst.StateMask(data[1])
  58  }
  59