diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..713205a --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,5 @@ +# Changelog + +## 0.1_beta + +* beta release diff --git a/README.md b/README.md index 58fc00f..be5a71d 100644 --- a/README.md +++ b/README.md @@ -1,14 +1,13 @@ # go-game (package "goga") Game engine written in Go using OpenGL and GLFW. Mostly for 2D rendering, but also capable of rendering 3D, providing everything to get you started. -**Under heavy development, do not use yet!** ## Install go-game requires OpenGL and GLFW. The following three steps install everything you need: ``` -go get github.com/go-gl/gl/v4.5-core/gl +go get github.com/go-gl/gl/v3.1-core/gl go get github.com/go-gl/glfw/v3.1/glfw go get github.com/DeKugelschieber/go-game ``` @@ -17,18 +16,16 @@ You also need a cgo compiler (typically gcc) and GL/GLFW development libraries a ## Usage -*WIP* - Examples can be found within the demo folder. For full reference visit: https://godoc.org/github.com/DeKugelschieber/go-game ## Dependencies * https://github.com/go-gl/gl - - 4.5-core + - 3.1-core * https://github.com/go-gl/glfw - 3.1 -To use an older GL version, you need to replace the GL imports in package goga. It should mostly be compatible down to 3.x. +To use a different GL version, you need to replace the GL imports in package goga. ## Contribute diff --git a/ToDo.md b/ToDo.md index 77af0cc..8d0d7b1 100644 --- a/ToDo.md +++ b/ToDo.md @@ -5,6 +5,6 @@ * ~~limit FPS~~ - does this really work? * ~~fullscreen~~ -* simple access to default resources like GetTex() +* ~~simple access to default resources like GetTex()~~ * ~~text rendering + font loading~~ * more options for GLFW diff --git a/actor.go b/actor.go index fd73d14..da247ee 100644 --- a/actor.go +++ b/actor.go @@ -1,7 +1,5 @@ package goga -import () - // An actor ID is a unique integer, // which can be used to reference an actor. type ActorId uint64 diff --git a/demo/input/assets/gopher.png b/demo/input/assets/gopher.png new file mode 100644 index 0000000..cb5e2e1 Binary files /dev/null and b/demo/input/assets/gopher.png differ diff --git a/demo/input/input.go b/demo/input/input.go new file mode 100644 index 0000000..f6a4f16 --- /dev/null +++ b/demo/input/input.go @@ -0,0 +1,87 @@ +package main + +import ( + "github.com/DeKugelschieber/go-game" + "github.com/go-gl/glfw/v3.2/glfw" +) + +const ( + gopher_path = "src/github.com/DeKugelschieber/go-game/demo/input/assets/gopher.png" +) + +type Game struct { + mouseX, mouseY float64 + sprite *goga.Sprite +} + +func (g *Game) Setup() { + // load texture + _, err := goga.LoadRes(gopher_path) + + if err != nil { + panic(err) + } + + // create sprite + tex, err := goga.GetTex("gopher.png") + + if err != nil { + panic(err) + } + + sprite := goga.NewSprite(tex) + sprite.Size.X = sprite.Size.X / 4 + sprite.Size.Y = sprite.Size.Y / 4 + g.sprite = sprite + renderer, ok := goga.GetSystemByName("spriteRenderer").(*goga.SpriteRenderer) + + if !ok { + panic("Could not find renderer") + } + + renderer.Add(sprite.Actor, sprite.Pos2D, sprite.Tex) + + culling, ok := goga.GetSystemByName("culling2d").(*goga.Culling2D) + + if !ok { + panic("Could not find culling") + } + + culling.Add(sprite.Actor, sprite.Pos2D) + + // register input listeners + goga.AddKeyboardListener(g) + goga.AddMouseListener(g) +} + +func (g *Game) Update(delta float64) {} + +func (g *Game) OnKeyEvent(key glfw.Key, code int, action glfw.Action, mod glfw.ModifierKey) { + // ESC + if key == 256 { + goga.Stop() + } +} + +func (g *Game) OnMouseButton(button glfw.MouseButton, action glfw.Action, mod glfw.ModifierKey) { + if button == 0 { + g.sprite.Pos.X = g.mouseX + g.sprite.Pos.Y = g.mouseY + } +} + +func (g *Game) OnMouseMove(x float64, y float64) { + g.mouseX = x + g.mouseY = y +} + +func (g *Game) OnMouseScroll(x float64, y float64) {} + +func main() { + game := Game{} + options := goga.RunOptions{ClearColor: goga.Vec4{1, 1, 1, 0}, + Resizable: true, + SetViewportOnResize: true, + ExitOnClose: true} + goga.Run(&game, &options) +} diff --git a/demo/text/text.go b/demo/text/text.go index 3e0769d..7ce3cb1 100644 --- a/demo/text/text.go +++ b/demo/text/text.go @@ -2,7 +2,7 @@ package main import ( "github.com/DeKugelschieber/go-game" - "github.com/go-gl/gl/v4.5-core/gl" + "github.com/go-gl/gl/v3.2-core/gl" ) const ( diff --git a/fbo.go b/fbo.go index 07c76c7..a168fec 100644 --- a/fbo.go +++ b/fbo.go @@ -1,7 +1,7 @@ package goga import ( - "github.com/go-gl/gl/v4.5-core/gl" + "github.com/go-gl/gl/v3.2-core/gl" ) // Frame Buffer Object. diff --git a/game.go b/game.go index f7d8acf..fedbb00 100644 --- a/game.go +++ b/game.go @@ -1,7 +1,7 @@ package goga import ( - "github.com/go-gl/gl/v4.5-core/gl" + "github.com/go-gl/gl/v3.2-core/gl" "github.com/go-gl/glfw/v3.2/glfw" "log" "math" @@ -58,7 +58,8 @@ var ( ) func init() { - // GL functions must be called from main thread. + // GL functions must be called from main thread, + // so we disable multithreading by the runtime here. log.Print("Locking OS thread") runtime.LockOSThread() } @@ -154,6 +155,8 @@ func Run(game Game, options *RunOptions) { } }) + initInput(wnd) + // make GL context current wnd.MakeContextCurrent() @@ -178,8 +181,8 @@ func Run(game Game, options *RunOptions) { delta := time.Duration(0) var deltaSec float64 - for running { - if exitOnClose && wnd.ShouldClose() { + for true { + if !running || exitOnClose && wnd.ShouldClose() { cleanup() return } diff --git a/gl_util.go b/gl_util.go index 6f46f10..b1fe6d6 100644 --- a/gl_util.go +++ b/gl_util.go @@ -1,7 +1,7 @@ package goga import ( - "github.com/go-gl/gl/v4.5-core/gl" + "github.com/go-gl/gl/v3.2-core/gl" "log" ) diff --git a/input.go b/input.go new file mode 100644 index 0000000..ec5d239 --- /dev/null +++ b/input.go @@ -0,0 +1,98 @@ +package goga + +import ( + "github.com/go-gl/glfw/v3.2/glfw" +) + +var ( + keyboardListener []KeyboardListener + mouseListener []MouseListener +) + +// Interface for keyboard input events. +// Implement and register to receive keyboard input. +type KeyboardListener interface { + OnKeyEvent(glfw.Key, int, glfw.Action, glfw.ModifierKey) +} + +// Interface for mouse input events. +// Implement and register to receive mouse input. +type MouseListener interface { + OnMouseButton(glfw.MouseButton, glfw.Action, glfw.ModifierKey) + OnMouseMove(float64, float64) + OnMouseScroll(float64, float64) +} + +func initInput(wnd *glfw.Window) { + wnd.SetKeyCallback(keyboardCallback) + wnd.SetMouseButtonCallback(mouseButtonCallback) + wnd.SetCursorPosCallback(mouseMoveCallback) + wnd.SetScrollCallback(mouseScrollCallback) + + keyboardListener = make([]KeyboardListener, 0) + mouseListener = make([]MouseListener, 0) +} + +func keyboardCallback(wnd *glfw.Window, key glfw.Key, code int, action glfw.Action, mod glfw.ModifierKey) { + for _, listener := range keyboardListener { + listener.OnKeyEvent(key, code, action, mod) + } +} + +func mouseButtonCallback(wnd *glfw.Window, button glfw.MouseButton, action glfw.Action, mod glfw.ModifierKey) { + for _, listener := range mouseListener { + listener.OnMouseButton(button, action, mod) + } +} + +func mouseMoveCallback(wnd *glfw.Window, x float64, y float64) { + for _, listener := range mouseListener { + listener.OnMouseMove(x, float64(viewportHeight)-y) + } +} + +func mouseScrollCallback(wnd *glfw.Window, x float64, y float64) { + for _, listener := range mouseListener { + listener.OnMouseScroll(x, y) + } +} + +// Adds a new keyboard listener. +func AddKeyboardListener(listener KeyboardListener) { + keyboardListener = append(keyboardListener, listener) +} + +// Removes given keyboard listener if found. +func RemoveKeyboardListener(listener KeyboardListener) { + for i, l := range keyboardListener { + if l == listener { + keyboardListener = append(keyboardListener[:i], keyboardListener[i+1:]...) + return + } + } +} + +// Removes all registered keyboard listeners. +func RemoveAllKeyboardListener() { + keyboardListener = make([]KeyboardListener, 0) +} + +// Adds a new mouse listener. +func AddMouseListener(listener MouseListener) { + mouseListener = append(mouseListener, listener) +} + +// Removes given mouse listener if found. +func RemoveMouseListener(listener MouseListener) { + for i, l := range mouseListener { + if l == listener { + mouseListener = append(mouseListener[:i], mouseListener[i+1:]...) + return + } + } +} + +// Removes all registered mouse listeners. +func RemoveAllMouseListener() { + mouseListener = make([]MouseListener, 0) +} diff --git a/keyframe.go b/keyframe.go index 432eb7d..cc3d0f4 100644 --- a/keyframe.go +++ b/keyframe.go @@ -1,7 +1,7 @@ package goga import ( - "github.com/go-gl/gl/v4.5-core/gl" + "github.com/go-gl/gl/v3.2-core/gl" ) const ( diff --git a/loader.go b/loader.go index e083629..90b4148 100644 --- a/loader.go +++ b/loader.go @@ -3,7 +3,7 @@ package goga import ( "bufio" "errors" - "github.com/go-gl/gl/v4.5-core/gl" + "github.com/go-gl/gl/v3.2-core/gl" "image" "image/draw" "image/png" diff --git a/model.go b/model.go index 428e7f2..f4033e5 100644 --- a/model.go +++ b/model.go @@ -1,7 +1,7 @@ package goga import ( - "github.com/go-gl/gl/v4.5-core/gl" + "github.com/go-gl/gl/v3.2-core/gl" ) const ( diff --git a/shader.go b/shader.go index 33129bc..a3f2e1c 100644 --- a/shader.go +++ b/shader.go @@ -2,7 +2,7 @@ package goga import ( "errors" - "github.com/go-gl/gl/v4.5-core/gl" + "github.com/go-gl/gl/v3.2-core/gl" "log" "strings" ) diff --git a/sprite.go b/sprite.go index 9da1067..06aea5c 100644 --- a/sprite.go +++ b/sprite.go @@ -1,7 +1,7 @@ package goga import ( - "github.com/go-gl/gl/v4.5-core/gl" + "github.com/go-gl/gl/v3.2-core/gl" ) const ( diff --git a/tex.go b/tex.go index dccefb5..9f39779 100644 --- a/tex.go +++ b/tex.go @@ -1,7 +1,7 @@ package goga import ( - "github.com/go-gl/gl/v4.5-core/gl" + "github.com/go-gl/gl/v3.2-core/gl" "image" ) diff --git a/text.go b/text.go index ea37efe..5990374 100644 --- a/text.go +++ b/text.go @@ -2,7 +2,7 @@ package goga import ( "encoding/json" - "github.com/go-gl/gl/v4.5-core/gl" + "github.com/go-gl/gl/v3.2-core/gl" "io/ioutil" ) diff --git a/vao.go b/vao.go index 92995af..2aad4ef 100644 --- a/vao.go +++ b/vao.go @@ -1,7 +1,7 @@ package goga import ( - "github.com/go-gl/gl/v4.5-core/gl" + "github.com/go-gl/gl/v3.2-core/gl" ) // Vertex Array Object. diff --git a/vbo.go b/vbo.go index 1445ec2..175121d 100644 --- a/vbo.go +++ b/vbo.go @@ -1,7 +1,7 @@ package goga import ( - "github.com/go-gl/gl/v4.5-core/gl" + "github.com/go-gl/gl/v3.2-core/gl" "unsafe" )