1
Fork 0

use internal type for colors

This commit is contained in:
Alex Yatskov 2018-12-31 10:54:33 -08:00
parent 9a9183a531
commit 90ce4409ce
6 changed files with 44 additions and 16 deletions

View File

@ -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

View File

@ -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

View File

@ -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)
}

View File

@ -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()

View File

@ -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)
}
}

View File

@ -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
}