cleanup global state

This commit is contained in:
Alex Yatskov 2018-12-27 19:28:33 -08:00
parent 6736cc5067
commit 5d95d2ef77
2 changed files with 43 additions and 43 deletions

View File

@ -10,25 +10,25 @@ import (
"github.com/veandco/go-sdl2/sdl" "github.com/veandco/go-sdl2/sdl"
) )
var (
imguiIsInit bool
imguiButtonsDown [3]bool
imguiLastTime uint64
imguiFontTexture uint32
imguiContext *imgui.Context
)
var ( var (
ErrAlreadyInit = errors.New("imgui backend is already initialized") ErrAlreadyInit = errors.New("imgui backend is already initialized")
ErrWasNotInit = errors.New("imgui backend was not initialized") ErrWasNotInit = errors.New("imgui backend was not initialized")
) )
var state struct {
isInit bool
buttonsDown [3]bool
lastTime uint64
fontTexture uint32
context *imgui.Context
}
func Init() error { func Init() error {
if imguiIsInit { if state.isInit {
return ErrAlreadyInit return ErrAlreadyInit
} }
imguiContext = imgui.CreateContext(nil) state.context = imgui.CreateContext(nil)
keys := map[int]int{ keys := map[int]int{
imgui.KeyTab: sdl.SCANCODE_TAB, imgui.KeyTab: sdl.SCANCODE_TAB,
@ -60,30 +60,30 @@ func Init() error {
io.KeyMap(imguiKey, nativeKey) io.KeyMap(imguiKey, nativeKey)
} }
imguiFontTexture = createFontTexture() state.fontTexture = createFontTexture()
imguiIsInit = true state.isInit = true
return nil return nil
} }
func Shutdown() error { func Shutdown() error {
if !imguiIsInit { if !state.isInit {
return ErrWasNotInit return ErrWasNotInit
} }
imguiIsInit = false state.isInit = false
destroyFontTexture(imguiFontTexture) destroyFontTexture(state.fontTexture)
imguiFontTexture = 0 state.fontTexture = 0
imguiContext.Destroy() state.context.Destroy()
imguiContext = nil state.context = nil
return nil return nil
} }
func NewFrame(windowSize math.Vec2i) error { func NewFrame(windowSize math.Vec2i) error {
if !imguiIsInit { if !state.isInit {
return ErrWasNotInit return ErrWasNotInit
} }
@ -94,18 +94,18 @@ func NewFrame(windowSize math.Vec2i) error {
// Setup time step (we don't use SDL_GetTicks() because it is using millisecond resolution) // Setup time step (we don't use SDL_GetTicks() because it is using millisecond resolution)
frequency := sdl.GetPerformanceFrequency() frequency := sdl.GetPerformanceFrequency()
currentTime := sdl.GetPerformanceCounter() currentTime := sdl.GetPerformanceCounter()
if imguiLastTime > 0 { if state.lastTime > 0 {
io.SetDeltaTime(float32(currentTime-imguiLastTime) / float32(frequency)) io.SetDeltaTime(float32(currentTime-state.lastTime) / float32(frequency))
} else { } else {
io.SetDeltaTime(1.0 / 60.0) io.SetDeltaTime(1.0 / 60.0)
} }
imguiLastTime = currentTime state.lastTime = currentTime
// If a mouse press event came, always pass it as "mouse held this frame", so we don't miss click-release events that are shorter than 1 frame. // If a mouse press event came, always pass it as "mouse held this frame", so we don't miss click-release events that are shorter than 1 frame.
x, y, state := sdl.GetMouseState() x, y, state := sdl.GetMouseState()
for i, button := range []uint32{sdl.BUTTON_LEFT, sdl.BUTTON_RIGHT, sdl.BUTTON_MIDDLE} { for i, button := range []uint32{sdl.BUTTON_LEFT, sdl.BUTTON_RIGHT, sdl.BUTTON_MIDDLE} {
io.SetMouseButtonDown(i, imguiButtonsDown[i] || (state&sdl.Button(button)) != 0) io.SetMouseButtonDown(i, state.buttonsDown[i] || (state&sdl.Button(button)) != 0)
imguiButtonsDown[i] = false state.buttonsDown[i] = false
} }
io.SetMousePosition(imgui.Vec2{X: float32(x), Y: float32(y)}) io.SetMousePosition(imgui.Vec2{X: float32(x), Y: float32(y)})
@ -120,7 +120,7 @@ func NewFrame(windowSize math.Vec2i) error {
// Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags. // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
// If you have multiple SDL events and some of them are not meant to be used by dear imgui, you may need to filter events based on their windowID field. // If you have multiple SDL events and some of them are not meant to be used by dear imgui, you may need to filter events based on their windowID field.
func ProcessEvent(event sdl.Event) (bool, error) { func ProcessEvent(event sdl.Event) (bool, error) {
if !imguiIsInit { if !state.isInit {
return false, ErrWasNotInit return false, ErrWasNotInit
} }
@ -143,13 +143,13 @@ func ProcessEvent(event sdl.Event) (bool, error) {
buttonEvent := event.(*sdl.MouseButtonEvent) buttonEvent := event.(*sdl.MouseButtonEvent)
switch buttonEvent.Button { switch buttonEvent.Button {
case sdl.BUTTON_LEFT: case sdl.BUTTON_LEFT:
imguiButtonsDown[0] = true state.buttonsDown[0] = true
break break
case sdl.BUTTON_RIGHT: case sdl.BUTTON_RIGHT:
imguiButtonsDown[1] = true state.buttonsDown[1] = true
break break
case sdl.BUTTON_MIDDLE: case sdl.BUTTON_MIDDLE:
imguiButtonsDown[2] = true state.buttonsDown[2] = true
break break
} }
return true, nil return true, nil
@ -180,7 +180,7 @@ func ProcessEvent(event sdl.Event) (bool, error) {
// OpenGL2 Render function. // OpenGL2 Render function.
// Note that this implementation is little overcomplicated because we are saving/setting up/restoring every OpenGL state explicitly, in order to be able to run within any OpenGL engine that doesn't do so. // Note that this implementation is little overcomplicated because we are saving/setting up/restoring every OpenGL state explicitly, in order to be able to run within any OpenGL engine that doesn't do so.
func Render(windowSize, fbSize math.Vec2i, drawData imgui.DrawData) error { func Render(windowSize, fbSize math.Vec2i, drawData imgui.DrawData) error {
if !imguiIsInit { if !state.isInit {
return ErrWasNotInit return ErrWasNotInit
} }

View File

@ -10,18 +10,18 @@ import (
"github.com/veandco/go-sdl2/sdl" "github.com/veandco/go-sdl2/sdl"
) )
var (
platformIsInit bool
platformWindows []Window
)
var ( var (
ErrAlreadyInit = errors.New("platform is already initialized") ErrAlreadyInit = errors.New("platform is already initialized")
ErrWasNotInit = errors.New("platform was not initialized") ErrWasNotInit = errors.New("platform was not initialized")
) )
var state struct {
isInit bool
windows []Window
}
func Init() error { func Init() error {
if platformIsInit { if state.isInit {
return ErrAlreadyInit return ErrAlreadyInit
} }
@ -39,16 +39,16 @@ func Init() error {
return err return err
} }
platformIsInit = true state.isInit = true
return nil return nil
} }
func Shutdown() error { func Shutdown() error {
if !platformIsInit { if !state.isInit {
return ErrWasNotInit return ErrWasNotInit
} }
for _, w := range platformWindows { for _, w := range state.windows {
if err := w.Destroy(); err != nil { if err := w.Destroy(); err != nil {
return err return err
} }
@ -58,14 +58,14 @@ func Shutdown() error {
return err return err
} }
platformWindows = nil state.windows = nil
platformIsInit = false state.isInit = false
return nil return nil
} }
func ProcessEvents() error { func ProcessEvents() error {
if !platformIsInit { if !state.isInit {
return ErrWasNotInit return ErrWasNotInit
} }
@ -86,7 +86,7 @@ func ProcessEvents() error {
} }
func CreateWindow(title string, width, height int) (Window, error) { func CreateWindow(title string, width, height int) (Window, error) {
if !platformIsInit { if !state.isInit {
return nil, ErrWasNotInit return nil, ErrWasNotInit
} }
@ -95,7 +95,7 @@ func CreateWindow(title string, width, height int) (Window, error) {
return nil, err return nil, err
} }
platformWindows = append(platformWindows, window) state.windows = append(state.windows, window)
return window, err return window, err
} }