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-06-17 16:10:55 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
// #cgo LDFLAGS: -framework GLUT -framework OpenGL
|
|
|
|
//
|
|
|
|
// #include <stdlib.h>
|
|
|
|
// #include <GLUT/glut.h>
|
|
|
|
//
|
|
|
|
// void display(void);
|
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-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"
|
2013-06-24 15:27:43 +02:00
|
|
|
"github.com/hajimehoshi/go.ebiten/example/game/blank"
|
2013-07-02 18:27:04 +02:00
|
|
|
"github.com/hajimehoshi/go.ebiten/example/game/input"
|
2013-06-23 16:43:44 +02:00
|
|
|
"github.com/hajimehoshi/go.ebiten/example/game/monochrome"
|
2013-06-22 19:01:26 +02:00
|
|
|
"github.com/hajimehoshi/go.ebiten/example/game/rects"
|
2013-06-22 16:16:01 +02:00
|
|
|
"github.com/hajimehoshi/go.ebiten/example/game/rotating"
|
|
|
|
"github.com/hajimehoshi/go.ebiten/example/game/sprites"
|
2013-06-20 16:29:51 +02:00
|
|
|
"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-19 16:49:24 +02:00
|
|
|
"runtime"
|
2013-06-26 18:46:13 +02:00
|
|
|
"time"
|
2013-06-17 16:10:55 +02:00
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
2013-06-25 03:27:32 +02:00
|
|
|
type GlutInputEvent struct {
|
2013-07-02 18:27:04 +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-07-02 18:27:04 +02:00
|
|
|
glutInputting chan GlutInputEvent
|
|
|
|
updating chan chan func()
|
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-06-26 18:46:13 +02:00
|
|
|
ch := make(chan func())
|
|
|
|
currentUI.updating <- ch
|
|
|
|
f := <-ch
|
|
|
|
f()
|
2013-06-17 16:51:15 +02:00
|
|
|
C.glutSwapBuffers()
|
|
|
|
}
|
|
|
|
|
2013-07-02 18:27:04 +02:00
|
|
|
//export motion
|
|
|
|
func motion(x, y C.int) {
|
|
|
|
currentUI.glutInputting <- GlutInputEvent{
|
|
|
|
X: int(x),
|
|
|
|
Y: int(y),
|
2013-06-25 03:27:32 +02:00
|
|
|
}
|
2013-06-23 14:51:23 +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-06-26 18:46:13 +02:00
|
|
|
func NewGlutUI(screenWidth, screenHeight, screenScale int) *GlutUI {
|
2013-06-26 18:13:09 +02:00
|
|
|
ui := &GlutUI{
|
2013-07-02 18:27:04 +02:00
|
|
|
glutInputting: make(chan GlutInputEvent, 10),
|
|
|
|
updating: make(chan chan func()),
|
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
|
|
|
|
|
|
|
title := C.CString("Ebiten Demo")
|
|
|
|
defer C.free(unsafe.Pointer(title))
|
|
|
|
C.glutCreateWindow(title)
|
|
|
|
|
2013-06-17 19:01:59 +02:00
|
|
|
C.setGlutFuncs()
|
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-06-26 18:46:13 +02:00
|
|
|
func (ui *GlutUI) Run() {
|
2013-06-17 16:10:55 +02:00
|
|
|
C.glutMainLoop()
|
|
|
|
}
|
2013-06-18 17:48:41 +02:00
|
|
|
|
|
|
|
func main() {
|
2013-06-19 16:49:24 +02:00
|
|
|
runtime.GOMAXPROCS(runtime.NumCPU())
|
|
|
|
|
2013-06-21 04:02:49 +02:00
|
|
|
gameName := ""
|
|
|
|
if 2 <= len(os.Args) {
|
|
|
|
gameName = os.Args[1]
|
|
|
|
}
|
|
|
|
|
2013-06-26 18:46:13 +02:00
|
|
|
var game ebiten.Game
|
2013-06-21 04:02:49 +02:00
|
|
|
switch gameName {
|
2013-06-24 15:27:43 +02:00
|
|
|
case "blank":
|
2013-06-26 18:46:13 +02:00
|
|
|
game = blank.New()
|
2013-07-02 18:27:04 +02:00
|
|
|
case "input":
|
|
|
|
game = input.New()
|
2013-06-23 16:43:44 +02:00
|
|
|
case "monochrome":
|
2013-06-26 18:46:13 +02:00
|
|
|
game = monochrome.New()
|
2013-06-22 19:01:26 +02:00
|
|
|
case "rects":
|
2013-06-26 18:46:13 +02:00
|
|
|
game = rects.New()
|
2013-06-22 16:16:01 +02:00
|
|
|
case "rotating":
|
2013-06-26 18:46:13 +02:00
|
|
|
game = rotating.New()
|
2013-06-22 19:01:26 +02:00
|
|
|
case "sprites":
|
2013-06-26 18:46:13 +02:00
|
|
|
game = sprites.New()
|
2013-06-21 04:02:49 +02:00
|
|
|
default:
|
2013-06-26 18:46:13 +02:00
|
|
|
game = rotating.New()
|
2013-06-21 04:02:49 +02:00
|
|
|
}
|
2013-06-21 16:16:52 +02:00
|
|
|
|
2013-07-01 17:42:47 +02:00
|
|
|
const screenScale = 2
|
2013-07-02 18:27:04 +02:00
|
|
|
screenWidth := game.ScreenWidth()
|
|
|
|
screenHeight := game.ScreenHeight()
|
|
|
|
currentUI = NewGlutUI(screenWidth, screenHeight, screenScale)
|
2013-07-01 17:42:47 +02:00
|
|
|
|
|
|
|
graphicsDevice := opengl.NewDevice(
|
2013-07-02 18:27:04 +02:00
|
|
|
screenWidth, screenHeight, screenScale,
|
2013-07-01 17:42:47 +02:00
|
|
|
currentUI.updating)
|
|
|
|
|
|
|
|
game.Init(graphicsDevice.TextureFactory())
|
|
|
|
draw := graphicsDevice.Drawing()
|
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-02 18:27:04 +02:00
|
|
|
ch := currentUI.glutInputting
|
2013-06-25 03:27:32 +02:00
|
|
|
for {
|
2013-06-26 19:10:56 +02:00
|
|
|
event := <-ch
|
2013-07-02 18:27:04 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
input <- ebiten.InputState{
|
|
|
|
X: x,
|
|
|
|
Y: y,
|
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(
|
|
|
|
int64(time.Second) / int64(game.Fps()))
|
2013-06-26 18:46:13 +02:00
|
|
|
update := time.Tick(frameTime)
|
2013-07-04 16:57:53 +02:00
|
|
|
inputState := ebiten.InputState{-1, -1}
|
2013-06-26 18:46:13 +02:00
|
|
|
for {
|
|
|
|
select {
|
2013-07-04 16:57:53 +02:00
|
|
|
case inputState = <-input:
|
2013-06-26 18:46:13 +02:00
|
|
|
case <-update:
|
2013-07-04 16:57:53 +02:00
|
|
|
game.Update(inputState)
|
2013-06-27 17:33:03 +02:00
|
|
|
case drawing := <-draw:
|
2013-06-26 18:46:13 +02:00
|
|
|
ch := make(chan interface{})
|
2013-07-04 16:57:53 +02:00
|
|
|
drawing <- func(context graphics.Context) {
|
|
|
|
game.Draw(context)
|
2013-06-27 17:33:03 +02:00
|
|
|
close(ch)
|
|
|
|
}
|
2013-06-26 18:46:13 +02:00
|
|
|
<-ch
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
currentUI.Run()
|
|
|
|
}
|