From 8c0c4890cbf3c6079e0f9ac86af2d50a44dede14 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Thu, 27 Dec 2018 13:36:34 -0800 Subject: [PATCH] add basic stubs for platform and window --- graphics/window.go | 34 ++++++++++++++++++++++++++++ platform/platform.go | 53 ++++++++++++++++++++++++++++++++++++++++++++ platform/window.go | 47 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 134 insertions(+) create mode 100644 graphics/window.go create mode 100644 platform/platform.go create mode 100644 platform/window.go diff --git a/graphics/window.go b/graphics/window.go new file mode 100644 index 0000000..aa45762 --- /dev/null +++ b/graphics/window.go @@ -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() { + +} diff --git a/platform/platform.go b/platform/platform.go new file mode 100644 index 0000000..814f3ac --- /dev/null +++ b/platform/platform.go @@ -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 +} diff --git a/platform/window.go b/platform/window.go new file mode 100644 index 0000000..47aba97 --- /dev/null +++ b/platform/window.go @@ -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 +}