From 90ce4409cead352c9f892c5d21f11b18a71f38af Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Mon, 31 Dec 2018 10:54:33 -0800 Subject: [PATCH] use internal type for colors --- formats/dat/dat.go | 9 +++++---- math/math.go | 6 ++++++ platform/texture.go | 20 ++++++++++++++++++-- platform/window.go | 8 +++++--- tools/dc6/dc6.go | 11 +++++++---- tools/viewer/main.go | 6 +++--- 6 files changed, 44 insertions(+), 16 deletions(-) diff --git a/formats/dat/dat.go b/formats/dat/dat.go index 4680494..5b4c224 100644 --- a/formats/dat/dat.go +++ b/formats/dat/dat.go @@ -2,12 +2,13 @@ package dat import ( "encoding/binary" - imageColor "image/color" "io" + + "github.com/FooSoft/lazarus/math" ) type Palette struct { - Colors [256]imageColor.RGBA + Colors [256]math.Color3b } type color struct { @@ -24,7 +25,7 @@ func NewFromReader(reader io.Reader) (*Palette, error) { palette := new(Palette) for i, color := range colors { - palette.Colors[i] = imageColor.RGBA{color.R, color.G, color.B, 0xff} + palette.Colors[i] = math.Color3b{R: color.R, G: color.G, B: color.B} } return palette, nil @@ -34,7 +35,7 @@ func NewFromGrayscale() *Palette { palette := new(Palette) for i := 0; i < 256; i++ { value := uint8(i) - palette.Colors[i] = imageColor.RGBA{value, value, value, 0xff} + palette.Colors[i] = math.Color3b{R: value, G: value, B: value} } return palette diff --git a/math/math.go b/math/math.go index ebab7a5..14eefe9 100644 --- a/math/math.go +++ b/math/math.go @@ -17,6 +17,12 @@ type Rect4i struct { H int } +type Color3b struct { + R byte + G byte + B byte +} + type Color4b struct { R byte G byte diff --git a/platform/texture.go b/platform/texture.go index 2dfdad8..85fa62a 100644 --- a/platform/texture.go +++ b/platform/texture.go @@ -1,7 +1,6 @@ package platform import ( - "image/color" "unsafe" "github.com/FooSoft/lazarus/math" @@ -13,7 +12,7 @@ type Texture struct { glTexture uint32 } -func newTextureFromRgba(colors []color.RGBA, size math.Vec2i) (*Texture, error) { +func newTextureFromRgba(colors []math.Color4b, size math.Vec2i) (*Texture, error) { var glLastTexture int32 gl.GetIntegerv(gl.TEXTURE_BINDING_2D, &glLastTexture) @@ -29,6 +28,23 @@ func newTextureFromRgba(colors []color.RGBA, size math.Vec2i) (*Texture, error) return &Texture{size, glTexture}, nil } +func newTextureFromRgb(colors []math.Color3b, size math.Vec2i) (*Texture, error) { + var glLastTexture int32 + gl.GetIntegerv(gl.TEXTURE_BINDING_2D, &glLastTexture) + + 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.PixelStorei(gl.UNPACK_ALIGNMENT, 1) + gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGB, int32(size.X), int32(size.Y), 0, gl.RGB, gl.UNSIGNED_BYTE, unsafe.Pointer(&colors[0])) + + gl.BindTexture(gl.TEXTURE_2D, uint32(glLastTexture)) + return &Texture{size, glTexture}, nil +} + func (t *Texture) Handle() Handle { return Handle(t.glTexture) } diff --git a/platform/window.go b/platform/window.go index 128d8d0..6bd1a7a 100644 --- a/platform/window.go +++ b/platform/window.go @@ -1,8 +1,6 @@ package platform import ( - "image/color" - imgui "github.com/FooSoft/imgui-go" "github.com/FooSoft/lazarus/math" "github.com/FooSoft/lazarus/platform/imgui_backend" @@ -69,10 +67,14 @@ func (w *Window) Destroy() error { return nil } -func (w *Window) CreateTextureRgba(colors []color.RGBA, size math.Vec2i) (*Texture, error) { +func (w *Window) CreateTextureRgba(colors []math.Color4b, size math.Vec2i) (*Texture, error) { return newTextureFromRgba(colors, size) } +func (w *Window) CreateTextureRgb(colors []math.Color3b, size math.Vec2i) (*Texture, error) { + return newTextureFromRgb(colors, size) +} + func (w *Window) RenderTexture(texture *Texture, position math.Vec2i) { size := texture.Size() diff --git a/tools/dc6/dc6.go b/tools/dc6/dc6.go index dc76542..0e59ed1 100644 --- a/tools/dc6/dc6.go +++ b/tools/dc6/dc6.go @@ -4,6 +4,7 @@ import ( "flag" "fmt" "image" + "image/color" "image/png" "os" "path" @@ -39,10 +40,12 @@ func extractSprite(spritePath string, palette *dat.Palette, targetDir string) er for di, direction := range sprite.Directions { for fi, frame := range direction.Frames { - img := image.NewRGBA(image.Rect(0, 0, frame.Width, frame.Height)) - for y := 0; y < frame.Height; y++ { - for x := 0; x < frame.Width; x++ { - img.Set(x, y, palette.Colors[frame.Data[y*frame.Width+x]]) + img := image.NewRGBA(image.Rect(0, 0, frame.Size.X, frame.Size.Y)) + for y := 0; y < frame.Size.Y; y++ { + for x := 0; x < frame.Size.X; x++ { + colorSrc := palette.Colors[frame.Data[y*frame.Size.X+x]] + colorDst := color.RGBA{colorSrc.R, colorSrc.G, colorSrc.B, 0xff} + img.Set(x, y, colorDst) } } diff --git a/tools/viewer/main.go b/tools/viewer/main.go index c501274..0d66c75 100644 --- a/tools/viewer/main.go +++ b/tools/viewer/main.go @@ -3,7 +3,6 @@ package main import ( "flag" "fmt" - "image/color" "log" "os" "path/filepath" @@ -11,6 +10,7 @@ import ( imgui "github.com/FooSoft/imgui-go" "github.com/FooSoft/lazarus/formats/dat" "github.com/FooSoft/lazarus/formats/dc6" + "github.com/FooSoft/lazarus/math" "github.com/FooSoft/lazarus/platform" ) @@ -89,7 +89,7 @@ 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.Size.X*frame.Size.Y) + colors := make([]math.Color3b, 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.Size) + s.texture, err = window.CreateTextureRgb(colors, frame.Size) if err != nil { return err }