2014-12-09 15:16:04 +01:00
|
|
|
/*
|
|
|
|
Copyright 2014 Hajime Hoshi
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2014-12-14 08:53:32 +01:00
|
|
|
package ebiten
|
2014-12-05 18:26:02 +01:00
|
|
|
|
|
|
|
import (
|
2014-12-13 06:41:38 +01:00
|
|
|
"github.com/go-gl/gl"
|
2014-12-05 18:26:02 +01:00
|
|
|
glfw "github.com/go-gl/glfw3"
|
|
|
|
"image"
|
|
|
|
"runtime"
|
|
|
|
)
|
|
|
|
|
2014-12-07 15:18:56 +01:00
|
|
|
type canvas struct {
|
2014-12-10 15:52:37 +01:00
|
|
|
window *glfw.Window
|
2014-12-10 19:50:35 +01:00
|
|
|
scale int
|
2014-12-14 08:53:32 +01:00
|
|
|
graphicsContext *graphicsContext
|
2014-12-14 09:07:45 +01:00
|
|
|
input input
|
2014-12-10 15:52:37 +01:00
|
|
|
funcs chan func()
|
|
|
|
funcsDone chan struct{}
|
2014-12-05 18:26:02 +01:00
|
|
|
}
|
|
|
|
|
2014-12-14 08:53:32 +01:00
|
|
|
func (c *canvas) draw(game Game) (err error) {
|
2014-12-05 18:26:02 +01:00
|
|
|
c.use(func() {
|
2014-12-10 15:52:37 +01:00
|
|
|
c.graphicsContext.PreUpdate()
|
2014-12-08 17:08:47 +01:00
|
|
|
})
|
2014-12-14 08:53:32 +01:00
|
|
|
if err = game.Draw(&syncGraphicsContext{c}); err != nil {
|
2014-12-08 17:08:47 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
c.use(func() {
|
2014-12-10 15:52:37 +01:00
|
|
|
c.graphicsContext.PostUpdate()
|
2014-12-05 18:26:02 +01:00
|
|
|
c.window.SwapBuffers()
|
|
|
|
})
|
2014-12-06 20:14:35 +01:00
|
|
|
return
|
2014-12-05 18:26:02 +01:00
|
|
|
}
|
|
|
|
|
2014-12-10 02:42:47 +01:00
|
|
|
func (c *canvas) isClosed() bool {
|
2014-12-05 18:26:02 +01:00
|
|
|
return c.window.ShouldClose()
|
|
|
|
}
|
|
|
|
|
2014-12-14 08:53:32 +01:00
|
|
|
func (c *canvas) NewTextureID(img image.Image, filter Filter) (TextureID, error) {
|
|
|
|
var id TextureID
|
2014-12-05 18:26:02 +01:00
|
|
|
var err error
|
|
|
|
c.use(func() {
|
2014-12-13 06:41:38 +01:00
|
|
|
glFilter := 0
|
|
|
|
switch filter {
|
2014-12-14 08:53:32 +01:00
|
|
|
case FilterNearest:
|
2014-12-13 06:41:38 +01:00
|
|
|
glFilter = gl.NEAREST
|
2014-12-14 08:53:32 +01:00
|
|
|
case FilterLinear:
|
2014-12-13 06:41:38 +01:00
|
|
|
glFilter = gl.LINEAR
|
|
|
|
default:
|
|
|
|
panic("not reached")
|
|
|
|
}
|
2014-12-14 08:53:32 +01:00
|
|
|
id, err = newTextureID(img, glFilter)
|
2014-12-05 18:26:02 +01:00
|
|
|
})
|
|
|
|
return id, err
|
|
|
|
}
|
|
|
|
|
2014-12-14 08:53:32 +01:00
|
|
|
func (c *canvas) NewRenderTargetID(width, height int, filter Filter) (RenderTargetID, error) {
|
|
|
|
var id RenderTargetID
|
2014-12-05 18:26:02 +01:00
|
|
|
var err error
|
|
|
|
c.use(func() {
|
2014-12-13 06:41:38 +01:00
|
|
|
glFilter := 0
|
|
|
|
switch filter {
|
2014-12-14 08:53:32 +01:00
|
|
|
case FilterNearest:
|
2014-12-13 06:41:38 +01:00
|
|
|
glFilter = gl.NEAREST
|
2014-12-14 08:53:32 +01:00
|
|
|
case FilterLinear:
|
2014-12-13 06:41:38 +01:00
|
|
|
glFilter = gl.LINEAR
|
|
|
|
default:
|
|
|
|
panic("not reached")
|
|
|
|
}
|
2014-12-14 08:53:32 +01:00
|
|
|
id, err = newRenderTargetID(width, height, glFilter)
|
2014-12-05 18:26:02 +01:00
|
|
|
})
|
|
|
|
return id, err
|
|
|
|
}
|
|
|
|
|
2014-12-07 20:22:50 +01:00
|
|
|
func (c *canvas) run(width, height, scale int) {
|
2014-12-05 18:26:02 +01:00
|
|
|
go func() {
|
|
|
|
runtime.LockOSThread()
|
|
|
|
c.window.MakeContextCurrent()
|
|
|
|
glfw.SwapInterval(1)
|
|
|
|
for {
|
2014-12-08 17:35:35 +01:00
|
|
|
(<-c.funcs)()
|
2014-12-05 18:26:02 +01:00
|
|
|
c.funcsDone <- struct{}{}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2014-12-07 15:18:56 +01:00
|
|
|
func (c *canvas) use(f func()) {
|
2014-12-05 18:26:02 +01:00
|
|
|
c.funcs <- f
|
|
|
|
<-c.funcsDone
|
|
|
|
}
|
2014-12-05 19:10:17 +01:00
|
|
|
|
2014-12-07 15:18:56 +01:00
|
|
|
func (c *canvas) update() {
|
2014-12-14 08:53:32 +01:00
|
|
|
c.input.Update(c.window, c.scale)
|
2014-12-05 19:10:17 +01:00
|
|
|
}
|
2014-12-10 15:52:37 +01:00
|
|
|
|
2014-12-14 08:53:32 +01:00
|
|
|
func (c *canvas) IsKeyPressed(key Key) bool {
|
2014-12-10 15:52:37 +01:00
|
|
|
return c.input.IsKeyPressed(key)
|
|
|
|
}
|
|
|
|
|
2014-12-14 08:53:32 +01:00
|
|
|
func (c *canvas) IsMouseButtonPressed(button MouseButton) bool {
|
2014-12-10 15:52:37 +01:00
|
|
|
return c.input.IsMouseButtonPressed(button)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *canvas) CursorPosition() (x, y int) {
|
|
|
|
return c.input.CursorPosition()
|
|
|
|
}
|