mirror of
https://github.com/Kugelschieber/go-game.git
synced 2026-01-18 06:40:28 +00:00
Set GL version to 3.1 (lowest possible version), fixed cleanup bug when calling Stop(), added input handling.
This commit is contained in:
5
CHANGELOG.md
Normal file
5
CHANGELOG.md
Normal file
@@ -0,0 +1,5 @@
|
||||
# Changelog
|
||||
|
||||
## 0.1_beta
|
||||
|
||||
* beta release
|
||||
@@ -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
|
||||
|
||||
|
||||
2
ToDo.md
2
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
|
||||
|
||||
2
actor.go
2
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
|
||||
|
||||
BIN
demo/input/assets/gopher.png
Normal file
BIN
demo/input/assets/gopher.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 40 KiB |
87
demo/input/input.go
Normal file
87
demo/input/input.go
Normal 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)
|
||||
}
|
||||
@@ -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 (
|
||||
|
||||
2
fbo.go
2
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.
|
||||
|
||||
11
game.go
11
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
|
||||
}
|
||||
|
||||
@@ -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"
|
||||
)
|
||||
|
||||
|
||||
98
input.go
Normal file
98
input.go
Normal 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)
|
||||
}
|
||||
@@ -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 (
|
||||
|
||||
@@ -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"
|
||||
|
||||
2
model.go
2
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 (
|
||||
|
||||
@@ -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"
|
||||
)
|
||||
|
||||
@@ -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 (
|
||||
|
||||
2
tex.go
2
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"
|
||||
)
|
||||
|
||||
|
||||
2
text.go
2
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"
|
||||
)
|
||||
|
||||
|
||||
2
vao.go
2
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.
|
||||
|
||||
Reference in New Issue
Block a user