switch all rendering to opengl

This commit is contained in:
Alex Yatskov 2018-12-30 16:34:53 -08:00
parent caf1aac6e6
commit 9eac33ef79
4 changed files with 74 additions and 52 deletions

View File

@ -20,6 +20,8 @@ var state struct {
windows []*Window windows []*Window
} }
type Handle uint32
type Scene interface { type Scene interface {
Init(window *Window) error Init(window *Window) error
Advance(window *Window) error Advance(window *Window) error

View File

@ -4,47 +4,44 @@ import (
"image/color" "image/color"
"unsafe" "unsafe"
"github.com/veandco/go-sdl2/sdl" "github.com/FooSoft/lazarus/math"
"github.com/go-gl/gl/v2.1/gl"
) )
type Texture struct { type Texture struct {
sdlTexture *sdl.Texture size math.Vec2i
glHandle uint32
} }
func newTextureFromRgba(renderer *sdl.Renderer, colors []color.RGBA, width, height int) (*Texture, error) { func newTextureFromRgba(colors []color.RGBA, width, height int) (*Texture, error) {
surface, err := sdl.CreateRGBSurfaceFrom( var glHandleLast int32
unsafe.Pointer(&colors[0]), gl.GetIntegerv(gl.TEXTURE_BINDING_2D, &glHandleLast)
int32(width),
int32(height),
32,
width*4,
0x000000ff,
0x0000ff00,
0x00ff0000,
0xff000000,
)
if err != nil { var glHandle uint32
return nil, nil gl.GenTextures(1, &glHandle)
gl.BindTexture(gl.TEXTURE_2D, glHandle)
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.BindTexture(gl.TEXTURE_2D, uint32(glHandleLast))
return &Texture{math.Vec2i{width, height}, glHandle}, nil
} }
sdlTexture, err := renderer.CreateTextureFromSurface(surface) func (t *Texture) Handle() Handle {
if err != nil { return Handle(t.glHandle)
surface.Free()
} }
return &Texture{sdlTexture}, nil func (t *Texture) Size() math.Vec2i {
return t.size
} }
func (t *Texture) Destroy() error { func (t *Texture) Destroy() error {
if t == nil { if t.glHandle != 0 {
return nil gl.DeleteTextures(1, &t.glHandle)
t.glHandle = 0
} }
if err := t.sdlTexture.Destroy(); err != nil {
return err
}
t.sdlTexture = nil
return nil return nil
} }

View File

@ -6,12 +6,13 @@ import (
imgui "github.com/FooSoft/imgui-go" imgui "github.com/FooSoft/imgui-go"
"github.com/FooSoft/lazarus/math" "github.com/FooSoft/lazarus/math"
"github.com/FooSoft/lazarus/platform/imgui_backend" "github.com/FooSoft/lazarus/platform/imgui_backend"
"github.com/go-gl/gl/v2.1/gl"
"github.com/veandco/go-sdl2/sdl" "github.com/veandco/go-sdl2/sdl"
) )
type Window struct { type Window struct {
sdlWindow *sdl.Window sdlWindow *sdl.Window
sdlRenderer *sdl.Renderer sdlGlContext sdl.GLContext
scene Scene scene Scene
} }
@ -28,18 +29,23 @@ func newWindow(title string, width, height int, scene Scene) (*Window, error) {
return nil, err return nil, err
} }
sdlRenderer, err := sdl.CreateRenderer(sdlWindow, -1, sdl.RENDERER_ACCELERATED) sdlGlContext, err := sdlWindow.GLCreateContext()
if err != nil { if err != nil {
sdlWindow.Destroy() sdlWindow.Destroy()
return nil, err return nil, err
} }
window := &Window{sdlWindow, sdlRenderer, scene} sdl.GLSetAttribute(sdl.GL_CONTEXT_MAJOR_VERSION, 2)
if err := scene.Init(window); err != nil { sdl.GLSetAttribute(sdl.GL_CONTEXT_MINOR_VERSION, 1)
sdl.GLSetAttribute(sdl.GL_DOUBLEBUFFER, 1)
w := &Window{sdlWindow, sdlGlContext, scene}
if err := scene.Init(w); err != nil {
w.Destroy()
return nil, err return nil, err
} }
return window, nil return w, nil
} }
func (w *Window) Destroy() error { func (w *Window) Destroy() error {
@ -50,36 +56,58 @@ func (w *Window) Destroy() error {
if err := w.scene.Shutdown(w); err != nil { if err := w.scene.Shutdown(w); err != nil {
return err return err
} }
w.scene = nil
sdl.GLDeleteContext(w.sdlGlContext)
w.sdlGlContext = nil
if err := w.sdlWindow.Destroy(); err != nil { if err := w.sdlWindow.Destroy(); err != nil {
return err return err
} }
w.sdlWindow = nil w.sdlWindow = nil
return nil return nil
} }
func (w *Window) CreateTextureRgba(colors []color.RGBA, width, height int) (*Texture, error) { func (w *Window) CreateTextureRgba(colors []color.RGBA, width, height int) (*Texture, error) {
return newTextureFromRgba(w.sdlRenderer, colors, width, height) return newTextureFromRgba(colors, width, height)
} }
func (w *Window) RenderTexture(texture *Texture, srcRect, dstRect math.Rect4i) { func (w *Window) RenderTexture(texture *Texture, position math.Vec2i) {
w.sdlRenderer.Copy( size := texture.Size()
texture.sdlTexture,
&sdl.Rect{X: int32(srcRect.X), Y: int32(srcRect.Y), W: int32(srcRect.W), H: int32(srcRect.H)}, gl.Enable(gl.TEXTURE_2D)
&sdl.Rect{X: int32(dstRect.X), Y: int32(dstRect.Y), W: int32(dstRect.W), H: int32(dstRect.H)}, gl.BindTexture(gl.TEXTURE_2D, uint32(texture.Handle()))
)
gl.Begin(gl.QUADS)
gl.TexCoord2f(0, 0)
gl.Vertex2f(0, 0)
gl.TexCoord2f(0, 1)
gl.Vertex2f(0, float32(size.Y))
gl.TexCoord2f(1, 1)
gl.Vertex2f(float32(size.X), float32(size.Y))
gl.TexCoord2f(1, 0)
gl.Vertex2f(float32(size.X), 0)
gl.End()
} }
func (w *Window) advance() { func (w *Window) advance() {
imgui_backend.NewFrame(w.displaySize()) size := w.displaySize()
w.sdlRenderer.Clear() imgui_backend.NewFrame(size)
gl.Viewport(0, 0, int32(size.X), int32(size.Y))
gl.Clear(gl.COLOR_BUFFER_BIT)
gl.MatrixMode(gl.PROJECTION)
gl.LoadIdentity()
gl.Ortho(0, float64(size.X), float64(size.Y), 0, -1, 1)
gl.MatrixMode(gl.MODELVIEW)
gl.LoadIdentity()
w.scene.Advance(w) w.scene.Advance(w)
imgui.Render() imgui.Render()
imgui_backend.Render(w.displaySize(), w.bufferSize(), imgui.RenderedDrawData()) imgui_backend.Render(w.displaySize(), w.bufferSize(), imgui.RenderedDrawData())
w.sdlWindow.GLSwap() w.sdlWindow.GLSwap()
} }

View File

@ -66,7 +66,6 @@ func (s *scene) Advance(window *platform.Window) error {
} }
frame := direction.Frames[frameIndex] frame := direction.Frames[frameIndex]
imgui.SliderInt("Frame", &frameIndex, 0, int32(len(direction.Frames))-1) imgui.SliderInt("Frame", &frameIndex, 0, int32(len(direction.Frames))-1)
imgui.Text(fmt.Sprintf("Height: %d", frame.Height)) imgui.Text(fmt.Sprintf("Height: %d", frame.Height))
imgui.Text(fmt.Sprintf("Width: %d", frame.Width)) imgui.Text(fmt.Sprintf("Width: %d", frame.Width))
imgui.Text(fmt.Sprintf("OffsetX: %d", frame.OffsetX)) imgui.Text(fmt.Sprintf("OffsetX: %d", frame.OffsetX))
@ -87,11 +86,7 @@ func (s *scene) Advance(window *platform.Window) error {
} }
} }
window.RenderTexture( window.RenderTexture(s.texture, math.Vec2i{X: 0, Y: 0})
s.texture,
math.Rect4i{X: 0, Y: 0, W: 256, H: 256},
math.Rect4i{X: 0, Y: 0, W: 256, H: 256},
)
s.directionIndex = directionIndex s.directionIndex = directionIndex
s.frameIndex = frameIndex s.frameIndex = frameIndex