ebiten/ui/glut/glut.go

213 lines
4.8 KiB
Go
Raw Normal View History

2013-07-01 15:25:41 +02:00
// 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.
2013-07-05 15:25:45 +02:00
// This package is experimental.
package glut
2013-06-17 16:10:55 +02:00
// #cgo LDFLAGS: -framework GLUT -framework OpenGL
//
// #include <stdlib.h>
// #include <GLUT/glut.h>
//
// void display(void);
2013-07-05 14:39:50 +02:00
// void mouse(int button, int state, int x, int y);
2013-07-02 18:27:04 +02:00
// void motion(int x, int y);
2013-06-17 16:51:15 +02:00
// void idle(void);
2013-06-17 16:10:55 +02:00
//
2013-06-17 19:01:59 +02:00
// static void setGlutFuncs(void) {
2013-06-17 16:10:55 +02:00
// glutDisplayFunc(display);
2013-07-05 14:39:50 +02:00
// glutMouseFunc(mouse);
2013-07-02 18:27:04 +02:00
// glutMotionFunc(motion);
2013-06-17 16:51:15 +02:00
// glutIdleFunc(idle);
// }
//
2013-06-17 16:10:55 +02:00
import "C"
import (
2013-06-20 16:29:51 +02:00
"github.com/hajimehoshi/go.ebiten"
"github.com/hajimehoshi/go.ebiten/graphics"
2013-06-26 18:13:09 +02:00
"github.com/hajimehoshi/go.ebiten/graphics/opengl"
2013-06-17 16:10:55 +02:00
"os"
2013-06-26 18:46:13 +02:00
"time"
2013-06-17 16:10:55 +02:00
"unsafe"
)
2013-07-05 15:25:45 +02:00
type glutInputEvent struct {
2013-07-05 14:39:50 +02:00
IsActive bool
2013-07-06 15:21:23 +02:00
X int
Y int
2013-06-25 03:27:32 +02:00
}
2013-06-19 16:51:41 +02:00
type GlutUI struct {
2013-10-05 18:47:19 +02:00
screenScale int
glutInputting chan glutInputEvent
graphicsDevice *opengl.Device
2013-10-06 11:22:45 +02:00
updating chan func(graphics.Context)
updated chan bool
2013-06-17 16:10:55 +02:00
}
2013-06-18 17:48:41 +02:00
var currentUI *GlutUI
2013-06-17 16:10:55 +02:00
//export display
func display() {
2013-10-06 11:22:45 +02:00
draw := <-currentUI.updating
currentUI.graphicsDevice.Update(draw)
currentUI.updated <- true
2013-06-17 16:51:15 +02:00
C.glutSwapBuffers()
}
2013-07-05 14:39:50 +02:00
//export mouse
func mouse(button, state, x, y C.int) {
2013-07-05 15:25:45 +02:00
event := glutInputEvent{false, -1, -1}
2013-07-05 14:46:52 +02:00
if state == C.GLUT_DOWN {
event.IsActive = true
event.X = int(x)
event.Y = int(y)
2013-07-05 14:39:50 +02:00
}
2013-07-05 14:46:52 +02:00
currentUI.glutInputting <- event
2013-07-05 14:39:50 +02:00
}
2013-07-02 18:27:04 +02:00
//export motion
func motion(x, y C.int) {
2013-07-05 15:25:45 +02:00
currentUI.glutInputting <- glutInputEvent{
2013-07-05 14:39:50 +02:00
IsActive: true,
2013-07-06 15:21:23 +02:00
X: int(x),
Y: int(y),
2013-06-25 03:27:32 +02:00
}
}
2013-06-17 16:51:15 +02:00
//export idle
func idle() {
C.glutPostRedisplay()
2013-06-17 16:10:55 +02:00
}
2013-07-06 18:45:19 +02:00
func new(screenWidth, screenHeight, screenScale int, title string) *GlutUI {
2013-10-05 18:47:19 +02:00
graphicsDevice := opengl.NewDevice(
screenWidth, screenHeight, screenScale)
2013-06-26 18:13:09 +02:00
ui := &GlutUI{
2013-10-05 18:47:19 +02:00
glutInputting: make(chan glutInputEvent, 10),
graphicsDevice: graphicsDevice,
2013-10-06 11:22:45 +02:00
updating: make(chan func(graphics.Context)),
updated: make(chan bool),
2013-06-26 18:13:09 +02:00
}
2013-06-25 03:27:32 +02:00
2013-06-17 16:10:55 +02:00
cargs := []*C.char{}
for _, arg := range os.Args {
cargs = append(cargs, C.CString(arg))
}
defer func() {
for _, carg := range cargs {
C.free(unsafe.Pointer(carg))
}
}()
cargc := C.int(len(cargs))
C.glutInit(&cargc, &cargs[0])
2013-06-19 16:51:41 +02:00
C.glutInitDisplayMode(C.GLUT_RGBA)
2013-06-18 17:48:41 +02:00
C.glutInitWindowSize(
2013-06-21 16:16:52 +02:00
C.int(screenWidth*screenScale),
C.int(screenHeight*screenScale))
2013-06-17 16:10:55 +02:00
2013-07-06 18:45:19 +02:00
cTitle := C.CString(title)
defer C.free(unsafe.Pointer(cTitle))
C.glutCreateWindow(cTitle)
2013-06-17 16:10:55 +02:00
2013-06-17 19:01:59 +02:00
C.setGlutFuncs()
2013-06-26 18:13:09 +02:00
2013-10-05 18:47:19 +02:00
graphicsDevice.Init()
2013-06-26 18:13:09 +02:00
return ui
2013-06-18 17:48:41 +02:00
}
2013-06-17 16:10:55 +02:00
2013-07-06 18:45:19 +02:00
func Run(game ebiten.Game, screenScale int, title string) {
2013-07-05 17:27:36 +02:00
screenWidth := game.ScreenWidth()
screenHeight := game.ScreenHeight()
2013-07-06 18:45:19 +02:00
ui := new(screenWidth, screenHeight, screenScale, title)
2013-07-05 17:27:36 +02:00
currentUI = ui
2013-10-05 18:47:19 +02:00
game.Init(ui.graphicsDevice.TextureFactory())
2013-06-18 17:48:41 +02:00
2013-06-25 03:27:32 +02:00
input := make(chan ebiten.InputState)
go func() {
2013-07-05 17:27:36 +02:00
ch := ui.glutInputting
2013-06-25 03:27:32 +02:00
for {
2013-06-26 19:10:56 +02:00
event := <-ch
2013-07-05 14:46:52 +02:00
inputState := ebiten.InputState{-1, -1}
2013-07-05 14:39:50 +02:00
if event.IsActive {
x := event.X / screenScale
y := event.Y / screenScale
if x < 0 {
x = 0
} else if screenWidth <= x {
x = screenWidth - 1
}
if y < 0 {
y = 0
} else if screenHeight <= y {
y = screenHeight - 1
}
2013-07-05 14:46:52 +02:00
inputState.X = x
inputState.Y = y
2013-06-25 03:27:32 +02:00
}
2013-07-05 14:46:52 +02:00
input <- inputState
2013-06-25 03:27:32 +02:00
}
}()
2013-06-26 18:46:13 +02:00
go func() {
2013-07-01 17:42:47 +02:00
frameTime := time.Duration(
2013-10-06 11:44:15 +02:00
int64(time.Second) / int64(ebiten.FPS))
2013-10-06 11:22:45 +02:00
tick := time.Tick(frameTime)
2013-07-05 14:02:17 +02:00
gameContext := &GameContext{
inputState: ebiten.InputState{-1, -1},
}
2013-10-06 11:22:45 +02:00
draw := func(context graphics.Context) {
game.Draw(context)
}
2013-06-26 18:46:13 +02:00
for {
select {
2013-07-05 14:02:17 +02:00
case gameContext.inputState = <-input:
2013-10-06 11:22:45 +02:00
case <-tick:
2013-07-05 14:02:17 +02:00
game.Update(gameContext)
2013-10-06 11:22:45 +02:00
case ui.updating <- draw:
<-ui.updated
2013-06-26 18:46:13 +02:00
}
2013-07-16 18:42:53 +02:00
if gameContext.terminated {
break
}
2013-06-26 18:46:13 +02:00
}
2013-07-16 18:42:53 +02:00
os.Exit(0)
2013-06-26 18:46:13 +02:00
}()
2013-07-05 15:25:45 +02:00
C.glutMainLoop()
}
type GameContext struct {
inputState ebiten.InputState
2013-07-16 18:42:53 +02:00
terminated bool
2013-07-05 15:25:45 +02:00
}
func (context *GameContext) InputState() ebiten.InputState {
return context.inputState
2013-06-26 18:46:13 +02:00
}
2013-07-16 18:42:53 +02:00
func (context *GameContext) Terminate() {
context.terminated = true
}