doc: Change indent size 4 -> 8

This commit is contained in:
Hajime Hoshi 2016-08-27 05:36:52 +09:00
parent 69dafff704
commit 12920a79da
18 changed files with 1108 additions and 1108 deletions

View File

@ -185,7 +185,7 @@ func (e *example) Source() string {
panic(err) panic(err)
} }
str := regexp.MustCompile("(?s)^.*?\n\n").ReplaceAllString(string(b), "") str := regexp.MustCompile("(?s)^.*?\n\n").ReplaceAllString(string(b), "")
str = strings.Replace(str, "\t", " ", -1) str = strings.Replace(str, "\t", " ", -1)
return str return str
} }

View File

@ -70,17 +70,17 @@ the <code>main.go</code> file:</p>
<pre><code>package main <pre><code>package main
import ( import (
"github.com/hajimehoshi/ebiten" "github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/ebitenutil" "github.com/hajimehoshi/ebiten/ebitenutil"
) )
func update(screen *ebiten.Image) error { func update(screen *ebiten.Image) error {
ebitenutil.DebugPrint(screen, "Hello world!") ebitenutil.DebugPrint(screen, "Hello world!")
return nil return nil
} }
func main() { func main() {
ebiten.Run(update, 320, 240, 2, "Hello world!") ebiten.Run(update, 320, 240, 2, "Hello world!")
} }
</code></pre> </code></pre>

View File

