create texture interface, move to graphics

This commit is contained in:
Alex Yatskov 2018-12-31 17:42:00 -08:00
parent 0523e41732
commit dd260996de
5 changed files with 32 additions and 18 deletions

View File

@ -1,3 +1,11 @@
package graphics package graphics
type Handle uintptr import "github.com/FooSoft/lazarus/math"
type TextureId uintptr
type Texture interface {
Id() TextureId
Size() math.Vec2i
Destroy() error
}

View File

@ -18,8 +18,12 @@ func (*Context) Button(label string) bool {
return imgui.Button(label) return imgui.Button(label)
} }
func (*Context) Image(texture graphics.Handle, size math.Vec2i) { func (c *Context) Image(texture graphics.Texture) {
imgui.Image(imgui.TextureID(texture), imgui.Vec2{X: float32(size.X), Y: float32(size.Y)}) c.ImageSized(texture, texture.Size())
}
func (*Context) ImageSized(texture graphics.Texture, size math.Vec2i) {
imgui.Image(imgui.TextureID(texture.Id()), imgui.Vec2{X: float32(size.X), Y: float32(size.Y)})
} }
func (*Context) SliderInt(label string, value *int, min, max int) bool { func (*Context) SliderInt(label string, value *int, min, max int) bool {

View File

@ -8,12 +8,12 @@ import (
"github.com/go-gl/gl/v2.1/gl" "github.com/go-gl/gl/v2.1/gl"
) )
type Texture struct { type texture struct {
size math.Vec2i size math.Vec2i
glTexture uint32 glTexture uint32
} }
func newTextureFromRgba(colors []math.Color4b, size math.Vec2i) (*Texture, error) { func newTextureFromRgba(colors []math.Color4b, size math.Vec2i) (graphics.Texture, error) {
var glLastTexture int32 var glLastTexture int32
gl.GetIntegerv(gl.TEXTURE_BINDING_2D, &glLastTexture) gl.GetIntegerv(gl.TEXTURE_BINDING_2D, &glLastTexture)
@ -26,10 +26,10 @@ func newTextureFromRgba(colors []math.Color4b, size math.Vec2i) (*Texture, error
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.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(glLastTexture)) gl.BindTexture(gl.TEXTURE_2D, uint32(glLastTexture))
return &Texture{size, glTexture}, nil return &texture{size, glTexture}, nil
} }
func newTextureFromRgb(colors []math.Color3b, size math.Vec2i) (*Texture, error) { func newTextureFromRgb(colors []math.Color3b, size math.Vec2i) (graphics.Texture, error) {
var glLastTexture int32 var glLastTexture int32
gl.GetIntegerv(gl.TEXTURE_BINDING_2D, &glLastTexture) gl.GetIntegerv(gl.TEXTURE_BINDING_2D, &glLastTexture)
@ -43,18 +43,18 @@ func newTextureFromRgb(colors []math.Color3b, size math.Vec2i) (*Texture, error)
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.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)) gl.BindTexture(gl.TEXTURE_2D, uint32(glLastTexture))
return &Texture{size, glTexture}, nil return &texture{size, glTexture}, nil
} }
func (t *Texture) Handle() graphics.Handle { func (t *texture) Id() graphics.TextureId {
return graphics.Handle(t.glTexture) return graphics.TextureId(t.glTexture)
} }
func (t *Texture) Size() math.Vec2i { func (t *texture) Size() math.Vec2i {
return t.size return t.size
} }
func (t *Texture) Destroy() error { func (t *texture) Destroy() error {
if t == nil || t.glTexture == 0 { if t == nil || t.glTexture == 0 {
return nil return nil
} }

View File

@ -3,6 +3,7 @@ package platform
import ( import (
"log" "log"
"github.com/FooSoft/lazarus/graphics"
"github.com/FooSoft/lazarus/math" "github.com/FooSoft/lazarus/math"
"github.com/FooSoft/lazarus/platform/imgui" "github.com/FooSoft/lazarus/platform/imgui"
"github.com/go-gl/gl/v2.1/gl" "github.com/go-gl/gl/v2.1/gl"
@ -113,21 +114,21 @@ func (w *Window) Destroy() error {
return nil return nil
} }
func (w *Window) CreateTextureRgba(colors []math.Color4b, size math.Vec2i) (*Texture, error) { func (w *Window) CreateTextureRgba(colors []math.Color4b, size math.Vec2i) (graphics.Texture, error) {
w.makeCurrent() w.makeCurrent()
return newTextureFromRgba(colors, size) return newTextureFromRgba(colors, size)
} }
func (w *Window) CreateTextureRgb(colors []math.Color3b, size math.Vec2i) (*Texture, error) { func (w *Window) CreateTextureRgb(colors []math.Color3b, size math.Vec2i) (graphics.Texture, error) {
w.makeCurrent() w.makeCurrent()
return newTextureFromRgb(colors, size) return newTextureFromRgb(colors, size)
} }
func (w *Window) RenderTexture(texture *Texture, position math.Vec2i) { func (w *Window) RenderTexture(texture graphics.Texture, position math.Vec2i) {
size := texture.Size() size := texture.Size()
gl.Enable(gl.TEXTURE_2D) gl.Enable(gl.TEXTURE_2D)
gl.BindTexture(gl.TEXTURE_2D, uint32(texture.Handle())) gl.BindTexture(gl.TEXTURE_2D, uint32(texture.Id()))
gl.Begin(gl.QUADS) gl.Begin(gl.QUADS)
gl.TexCoord2f(0, 0) gl.TexCoord2f(0, 0)

View File

@ -9,6 +9,7 @@ import (
"github.com/FooSoft/lazarus/formats/dat" "github.com/FooSoft/lazarus/formats/dat"
"github.com/FooSoft/lazarus/formats/dc6" "github.com/FooSoft/lazarus/formats/dc6"
"github.com/FooSoft/lazarus/graphics"
"github.com/FooSoft/lazarus/math" "github.com/FooSoft/lazarus/math"
"github.com/FooSoft/lazarus/platform" "github.com/FooSoft/lazarus/platform"
) )
@ -34,7 +35,7 @@ func loadSprite(path string) (*dc6.Sprite, error) {
type scene struct { type scene struct {
sprite *dc6.Sprite sprite *dc6.Sprite
palette *dat.Palette palette *dat.Palette
texture *platform.Texture texture graphics.Texture
directionIndex int directionIndex int
frameIndex int frameIndex int
@ -63,7 +64,7 @@ func (s *scene) Advance(window *platform.Window) error {
imgui := window.Imgui() imgui := window.Imgui()
imgui.DialogBegin("DC6 Viewer") imgui.DialogBegin("DC6 Viewer")
imgui.Image(s.texture.Handle(), s.texture.Size()) imgui.Image(s.texture)
direction := s.sprite.Directions[directionIndex] direction := s.sprite.Directions[directionIndex]
if imgui.SliderInt("Direction", &directionIndex, 0, len(s.sprite.Directions)-1) { if imgui.SliderInt("Direction", &directionIndex, 0, len(s.sprite.Directions)-1) {
frameIndex = 0 frameIndex = 0