type cleanup

This commit is contained in:
Alex Yatskov 2018-12-31 09:59:58 -08:00
parent 3f3a5aa5b1
commit 9a9183a531
5 changed files with 43 additions and 37 deletions

View File

@ -4,6 +4,7 @@ import (
"encoding/binary" "encoding/binary"
"io" "io"
"github.com/FooSoft/lazarus/math"
"github.com/FooSoft/lazarus/streaming" "github.com/FooSoft/lazarus/streaming"
) )
@ -32,10 +33,8 @@ type Direction struct {
} }
type Frame struct { type Frame struct {
Width int Size math.Vec2i
Height int Offset math.Vec2i
OffsetX int
OffsetY int
Data []byte Data []byte
} }
@ -79,15 +78,13 @@ func NewFromReader(reader io.ReadSeeker) (*Sprite, error) {
return nil, err return nil, err
} }
frame := Frame{ var (
int(frameHead.Width), size = math.Vec2i{X: int(frameHead.Width), Y: int(frameHead.Height)}
int(frameHead.Height), offset = math.Vec2i{X: int(frameHead.OffsetX), Y: int(frameHead.OffsetY)}
int(frameHead.OffsetX), frame = Frame{size, offset, data}
int(frameHead.OffsetY), direction = &sprite.Directions[i/int(fileHead.FramesPerDir)]
data, )
}
direction := &sprite.Directions[i/int(fileHead.FramesPerDir)]
direction.Frames = append(direction.Frames, frame) direction.Frames = append(direction.Frames, frame)
} }

View File

@ -16,3 +16,10 @@ type Rect4i struct {
W int W int
H int H int
} }
type Color4b struct {
R byte
G byte
B byte
A byte
}

View File

@ -10,27 +10,27 @@ import (
type Texture struct { type Texture struct {
size math.Vec2i size math.Vec2i
glHandle uint32 glTexture uint32
} }
func newTextureFromRgba(colors []color.RGBA, width, height int) (*Texture, error) { func newTextureFromRgba(colors []color.RGBA, size math.Vec2i) (*Texture, error) {
var glHandleLast int32 var glLastTexture int32
gl.GetIntegerv(gl.TEXTURE_BINDING_2D, &glHandleLast) gl.GetIntegerv(gl.TEXTURE_BINDING_2D, &glLastTexture)
var glHandle uint32 var glTexture uint32
gl.GenTextures(1, &glHandle) gl.GenTextures(1, &glTexture)
gl.BindTexture(gl.TEXTURE_2D, glHandle) gl.BindTexture(gl.TEXTURE_2D, glTexture)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
gl.PixelStorei(gl.UNPACK_ROW_LENGTH, 0) gl.PixelStorei(gl.UNPACK_ROW_LENGTH, 0)
gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, int32(width), int32(height), 0, gl.RGBA, gl.UNSIGNED_BYTE, unsafe.Pointer(&colors[0])) gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, int32(size.X), int32(size.Y), 0, gl.RGBA, gl.UNSIGNED_BYTE, unsafe.Pointer(&colors[0]))
gl.BindTexture(gl.TEXTURE_2D, uint32(glHandleLast)) gl.BindTexture(gl.TEXTURE_2D, uint32(glLastTexture))
return &Texture{size: math.Vec2i{X: width, Y: height}, glHandle: glHandle}, nil return &Texture{size, glTexture}, nil
} }
func (t *Texture) Handle() Handle { func (t *Texture) Handle() Handle {
return Handle(t.glHandle) return Handle(t.glTexture)
} }
func (t *Texture) Size() math.Vec2i { func (t *Texture) Size() math.Vec2i {
@ -38,10 +38,12 @@ func (t *Texture) Size() math.Vec2i {
} }
func (t *Texture) Destroy() error { func (t *Texture) Destroy() error {
if t.glHandle != 0 { if t == nil || t.glTexture == 0 {
gl.DeleteTextures(1, &t.glHandle) return nil
t.glHandle = 0
} }
gl.DeleteTextures(1, &t.glTexture)
t.glTexture = 0
return nil return nil
} }

View File

@ -69,8 +69,8 @@ func (w *Window) Destroy() error {
return nil return nil
} }
func (w *Window) CreateTextureRgba(colors []color.RGBA, width, height int) (*Texture, error) { func (w *Window) CreateTextureRgba(colors []color.RGBA, size math.Vec2i) (*Texture, error) {
return newTextureFromRgba(colors, width, height) return newTextureFromRgba(colors, size)
} }
func (w *Window) RenderTexture(texture *Texture, position math.Vec2i) { func (w *Window) RenderTexture(texture *Texture, position math.Vec2i) {

View File

@ -74,8 +74,8 @@ func (s *scene) Advance(window *platform.Window) error {
} }
frame := direction.Frames[frameIndex] frame := direction.Frames[frameIndex]
imgui.SliderInt("Frame", &frameIndex, 0, int32(len(direction.Frames))-1) imgui.SliderInt("Frame", &frameIndex, 0, int32(len(direction.Frames))-1)
imgui.Text(fmt.Sprintf("Size: %dx%d", frame.Width, frame.Height)) imgui.Text(fmt.Sprintf("Size: %+v", frame.Size))
imgui.Text(fmt.Sprintf("Offset: %dx%d", frame.OffsetX, frame.OffsetY)) imgui.Text(fmt.Sprintf("Offset: %+v", frame.Offset))
if directionIndex != s.directionIndex || frameIndex != s.frameIndex { if directionIndex != s.directionIndex || frameIndex != s.frameIndex {
s.directionIndex = directionIndex s.directionIndex = directionIndex
@ -89,10 +89,10 @@ func (s *scene) Advance(window *platform.Window) error {
func (s *scene) updateTexture(window *platform.Window) error { func (s *scene) updateTexture(window *platform.Window) error {
frame := s.sprite.Directions[s.directionIndex].Frames[s.frameIndex] frame := s.sprite.Directions[s.directionIndex].Frames[s.frameIndex]
colors := make([]color.RGBA, frame.Width*frame.Height) colors := make([]color.RGBA, frame.Size.X*frame.Size.Y)
for y := 0; y < frame.Height; y++ { for y := 0; y < frame.Size.Y; y++ {
for x := 0; x < frame.Width; x++ { for x := 0; x < frame.Size.X; x++ {
colors[y*frame.Width+x] = s.palette.Colors[frame.Data[y*frame.Width+x]] colors[y*frame.Size.X+x] = s.palette.Colors[frame.Data[y*frame.Size.X+x]]
} }
} }
@ -103,7 +103,7 @@ func (s *scene) updateTexture(window *platform.Window) error {
} }
var err error var err error
s.texture, err = window.CreateTextureRgba(colors, frame.Width, frame.Height) s.texture, err = window.CreateTextureRgba(colors, frame.Size)
if err != nil { if err != nil {
return err return err
} }