create texture interface, move to graphics
This commit is contained in:
parent
0523e41732
commit
dd260996de
@ -1,3 +1,11 @@
|
||||
package graphics
|
||||
|
||||
type Handle uintptr
|
||||
import "github.com/FooSoft/lazarus/math"
|
||||
|
||||
type TextureId uintptr
|
||||
|
||||
type Texture interface {
|
||||
Id() TextureId
|
||||
Size() math.Vec2i
|
||||
Destroy() error
|
||||
}
|
||||
|
@ -18,8 +18,12 @@ func (*Context) Button(label string) bool {
|
||||
return imgui.Button(label)
|
||||
}
|
||||
|
||||
func (*Context) Image(texture graphics.Handle, size math.Vec2i) {
|
||||
imgui.Image(imgui.TextureID(texture), imgui.Vec2{X: float32(size.X), Y: float32(size.Y)})
|
||||
func (c *Context) Image(texture graphics.Texture) {
|
||||
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 {
|
||||
|
@ -8,12 +8,12 @@ import (
|
||||
"github.com/go-gl/gl/v2.1/gl"
|
||||
)
|
||||
|
||||
type Texture struct {
|
||||
type texture struct {
|
||||
size math.Vec2i
|
||||
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
|
||||
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.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
|
||||
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.BindTexture(gl.TEXTURE_2D, uint32(glLastTexture))
|
||||
return &Texture{size, glTexture}, nil
|
||||
return &texture{size, glTexture}, nil
|
||||
}
|
||||
|
||||
func (t *Texture) Handle() graphics.Handle {
|
||||
return graphics.Handle(t.glTexture)
|
||||
func (t *texture) Id() graphics.TextureId {
|
||||
return graphics.TextureId(t.glTexture)
|
||||
}
|
||||
|
||||
func (t *Texture) Size() math.Vec2i {
|
||||
func (t *texture) Size() math.Vec2i {
|
||||
return t.size
|
||||
}
|
||||
|
||||
func (t *Texture) Destroy() error {
|
||||
func (t *texture) Destroy() error {
|
||||
if t == nil || t.glTexture == 0 {
|
||||
return nil
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ package platform
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/FooSoft/lazarus/graphics"
|
||||
"github.com/FooSoft/lazarus/math"
|
||||
"github.com/FooSoft/lazarus/platform/imgui"
|
||||
"github.com/go-gl/gl/v2.1/gl"
|
||||
@ -113,21 +114,21 @@ func (w *Window) Destroy() error {
|
||||
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()
|
||||
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()
|
||||
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()
|
||||
|
||||
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.TexCoord2f(0, 0)
|
||||
|
@ -9,6 +9,7 @@ import (
|
||||
|
||||
"github.com/FooSoft/lazarus/formats/dat"
|
||||
"github.com/FooSoft/lazarus/formats/dc6"
|
||||
"github.com/FooSoft/lazarus/graphics"
|
||||
"github.com/FooSoft/lazarus/math"
|
||||
"github.com/FooSoft/lazarus/platform"
|
||||
)
|
||||
@ -34,7 +35,7 @@ func loadSprite(path string) (*dc6.Sprite, error) {
|
||||
type scene struct {
|
||||
sprite *dc6.Sprite
|
||||
palette *dat.Palette
|
||||
texture *platform.Texture
|
||||
texture graphics.Texture
|
||||
|
||||
directionIndex int
|
||||
frameIndex int
|
||||
@ -63,7 +64,7 @@ func (s *scene) Advance(window *platform.Window) error {
|
||||
imgui := window.Imgui()
|
||||
|
||||
imgui.DialogBegin("DC6 Viewer")
|
||||
imgui.Image(s.texture.Handle(), s.texture.Size())
|
||||
imgui.Image(s.texture)
|
||||
direction := s.sprite.Directions[directionIndex]
|
||||
if imgui.SliderInt("Direction", &directionIndex, 0, len(s.sprite.Directions)-1) {
|
||||
frameIndex = 0
|
||||
|
Loading…
Reference in New Issue
Block a user