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

View File

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

View File

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

View File

@ -69,8 +69,8 @@ func (w *Window) Destroy() error {
return nil
}
func (w *Window) CreateTextureRgba(colors []color.RGBA, width, height int) (*Texture, error) {
return newTextureFromRgba(colors, width, height)
func (w *Window) CreateTextureRgba(colors []color.RGBA, size math.Vec2i) (*Texture, error) {
return newTextureFromRgba(colors, size)
}
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]
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("Offset: %dx%d", frame.OffsetX, frame.OffsetY))
imgui.Text(fmt.Sprintf("Size: %+v", frame.Size))
imgui.Text(fmt.Sprintf("Offset: %+v", frame.Offset))
if directionIndex != s.directionIndex || frameIndex != s.frameIndex {
s.directionIndex = directionIndex
@ -89,10 +89,10 @@ func (s *scene) Advance(window *platform.Window) error {
func (s *scene) updateTexture(window *platform.Window) error {
frame := s.sprite.Directions[s.directionIndex].Frames[s.frameIndex]
colors := make([]color.RGBA, frame.Width*frame.Height)
for y := 0; y < frame.Height; y++ {
for x := 0; x < frame.Width; x++ {
colors[y*frame.Width+x] = s.palette.Colors[frame.Data[y*frame.Width+x]]
colors := make([]color.RGBA, frame.Size.X*frame.Size.Y)
for y := 0; y < frame.Size.Y; y++ {
for x := 0; x < frame.Size.X; 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
s.texture, err = window.CreateTextureRgba(colors, frame.Width, frame.Height)
s.texture, err = window.CreateTextureRgba(colors, frame.Size)
if err != nil {
return err
}