@ -27,78 +27,78 @@
package main package main
import ( import (
&#34;image/color&#34; &#34;image/color&#34;
_ &#34;image/png&#34; _ &#34;image/png&#34;
&#34;log&#34; &#34;log&#34;
&#34;github.com/hajimehoshi/ebiten&#34; &#34;github.com/hajimehoshi/ebiten&#34;
&#34;github.com/hajimehoshi/ebiten/ebitenutil&#34; &#34;github.com/hajimehoshi/ebiten/ebitenutil&#34;
) )
const ( const (
screenWidth = 320 screenWidth = 320
screenHeight = 240 screenHeight = 240
) )
var ( var (
count int count int
ebitenImage *ebiten.Image ebitenImage *ebiten.Image
) )
type imageParts struct { type imageParts struct {
diff float64 diff float64
} }
func (p *imageParts) Src(i int) (int, int, int, int) { func (p *imageParts) Src(i int) (int, int, int, int) {
w, h := ebitenImage.Size() w, h := ebitenImage.Size()
return 0, 0, w, h return 0, 0, w, h
} }
func (p *imageParts) Dst(i int) (int, int, int, int) { func (p *imageParts) Dst(i int) (int, int, int, int) {
x := int(float64(i%10)*p.diff &#43; 15) x := int(float64(i%10)*p.diff &#43; 15)
y := int(float64(i/10)*p.diff &#43; 20) y := int(float64(i/10)*p.diff &#43; 20)
w, h := ebitenImage.Size() w, h := ebitenImage.Size()
return x, y, x &#43; w, y &#43; h return x, y, x &#43; w, y &#43; h
} }
func (p *imageParts) Len() int { func (p *imageParts) Len() int {
return 10 * 10 return 10 * 10
} }
func update(screen *ebiten.Image) error { func update(screen *ebiten.Image) error {
count&#43;&#43; count&#43;&#43;
count %= ebiten.FPS * 10 count %= ebiten.FPS * 10
diff := float64(count) * 0.2 diff := float64(count) * 0.2
switch { switch {
case 480 &lt; count: case 480 &lt; count:
diff = 0 diff = 0
case 240 &lt; count: case 240 &lt; count:
diff = float64(480-count) * 0.2 diff = float64(480-count) * 0.2
} }
if err := screen.Fill(color.NRGBA{0x00, 0x00, 0x80, 0xff}); err != nil { if err := screen.Fill(color.NRGBA{0x00, 0x00, 0x80, 0xff}); err != nil {
return err return err
} }
op := &amp;ebiten.DrawImageOptions{} op := &amp;ebiten.DrawImageOptions{}
op.ColorM.Scale(1.0, 1.0, 1.0, 0.5) op.ColorM.Scale(1.0, 1.0, 1.0, 0.5)
op.ImageParts = &amp;imageParts{ op.ImageParts = &amp;imageParts{
diff: diff, diff: diff,
} }
if err := screen.DrawImage(ebitenImage, op); err != nil { if err := screen.DrawImage(ebitenImage, op); err != nil {
return err return err
} }
return nil return nil
} }
func main() { func main() {
var err error var err error
ebitenImage, _, err = ebitenutil.NewImageFromFile(&#34;_resources/images/ebiten.png&#34;, ebiten.FilterNearest) ebitenImage, _, err = ebitenutil.NewImageFromFile(&#34;_resources/images/ebiten.png&#34;, ebiten.FilterNearest)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
if err := ebiten.Run(update, screenWidth, screenHeight, 2, &#34;Alpha Blending (Ebiten Demo)&#34;); err != nil { if err := ebiten.Run(update, screenWidth, screenHeight, 2, &#34;Alpha Blending (Ebiten Demo)&#34;); err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }
</code></pre> </code></pre>

View File

@ -27,287 +27,287 @@
package main package main
import ( import (
&#34;fmt&#34; &#34;fmt&#34;
&#34;image/color&#34; &#34;image/color&#34;
&#34;io/ioutil&#34; &#34;io/ioutil&#34;
&#34;log&#34; &#34;log&#34;
&#34;time&#34; &#34;time&#34;
&#34;github.com/hajimehoshi/ebiten&#34; &#34;github.com/hajimehoshi/ebiten&#34;
&#34;github.com/hajimehoshi/ebiten/audio&#34; &#34;github.com/hajimehoshi/ebiten/audio&#34;
&#34;github.com/hajimehoshi/ebiten/audio/vorbis&#34; &#34;github.com/hajimehoshi/ebiten/audio/vorbis&#34;
&#34;github.com/hajimehoshi/ebiten/audio/wav&#34; &#34;github.com/hajimehoshi/ebiten/audio/wav&#34;
&#34;github.com/hajimehoshi/ebiten/ebitenutil&#34; &#34;github.com/hajimehoshi/ebiten/ebitenutil&#34;
) )
const ( const (
screenWidth = 320 screenWidth = 320
screenHeight = 240 screenHeight = 240
) )
var ( var (
playerBarImage *ebiten.Image playerBarImage *ebiten.Image
playerCurrentImage *ebiten.Image playerCurrentImage *ebiten.Image
) )
func init() { func init() {
var err error var err error
playerBarImage, err = ebiten.NewImage(300, 4, ebiten.FilterNearest) playerBarImage, err = ebiten.NewImage(300, 4, ebiten.FilterNearest)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
if err := playerBarImage.Fill(&amp;color.RGBA{0x80, 0x80, 0x80, 0xff}); err != nil { if err := playerBarImage.Fill(&amp;color.RGBA{0x80, 0x80, 0x80, 0xff}); err != nil {
log.Fatal(err) log.Fatal(err)
} }
playerCurrentImage, err = ebiten.NewImage(4, 10, ebiten.FilterNearest) playerCurrentImage, err = ebiten.NewImage(4, 10, ebiten.FilterNearest)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
if err := playerCurrentImage.Fill(&amp;color.RGBA{0xff, 0xff, 0xff, 0xff}); err != nil { if err := playerCurrentImage.Fill(&amp;color.RGBA{0xff, 0xff, 0xff, 0xff}); err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }
type Player struct { type Player struct {
audioPlayer *audio.Player audioPlayer *audio.Player
total time.Duration total time.Duration
} }
var ( var (
audioContext *audio.Context audioContext *audio.Context
musicPlayer *Player musicPlayer *Player
seBytes []byte seBytes []byte
musicCh = make(chan *Player) musicCh = make(chan *Player)
seCh = make(chan []byte) seCh = make(chan []byte)
mouseButtonState = map[ebiten.MouseButton]int{} mouseButtonState = map[ebiten.MouseButton]int{}
keyState = map[ebiten.Key]int{} keyState = map[ebiten.Key]int{}
volume128 = 128 volume128 = 128
) )
func playerBarRect() (x, y, w, h int) { func playerBarRect() (x, y, w, h int) {
w, h = playerBarImage.Size() w, h = playerBarImage.Size()
x = (screenWidth - w) / 2 x = (screenWidth - w) / 2
y = screenHeight - h - 16 y = screenHeight - h - 16
return return
} }
func (p *Player) updateSE() error { func (p *Player) updateSE() error {
if seBytes == nil { if seBytes == nil {
return nil return nil
} }
if !ebiten.IsKeyPressed(ebiten.KeyP) { if !ebiten.IsKeyPressed(ebiten.KeyP) {
keyState[ebiten.KeyP] = 0 keyState[ebiten.KeyP] = 0
return nil return nil
} }
keyState[ebiten.KeyP]&#43;&#43; keyState[ebiten.KeyP]&#43;&#43;
if keyState[ebiten.KeyP] != 1 { if keyState[ebiten.KeyP] != 1 {
return nil return nil
} }
sePlayer, err := audio.NewPlayerFromBytes(audioContext, seBytes) sePlayer, err := audio.NewPlayerFromBytes(audioContext, seBytes)
if err != nil { if err != nil {
return err return err
} }
return sePlayer.Play() return sePlayer.Play()
} }
func (p *Player) updateVolume() error { func (p *Player) updateVolume() error {
if p.audioPlayer == nil { if p.audioPlayer == nil {
return nil
}
if ebiten.IsKeyPressed(ebiten.KeyZ) {
volume128--
}
if ebiten.IsKeyPressed(ebiten.KeyX) {
volume128&#43;&#43;
}
if volume128 &lt; 0 {
volume128 = 0
}
if 128 &lt; volume128 {
volume128 = 128
}
p.audioPlayer.SetVolume(float64(volume128) / 128)
return nil return nil
}
if ebiten.IsKeyPressed(ebiten.KeyZ) {
volume128--
}
if ebiten.IsKeyPressed(ebiten.KeyX) {
volume128&#43;&#43;
}
if volume128 &lt; 0 {
volume128 = 0
}
if 128 &lt; volume128 {
volume128 = 128
}
p.audioPlayer.SetVolume(float64(volume128) / 128)
return nil
} }
func (p *Player) updatePlayPause() error { func (p *Player) updatePlayPause() error {
if p.audioPlayer == nil { if p.audioPlayer == nil {
return nil return nil
} }
if !ebiten.IsKeyPressed(ebiten.KeyS) { if !ebiten.IsKeyPressed(ebiten.KeyS) {
keyState[ebiten.KeyS] = 0 keyState[ebiten.KeyS] = 0
return nil return nil
} }
keyState[ebiten.KeyS]&#43;&#43; keyState[ebiten.KeyS]&#43;&#43;
if keyState[ebiten.KeyS] != 1 { if keyState[ebiten.KeyS] != 1 {
return nil return nil
} }
if p.audioPlayer.IsPlaying() { if p.audioPlayer.IsPlaying() {
return p.audioPlayer.Pause() return p.audioPlayer.Pause()
} }
return p.audioPlayer.Play() return p.audioPlayer.Play()
} }
func (p *Player) updateBar() error { func (p *Player) updateBar() error {
if p.audioPlayer == nil { if p.audioPlayer == nil {
return nil return nil
} }
if !ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) { if !ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) {
mouseButtonState[ebiten.MouseButtonLeft] = 0 mouseButtonState[ebiten.MouseButtonLeft] = 0
return nil return nil
} }
mouseButtonState[ebiten.MouseButtonLeft]&#43;&#43; mouseButtonState[ebiten.MouseButtonLeft]&#43;&#43;
if mouseButtonState[ebiten.MouseButtonLeft] != 1 { if mouseButtonState[ebiten.MouseButtonLeft] != 1 {
return nil return nil
} }
x, y := ebiten.CursorPosition() x, y := ebiten.CursorPosition()
bx, by, bw, bh := playerBarRect() bx, by, bw, bh := playerBarRect()
const padding = 4 const padding = 4
if y &lt; by-padding || by&#43;bh&#43;padding &lt;= y { if y &lt; by-padding || by&#43;bh&#43;padding &lt;= y {
return nil return nil
} }
if x &lt; bx || bx&#43;bw &lt;= x { if x &lt; bx || bx&#43;bw &lt;= x {
return nil return nil
} }
pos := time.Duration(x-bx) * p.total / time.Duration(bw) pos := time.Duration(x-bx) * p.total / time.Duration(bw)
return p.audioPlayer.Seek(pos) return p.audioPlayer.Seek(pos)
} }
func (p *Player) close() error { func (p *Player) close() error {
return p.audioPlayer.Close() return p.audioPlayer.Close()
} }
func update(screen *ebiten.Image) error { func update(screen *ebiten.Image) error {
if musicPlayer == nil { if musicPlayer == nil {
select { select {
case musicPlayer = &lt;-musicCh: case musicPlayer = &lt;-musicCh:
default: default:
}
} }
} if seBytes == nil {
if seBytes == nil { select {
select { case seBytes = &lt;-seCh:
case seBytes = &lt;-seCh: default:
default: }
} }
} if musicPlayer != nil {
if musicPlayer != nil { if err := musicPlayer.updateBar(); err != nil {
if err := musicPlayer.updateBar(); err != nil { return err
return err }
if err := musicPlayer.updatePlayPause(); err != nil {
return err
}
if err := musicPlayer.updateSE(); err != nil {
return err
}
if err := musicPlayer.updateVolume(); err != nil {
return err
}
} }
if err := musicPlayer.updatePlayPause(); err != nil {
return err
}
if err := musicPlayer.updateSE(); err != nil {
return err
}
if err := musicPlayer.updateVolume(); err != nil {
return err
}
}
op := &amp;ebiten.DrawImageOptions{}
x, y, w, h := playerBarRect()
op.GeoM.Translate(float64(x), float64(y))
if err := screen.DrawImage(playerBarImage, op); err != nil {
return err
}
currentTimeStr := &#34;00:00&#34;
if musicPlayer != nil {
c := musicPlayer.audioPlayer.Current()
// Current Time
m := (c / time.Minute) % 100
s := (c / time.Second) % 60
currentTimeStr = fmt.Sprintf(&#34;%02d:%02d&#34;, m, s)
// Bar
cw, ch := playerCurrentImage.Size()
cx := int(time.Duration(w)*c/musicPlayer.total) &#43; x - cw/2
cy := y - (ch-h)/2
op := &amp;ebiten.DrawImageOptions{} op := &amp;ebiten.DrawImageOptions{}
op.GeoM.Translate(float64(cx), float64(cy)) x, y, w, h := playerBarRect()
if err := screen.DrawImage(playerCurrentImage, op); err != nil { op.GeoM.Translate(float64(x), float64(y))
return err if err := screen.DrawImage(playerBarImage, op); err != nil {
return err
} }
} currentTimeStr := &#34;00:00&#34;
if musicPlayer != nil {
c := musicPlayer.audioPlayer.Current()
msg := fmt.Sprintf(`FPS: %0.2f // Current Time
m := (c / time.Minute) % 100
s := (c / time.Second) % 60
currentTimeStr = fmt.Sprintf(&#34;%02d:%02d&#34;, m, s)
// Bar
cw, ch := playerCurrentImage.Size()
cx := int(time.Duration(w)*c/musicPlayer.total) &#43; x - cw/2
cy := y - (ch-h)/2
op := &amp;ebiten.DrawImageOptions{}
op.GeoM.Translate(float64(cx), float64(cy))
if err := screen.DrawImage(playerCurrentImage, op); err != nil {
return err
}
}
msg := fmt.Sprintf(`FPS: %0.2f
Press S to toggle Play/Pause Press S to toggle Play/Pause
Press P to play SE Press P to play SE
Press Z or X to change volume of the music Press Z or X to change volume of the music
%s`, ebiten.CurrentFPS(), currentTimeStr) %s`, ebiten.CurrentFPS(), currentTimeStr)
if musicPlayer == nil { if musicPlayer == nil {
msg &#43;= &#34;\nNow Loading...&#34; msg &#43;= &#34;\nNow Loading...&#34;
} }
if err := ebitenutil.DebugPrint(screen, msg); err != nil { if err := ebitenutil.DebugPrint(screen, msg); err != nil {
return err return err
} }
if err := audioContext.Update(); err != nil { if err := audioContext.Update(); err != nil {
return err return err
} }
return nil return nil
} }
func main() { func main() {
wavF, err := ebitenutil.OpenFile(&#34;_resources/audio/jab.wav&#34;) wavF, err := ebitenutil.OpenFile(&#34;_resources/audio/jab.wav&#34;)
if err != nil {
log.Fatal(err)
}
oggF, err := ebitenutil.OpenFile(&#34;_resources/audio/ragtime.ogg&#34;)
if err != nil {
log.Fatal(err)
}
const sampleRate = 22050
const bytesPerSample = 4 // TODO: This should be defined in audio package
audioContext, err = audio.NewContext(sampleRate)
if err != nil {
log.Fatal(err)
}
go func() {
s, err := wav.Decode(audioContext, wavF)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
return
} }
b, err := ioutil.ReadAll(s) oggF, err := ebitenutil.OpenFile(&#34;_resources/audio/ragtime.ogg&#34;)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
return
} }
seCh &lt;- b const sampleRate = 22050
close(seCh) const bytesPerSample = 4 // TODO: This should be defined in audio package
}() audioContext, err = audio.NewContext(sampleRate)
go func() {
s, err := vorbis.Decode(audioContext, oggF)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
return
} }
p, err := audio.NewPlayer(audioContext, s) go func() {
if err != nil { s, err := wav.Decode(audioContext, wavF)
log.Fatal(err) if err != nil {
return log.Fatal(err)
return
}
b, err := ioutil.ReadAll(s)
if err != nil {
log.Fatal(err)
return
}
seCh &lt;- b
close(seCh)
}()
go func() {
s, err := vorbis.Decode(audioContext, oggF)
if err != nil {
log.Fatal(err)
return
}
p, err := audio.NewPlayer(audioContext, s)
if err != nil {
log.Fatal(err)
return
}
musicCh &lt;- &amp;Player{
audioPlayer: p,
total: time.Second * time.Duration(s.Size()) / bytesPerSample / sampleRate,
}
close(musicCh)
// TODO: Is this goroutine-safe?
if err := p.Play(); err != nil {
log.Fatal(err)
return
}
}()
if err := ebiten.Run(update, screenWidth, screenHeight, 2, &#34;Audio (Ebiten Demo)&#34;); err != nil {
log.Fatal(err)
} }
musicCh &lt;- &amp;Player{ if musicPlayer != nil {
audioPlayer: p, if err := musicPlayer.close(); err != nil {
total: time.Second * time.Duration(s.Size()) / bytesPerSample / sampleRate, log.Fatal(err)
}
} }
close(musicCh)
// TODO: Is this goroutine-safe?
if err := p.Play(); err != nil {
log.Fatal(err)
return
}
}()
if err := ebiten.Run(update, screenWidth, screenHeight, 2, &#34;Audio (Ebiten Demo)&#34;); err != nil {
log.Fatal(err)
}
if musicPlayer != nil {
if err := musicPlayer.close(); err != nil {
log.Fatal(err)
}
}
} }
</code></pre> </code></pre>

View File

@ -27,95 +27,95 @@
package main package main
import ( import (
&#34;image&#34; &#34;image&#34;
&#34;io/ioutil&#34; &#34;io/ioutil&#34;
&#34;log&#34; &#34;log&#34;
&#34;github.com/golang/freetype/truetype&#34; &#34;github.com/golang/freetype/truetype&#34;
&#34;github.com/hajimehoshi/ebiten&#34; &#34;github.com/hajimehoshi/ebiten&#34;
&#34;github.com/hajimehoshi/ebiten/ebitenutil&#34; &#34;github.com/hajimehoshi/ebiten/ebitenutil&#34;
&#34;golang.org/x/image/font&#34; &#34;golang.org/x/image/font&#34;
&#34;golang.org/x/image/math/fixed&#34; &#34;golang.org/x/image/math/fixed&#34;
) )
const ( const (
screenWidth = 640 screenWidth = 640
screenHeight = 480 screenHeight = 480
) )
var ( var (
textImage *ebiten.Image textImage *ebiten.Image
) )
var text = []string{ var text = []string{
&#34;The quick brown fox jumps over the lazy dog.&#34;, &#34;The quick brown fox jumps over the lazy dog.&#34;,
&#34;&#34;, &#34;&#34;,
// A head part of a Japanese novel 山月記 (Sangetsuki) // A head part of a Japanese novel 山月記 (Sangetsuki)
// See http://www.aozora.gr.jp/cards/000119/files/624_14544.html. // See http://www.aozora.gr.jp/cards/000119/files/624_14544.html.
&#34;隴西の李徴は博学才穎、天宝の末年、&#34;, &#34;隴西の李徴は博学才穎、天宝の末年、&#34;,
&#34;若くして名を虎榜に連ね、&#34;, &#34;若くして名を虎榜に連ね、&#34;,
&#34;ついで江南尉に補せられたが、&#34;, &#34;ついで江南尉に補せられたが、&#34;,
&#34;性、狷介、自ら恃むところ頗厚く、&#34;, &#34;性、狷介、自ら恃むところ頗厚く、&#34;,
&#34;賤吏に甘んずるを潔しとしなかった。&#34;, &#34;賤吏に甘んずるを潔しとしなかった。&#34;,
} }
func parseFont() error { func parseFont() error {
f, err := ebitenutil.OpenFile(&#34;_resources/fonts/mplus-1p-regular.ttf&#34;) f, err := ebitenutil.OpenFile(&#34;_resources/fonts/mplus-1p-regular.ttf&#34;)
if err != nil { if err != nil {
return err return err
} }
defer func() { defer func() {
_ = f.Close() _ = f.Close()
}() }()
b, err := ioutil.ReadAll(f) b, err := ioutil.ReadAll(f)
if err != nil { if err != nil {
return err return err
} }
tt, err := truetype.Parse(b) tt, err := truetype.Parse(b)
if err != nil { if err != nil {
return err return err
} }
w, h := textImage.Size() w, h := textImage.Size()
dst := image.NewRGBA(image.Rect(0, 0, w, h)) dst := image.NewRGBA(image.Rect(0, 0, w, h))
const size = 24 const size = 24
const dpi = 72 const dpi = 72
d := &amp;font.Drawer{ d := &amp;font.Drawer{
Dst: dst, Dst: dst,
Src: image.White, Src: image.White,
Face: truetype.NewFace(tt, &amp;truetype.Options{ Face: truetype.NewFace(tt, &amp;truetype.Options{
Size: size, Size: size,
DPI: dpi, DPI: dpi,
Hinting: font.HintingFull, Hinting: font.HintingFull,
}), }),
} }
y := size y := size
for _, s := range text { for _, s := range text {
d.Dot = fixed.P(0, y) d.Dot = fixed.P(0, y)
d.DrawString(s) d.DrawString(s)
y &#43;= size y &#43;= size
} }
return textImage.ReplacePixels(dst.Pix) return textImage.ReplacePixels(dst.Pix)
} }
func update(screen *ebiten.Image) error { func update(screen *ebiten.Image) error {
if err := screen.DrawImage(textImage, &amp;ebiten.DrawImageOptions{}); err != nil { if err := screen.DrawImage(textImage, &amp;ebiten.DrawImageOptions{}); err != nil {
return err return err
} }
return nil return nil
} }
func main() { func main() {
var err error var err error
textImage, err = ebiten.NewImage(screenWidth, screenHeight, ebiten.FilterNearest) textImage, err = ebiten.NewImage(screenWidth, screenHeight, ebiten.FilterNearest)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
if err := parseFont(); err != nil { if err := parseFont(); err != nil {
log.Fatal(err) log.Fatal(err)
} }
if err := ebiten.Run(update, screenWidth, screenHeight, 1, &#34;Font (Ebiten Demo)&#34;); err != nil { if err := ebiten.Run(update, screenWidth, screenHeight, 1, &#34;Font (Ebiten Demo)&#34;); err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }
</code></pre> </code></pre>

View File

@ -27,55 +27,55 @@
package main package main
import ( import (
&#34;fmt&#34; &#34;fmt&#34;
&#34;log&#34; &#34;log&#34;
&#34;strconv&#34; &#34;strconv&#34;
&#34;strings&#34; &#34;strings&#34;
&#34;github.com/hajimehoshi/ebiten&#34; &#34;github.com/hajimehoshi/ebiten&#34;
&#34;github.com/hajimehoshi/ebiten/ebitenutil&#34; &#34;github.com/hajimehoshi/ebiten/ebitenutil&#34;
) )
const ( const (
screenWidth = 320 screenWidth = 320
screenHeight = 240 screenHeight = 240
) )
func update(screen *ebiten.Image) error { func update(screen *ebiten.Image) error {
// TODO: API to get the available, lowest ID // TODO: API to get the available, lowest ID
const gamepadID = 0 const gamepadID = 0
axes := []string{} axes := []string{}
pressedButtons := []string{} pressedButtons := []string{}
maxAxis := ebiten.GamepadAxisNum(gamepadID) maxAxis := ebiten.GamepadAxisNum(gamepadID)
for a := 0; a &lt; maxAxis; a&#43;&#43; { for a := 0; a &lt; maxAxis; a&#43;&#43; {
v := ebiten.GamepadAxis(gamepadID, a) v := ebiten.GamepadAxis(gamepadID, a)
axes = append(axes, fmt.Sprintf(&#34;%d: %0.6f&#34;, a, v)) axes = append(axes, fmt.Sprintf(&#34;%d: %0.6f&#34;, a, v))
}
maxButton := ebiten.GamepadButton(ebiten.GamepadButtonNum(gamepadID))
for b := ebiten.GamepadButton(gamepadID); b &lt; maxButton; b&#43;&#43; {
if ebiten.IsGamepadButtonPressed(gamepadID, b) {
pressedButtons = append(pressedButtons, strconv.Itoa(int(b)))
} }
}
str := `Gamepad maxButton := ebiten.GamepadButton(ebiten.GamepadButtonNum(gamepadID))
for b := ebiten.GamepadButton(gamepadID); b &lt; maxButton; b&#43;&#43; {
if ebiten.IsGamepadButtonPressed(gamepadID, b) {
pressedButtons = append(pressedButtons, strconv.Itoa(int(b)))
}
}
str := `Gamepad
Axes: Axes:
{{.Axes}} {{.Axes}}
Pressed Buttons: {{.Buttons}}` Pressed Buttons: {{.Buttons}}`
str = strings.Replace(str, &#34;{{.Axes}}&#34;, strings.Join(axes, &#34;\n &#34;), -1) str = strings.Replace(str, &#34;{{.Axes}}&#34;, strings.Join(axes, &#34;\n &#34;), -1)
str = strings.Replace(str, &#34;{{.Buttons}}&#34;, strings.Join(pressedButtons, &#34;, &#34;), -1) str = strings.Replace(str, &#34;{{.Buttons}}&#34;, strings.Join(pressedButtons, &#34;, &#34;), -1)
if err := ebitenutil.DebugPrint(screen, str); err != nil { if err := ebitenutil.DebugPrint(screen, str); err != nil {
return err return err
} }
return nil return nil
} }
func main() { func main() {
if err := ebiten.Run(update, screenWidth, screenHeight, 2, &#34;Gamepad (Ebiten Demo)&#34;); err != nil { if err := ebiten.Run(update, screenWidth, screenHeight, 2, &#34;Gamepad (Ebiten Demo)&#34;); err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }
</code></pre> </code></pre>

View File

@ -27,92 +27,92 @@
package main package main
import ( import (
&#34;fmt&#34; &#34;fmt&#34;
_ &#34;image/jpeg&#34; _ &#34;image/jpeg&#34;
&#34;log&#34; &#34;log&#34;
&#34;math&#34; &#34;math&#34;
&#34;github.com/hajimehoshi/ebiten&#34; &#34;github.com/hajimehoshi/ebiten&#34;
&#34;github.com/hajimehoshi/ebiten/ebitenutil&#34; &#34;github.com/hajimehoshi/ebiten/ebitenutil&#34;
) )
const ( const (
screenWidth = 320 screenWidth = 320
screenHeight = 240 screenHeight = 240
) )
var ( var (
hueInt = 0 hueInt = 0
saturationInt = 128 saturationInt = 128
valueInt = 128 valueInt = 128
gophersImage *ebiten.Image gophersImage *ebiten.Image
) )
func clamp(v, min, max int) int { func clamp(v, min, max int) int {
if min &gt; max { if min &gt; max {
panic(&#34;min must &lt;= max&#34;) panic(&#34;min must &lt;= max&#34;)
} }
if v &lt; min { if v &lt; min {
return min return min
} }
if max &lt; v { if max &lt; v {
return max return max
} }
return v return v
} }
func update(screen *ebiten.Image) error { func update(screen *ebiten.Image) error {
if ebiten.IsKeyPressed(ebiten.KeyQ) { if ebiten.IsKeyPressed(ebiten.KeyQ) {
hueInt-- hueInt--
} }
if ebiten.IsKeyPressed(ebiten.KeyW) { if ebiten.IsKeyPressed(ebiten.KeyW) {
hueInt&#43;&#43; hueInt&#43;&#43;
} }
if ebiten.IsKeyPressed(ebiten.KeyA) { if ebiten.IsKeyPressed(ebiten.KeyA) {
saturationInt-- saturationInt--
} }
if ebiten.IsKeyPressed(ebiten.KeyS) { if ebiten.IsKeyPressed(ebiten.KeyS) {
saturationInt&#43;&#43; saturationInt&#43;&#43;
} }
if ebiten.IsKeyPressed(ebiten.KeyZ) { if ebiten.IsKeyPressed(ebiten.KeyZ) {
valueInt-- valueInt--
} }
if ebiten.IsKeyPressed(ebiten.KeyX) { if ebiten.IsKeyPressed(ebiten.KeyX) {
valueInt&#43;&#43; valueInt&#43;&#43;
} }
hueInt = clamp(hueInt, -256, 256) hueInt = clamp(hueInt, -256, 256)
saturationInt = clamp(saturationInt, 0, 256) saturationInt = clamp(saturationInt, 0, 256)
valueInt = clamp(valueInt, 0, 256) valueInt = clamp(valueInt, 0, 256)
w, h := gophersImage.Size() w, h := gophersImage.Size()
op := &amp;ebiten.DrawImageOptions{} op := &amp;ebiten.DrawImageOptions{}
op.GeoM.Translate(float64(screenWidth-w)/2, float64(screenHeight-h)/2) op.GeoM.Translate(float64(screenWidth-w)/2, float64(screenHeight-h)/2)
hue := float64(hueInt) * 2 * math.Pi / 128 hue := float64(hueInt) * 2 * math.Pi / 128
saturation := float64(saturationInt) / 128 saturation := float64(saturationInt) / 128
value := float64(valueInt) / 128 value := float64(valueInt) / 128
op.ColorM.ChangeHSV(hue, saturation, value) op.ColorM.ChangeHSV(hue, saturation, value)
if err := screen.DrawImage(gophersImage, op); err != nil { if err := screen.DrawImage(gophersImage, op); err != nil {
return err return err
} }
msg := fmt.Sprintf(`Hue: %0.2f [Q][W] msg := fmt.Sprintf(`Hue: %0.2f [Q][W]
Saturation: %0.2f [A][S] Saturation: %0.2f [A][S]
Value: %0.2f [Z][X]`, hue, saturation, value) Value: %0.2f [Z][X]`, hue, saturation, value)
if err := ebitenutil.DebugPrint(screen, msg); err != nil { if err := ebitenutil.DebugPrint(screen, msg); err != nil {
return err return err
} }
return nil return nil
} }
func main() { func main() {
var err error var err error
gophersImage, _, err = ebitenutil.NewImageFromFile(&#34;_resources/images/gophers.jpg&#34;, ebiten.FilterNearest) gophersImage, _, err = ebitenutil.NewImageFromFile(&#34;_resources/images/gophers.jpg&#34;, ebiten.FilterNearest)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
if err := ebiten.Run(update, screenWidth, screenHeight, 2, &#34;HSV (Ebiten Demo)&#34;); err != nil { if err := ebiten.Run(update, screenWidth, screenHeight, 2, &#34;HSV (Ebiten Demo)&#34;); err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }
</code></pre> </code></pre>

View File

@ -27,45 +27,45 @@
package main package main
import ( import (
_ &#34;image/jpeg&#34; _ &#34;image/jpeg&#34;
&#34;log&#34; &#34;log&#34;
&#34;math&#34; &#34;math&#34;
&#34;github.com/hajimehoshi/ebiten&#34; &#34;github.com/hajimehoshi/ebiten&#34;
&#34;github.com/hajimehoshi/ebiten/ebitenutil&#34; &#34;github.com/hajimehoshi/ebiten/ebitenutil&#34;
) )
const ( const (
screenWidth = 320 screenWidth = 320
screenHeight = 240 screenHeight = 240
) )
var ( var (
count int count int
gophersImage *ebiten.Image gophersImage *ebiten.Image
) )
func update(screen *ebiten.Image) error { func update(screen *ebiten.Image) error {
count&#43;&#43; count&#43;&#43;
w, h := gophersImage.Size() w, h := gophersImage.Size()
op := &amp;ebiten.DrawImageOptions{} op := &amp;ebiten.DrawImageOptions{}
op.GeoM.Translate(float64(screenWidth-w)/2, float64(screenHeight-h)/2) op.GeoM.Translate(float64(screenWidth-w)/2, float64(screenHeight-h)/2)
op.ColorM.RotateHue(float64(count%360) * 2 * math.Pi / 360) op.ColorM.RotateHue(float64(count%360) * 2 * math.Pi / 360)
if err := screen.DrawImage(gophersImage, op); err != nil { if err := screen.DrawImage(gophersImage, op); err != nil {
return err return err
} }
return nil return nil
} }
func main() { func main() {
var err error var err error
gophersImage, _, err = ebitenutil.NewImageFromFile(&#34;_resources/images/gophers.jpg&#34;, ebiten.FilterNearest) gophersImage, _, err = ebitenutil.NewImageFromFile(&#34;_resources/images/gophers.jpg&#34;, ebiten.FilterNearest)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
if err := ebiten.Run(update, screenWidth, screenHeight, 2, &#34;Hue (Ebiten Demo)&#34;); err != nil { if err := ebiten.Run(update, screenWidth, screenHeight, 2, &#34;Hue (Ebiten Demo)&#34;); err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }
</code></pre> </code></pre>

View File

@ -27,116 +27,116 @@
package main package main
import ( import (
&#34;log&#34; &#34;log&#34;
&#34;strconv&#34; &#34;strconv&#34;
&#34;github.com/hajimehoshi/ebiten&#34; &#34;github.com/hajimehoshi/ebiten&#34;
&#34;github.com/hajimehoshi/ebiten/ebitenutil&#34; &#34;github.com/hajimehoshi/ebiten/ebitenutil&#34;
&#34;github.com/hajimehoshi/ebiten/examples/keyboard/keyboard&#34; &#34;github.com/hajimehoshi/ebiten/examples/keyboard/keyboard&#34;
) )
const ( const (
screenWidth = 320 screenWidth = 320
screenHeight = 240 screenHeight = 240
) )
var keyboardImage *ebiten.Image var keyboardImage *ebiten.Image
func init() { func init() {
var err error var err error
keyboardImage, _, err = ebitenutil.NewImageFromFile(&#34;_resources/images/keyboard/keyboard.png&#34;, ebiten.FilterNearest) keyboardImage, _, err = ebitenutil.NewImageFromFile(&#34;_resources/images/keyboard/keyboard.png&#34;, ebiten.FilterNearest)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }
var keyNames = map[ebiten.Key]string{ var keyNames = map[ebiten.Key]string{
ebiten.KeyBackspace: &#34;BS&#34;, ebiten.KeyBackspace: &#34;BS&#34;,
ebiten.KeyComma: &#34;,&#34;, ebiten.KeyComma: &#34;,&#34;,
ebiten.KeyDelete: &#34;Del&#34;, ebiten.KeyDelete: &#34;Del&#34;,
ebiten.KeyEnter: &#34;Enter&#34;, ebiten.KeyEnter: &#34;Enter&#34;,
ebiten.KeyEscape: &#34;Esc&#34;, ebiten.KeyEscape: &#34;Esc&#34;,
ebiten.KeyPeriod: &#34;.&#34;, ebiten.KeyPeriod: &#34;.&#34;,
ebiten.KeySpace: &#34;Space&#34;, ebiten.KeySpace: &#34;Space&#34;,
ebiten.KeyTab: &#34;Tab&#34;, ebiten.KeyTab: &#34;Tab&#34;,
// Arrows // Arrows
ebiten.KeyDown: &#34;Down&#34;, ebiten.KeyDown: &#34;Down&#34;,
ebiten.KeyLeft: &#34;Left&#34;, ebiten.KeyLeft: &#34;Left&#34;,
ebiten.KeyRight: &#34;Right&#34;, ebiten.KeyRight: &#34;Right&#34;,
ebiten.KeyUp: &#34;Up&#34;, ebiten.KeyUp: &#34;Up&#34;,
// Mods // Mods
ebiten.KeyShift: &#34;Shift&#34;, ebiten.KeyShift: &#34;Shift&#34;,
ebiten.KeyControl: &#34;Ctrl&#34;, ebiten.KeyControl: &#34;Ctrl&#34;,
ebiten.KeyAlt: &#34;Alt&#34;, ebiten.KeyAlt: &#34;Alt&#34;,
} }
type pressedKeysParts []string type pressedKeysParts []string
func (p pressedKeysParts) Len() int { func (p pressedKeysParts) Len() int {
return len(p) return len(p)
} }
func (p pressedKeysParts) Dst(i int) (x0, y0, x1, y1 int) { func (p pressedKeysParts) Dst(i int) (x0, y0, x1, y1 int) {
k := p[i] k := p[i]
r, ok := keyboard.KeyRect(k) r, ok := keyboard.KeyRect(k)
if !ok { if !ok {
return 0, 0, 0, 0 return 0, 0, 0, 0
} }
return r.Min.X, r.Min.Y, r.Max.X, r.Max.Y return r.Min.X, r.Min.Y, r.Max.X, r.Max.Y
} }
func (p pressedKeysParts) Src(i int) (x0, y0, x1, y1 int) { func (p pressedKeysParts) Src(i int) (x0, y0, x1, y1 int) {
return p.Dst(i) return p.Dst(i)
} }
func update(screen *ebiten.Image) error { func update(screen *ebiten.Image) error {
const offsetX, offsetY = 24, 40 const offsetX, offsetY = 24, 40
op := &amp;ebiten.DrawImageOptions{} op := &amp;ebiten.DrawImageOptions{}
op.GeoM.Translate(offsetX, offsetY) op.GeoM.Translate(offsetX, offsetY)
op.ColorM.Scale(0.5, 0.5, 0.5, 1) op.ColorM.Scale(0.5, 0.5, 0.5, 1)
if err := screen.DrawImage(keyboardImage, op); err != nil { if err := screen.DrawImage(keyboardImage, op); err != nil {
return err return err
} }
pressed := []string{} pressed := []string{}
for i := 0; i &lt;= 9; i&#43;&#43; { for i := 0; i &lt;= 9; i&#43;&#43; {
if ebiten.IsKeyPressed(ebiten.Key(i) &#43; ebiten.Key0) { if ebiten.IsKeyPressed(ebiten.Key(i) &#43; ebiten.Key0) {
pressed = append(pressed, string(i&#43;&#39;0&#39;)) pressed = append(pressed, string(i&#43;&#39;0&#39;))
}
} }
} for c := &#39;A&#39;; c &lt;= &#39;Z&#39;; c&#43;&#43; {
for c := &#39;A&#39;; c &lt;= &#39;Z&#39;; c&#43;&#43; { if ebiten.IsKeyPressed(ebiten.Key(c) - &#39;A&#39; &#43; ebiten.KeyA) {
if ebiten.IsKeyPressed(ebiten.Key(c) - &#39;A&#39; &#43; ebiten.KeyA) { pressed = append(pressed, string(c))
pressed = append(pressed, string(c)) }
} }
} for i := 1; i &lt;= 12; i&#43;&#43; {
for i := 1; i &lt;= 12; i&#43;&#43; { if ebiten.IsKeyPressed(ebiten.Key(i) &#43; ebiten.KeyF1 - 1) {
if ebiten.IsKeyPressed(ebiten.Key(i) &#43; ebiten.KeyF1 - 1) { pressed = append(pressed, &#34;F&#34;&#43;strconv.Itoa(i))
pressed = append(pressed, &#34;F&#34;&#43;strconv.Itoa(i)) }
} }
} for key, name := range keyNames {
for key, name := range keyNames { if ebiten.IsKeyPressed(key) {
if ebiten.IsKeyPressed(key) { pressed = append(pressed, name)
pressed = append(pressed, name) }
} }
}
op = &amp;ebiten.DrawImageOptions{ op = &amp;ebiten.DrawImageOptions{
ImageParts: pressedKeysParts(pressed), ImageParts: pressedKeysParts(pressed),
} }
op.GeoM.Translate(offsetX, offsetY) op.GeoM.Translate(offsetX, offsetY)
if err := screen.DrawImage(keyboardImage, op); err != nil { if err := screen.DrawImage(keyboardImage, op); err != nil {
return err return err
} }
return nil return nil
} }
func main() { func main() {
if err := ebiten.Run(update, screenWidth, screenHeight, 2, &#34;Keyboard (Ebiten Demo)&#34;); err != nil { if err := ebiten.Run(update, screenWidth, screenHeight, 2, &#34;Keyboard (Ebiten Demo)&#34;); err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }
</code></pre> </code></pre>

View File

@ -27,129 +27,129 @@
package main package main
import ( import (
&#34;image&#34; &#34;image&#34;
&#34;image/color&#34; &#34;image/color&#34;
_ &#34;image/jpeg&#34; _ &#34;image/jpeg&#34;
&#34;log&#34; &#34;log&#34;
&#34;math&#34; &#34;math&#34;
&#34;github.com/hajimehoshi/ebiten&#34; &#34;github.com/hajimehoshi/ebiten&#34;
&#34;github.com/hajimehoshi/ebiten/ebitenutil&#34; &#34;github.com/hajimehoshi/ebiten/ebitenutil&#34;
) )
const ( const (
screenWidth = 320 screenWidth = 320
screenHeight = 240 screenHeight = 240
) )
var ( var (
gophersImage *ebiten.Image gophersImage *ebiten.Image
fiveyearsImage *ebiten.Image fiveyearsImage *ebiten.Image
maskImage *ebiten.Image maskImage *ebiten.Image
spotLightImage *ebiten.Image spotLightImage *ebiten.Image
spotLightX = 0 spotLightX = 0
spotLightY = 0 spotLightY = 0
spotLightVX = 1 spotLightVX = 1
spotLightVY = 1 spotLightVY = 1
) )
func update(screen *ebiten.Image) error { func update(screen *ebiten.Image) error {
spotLightX &#43;= spotLightVX spotLightX &#43;= spotLightVX
spotLightY &#43;= spotLightVY spotLightY &#43;= spotLightVY
if spotLightX &lt; 0 { if spotLightX &lt; 0 {
spotLightX = -spotLightX spotLightX = -spotLightX
spotLightVX = -spotLightVX spotLightVX = -spotLightVX
} }
if spotLightY &lt; 0 { if spotLightY &lt; 0 {
spotLightY = -spotLightY spotLightY = -spotLightY
spotLightVY = -spotLightVY spotLightVY = -spotLightVY
} }
w, h := spotLightImage.Size() w, h := spotLightImage.Size()
maxX, maxY := screenWidth-w, screenHeight-h maxX, maxY := screenWidth-w, screenHeight-h
if maxX &lt; spotLightX { if maxX &lt; spotLightX {
spotLightX = -spotLightX &#43; 2*maxX spotLightX = -spotLightX &#43; 2*maxX
spotLightVX = -spotLightVX spotLightVX = -spotLightVX
} }
if maxY &lt; spotLightY { if maxY &lt; spotLightY {
spotLightY = -spotLightY &#43; 2*maxY spotLightY = -spotLightY &#43; 2*maxY
spotLightVY = -spotLightVY spotLightVY = -spotLightVY
} }
if err := maskImage.Clear(); err != nil { if err := maskImage.Clear(); err != nil {
return err return err
} }
op := &amp;ebiten.DrawImageOptions{} op := &amp;ebiten.DrawImageOptions{}
op.GeoM.Translate(float64(spotLightX), float64(spotLightY)) op.GeoM.Translate(float64(spotLightX), float64(spotLightY))
if err := maskImage.DrawImage(spotLightImage, op); err != nil { if err := maskImage.DrawImage(spotLightImage, op); err != nil {
return err return err
} }
op = &amp;ebiten.DrawImageOptions{} op = &amp;ebiten.DrawImageOptions{}
op.CompositeMode = ebiten.CompositeModeSourceOut op.CompositeMode = ebiten.CompositeModeSourceOut
if err := maskImage.DrawImage(fiveyearsImage, op); err != nil { if err := maskImage.DrawImage(fiveyearsImage, op); err != nil {
return err return err
} }
if err := screen.Fill(color.RGBA{0x00, 0x00, 0x80, 0xff}); err != nil { if err := screen.Fill(color.RGBA{0x00, 0x00, 0x80, 0xff}); err != nil {
return err return err
} }
if err := screen.DrawImage(gophersImage, &amp;ebiten.DrawImageOptions{}); err != nil { if err := screen.DrawImage(gophersImage, &amp;ebiten.DrawImageOptions{}); err != nil {
return err return err
} }
if err := screen.DrawImage(maskImage, &amp;ebiten.DrawImageOptions{}); err != nil { if err := screen.DrawImage(maskImage, &amp;ebiten.DrawImageOptions{}); err != nil {
return err return err
} }
return nil return nil
} }
func max(a, b int) int { func max(a, b int) int {
if a &lt; b { if a &lt; b {
return b return b
} }
return a return a
} }
func min(a, b int) int { func min(a, b int) int {
if a &lt; b { if a &lt; b {
return a return a
} }
return b return b
} }
func main() { func main() {
var err error var err error
gophersImage, _, err = ebitenutil.NewImageFromFile(&#34;_resources/images/gophers.jpg&#34;, ebiten.FilterNearest) gophersImage, _, err = ebitenutil.NewImageFromFile(&#34;_resources/images/gophers.jpg&#34;, ebiten.FilterNearest)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
fiveyearsImage, _, err = ebitenutil.NewImageFromFile(&#34;_resources/images/fiveyears.jpg&#34;, ebiten.FilterNearest) fiveyearsImage, _, err = ebitenutil.NewImageFromFile(&#34;_resources/images/fiveyears.jpg&#34;, ebiten.FilterNearest)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
maskImage, err = ebiten.NewImage(screenWidth, screenHeight, ebiten.FilterNearest) maskImage, err = ebiten.NewImage(screenWidth, screenHeight, ebiten.FilterNearest)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
as := image.Point{128, 128} as := image.Point{128, 128}
a := image.NewAlpha(image.Rectangle{image.ZP, as}) a := image.NewAlpha(image.Rectangle{image.ZP, as})
for j := 0; j &lt; as.Y; j&#43;&#43; { for j := 0; j &lt; as.Y; j&#43;&#43; {
for i := 0; i &lt; as.X; i&#43;&#43; { for i := 0; i &lt; as.X; i&#43;&#43; {
r := as.X / 2 r := as.X / 2
d := math.Sqrt(float64((i-r)*(i-r) &#43; (j-r)*(j-r))) d := math.Sqrt(float64((i-r)*(i-r) &#43; (j-r)*(j-r)))
b := uint8(max(0, min(0xff, 3*0xff-int(d*3*0xff)/r))) b := uint8(max(0, min(0xff, 3*0xff-int(d*3*0xff)/r)))
a.SetAlpha(i, j, color.Alpha{b}) a.SetAlpha(i, j, color.Alpha{b})
}
}
spotLightImage, err = ebiten.NewImageFromImage(a, ebiten.FilterNearest)
if err != nil {
log.Fatal(err)
}
if err := ebiten.Run(update, screenWidth, screenHeight, 2, &#34;Masking (Ebiten Demo)&#34;); err != nil {
log.Fatal(err)
} }
}
spotLightImage, err = ebiten.NewImageFromImage(a, ebiten.FilterNearest)
if err != nil {
log.Fatal(err)
}
if err := ebiten.Run(update, screenWidth, screenHeight, 2, &#34;Masking (Ebiten Demo)&#34;); err != nil {
log.Fatal(err)
}
} }
</code></pre> </code></pre>

View File

@ -27,53 +27,53 @@
package main package main
import ( import (
_ &#34;image/jpeg&#34; _ &#34;image/jpeg&#34;
&#34;log&#34; &#34;log&#34;
&#34;github.com/hajimehoshi/ebiten&#34; &#34;github.com/hajimehoshi/ebiten&#34;
&#34;github.com/hajimehoshi/ebiten/ebitenutil&#34; &#34;github.com/hajimehoshi/ebiten/ebitenutil&#34;
) )
const ( const (
screenWidth = 320 screenWidth = 320
screenHeight = 240 screenHeight = 240
) )
const mosaicRatio = 16 const mosaicRatio = 16
var ( var (
gophersImage *ebiten.Image gophersImage *ebiten.Image
gophersRenderTarget *ebiten.Image gophersRenderTarget *ebiten.Image
) )
func update(screen *ebiten.Image) error { func update(screen *ebiten.Image) error {
op := &amp;ebiten.DrawImageOptions{} op := &amp;ebiten.DrawImageOptions{}
op.GeoM.Scale(1.0/mosaicRatio, 1.0/mosaicRatio) op.GeoM.Scale(1.0/mosaicRatio, 1.0/mosaicRatio)
if err := gophersRenderTarget.DrawImage(gophersImage, op); err != nil { if err := gophersRenderTarget.DrawImage(gophersImage, op); err != nil {
return err return err
} }
op = &amp;ebiten.DrawImageOptions{} op = &amp;ebiten.DrawImageOptions{}
op.GeoM.Scale(mosaicRatio, mosaicRatio) op.GeoM.Scale(mosaicRatio, mosaicRatio)
if err := screen.DrawImage(gophersRenderTarget, op); err != nil { if err := screen.DrawImage(gophersRenderTarget, op); err != nil {
return err return err
} }
return nil return nil
} }
func main() { func main() {
var err error var err error
gophersImage, _, err = ebitenutil.NewImageFromFile(&#34;_resources/images/gophers.jpg&#34;, ebiten.FilterNearest) gophersImage, _, err = ebitenutil.NewImageFromFile(&#34;_resources/images/gophers.jpg&#34;, ebiten.FilterNearest)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
w, h := gophersImage.Size() w, h := gophersImage.Size()
gophersRenderTarget, err = ebiten.NewImage(w/mosaicRatio, h/mosaicRatio, ebiten.FilterNearest) gophersRenderTarget, err = ebiten.NewImage(w/mosaicRatio, h/mosaicRatio, ebiten.FilterNearest)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
if err := ebiten.Run(update, screenWidth, screenHeight, 2, &#34;Mosaic (Ebiten Demo)&#34;); err != nil { if err := ebiten.Run(update, screenWidth, screenHeight, 2, &#34;Mosaic (Ebiten Demo)&#34;); err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }
</code></pre> </code></pre>

View File

@ -27,61 +27,61 @@
package main package main
import ( import (
&#34;fmt&#34; &#34;fmt&#34;
&#34;image&#34; &#34;image&#34;
&#34;log&#34; &#34;log&#34;
&#34;github.com/hajimehoshi/ebiten&#34; &#34;github.com/hajimehoshi/ebiten&#34;
&#34;github.com/hajimehoshi/ebiten/ebitenutil&#34; &#34;github.com/hajimehoshi/ebiten/ebitenutil&#34;
) )
const ( const (
screenWidth = 320 screenWidth = 320
screenHeight = 240 screenHeight = 240
) )
var ( var (
noiseImage *image.RGBA noiseImage *image.RGBA
) )
type rand struct { type rand struct {
x, y, z, w uint32 x, y, z, w uint32
} }
func (r *rand) next() uint32 { func (r *rand) next() uint32 {
// math/rand is too slow to keep 60 FPS on web browsers. // math/rand is too slow to keep 60 FPS on web browsers.
// Use Xorshift instead: http://en.wikipedia.org/wiki/Xorshift // Use Xorshift instead: http://en.wikipedia.org/wiki/Xorshift
t := r.x ^ (r.x &lt;&lt; 11) t := r.x ^ (r.x &lt;&lt; 11)
r.x, r.y, r.z = r.y, r.z, r.w r.x, r.y, r.z = r.y, r.z, r.w
r.w = (r.w ^ (r.w &gt;&gt; 19)) ^ (t ^ (t &gt;&gt; 8)) r.w = (r.w ^ (r.w &gt;&gt; 19)) ^ (t ^ (t &gt;&gt; 8))
return r.w return r.w
} }
var randInstance = &amp;rand{12345678, 4185243, 776511, 45411} var randInstance = &amp;rand{12345678, 4185243, 776511, 45411}
func update(screen *ebiten.Image) error { func update(screen *ebiten.Image) error {
const l = screenWidth * screenHeight const l = screenWidth * screenHeight
for i := 0; i &lt; l; i&#43;&#43; { for i := 0; i &lt; l; i&#43;&#43; {
x := randInstance.next() x := randInstance.next()
noiseImage.Pix[4*i] = uint8(x &gt;&gt; 24) noiseImage.Pix[4*i] = uint8(x &gt;&gt; 24)
noiseImage.Pix[4*i&#43;1] = uint8(x &gt;&gt; 16) noiseImage.Pix[4*i&#43;1] = uint8(x &gt;&gt; 16)
noiseImage.Pix[4*i&#43;2] = uint8(x &gt;&gt; 8) noiseImage.Pix[4*i&#43;2] = uint8(x &gt;&gt; 8)
noiseImage.Pix[4*i&#43;3] = 0xff noiseImage.Pix[4*i&#43;3] = 0xff
} }
if err := screen.ReplacePixels(noiseImage.Pix); err != nil { if err := screen.ReplacePixels(noiseImage.Pix); err != nil {
return err return err
} }
if err := ebitenutil.DebugPrint(screen, fmt.Sprintf(&#34;FPS: %f&#34;, ebiten.CurrentFPS())); err != nil { if err := ebitenutil.DebugPrint(screen, fmt.Sprintf(&#34;FPS: %f&#34;, ebiten.CurrentFPS())); err != nil {
return err return err
} }
return nil return nil
} }
func main() { func main() {
noiseImage = image.NewRGBA(image.Rect(0, 0, screenWidth, screenHeight)) noiseImage = image.NewRGBA(image.Rect(0, 0, screenWidth, screenHeight))
if err := ebiten.Run(update, screenWidth, screenHeight, 2, &#34;Noise (Ebiten Demo)&#34;); err != nil { if err := ebiten.Run(update, screenWidth, screenHeight, 2, &#34;Noise (Ebiten Demo)&#34;); err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }
</code></pre> </code></pre>

View File

@ -27,103 +27,103 @@
package main package main
import ( import (
&#34;fmt&#34; &#34;fmt&#34;
&#34;image&#34; &#34;image&#34;
&#34;image/color&#34; &#34;image/color&#34;
&#34;log&#34; &#34;log&#34;
&#34;math&#34; &#34;math&#34;
&#34;github.com/hajimehoshi/ebiten&#34; &#34;github.com/hajimehoshi/ebiten&#34;
&#34;github.com/hajimehoshi/ebiten/ebitenutil&#34; &#34;github.com/hajimehoshi/ebiten/ebitenutil&#34;
) )
const ( const (
screenWidth = 320 screenWidth = 320
screenHeight = 240 screenHeight = 240
) )
var ( var (
count int count int
brushImage *ebiten.Image brushImage *ebiten.Image
canvasImage *ebiten.Image canvasImage *ebiten.Image
) )
func paint(screen *ebiten.Image, x, y int) error { func paint(screen *ebiten.Image, x, y int) error {
op := &amp;ebiten.DrawImageOptions{} op := &amp;ebiten.DrawImageOptions{}
op.GeoM.Translate(float64(x), float64(y)) op.GeoM.Translate(float64(x), float64(y))
op.ColorM.Scale(1.0, 0.50, 0.125, 1.0) op.ColorM.Scale(1.0, 0.50, 0.125, 1.0)
theta := 2.0 * math.Pi * float64(count%60) / ebiten.FPS theta := 2.0 * math.Pi * float64(count%60) / ebiten.FPS
op.ColorM.RotateHue(theta) op.ColorM.RotateHue(theta)
if err := canvasImage.DrawImage(brushImage, op); err != nil { if err := canvasImage.DrawImage(brushImage, op); err != nil {
return err return err
} }
return nil return nil
} }
func update(screen *ebiten.Image) error { func update(screen *ebiten.Image) error {
drawn := false drawn := false
mx, my := ebiten.CursorPosition() mx, my := ebiten.CursorPosition()
if ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) { if ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) {
if err := paint(screen, mx, my); err != nil { if err := paint(screen, mx, my); err != nil {
return err return err
}
drawn = true
} }
drawn = true for _, t := range ebiten.Touches() {
} x, y := t.Position()
for _, t := range ebiten.Touches() { if err := paint(screen, x, y); err != nil {
x, y := t.Position() return err
if err := paint(screen, x, y); err != nil { }
return err drawn = true
}
if drawn {
count&#43;&#43;
} }
drawn = true
}
if drawn {
count&#43;&#43;
}
if err := screen.DrawImage(canvasImage, nil); err != nil { if err := screen.DrawImage(canvasImage, nil); err != nil {
return err return err
} }
msg := fmt.Sprintf(&#34;(%d, %d)&#34;, mx, my) msg := fmt.Sprintf(&#34;(%d, %d)&#34;, mx, my)
for _, t := range ebiten.Touches() { for _, t := range ebiten.Touches() {
x, y := t.Position() x, y := t.Position()
msg &#43;= fmt.Sprintf(&#34;\n(%d, %d) touch %d&#34;, x, y, t.ID()) msg &#43;= fmt.Sprintf(&#34;\n(%d, %d) touch %d&#34;, x, y, t.ID())
} }
if err := ebitenutil.DebugPrint(screen, msg); err != nil { if err := ebitenutil.DebugPrint(screen, msg); err != nil {
return err return err
} }
return nil return nil
} }
func main() { func main() {
var err error var err error
const a0, a1, a2 = 0x40, 0xc0, 0xff const a0, a1, a2 = 0x40, 0xc0, 0xff
pixels := []uint8{ pixels := []uint8{
a0, a1, a1, a0, a0, a1, a1, a0,
a1, a2, a2, a1, a1, a2, a2, a1,
a1, a2, a2, a1, a1, a2, a2, a1,
a0, a1, a1, a0, a0, a1, a1, a0,
} }
brushImage, err = ebiten.NewImageFromImage(&amp;image.Alpha{ brushImage, err = ebiten.NewImageFromImage(&amp;image.Alpha{
Pix: pixels, Pix: pixels,
Stride: 4, Stride: 4,
Rect: image.Rect(0, 0, 4, 4), Rect: image.Rect(0, 0, 4, 4),
}, ebiten.FilterNearest) }, ebiten.FilterNearest)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
canvasImage, err = ebiten.NewImage(screenWidth, screenHeight, ebiten.FilterNearest) canvasImage, err = ebiten.NewImage(screenWidth, screenHeight, ebiten.FilterNearest)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
if err := canvasImage.Fill(color.White); err != nil { if err := canvasImage.Fill(color.White); err != nil {
log.Fatal(err) log.Fatal(err)
} }
if err := ebiten.Run(update, screenWidth, screenHeight, 2, &#34;Paint (Ebiten Demo)&#34;); err != nil { if err := ebiten.Run(update, screenWidth, screenHeight, 2, &#34;Paint (Ebiten Demo)&#34;); err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }
</code></pre> </code></pre>

View File

@ -27,66 +27,66 @@
package main package main
import ( import (
_ &#34;image/jpeg&#34; _ &#34;image/jpeg&#34;
&#34;log&#34; &#34;log&#34;
&#34;github.com/hajimehoshi/ebiten&#34; &#34;github.com/hajimehoshi/ebiten&#34;
&#34;github.com/hajimehoshi/ebiten/ebitenutil&#34; &#34;github.com/hajimehoshi/ebiten/ebitenutil&#34;
) )
const ( const (
screenWidth = 320 screenWidth = 320
screenHeight = 240 screenHeight = 240
) )
var ( var (
gophersImage *ebiten.Image gophersImage *ebiten.Image
) )
type parts struct { type parts struct {
image *ebiten.Image image *ebiten.Image
} }
func (p parts) Len() int { func (p parts) Len() int {
_, h := p.image.Size() _, h := p.image.Size()
return h return h
} }
func (p parts) Dst(i int) (x0, y0, x1, y1 int) { func (p parts) Dst(i int) (x0, y0, x1, y1 int) {
w, h := p.image.Size() w, h := p.image.Size()
width := w &#43; i*3/4 width := w &#43; i*3/4
x := ((h - i) * 3 / 4) / 2 x := ((h - i) * 3 / 4) / 2
return x, i, x &#43; width, i &#43; 1 return x, i, x &#43; width, i &#43; 1
} }
func (p parts) Src(i int) (x0, y0, x1, y1 int) { func (p parts) Src(i int) (x0, y0, x1, y1 int) {
w, _ := p.image.Size() w, _ := p.image.Size()
return 0, i, w, i &#43; 1 return 0, i, w, i &#43; 1
} }
func update(screen *ebiten.Image) error { func update(screen *ebiten.Image) error {
op := &amp;ebiten.DrawImageOptions{ op := &amp;ebiten.DrawImageOptions{
ImageParts: &amp;parts{gophersImage}, ImageParts: &amp;parts{gophersImage},
} }
w, h := gophersImage.Size() w, h := gophersImage.Size()
maxWidth := float64(w) &#43; float64(h)*0.75 maxWidth := float64(w) &#43; float64(h)*0.75
op.GeoM.Translate(-maxWidth/2, -float64(h)/2) op.GeoM.Translate(-maxWidth/2, -float64(h)/2)
op.GeoM.Translate(screenWidth/2, screenHeight/2) op.GeoM.Translate(screenWidth/2, screenHeight/2)
if err := screen.DrawImage(gophersImage, op); err != nil { if err := screen.DrawImage(gophersImage, op); err != nil {
return err return err
} }
return nil return nil
} }
func main() { func main() {
var err error var err error
gophersImage, _, err = ebitenutil.NewImageFromFile(&#34;_resources/images/gophers.jpg&#34;, ebiten.FilterNearest) gophersImage, _, err = ebitenutil.NewImageFromFile(&#34;_resources/images/gophers.jpg&#34;, ebiten.FilterNearest)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
if err := ebiten.Run(update, screenWidth, screenHeight, 2, &#34;Perspective (Ebiten Demo)&#34;); err != nil { if err := ebiten.Run(update, screenWidth, screenHeight, 2, &#34;Perspective (Ebiten Demo)&#34;); err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }
</code></pre> </code></pre>

View File

@ -27,31 +27,31 @@
package main package main
import ( import (
&#34;fmt&#34; &#34;fmt&#34;
&#34;image/color&#34; &#34;image/color&#34;
&#34;log&#34; &#34;log&#34;
&#34;math&#34; &#34;math&#34;
&#34;github.com/hajimehoshi/ebiten&#34; &#34;github.com/hajimehoshi/ebiten&#34;
&#34;github.com/hajimehoshi/ebiten/audio&#34; &#34;github.com/hajimehoshi/ebiten/audio&#34;
&#34;github.com/hajimehoshi/ebiten/ebitenutil&#34; &#34;github.com/hajimehoshi/ebiten/ebitenutil&#34;
&#34;github.com/hajimehoshi/ebiten/examples/common&#34; &#34;github.com/hajimehoshi/ebiten/examples/common&#34;
) )
const ( const (
screenWidth = 320 screenWidth = 320
screenHeight = 240 screenHeight = 240
sampleRate = 44100 sampleRate = 44100
) )
var audioContext *audio.Context var audioContext *audio.Context
func init() { func init() {
var err error var err error
audioContext, err = audio.NewContext(sampleRate) audioContext, err = audio.NewContext(sampleRate)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }
var pcm = make([]float64, 4*sampleRate) var pcm = make([]float64, 4*sampleRate)
@ -59,203 +59,203 @@ var pcm = make([]float64, 4*sampleRate)
const baseFreq = 220 const baseFreq = 220
func init() { func init() {
s := float64(sampleRate) s := float64(sampleRate)
amp := []float64{1.0, 0.8, 0.6, 0.4, 0.2} amp := []float64{1.0, 0.8, 0.6, 0.4, 0.2}
x := []float64{4.0, 2.0, 1.0, 0.5, 0.25} x := []float64{4.0, 2.0, 1.0, 0.5, 0.25}
for i := 0; i &lt; len(pcm); i&#43;&#43; { for i := 0; i &lt; len(pcm); i&#43;&#43; {
v := 0.0 v := 0.0
twoPiF := 2.0 * math.Pi * baseFreq twoPiF := 2.0 * math.Pi * baseFreq
for j := 0; j &lt; len(amp); j&#43;&#43; { for j := 0; j &lt; len(amp); j&#43;&#43; {
a := amp[j] * math.Exp(-5*float64(i)/(x[j]*s)) a := amp[j] * math.Exp(-5*float64(i)/(x[j]*s))
v &#43;= a * math.Sin(float64(i)*twoPiF*float64(j&#43;1)/s) v &#43;= a * math.Sin(float64(i)*twoPiF*float64(j&#43;1)/s)
}
pcm[i] = v / 5.0
} }
pcm[i] = v / 5.0
}
} }
var ( var (
noteCache = map[int][]byte{} noteCache = map[int][]byte{}
) )
func toBytes(l, r []int16) []byte { func toBytes(l, r []int16) []byte {
if len(l) != len(r) { if len(l) != len(r) {
panic(&#34;len(l) must equal to len(r)&#34;) panic(&#34;len(l) must equal to len(r)&#34;)
} }
b := make([]byte, len(l)*4) b := make([]byte, len(l)*4)
for i := range l { for i := range l {
b[4*i] = byte(l[i]) b[4*i] = byte(l[i])
b[4*i&#43;1] = byte(l[i] &gt;&gt; 8) b[4*i&#43;1] = byte(l[i] &gt;&gt; 8)
b[4*i&#43;2] = byte(r[i]) b[4*i&#43;2] = byte(r[i])
b[4*i&#43;3] = byte(r[i] &gt;&gt; 8) b[4*i&#43;3] = byte(r[i] &gt;&gt; 8)
} }
return b return b
} }
func addNote(freq float64, vol float64) error { func addNote(freq float64, vol float64) error {
// TODO: Call Close method of *audio.Player. // TODO: Call Close method of *audio.Player.
// However, this works without Close because Close is automatically called when GC // However, this works without Close because Close is automatically called when GC
// collects a *audio.Player object. // collects a *audio.Player object.
f := int(freq) f := int(freq)
if n, ok := noteCache[f]; ok { if n, ok := noteCache[f]; ok {
p, err := audio.NewPlayerFromBytes(audioContext, n)
if err != nil {
return err
}
if err := p.Play(); err != nil {
return err
}
return nil
}
length := len(pcm) * baseFreq / f
l := make([]int16, length)
r := make([]int16, length)
j := 0
jj := 0
for i := 0; i &lt; len(l); i&#43;&#43; {
p := pcm[j]
l[i] = int16(p * vol * math.MaxInt16)
r[i] = l[i]
jj &#43;= f
j = jj / baseFreq
}
n := toBytes(l, r)
noteCache[f] = n
p, err := audio.NewPlayerFromBytes(audioContext, n) p, err := audio.NewPlayerFromBytes(audioContext, n)
if err != nil { if err != nil {
return err return err
} }
if err := p.Play(); err != nil { if err := p.Play(); err != nil {
return err return err
} }
return nil return nil
}
length := len(pcm) * baseFreq / f
l := make([]int16, length)
r := make([]int16, length)
j := 0
jj := 0
for i := 0; i &lt; len(l); i&#43;&#43; {
p := pcm[j]
l[i] = int16(p * vol * math.MaxInt16)
r[i] = l[i]
jj &#43;= f
j = jj / baseFreq
}
n := toBytes(l, r)
noteCache[f] = n
p, err := audio.NewPlayerFromBytes(audioContext, n)
if err != nil {
return err
}
if err := p.Play(); err != nil {
return err
}
return nil
} }
var keys = []ebiten.Key{ var keys = []ebiten.Key{
ebiten.KeyQ, ebiten.KeyQ,
ebiten.KeyA, ebiten.KeyA,
ebiten.KeyW, ebiten.KeyW,
ebiten.KeyS, ebiten.KeyS,
ebiten.KeyD, ebiten.KeyD,
ebiten.KeyR, ebiten.KeyR,
ebiten.KeyF, ebiten.KeyF,
ebiten.KeyT, ebiten.KeyT,
ebiten.KeyG, ebiten.KeyG,
ebiten.KeyH, ebiten.KeyH,
ebiten.KeyU, ebiten.KeyU,
ebiten.KeyJ, ebiten.KeyJ,
ebiten.KeyI, ebiten.KeyI,
ebiten.KeyK, ebiten.KeyK,
ebiten.KeyO, ebiten.KeyO,
ebiten.KeyL, ebiten.KeyL,
} }
var keyStates = map[ebiten.Key]int{} var keyStates = map[ebiten.Key]int{}
func init() { func init() {
for _, key := range keys { for _, key := range keys {
keyStates[key] = 0 keyStates[key] = 0
} }
} }
func updateInput() { func updateInput() {
for _, key := range keys { for _, key := range keys {
if !ebiten.IsKeyPressed(key) { if !ebiten.IsKeyPressed(key) {
keyStates[key] = 0 keyStates[key] = 0
continue continue
}
keyStates[key]&#43;&#43;
} }
keyStates[key]&#43;&#43;
}
} }
var ( var (
imagePiano *ebiten.Image imagePiano *ebiten.Image
) )
func init() { func init() {
var err error var err error
imageEmpty, err := ebiten.NewImage(16, 16, ebiten.FilterNearest) imageEmpty, err := ebiten.NewImage(16, 16, ebiten.FilterNearest)
if err != nil { if err != nil {
panic(err) panic(err)
}
if err := imageEmpty.Fill(color.White); err != nil {
panic(err)
}
imagePiano, err = ebiten.NewImage(screenWidth, screenHeight, ebiten.FilterNearest)
if err != nil {
panic(err)
}
whiteKeys := []string{&#34;A&#34;, &#34;S&#34;, &#34;D&#34;, &#34;F&#34;, &#34;G&#34;, &#34;H&#34;, &#34;J&#34;, &#34;K&#34;, &#34;L&#34;}
width := 24
y := 48
for i, k := range whiteKeys {
x := i*width &#43; 36
height := 112
op := &amp;ebiten.DrawImageOptions{}
w, h := imageEmpty.Size()
op.GeoM.Scale(float64(width-1)/float64(w), float64(height)/float64(h))
op.GeoM.Translate(float64(x), float64(y))
op.ColorM.Scale(1, 1, 1, 1)
if err := imagePiano.DrawImage(imageEmpty, op); err != nil {
panic(err)
} }
if err := common.ArcadeFont.DrawText(imagePiano, k, x&#43;8, y&#43;height-16, 1, color.Black); err != nil { if err := imageEmpty.Fill(color.White); err != nil {
panic(err) panic(err)
}
imagePiano, err = ebiten.NewImage(screenWidth, screenHeight, ebiten.FilterNearest)
if err != nil {
panic(err)
}
whiteKeys := []string{&#34;A&#34;, &#34;S&#34;, &#34;D&#34;, &#34;F&#34;, &#34;G&#34;, &#34;H&#34;, &#34;J&#34;, &#34;K&#34;, &#34;L&#34;}
width := 24
y := 48
for i, k := range whiteKeys {
x := i*width &#43; 36
height := 112
op := &amp;ebiten.DrawImageOptions{}
w, h := imageEmpty.Size()
op.GeoM.Scale(float64(width-1)/float64(w), float64(height)/float64(h))
op.GeoM.Translate(float64(x), float64(y))
op.ColorM.Scale(1, 1, 1, 1)
if err := imagePiano.DrawImage(imageEmpty, op); err != nil {
panic(err)
}
if err := common.ArcadeFont.DrawText(imagePiano, k, x&#43;8, y&#43;height-16, 1, color.Black); err != nil {
panic(err)
}
} }
}
blackKeys := []string{&#34;Q&#34;, &#34;W&#34;, &#34;&#34;, &#34;R&#34;, &#34;T&#34;, &#34;&#34;, &#34;U&#34;, &#34;I&#34;, &#34;O&#34;} blackKeys := []string{&#34;Q&#34;, &#34;W&#34;, &#34;&#34;, &#34;R&#34;, &#34;T&#34;, &#34;&#34;, &#34;U&#34;, &#34;I&#34;, &#34;O&#34;}
for i, k := range blackKeys { for i, k := range blackKeys {
if k == &#34;&#34; { if k == &#34;&#34; {
continue continue
}
x := i*width &#43; 24
height := 64
op := &amp;ebiten.DrawImageOptions{}
w, h := imageEmpty.Size()
op.GeoM.Scale(float64(width-1)/float64(w), float64(height)/float64(h))
op.GeoM.Translate(float64(x), float64(y))
op.ColorM.Scale(0, 0, 0, 1)
if err := imagePiano.DrawImage(imageEmpty, op); err != nil {
panic(err)
}
if err := common.ArcadeFont.DrawText(imagePiano, k, x&#43;8, y&#43;height-16, 1, color.White); err != nil {
panic(err)
}
} }
x := i*width &#43; 24
height := 64
op := &amp;ebiten.DrawImageOptions{}
w, h := imageEmpty.Size()
op.GeoM.Scale(float64(width-1)/float64(w), float64(height)/float64(h))
op.GeoM.Translate(float64(x), float64(y))
op.ColorM.Scale(0, 0, 0, 1)
if err := imagePiano.DrawImage(imageEmpty, op); err != nil {
panic(err)
}
if err := common.ArcadeFont.DrawText(imagePiano, k, x&#43;8, y&#43;height-16, 1, color.White); err != nil {
panic(err)
}
}
} }
func update(screen *ebiten.Image) error { func update(screen *ebiten.Image) error {
updateInput() updateInput()
for i, key := range keys { for i, key := range keys {
if keyStates[key] != 1 { if keyStates[key] != 1 {
continue continue
}
if err := addNote(220*math.Exp2(float64(i-1)/12.0), 1.0); err != nil {
return err
}
} }
if err := addNote(220*math.Exp2(float64(i-1)/12.0), 1.0); err != nil {
return err if err := screen.Fill(color.RGBA{0x80, 0x80, 0xc0, 0xff}); err != nil {
return err
}
if err := screen.DrawImage(imagePiano, nil); err != nil {
return err
} }
}
if err := screen.Fill(color.RGBA{0x80, 0x80, 0xc0, 0xff}); err != nil { if err := ebitenutil.DebugPrint(screen, fmt.Sprintf(&#34;FPS: %0.2f&#34;, ebiten.CurrentFPS())); err != nil {
return err return err
} }
if err := screen.DrawImage(imagePiano, nil); err != nil {
return err
}
if err := ebitenutil.DebugPrint(screen, fmt.Sprintf(&#34;FPS: %0.2f&#34;, ebiten.CurrentFPS())); err != nil { if err := audioContext.Update(); err != nil {
return err return err
} }
return nil
if err := audioContext.Update(); err != nil {
return err
}
return nil
} }
func main() { func main() {
if err := ebiten.Run(update, screenWidth, screenHeight, 2, &#34;Piano (Ebiten Demo)&#34;); err != nil { if err := ebiten.Run(update, screenWidth, screenHeight, 2, &#34;Piano (Ebiten Demo)&#34;); err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }
</code></pre> </code></pre>

View File

@ -27,46 +27,46 @@
package main package main
import ( import (
_ &#34;image/jpeg&#34; _ &#34;image/jpeg&#34;
&#34;log&#34; &#34;log&#34;
&#34;math&#34; &#34;math&#34;
&#34;github.com/hajimehoshi/ebiten&#34; &#34;github.com/hajimehoshi/ebiten&#34;
&#34;github.com/hajimehoshi/ebiten/ebitenutil&#34; &#34;github.com/hajimehoshi/ebiten/ebitenutil&#34;
) )
const ( const (
screenWidth = 320 screenWidth = 320
screenHeight = 240 screenHeight = 240
) )
var ( var (
count int count int
gophersImage *ebiten.Image gophersImage *ebiten.Image
) )
func update(screen *ebiten.Image) error { func update(screen *ebiten.Image) error {
count&#43;&#43; count&#43;&#43;
w, h := gophersImage.Size() w, h := gophersImage.Size()
op := &amp;ebiten.DrawImageOptions{} op := &amp;ebiten.DrawImageOptions{}
op.GeoM.Translate(-float64(w)/2, -float64(h)/2) op.GeoM.Translate(-float64(w)/2, -float64(h)/2)
op.GeoM.Rotate(float64(count%360) * 2 * math.Pi / 360) op.GeoM.Rotate(float64(count%360) * 2 * math.Pi / 360)
op.GeoM.Translate(screenWidth/2, screenHeight/2) op.GeoM.Translate(screenWidth/2, screenHeight/2)
if err := screen.DrawImage(gophersImage, op); err != nil { if err := screen.DrawImage(gophersImage, op); err != nil {
return err return err
} }
return nil return nil
} }
func main() { func main() {
var err error var err error
gophersImage, _, err = ebitenutil.NewImageFromFile(&#34;_resources/images/gophers.jpg&#34;, ebiten.FilterNearest) gophersImage, _, err = ebitenutil.NewImageFromFile(&#34;_resources/images/gophers.jpg&#34;, ebiten.FilterNearest)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
if err := ebiten.Run(update, screenWidth, screenHeight, 2, &#34;Rotate (Ebiten Demo)&#34;); err != nil { if err := ebiten.Run(update, screenWidth, screenHeight, 2, &#34;Rotate (Ebiten Demo)&#34;); err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }
</code></pre> </code></pre>

View File

@ -27,149 +27,149 @@
package main package main
import ( import (
&#34;fmt&#34; &#34;fmt&#34;
_ &#34;image/png&#34; _ &#34;image/png&#34;
&#34;log&#34; &#34;log&#34;
&#34;math/rand&#34; &#34;math/rand&#34;
&#34;github.com/hajimehoshi/ebiten&#34; &#34;github.com/hajimehoshi/ebiten&#34;
&#34;github.com/hajimehoshi/ebiten/ebitenutil&#34; &#34;github.com/hajimehoshi/ebiten/ebitenutil&#34;
) )
const ( const (
screenWidth = 320 screenWidth = 320
screenHeight = 240 screenHeight = 240
) )
var ( var (
ebitenImage *ebiten.Image ebitenImage *ebiten.Image
ebitenImageWidth = 0 ebitenImageWidth = 0
ebitenImageHeight = 0 ebitenImageHeight = 0
) )
type Sprite struct { type Sprite struct {
image *ebiten.Image image *ebiten.Image
x int x int
y int y int
vx int vx int
vy int vy int
} }
func (s *Sprite) Update() { func (s *Sprite) Update() {
s.x &#43;= s.vx s.x &#43;= s.vx
s.y &#43;= s.vy s.y &#43;= s.vy
if s.x &lt; 0 { if s.x &lt; 0 {
s.x = -s.x s.x = -s.x
s.vx = -s.vx s.vx = -s.vx
} }
if s.y &lt; 0 { if s.y &lt; 0 {
s.y = -s.y s.y = -s.y
s.vy = -s.vy s.vy = -s.vy
} }
w, h := s.image.Size() w, h := s.image.Size()
if screenWidth &lt;= s.x&#43;w { if screenWidth &lt;= s.x&#43;w {
s.x = 2*(screenWidth-w) - s.x s.x = 2*(screenWidth-w) - s.x
s.vx = -s.vx s.vx = -s.vx
} }
if screenHeight &lt;= s.y&#43;h { if screenHeight &lt;= s.y&#43;h {
s.y = 2*(screenHeight-h) - s.y s.y = 2*(screenHeight-h) - s.y
s.vy = -s.vy s.vy = -s.vy
} }
} }
type Sprites struct { type Sprites struct {
sprites []*Sprite sprites []*Sprite
num int num int
} }
func (s Sprites) Update() { func (s Sprites) Update() {
for _, sprite := range s.sprites { for _, sprite := range s.sprites {
sprite.Update() sprite.Update()
} }
} }
func (s Sprites) Len() int { func (s Sprites) Len() int {
return s.num return s.num
} }
func (s Sprites) Dst(i int) (x0, y0, x1, y1 int) { func (s Sprites) Dst(i int) (x0, y0, x1, y1 int) {
if s.num &lt;= i { if s.num &lt;= i {
return 0, 0, 0, 0 return 0, 0, 0, 0
} }
ss := s.sprites[i] ss := s.sprites[i]
return ss.x, ss.y, ss.x &#43; ebitenImageWidth, ss.y &#43; ebitenImageHeight return ss.x, ss.y, ss.x &#43; ebitenImageWidth, ss.y &#43; ebitenImageHeight
} }
func (s Sprites) Src(i int) (x0, y0, x1, y1 int) { func (s Sprites) Src(i int) (x0, y0, x1, y1 int) {
if s.num &lt;= i { if s.num &lt;= i {
return 0, 0, 0, 0 return 0, 0, 0, 0
} }
return 0, 0, ebitenImageWidth, ebitenImageHeight return 0, 0, ebitenImageWidth, ebitenImageHeight
} }
const ( const (
MinSprites = 0 MinSprites = 0
MaxSprites = 50000 MaxSprites = 50000
) )
var sprites = &amp;Sprites{make([]*Sprite, MaxSprites), 500} var sprites = &amp;Sprites{make([]*Sprite, MaxSprites), 500}
func update(screen *ebiten.Image) error { func update(screen *ebiten.Image) error {
if ebiten.IsKeyPressed(ebiten.KeyLeft) { if ebiten.IsKeyPressed(ebiten.KeyLeft) {
sprites.num -= 20 sprites.num -= 20
if sprites.num &lt; MinSprites { if sprites.num &lt; MinSprites {
sprites.num = MinSprites sprites.num = MinSprites
}
} }
} if ebiten.IsKeyPressed(ebiten.KeyRight) {
if ebiten.IsKeyPressed(ebiten.KeyRight) { sprites.num &#43;= 20
sprites.num &#43;= 20 if MaxSprites &lt; sprites.num {
if MaxSprites &lt; sprites.num { sprites.num = MaxSprites
sprites.num = MaxSprites }
} }
} sprites.Update()
sprites.Update()
if ebiten.IsRunningSlowly() { if ebiten.IsRunningSlowly() {
return nil return nil
} }
op := &amp;ebiten.DrawImageOptions{ op := &amp;ebiten.DrawImageOptions{
ImageParts: sprites, ImageParts: sprites,
} }
op.ColorM.Scale(1.0, 1.0, 1.0, 0.5) op.ColorM.Scale(1.0, 1.0, 1.0, 0.5)
if err := screen.DrawImage(ebitenImage, op); err != nil { if err := screen.DrawImage(ebitenImage, op); err != nil {
return err return err
} }
msg := fmt.Sprintf(`FPS: %0.2f msg := fmt.Sprintf(`FPS: %0.2f
Num of sprites: %d Num of sprites: %d
Press &lt;- or -&gt; to change the number of sprites`, ebiten.CurrentFPS(), sprites.Len()) Press &lt;- or -&gt; to change the number of sprites`, ebiten.CurrentFPS(), sprites.Len())
if err := ebitenutil.DebugPrint(screen, msg); err != nil { if err := ebitenutil.DebugPrint(screen, msg); err != nil {
return err return err
} }
return nil return nil
} }
func main() { func main() {
var err error var err error
ebitenImage, _, err = ebitenutil.NewImageFromFile(&#34;_resources/images/ebiten.png&#34;, ebiten.FilterNearest) ebitenImage, _, err = ebitenutil.NewImageFromFile(&#34;_resources/images/ebiten.png&#34;, ebiten.FilterNearest)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
ebitenImageWidth, ebitenImageHeight = ebitenImage.Size() ebitenImageWidth, ebitenImageHeight = ebitenImage.Size()
for i := range sprites.sprites { for i := range sprites.sprites {
w, h := ebitenImage.Size() w, h := ebitenImage.Size()
x, y := rand.Intn(screenWidth-w), rand.Intn(screenHeight-h) x, y := rand.Intn(screenWidth-w), rand.Intn(screenHeight-h)
vx, vy := 2*rand.Intn(2)-1, 2*rand.Intn(2)-1 vx, vy := 2*rand.Intn(2)-1, 2*rand.Intn(2)-1
sprites.sprites[i] = &amp;Sprite{ sprites.sprites[i] = &amp;Sprite{
image: ebitenImage, image: ebitenImage,
x: x, x: x,
y: y, y: y,
vx: vx, vx: vx,
vy: vy, vy: vy,
}
}
if err := ebiten.Run(update, screenWidth, screenHeight, 2, &#34;Sprites (Ebiten Demo)&#34;); err != nil {
log.Fatal(err)
} }
}
if err := ebiten.Run(update, screenWidth, screenHeight, 2, &#34;Sprites (Ebiten Demo)&#34;); err != nil {
log.Fatal(err)
}
} }
</code></pre> </code></pre>

View File

@ -98,17 +98,17 @@ the <code>main.go</code> file:</p>
<pre><code>package main <pre><code>package main
import ( import (
"github.com/hajimehoshi/ebiten" "github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/ebitenutil" "github.com/hajimehoshi/ebiten/ebitenutil"
) )
func update(screen *ebiten.Image) error { func update(screen *ebiten.Image) error {
ebitenutil.DebugPrint(screen, "Hello world!") ebitenutil.DebugPrint(screen, "Hello world!")
return nil return nil
} }
func main() { func main() {
ebiten.Run(update, 320, 240, 2, "Hello world!") ebiten.Run(update, 320, 240, 2, "Hello world!")
} }
</code></pre> </code></pre>