2018-12-27 21:36:34 +00:00
|
|
|
package platform
|
|
|
|
|
2018-12-28 17:26:05 +00:00
|
|
|
import (
|
2018-12-29 19:52:30 +00:00
|
|
|
"image/color"
|
|
|
|
|
|
|
|
imgui "github.com/FooSoft/imgui-go"
|
2018-12-28 17:26:05 +00:00
|
|
|
"github.com/FooSoft/lazarus/math"
|
2018-12-29 19:52:30 +00:00
|
|
|
"github.com/FooSoft/lazarus/platform/imgui_backend"
|
2018-12-28 17:26:05 +00:00
|
|
|
"github.com/veandco/go-sdl2/sdl"
|
|
|
|
)
|
2018-12-27 21:36:34 +00:00
|
|
|
|
2018-12-29 19:52:30 +00:00
|
|
|
type Window struct {
|
2018-12-27 21:36:34 +00:00
|
|
|
sdlWindow *sdl.Window
|
|
|
|
sdlGlContext sdl.GLContext
|
2018-12-27 21:59:19 +00:00
|
|
|
sdlRenderer *sdl.Renderer
|
2018-12-28 17:26:05 +00:00
|
|
|
scene Scene
|
2018-12-27 21:36:34 +00:00
|
|
|
}
|
|
|
|
|
2018-12-29 19:52:30 +00:00
|
|
|
func newWindow(title string, width, height int, scene Scene) (*Window, error) {
|
2018-12-27 21:36:34 +00:00
|
|
|
sdlWindow, err := sdl.CreateWindow(
|
|
|
|
title,
|
|
|
|
sdl.WINDOWPOS_CENTERED,
|
|
|
|
sdl.WINDOWPOS_CENTERED,
|
|
|
|
int32(width),
|
|
|
|
int32(height),
|
|
|
|
sdl.WINDOW_OPENGL,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-12-29 21:24:55 +00:00
|
|
|
sdlRenderer, err := sdl.CreateRenderer(sdlWindow, -1, sdl.RENDERER_ACCELERATED)
|
2018-12-27 21:36:34 +00:00
|
|
|
if err != nil {
|
|
|
|
sdlWindow.Destroy()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-12-29 21:24:55 +00:00
|
|
|
sdlGlContext, err := sdlWindow.GLCreateContext()
|
2018-12-27 21:59:19 +00:00
|
|
|
if err != nil {
|
|
|
|
sdlWindow.Destroy()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-12-29 19:52:30 +00:00
|
|
|
window := &Window{sdlWindow, sdlGlContext, sdlRenderer, scene}
|
|
|
|
if err := scene.Init(window); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return window, nil
|
2018-12-27 21:36:34 +00:00
|
|
|
}
|
|
|
|
|
2018-12-29 19:52:30 +00:00
|
|
|
func (w *Window) Destroy() error {
|
|
|
|
if w.sdlWindow == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := w.scene.Shutdown(w); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := w.sdlWindow.Destroy(); err != nil {
|
|
|
|
return err
|
2018-12-27 21:36:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
w.sdlGlContext = nil
|
|
|
|
w.sdlWindow = nil
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2018-12-28 17:26:05 +00:00
|
|
|
|
2018-12-29 19:52:30 +00:00
|
|
|
func (w *Window) CreateTextureRgba(colors []color.RGBA, width, height int) (*Texture, error) {
|
|
|
|
return newTextureFromRgba(w.sdlRenderer, colors, width, height)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Window) RenderTexture(texture *Texture, srcRect, dstRect math.Rect4i) {
|
|
|
|
w.sdlRenderer.Copy(
|
|
|
|
texture.sdlTexture,
|
|
|
|
&sdl.Rect{X: int32(srcRect.X), Y: int32(srcRect.Y), W: int32(srcRect.W), H: int32(srcRect.H)},
|
|
|
|
&sdl.Rect{X: int32(dstRect.X), Y: int32(dstRect.Y), W: int32(dstRect.W), H: int32(dstRect.H)},
|
|
|
|
)
|
2018-12-28 17:26:05 +00:00
|
|
|
}
|
|
|
|
|
2018-12-29 19:52:30 +00:00
|
|
|
func (w *Window) advance() {
|
|
|
|
imgui_backend.NewFrame(w.displaySize())
|
2018-12-30 01:04:37 +00:00
|
|
|
w.sdlRenderer.Clear()
|
|
|
|
|
2018-12-29 19:52:30 +00:00
|
|
|
w.scene.Advance(w)
|
2018-12-29 21:24:55 +00:00
|
|
|
|
2018-12-30 02:10:26 +00:00
|
|
|
w.sdlWindow.GLMakeCurrent(w.sdlGlContext)
|
2018-12-29 19:52:30 +00:00
|
|
|
imgui.Render()
|
|
|
|
imgui_backend.Render(w.displaySize(), w.bufferSize(), imgui.RenderedDrawData())
|
|
|
|
w.sdlWindow.GLSwap()
|
2018-12-28 17:26:05 +00:00
|
|
|
}
|
|
|
|
|
2018-12-29 19:52:30 +00:00
|
|
|
func (w *Window) displaySize() math.Vec2i {
|
2018-12-28 17:26:05 +00:00
|
|
|
width, height := w.sdlWindow.GetSize()
|
|
|
|
return math.Vec2i{X: int(width), Y: int(height)}
|
|
|
|
}
|
|
|
|
|
2018-12-29 19:52:30 +00:00
|
|
|
func (w *Window) bufferSize() math.Vec2i {
|
2018-12-28 17:26:05 +00:00
|
|
|
width, height := w.sdlWindow.GLGetDrawableSize()
|
|
|
|
return math.Vec2i{X: int(width), Y: int(height)}
|
|
|
|
}
|