mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-24 18:58:54 +01:00
Add examples/image
This commit is contained in:
parent
46a9901be8
commit
1c460d80c3
@ -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},
|
||||
|
68
example/image/main.go
Normal file
68
example/image/main.go
Normal file
@ -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)
|
||||
}
|
||||
}
|
BIN
example/images/gophers.jpg
Normal file
BIN
example/images/gophers.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 38 KiB |
@ -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
|
||||
```
|
||||
|
@ -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 (
|
||||
|
@ -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.
|
||||
```
|
||||
|
10
run.go
10
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:
|
||||
|
34
ui.go
34
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")
|
||||
}
|
||||
@ -53,7 +49,6 @@ func newUI(game Game, width, height, scale int, title string) (*ui, error) {
|
||||
window: window,
|
||||
scale: scale,
|
||||
funcs: make(chan func()),
|
||||
funcsDone: make(chan struct{}),
|
||||
}
|
||||
|
||||
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() {
|
||||
|
Loading…
Reference in New Issue
Block a user