mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-11 19:48: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
|
// RotateHue returns a color matrix to rotate the hue
|
||||||
func RotateHue(theta float64) ColorMatrix {
|
func RotateHue(theta float64) ColorMatrix {
|
||||||
sin, cos := math.Sincos(theta)
|
sin, cos := math.Sincos(theta)
|
||||||
v1 := cos + (1.0 - cos) / 3.0
|
v1 := cos + (1.0-cos)/3.0
|
||||||
v2 := (1.0 / 3.0) * (1.0 - cos) - math.Sqrt(1.0 / 3.0) * sin
|
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
|
v3 := (1.0/3.0)*(1.0-cos) + math.Sqrt(1.0/3.0)*sin
|
||||||
return ColorMatrix{
|
return ColorMatrix{
|
||||||
[ColorMatrixDim - 1][ColorMatrixDim]float64{
|
[ColorMatrixDim - 1][ColorMatrixDim]float64{
|
||||||
{v1, v2, v3, 0, 0},
|
{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
|
http://www.sozai-page.com/02_sozai/b/b04/b04_002/b04_002.html
|
||||||
```
|
```
|
||||||
|
|
||||||
## Other files
|
## gophers.jpg
|
||||||
|
|
||||||
```
|
```
|
||||||
Copyright 2014 Hajime Hoshi
|
http://blog.golang.org/go-programming-language-turns-two
|
||||||
|
```
|
||||||
Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
you may not use this file except in compliance with the License.
|
## Other image files
|
||||||
You may obtain a copy of the License at
|
|
||||||
|
```
|
||||||
http://www.apache.org/licenses/LICENSE-2.0
|
Creative Commons Attribution 3.0 License
|
||||||
|
|
||||||
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.
|
|
||||||
```
|
```
|
||||||
|
@ -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
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -17,21 +17,3 @@ Unlimited permission is granted to use, copy, and distribute it, with
|
|||||||
or without modification, either commercially and noncommercially.
|
or without modification, either commercially and noncommercially.
|
||||||
THESE FONTS ARE PROVIDED "AS IS" WITHOUT WARRANTY.
|
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 (
|
import (
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
|
"runtime"
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@ -31,9 +32,14 @@ type Game interface {
|
|||||||
|
|
||||||
var currentUI *ui
|
var currentUI *ui
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
runtime.LockOSThread()
|
||||||
|
}
|
||||||
|
|
||||||
// Run runs the game.
|
// 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 {
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -56,7 +62,7 @@ func Run(game Game, width, height, scale int, title string, fps int) error {
|
|||||||
}
|
}
|
||||||
select {
|
select {
|
||||||
default:
|
default:
|
||||||
if err := ui.drawGame(game); err != nil {
|
if err := ui.draw(game.Draw); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
case <-tick:
|
case <-tick:
|
||||||
|
40
ui.go
40
ui.go
@ -24,22 +24,18 @@ import (
|
|||||||
"runtime"
|
"runtime"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
|
||||||
glfw.SetErrorCallback(func(err glfw.ErrorCode, desc string) {
|
|
||||||
panic(fmt.Sprintf("%v: %v\n", err, desc))
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
type ui struct {
|
type ui struct {
|
||||||
window *glfw.Window
|
window *glfw.Window
|
||||||
scale int
|
scale int
|
||||||
graphicsContext *graphicsContext
|
graphicsContext *graphicsContext
|
||||||
input input
|
input input
|
||||||
funcs chan func()
|
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() {
|
if !glfw.Init() {
|
||||||
return nil, errors.New("glfw.Init() fails")
|
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{
|
u := &ui{
|
||||||
window: window,
|
window: window,
|
||||||
scale: scale,
|
scale: scale,
|
||||||
funcs: make(chan func()),
|
funcs: make(chan func()),
|
||||||
funcsDone: make(chan struct{}),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
u.run(width, height, scale)
|
u.run(width, height, scale)
|
||||||
@ -83,19 +78,15 @@ func (u *ui) isClosed() bool {
|
|||||||
return u.window.ShouldClose()
|
return u.window.ShouldClose()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *ui) drawGame(game Game) error {
|
|
||||||
return u.draw(game)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (u *ui) Sync(f func()) {
|
func (u *ui) Sync(f func()) {
|
||||||
u.use(f)
|
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.use(func() {
|
||||||
u.graphicsContext.preUpdate()
|
u.graphicsContext.preUpdate()
|
||||||
})
|
})
|
||||||
if err = game.Draw(&syncGraphicsContext{
|
if err = f(&syncGraphicsContext{
|
||||||
syncer: u,
|
syncer: u,
|
||||||
innerGraphicsContext: u.graphicsContext,
|
innerGraphicsContext: u.graphicsContext,
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
@ -131,16 +122,19 @@ func (u *ui) run(width, height, scale int) {
|
|||||||
runtime.LockOSThread()
|
runtime.LockOSThread()
|
||||||
u.window.MakeContextCurrent()
|
u.window.MakeContextCurrent()
|
||||||
glfw.SwapInterval(1)
|
glfw.SwapInterval(1)
|
||||||
for {
|
for f := range u.funcs {
|
||||||
(<-u.funcs)()
|
f()
|
||||||
u.funcsDone <- struct{}{}
|
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *ui) use(f func()) {
|
func (u *ui) use(f func()) {
|
||||||
u.funcs <- f
|
ch := make(chan struct{})
|
||||||
<-u.funcsDone
|
u.funcs <- func() {
|
||||||
|
f()
|
||||||
|
close(ch)
|
||||||
|
}
|
||||||
|
<-ch
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *ui) update() {
|
func (u *ui) update() {
|
||||||
|
Loading…
Reference in New Issue
Block a user