Add ui/glut

This commit is contained in:
Hajime Hoshi 2013-07-05 22:25:45 +09:00
parent 55ae4a6855
commit 0c8ba5fef6
4 changed files with 96 additions and 59 deletions

View File

Before

Width:  |  Height:  |  Size: 2.1 KiB

After

Width:  |  Height:  |  Size: 2.1 KiB

View File

Before

Width:  |  Height:  |  Size: 3.0 KiB

After

Width:  |  Height:  |  Size: 3.0 KiB

67
example/main.go Normal file
View File

@ -0,0 +1,67 @@
// Copyright 2013 Hajime Hoshi
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package main
import (
"github.com/hajimehoshi/go.ebiten"
"github.com/hajimehoshi/go.ebiten/example/game/blank"
"github.com/hajimehoshi/go.ebiten/example/game/input"
"github.com/hajimehoshi/go.ebiten/example/game/monochrome"
"github.com/hajimehoshi/go.ebiten/example/game/rects"
"github.com/hajimehoshi/go.ebiten/example/game/rotating"
"github.com/hajimehoshi/go.ebiten/example/game/sprites"
"github.com/hajimehoshi/go.ebiten/ui/glut"
"os"
"runtime"
)
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
gameName := ""
if 2 <= len(os.Args) {
gameName = os.Args[1]
}
var game ebiten.Game
switch gameName {
case "blank":
game = blank.New()
case "input":
game = input.New()
case "monochrome":
game = monochrome.New()
case "rects":
game = rects.New()
case "rotating":
game = rotating.New()
case "sprites":
game = sprites.New()
default:
game = rotating.New()
}
const screenScale = 2
screenWidth := game.ScreenWidth()
screenHeight := game.ScreenHeight()
ui := glut.New(screenWidth, screenHeight, screenScale)
ui.Run(game)
}

View File

@ -18,7 +18,8 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE. // SOFTWARE.
package main // This package is experimental.
package glut
// #cgo LDFLAGS: -framework GLUT -framework OpenGL // #cgo LDFLAGS: -framework GLUT -framework OpenGL
// //
@ -40,28 +41,24 @@ package main
import "C" import "C"
import ( import (
"github.com/hajimehoshi/go.ebiten" "github.com/hajimehoshi/go.ebiten"
"github.com/hajimehoshi/go.ebiten/example/game/blank"
"github.com/hajimehoshi/go.ebiten/example/game/input"
"github.com/hajimehoshi/go.ebiten/example/game/monochrome"
"github.com/hajimehoshi/go.ebiten/example/game/rects"
"github.com/hajimehoshi/go.ebiten/example/game/rotating"
"github.com/hajimehoshi/go.ebiten/example/game/sprites"
"github.com/hajimehoshi/go.ebiten/graphics" "github.com/hajimehoshi/go.ebiten/graphics"
"github.com/hajimehoshi/go.ebiten/graphics/opengl" "github.com/hajimehoshi/go.ebiten/graphics/opengl"
"os" "os"
"runtime"
"time" "time"
"unsafe" "unsafe"
) )
type GlutInputEvent struct { type glutInputEvent struct {
IsActive bool IsActive bool
X int X int
Y int Y int
} }
type GlutUI struct { type GlutUI struct {
glutInputting chan GlutInputEvent screenWidth int
screenHeight int
screenScale int
glutInputting chan glutInputEvent
updating chan chan func() updating chan chan func()
} }
@ -78,7 +75,7 @@ func display() {
//export mouse //export mouse
func mouse(button, state, x, y C.int) { func mouse(button, state, x, y C.int) {
event := GlutInputEvent{false, -1, -1} event := glutInputEvent{false, -1, -1}
if state == C.GLUT_DOWN { if state == C.GLUT_DOWN {
event.IsActive = true event.IsActive = true
event.X = int(x) event.X = int(x)
@ -89,7 +86,7 @@ func mouse(button, state, x, y C.int) {
//export motion //export motion
func motion(x, y C.int) { func motion(x, y C.int) {
currentUI.glutInputting <- GlutInputEvent{ currentUI.glutInputting <- glutInputEvent{
IsActive: true, IsActive: true,
X: int(x), X: int(x),
Y: int(y), Y: int(y),
@ -101,9 +98,12 @@ func idle() {
C.glutPostRedisplay() C.glutPostRedisplay()
} }
func NewGlutUI(screenWidth, screenHeight, screenScale int) *GlutUI { func New(screenWidth, screenHeight, screenScale int) *GlutUI {
ui := &GlutUI{ ui := &GlutUI{
glutInputting: make(chan GlutInputEvent, 10), screenWidth: screenWidth,
screenHeight: screenHeight,
screenScale: screenScale,
glutInputting: make(chan glutInputEvent, 10),
updating: make(chan chan func()), updating: make(chan chan func()),
} }
@ -130,54 +130,13 @@ func NewGlutUI(screenWidth, screenHeight, screenScale int) *GlutUI {
C.setGlutFuncs() C.setGlutFuncs()
currentUI = ui
return ui return ui
} }
func (ui *GlutUI) Run() { func (ui *GlutUI) Run(game ebiten.Game) {
C.glutMainLoop()
}
type GameContext struct {
inputState ebiten.InputState
}
func (context *GameContext) InputState() ebiten.InputState {
return context.inputState
}
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
gameName := ""
if 2 <= len(os.Args) {
gameName = os.Args[1]
}
var game ebiten.Game
switch gameName {
case "blank":
game = blank.New()
case "input":
game = input.New()
case "monochrome":
game = monochrome.New()
case "rects":
game = rects.New()
case "rotating":
game = rotating.New()
case "sprites":
game = sprites.New()
default:
game = rotating.New()
}
const screenScale = 2
screenWidth := game.ScreenWidth()
screenHeight := game.ScreenHeight()
currentUI = NewGlutUI(screenWidth, screenHeight, screenScale)
graphicsDevice := opengl.NewDevice( graphicsDevice := opengl.NewDevice(
screenWidth, screenHeight, screenScale, ui.screenWidth, ui.screenHeight, ui.screenScale,
currentUI.updating) currentUI.updating)
game.Init(graphicsDevice.TextureFactory()) game.Init(graphicsDevice.TextureFactory())
@ -185,6 +144,9 @@ func main() {
input := make(chan ebiten.InputState) input := make(chan ebiten.InputState)
go func() { go func() {
screenWidth := ui.screenWidth
screenHeight := ui.screenHeight
screenScale := ui.screenScale
ch := currentUI.glutInputting ch := currentUI.glutInputting
for { for {
event := <-ch event := <-ch
@ -232,5 +194,13 @@ func main() {
} }
}() }()
currentUI.Run() C.glutMainLoop()
}
type GameContext struct {
inputState ebiten.InputState
}
func (context *GameContext) InputState() ebiten.InputState {
return context.inputState
} }