add explicit platform initialization and shutdown

This commit is contained in:
Alex Yatskov 2019-01-08 19:15:23 -08:00
parent 3ee908c191
commit 055c815406
5 changed files with 112 additions and 20 deletions

View File

@ -17,6 +17,16 @@ func FileUnmountArchive(mountPath string) error {
return nil return nil
} }
func FileUnmountAll() error {
for _, archive := range fileState.mountPoints {
if err := archive.Close(); err != nil {
return err
}
}
return nil
}
func FileOpen(path string) (*File, error) { func FileOpen(path string) (*File, error) {
return nil, nil return nil, nil
} }

View File

@ -1,17 +1,57 @@
package platform package platform
import ( import (
"errors"
"log" "log"
"runtime" "runtime"
"github.com/veandco/go-sdl2/sdl" "github.com/veandco/go-sdl2/sdl"
) )
var platfromState struct { var (
sdlIsInit bool ErrPlatformNotInit = errors.New("platform is not initialized")
)
var platformState struct {
isInit bool
}
func Initialize() error {
log.Println("platform init")
if platformState.isInit {
return nil
}
runtime.LockOSThread()
if err := sdl.Init(sdl.INIT_VIDEO); err != nil {
return err
}
platformState.isInit = true
return nil
}
func Shutdown() error {
log.Println("platform shutdown")
if !platformState.isInit {
return nil
}
if err := FileUnmountAll(); err != nil {
return err
}
return nil
} }
func Advance() (bool, error) { func Advance() (bool, error) {
if !platformState.isInit {
return false, ErrPlatformNotInit
}
for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() { for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
switch event.(type) { switch event.(type) {
case *sdl.QuitEvent: case *sdl.QuitEvent:
@ -30,19 +70,3 @@ func Advance() (bool, error) {
return run, err return run, err
} }
func platformInit() error {
if platfromState.sdlIsInit {
return nil
}
runtime.LockOSThread()
log.Println("sdl init")
if err := sdl.Init(sdl.INIT_VIDEO); err != nil {
return err
}
platfromState.sdlIsInit = true
return nil
}

View File

@ -43,8 +43,6 @@ func WindowCreate(title string, size math.Vec2i, scene Scene) error {
return ErrWindowExists return ErrWindowExists
} }
platformInit()
var err error var err error
log.Println("window create") log.Println("window create")
if windowState.sdlWindow, err = sdl.CreateWindow(title, sdl.WINDOWPOS_CENTERED, sdl.WINDOWPOS_CENTERED, int32(size.X), int32(size.Y), sdl.WINDOW_OPENGL); err != nil { if windowState.sdlWindow, err = sdl.CreateWindow(title, sdl.WINDOWPOS_CENTERED, sdl.WINDOWPOS_CENTERED, int32(size.X), int32(size.Y), sdl.WINDOW_OPENGL); err != nil {

57
streaming/bytewriter.go Normal file
View File

@ -0,0 +1,57 @@
package streaming
import (
"errors"
"io"
)
type Writer interface {
io.WriteSeeker
}
type writer struct {
data []byte
offset int
}
func NewWriter(data []byte) Writer {
return &writer{data, 0}
}
func (w *writer) Write(data []byte) (int, error) {
length := len(data)
if w.offset+length > len(w.data) {
length = len(w.data) - w.offset
}
if length == 0 {
return 0, errors.New("cannot write past end of buffer")
}
copy(w.data[w.offset:], data[:length])
w.offset += length
return length, nil
}
func (w *writer) Seek(offset int64, whence int) (int64, error) {
result := w.offset
switch whence {
case io.SeekStart:
result = int(offset)
case io.SeekCurrent:
result = w.offset + int(offset)
case io.SeekEnd:
result = len(w.data) - int(offset)
}
if result < 0 {
return int64(w.offset), errors.New("cannot seek before beginning of buffer")
}
if result >= len(w.data) {
return int64(w.offset), errors.New("cannot seek past end of buffer")
}
w.offset = result
return int64(w.offset), nil
}

View File

@ -122,6 +122,9 @@ func (s *scene) updateTexture() error {
} }
func main() { func main() {
platform.Initialize()
defer platform.Shutdown()
var ( var (
palettePath = flag.String("palette", "", "path to palette file") palettePath = flag.String("palette", "", "path to palette file")
) )