diff --git a/colormatrix.go b/colormatrix.go index 72d28de9a..47a58946c 100644 --- a/colormatrix.go +++ b/colormatrix.go @@ -110,9 +110,9 @@ func TranslateColor(clr color.Color) ColorMatrix { // RotateHue returns a color matrix to rotate the hue func RotateHue(theta float64) ColorMatrix { sin, cos := math.Sincos(theta) - v1 := cos + (1.0 - cos) / 3.0 - v2 := (1.0 / 3.0) * (1.0 - cos) - math.Sqrt(1.0 / 3.0) * sin - v3 := (1.0 / 3.0) * (1.0 - cos) + math.Sqrt(1.0 / 3.0) * sin + v1 := cos + (1.0-cos)/3.0 + v2 := (1.0/3.0)*(1.0-cos) - math.Sqrt(1.0/3.0)*sin + v3 := (1.0/3.0)*(1.0-cos) + math.Sqrt(1.0/3.0)*sin return ColorMatrix{ [ColorMatrixDim - 1][ColorMatrixDim]float64{ {v1, v2, v3, 0, 0}, diff --git a/example/image/main.go b/example/image/main.go new file mode 100644 index 000000000..6cb0ce62f --- /dev/null +++ b/example/image/main.go @@ -0,0 +1,68 @@ +/* +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. +*/ + +package main + +import ( + "github.com/hajimehoshi/ebiten" + "image" + _ "image/jpeg" + "log" + "os" +) + +const ( + screenWidth = 320 + screenHeight = 240 +) + +type Game struct { + gophersTexture ebiten.TextureID +} + +func (g *Game) Update() error { + if g.gophersTexture.IsNil() { + file, err := os.Open("images/gophers.jpg") + if err != nil { + return err + } + defer file.Close() + img, _, err := image.Decode(file) + if err != nil { + return err + } + id, err := ebiten.NewTextureID(img, ebiten.FilterLinear) + if err != nil { + return err + } + g.gophersTexture = id + } + return nil +} + +func (g *Game) Draw(gr ebiten.GraphicsContext) error { + if g.gophersTexture.IsNil() { + return nil + } + ebiten.DrawWhole(gr.Texture(g.gophersTexture), 500, 414, ebiten.GeometryMatrixI(), ebiten.ColorMatrixI()) + return nil +} + +func main() { + if err := ebiten.Run(new(Game), screenWidth, screenHeight, 2, "Image (Ebiten Demo)", 60); err != nil { + log.Fatal(err) + } +} diff --git a/example/images/gophers.jpg b/example/images/gophers.jpg new file mode 100644 index 000000000..72075da31 Binary files /dev/null and b/example/images/gophers.jpg differ diff --git a/example/images/license.md b/example/images/license.md index 1e07bf581..2c2bb03c7 100644 --- a/example/images/license.md +++ b/example/images/license.md @@ -28,20 +28,14 @@ Email: info@9031.com http://www.sozai-page.com/02_sozai/b/b04/b04_002/b04_002.html ``` -## Other files +## gophers.jpg ``` -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. +http://blog.golang.org/go-programming-language-turns-two +``` + +## Other image files + +``` +Creative Commons Attribution 3.0 License ``` diff --git a/example/paint/main.go b/example/paint/main.go index 84d8c70a5..354117e5e 100644 --- a/example/paint/main.go +++ b/example/paint/main.go @@ -1,3 +1,19 @@ +/* +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. +*/ + package main import ( diff --git a/internal/assets/license.md b/internal/assets/license.md index 68948fdb5..74d967ac0 100644 --- a/internal/assets/license.md +++ b/internal/assets/license.md @@ -17,21 +17,3 @@ Unlimited permission is granted to use, copy, and distribute it, with or without modification, either commercially and noncommercially. THESE FONTS ARE PROVIDED "AS IS" WITHOUT WARRANTY. ``` - -## Other files - -``` -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. -``` diff --git a/run.go b/run.go index 11bc2a3ce..096dc7f4b 100644 --- a/run.go +++ b/run.go @@ -19,6 +19,7 @@ package ebiten import ( "os" "os/signal" + "runtime" "syscall" "time" ) @@ -31,9 +32,14 @@ type Game interface { var currentUI *ui +func init() { + runtime.LockOSThread() +} + // Run runs the game. +// This function must be called from the main thread. func Run(game Game, width, height, scale int, title string, fps int) error { - ui, err := newUI(game, width, height, scale, title) + ui, err := newUI(width, height, scale, title) if err != nil { return err } @@ -56,7 +62,7 @@ func Run(game Game, width, height, scale int, title string, fps int) error { } select { default: - if err := ui.drawGame(game); err != nil { + if err := ui.draw(game.Draw); err != nil { return err } case <-tick: diff --git a/ui.go b/ui.go index 2bd736db7..a23ce48bf 100644 --- a/ui.go +++ b/ui.go @@ -24,22 +24,18 @@ import ( "runtime" ) -func init() { - glfw.SetErrorCallback(func(err glfw.ErrorCode, desc string) { - panic(fmt.Sprintf("%v: %v\n", err, desc)) - }) -} - type ui struct { window *glfw.Window scale int graphicsContext *graphicsContext input input funcs chan func() - funcsDone chan struct{} } -func newUI(game Game, width, height, scale int, title string) (*ui, error) { +func newUI(width, height, scale int, title string) (*ui, error) { + glfw.SetErrorCallback(func(err glfw.ErrorCode, desc string) { + panic(fmt.Sprintf("%v: %v\n", err, desc)) + }) if !glfw.Init() { return nil, errors.New("glfw.Init() fails") } @@ -50,10 +46,9 @@ func newUI(game Game, width, height, scale int, title string) (*ui, error) { } u := &ui{ - window: window, - scale: scale, - funcs: make(chan func()), - funcsDone: make(chan struct{}), + window: window, + scale: scale, + funcs: make(chan func()), } u.run(width, height, scale) @@ -83,19 +78,15 @@ func (u *ui) isClosed() bool { return u.window.ShouldClose() } -func (u *ui) drawGame(game Game) error { - return u.draw(game) -} - func (u *ui) Sync(f func()) { u.use(f) } -func (u *ui) draw(game Game) (err error) { +func (u *ui) draw(f func(GraphicsContext) error) (err error) { u.use(func() { u.graphicsContext.preUpdate() }) - if err = game.Draw(&syncGraphicsContext{ + if err = f(&syncGraphicsContext{ syncer: u, innerGraphicsContext: u.graphicsContext, }); err != nil { @@ -131,16 +122,19 @@ func (u *ui) run(width, height, scale int) { runtime.LockOSThread() u.window.MakeContextCurrent() glfw.SwapInterval(1) - for { - (<-u.funcs)() - u.funcsDone <- struct{}{} + for f := range u.funcs { + f() } }() } func (u *ui) use(f func()) { - u.funcs <- f - <-u.funcsDone + ch := make(chan struct{}) + u.funcs <- func() { + f() + close(ch) + } + <-ch } func (u *ui) update() {