add basic stubs for platform and window

This commit is contained in:
Alex Yatskov 2018-12-27 13:36:34 -08:00
parent 454b9210c1
commit 8c0c4890cb
3 changed files with 134 additions and 0 deletions

34
graphics/window.go Normal file
View File

@ -0,0 +1,34 @@
package graphics
import "github.com/veandco/go-sdl2/sdl"
type Window struct {
window *sdl.Window
renderer *sdl.Renderer
}
func NewWindow(title string, width, height int) (*Window, error) {
window, err := sdl.CreateWindow(
title,
sdl.WINDOWPOS_UNDEFINED,
sdl.WINDOWPOS_UNDEFINED,
int32(width),
int32(height),
sdl.WINDOW_SHOWN,
)
if err != nil {
return nil, err
}
renderer, err := sdl.CreateRenderer(window, -1, sdl.RENDERER_ACCELERATED)
if err != nil {
window.Destroy()
return nil, err
}
return &Window{window, renderer}, nil
}
func (w *Window) Destroy() {
}

53
platform/platform.go Normal file
View File

@ -0,0 +1,53 @@
package platform
import (
"github.com/go-gl/gl/v2.1/gl"
"github.com/veandco/go-sdl2/sdl"
)
type Platform interface {
CreateWindow(title string, width, height int) (Window, error)
Destroy() error
}
var globalPlatformInit bool
type platform struct {
windows []Window
}
func New() (*Platform, error) {
if !globalPlatformInit {
if err := sdl.Init(sdl.INIT_VIDEO); err != nil {
return nil, err
}
if err := gl.Init(); err != nil {
return nil, err
}
globalPlatformInit = true
}
return nil, nil
}
func (p *platform) CreateWindow(title string, width, height int) (Window, error) {
window, err := newWindow(title, width, height)
if err != nil {
return nil, err
}
return window, err
}
func (p *platform) Destroy() error {
for _, w := range p.windows {
if err := w.Destroy(); err != nil {
return err
}
}
p.windows = nil
return nil
}

47
platform/window.go Normal file
View File

@ -0,0 +1,47 @@
package platform
import "github.com/veandco/go-sdl2/sdl"
type Window interface {
Destroy() error
}
type window struct {
sdlWindow *sdl.Window
sdlGlContext sdl.GLContext
}
func newWindow(title string, width, height int) (Window, error) {
sdlWindow, err := sdl.CreateWindow(
title,
sdl.WINDOWPOS_CENTERED,
sdl.WINDOWPOS_CENTERED,
int32(width),
int32(height),
sdl.WINDOW_OPENGL,
)
if err != nil {
return nil, err
}
sdlGlContext, err := sdlWindow.GLCreateContext()
if err != nil {
sdlWindow.Destroy()
return nil, err
}
return &window{sdlWindow, sdlGlContext}, nil
}
func (w *window) Destroy() error {
if w.sdlWindow != nil {
if err := w.sdlWindow.Destroy(); err != nil {
return err
}
}
w.sdlGlContext = nil
w.sdlWindow = nil
return nil
}