Set GL version to 3.1 (lowest possible version), fixed cleanup bug when calling Stop(), added input handling.

This commit is contained in:
Marvin Blum
2016-08-14 13:20:01 +02:00
parent 092ad543e5
commit 25814cf022
20 changed files with 213 additions and 25 deletions

5
CHANGELOG.md Normal file
View File

@@ -0,0 +1,5 @@
# Changelog
## 0.1_beta
* beta release

View File

@@ -1,14 +1,13 @@
# go-game (package "goga") # 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. 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 ## Install
go-game requires OpenGL and GLFW. The following three steps install everything you need: 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/go-gl/glfw/v3.1/glfw
go get github.com/DeKugelschieber/go-game 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 ## Usage
*WIP*
Examples can be found within the demo folder. For full reference visit: https://godoc.org/github.com/DeKugelschieber/go-game Examples can be found within the demo folder. For full reference visit: https://godoc.org/github.com/DeKugelschieber/go-game
## Dependencies ## Dependencies
* https://github.com/go-gl/gl * https://github.com/go-gl/gl
- 4.5-core - 3.1-core
* https://github.com/go-gl/glfw * https://github.com/go-gl/glfw
- 3.1 - 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 ## Contribute

View File

@@ -5,6 +5,6 @@
* ~~limit FPS~~ * ~~limit FPS~~
- does this really work? - does this really work?
* ~~fullscreen~~ * ~~fullscreen~~
* simple access to default resources like GetTex() * ~~simple access to default resources like GetTex()~~
* ~~text rendering + font loading~~ * ~~text rendering + font loading~~
* more options for GLFW * more options for GLFW

View File

@@ -1,7 +1,5 @@
package goga package goga
import ()
// An actor ID is a unique integer, // An actor ID is a unique integer,
// which can be used to reference an actor. // which can be used to reference an actor.
type ActorId uint64 type ActorId uint64

Binary file not shown.

After

Width:  |  Height:  |  Size: 40 KiB

87
demo/input/input.go Normal file
View File

@@ -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)
}

View File

@@ -2,7 +2,7 @@ package main
import ( import (
"github.com/DeKugelschieber/go-game" "github.com/DeKugelschieber/go-game"
"github.com/go-gl/gl/v4.5-core/gl" "github.com/go-gl/gl/v3.2-core/gl"
) )
const ( const (

2
fbo.go
View File

@@ -1,7 +1,7 @@
package goga package goga
import ( import (
"github.com/go-gl/gl/v4.5-core/gl" "github.com/go-gl/gl/v3.2-core/gl"
) )
// Frame Buffer Object. // Frame Buffer Object.

11
game.go
View File

@@ -1,7 +1,7 @@
package goga package goga
import ( 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" "github.com/go-gl/glfw/v3.2/glfw"
"log" "log"
"math" "math"
@@ -58,7 +58,8 @@ var (
) )
func init() { 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") log.Print("Locking OS thread")
runtime.LockOSThread() runtime.LockOSThread()
} }
@@ -154,6 +155,8 @@ func Run(game Game, options *RunOptions) {
} }
}) })
initInput(wnd)
// make GL context current // make GL context current
wnd.MakeContextCurrent() wnd.MakeContextCurrent()
@@ -178,8 +181,8 @@ func Run(game Game, options *RunOptions) {
delta := time.Duration(0) delta := time.Duration(0)
var deltaSec float64 var deltaSec float64
for running { for true {
if exitOnClose && wnd.ShouldClose() { if !running || exitOnClose && wnd.ShouldClose() {
cleanup() cleanup()
return return
} }

View File

@@ -1,7 +1,7 @@
package goga package goga
import ( import (
"github.com/go-gl/gl/v4.5-core/gl" "github.com/go-gl/gl/v3.2-core/gl"
"log" "log"
) )

98
input.go Normal file
View File

@@ -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)
}

View File

@@ -1,7 +1,7 @@
package goga package goga
import ( import (
"github.com/go-gl/gl/v4.5-core/gl" "github.com/go-gl/gl/v3.2-core/gl"
) )
const ( const (

View File

@@ -3,7 +3,7 @@ package goga
import ( import (
"bufio" "bufio"
"errors" "errors"
"github.com/go-gl/gl/v4.5-core/gl" "github.com/go-gl/gl/v3.2-core/gl"
"image" "image"
"image/draw" "image/draw"
"image/png" "image/png"

View File

@@ -1,7 +1,7 @@
package goga package goga
import ( import (
"github.com/go-gl/gl/v4.5-core/gl" "github.com/go-gl/gl/v3.2-core/gl"
) )
const ( const (

View File

@@ -2,7 +2,7 @@ package goga
import ( import (
"errors" "errors"
"github.com/go-gl/gl/v4.5-core/gl" "github.com/go-gl/gl/v3.2-core/gl"
"log" "log"
"strings" "strings"
) )

View File

@@ -1,7 +1,7 @@
package goga package goga
import ( import (
"github.com/go-gl/gl/v4.5-core/gl" "github.com/go-gl/gl/v3.2-core/gl"
) )
const ( const (

2
tex.go
View File

@@ -1,7 +1,7 @@
package goga package goga
import ( import (
"github.com/go-gl/gl/v4.5-core/gl" "github.com/go-gl/gl/v3.2-core/gl"
"image" "image"
) )

View File

@@ -2,7 +2,7 @@ package goga
import ( import (
"encoding/json" "encoding/json"
"github.com/go-gl/gl/v4.5-core/gl" "github.com/go-gl/gl/v3.2-core/gl"
"io/ioutil" "io/ioutil"
) )

2
vao.go
View File

@@ -1,7 +1,7 @@
package goga package goga
import ( import (
"github.com/go-gl/gl/v4.5-core/gl" "github.com/go-gl/gl/v3.2-core/gl"
) )
// Vertex Array Object. // Vertex Array Object.

2
vbo.go
View File

@@ -1,7 +1,7 @@
package goga package goga
import ( import (
"github.com/go-gl/gl/v4.5-core/gl" "github.com/go-gl/gl/v3.2-core/gl"
"unsafe" "unsafe"
) )