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)
}
str := regexp.MustCompile("(?s)^.*?\n\n").ReplaceAllString(string(b), "")
str = strings.Replace(str, "\t", " ", -1)
str = strings.Replace(str, "\t", " ", -1)
return str
}

View File

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

View File

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

View File

@ -27,287 +27,287 @@
package main
import (
&#34;fmt&#34;
&#34;image/color&#34;
&#34;io/ioutil&#34;
&#34;log&#34;
&#34;time&#34;
&#34;fmt&#34;
&#34;image/color&#34;
&#34;io/ioutil&#34;
&#34;log&#34;
&#34;time&#34;
&#34;github.com/hajimehoshi/ebiten&#34;
&#34;github.com/hajimehoshi/ebiten/audio&#34;
&#34;github.com/hajimehoshi/ebiten/audio/vorbis&#34;
&#34;github.com/hajimehoshi/ebiten/audio/wav&#34;
&#34;github.com/hajimehoshi/ebiten/ebitenutil&#34;
&#34;github.com/hajimehoshi/ebiten&#34;
&#34;github.com/hajimehoshi/ebiten/audio&#34;
&#34;github.com/hajimehoshi/ebiten/audio/vorbis&#34;
&#34;github.com/hajimehoshi/ebiten/audio/wav&#34;
&#34;github.com/hajimehoshi/ebiten/ebitenutil&#34;
)
const (
screenWidth = 320
screenHeight = 240
screenWidth = 320
screenHeight = 240
)
var (
playerBarImage *ebiten.Image
playerCurrentImage *ebiten.Image
playerBarImage *ebiten.Image
playerCurrentImage *ebiten.Image
)
func init() {
var err error
playerBarImage, err = ebiten.NewImage(300, 4, ebiten.FilterNearest)
if err != nil {
log.Fatal(err)
}
if err := playerBarImage.Fill(&amp;color.RGBA{0x80, 0x80, 0x80, 0xff}); err != nil {
log.Fatal(err)
}
var err error
playerBarImage, err = ebiten.NewImage(300, 4, ebiten.FilterNearest)
if err != nil {
log.Fatal(err)
}
if err := playerBarImage.Fill(&amp;color.RGBA{0x80, 0x80, 0x80, 0xff}); err != nil {
log.Fatal(err)
}
playerCurrentImage, err = ebiten.NewImage(4, 10, ebiten.FilterNearest)
if err != nil {
log.Fatal(err)
}
if err := playerCurrentImage.Fill(&amp;color.RGBA{0xff, 0xff, 0xff, 0xff}); err != nil {
log.Fatal(err)
}
playerCurrentImage, err = ebiten.NewImage(4, 10, ebiten.FilterNearest)
if err != nil {
log.Fatal(err)
}
if err := playerCurrentImage.Fill(&amp;color.RGBA{0xff, 0xff, 0xff, 0xff}); err != nil {
log.Fatal(err)
}
}
type Player struct {
audioPlayer *audio.Player
total time.Duration
audioPlayer *audio.Player
total time.Duration
}
var (
audioContext *audio.Context
musicPlayer *Player
seBytes []byte
musicCh = make(chan *Player)
seCh = make(chan []byte)
mouseButtonState = map[ebiten.MouseButton]int{}
keyState = map[ebiten.Key]int{}
volume128 = 128
audioContext *audio.Context
musicPlayer *Player
seBytes []byte
musicCh = make(chan *Player)
seCh = make(chan []byte)
mouseButtonState = map[ebiten.MouseButton]int{}
keyState = map[ebiten.Key]int{}
volume128 = 128
)
func playerBarRect() (x, y, w, h int) {
w, h = playerBarImage.Size()
x = (screenWidth - w) / 2
y = screenHeight - h - 16
return
w, h = playerBarImage.Size()
x = (screenWidth - w) / 2
y = screenHeight - h - 16
return
}
func (p *Player) updateSE() error {
if seBytes == nil {
return nil
}
if !ebiten.IsKeyPressed(ebiten.KeyP) {
keyState[ebiten.KeyP] = 0
return nil
}
keyState[ebiten.KeyP]&#43;&#43;
if keyState[ebiten.KeyP] != 1 {
return nil
}
sePlayer, err := audio.NewPlayerFromBytes(audioContext, seBytes)
if err != nil {
return err
}
return sePlayer.Play()
if seBytes == nil {
return nil
}
if !ebiten.IsKeyPressed(ebiten.KeyP) {
keyState[ebiten.KeyP] = 0
return nil
}
keyState[ebiten.KeyP]&#43;&#43;
if keyState[ebiten.KeyP] != 1 {
return nil
}
sePlayer, err := audio.NewPlayerFromBytes(audioContext, seBytes)
if err != nil {
return err
}
return sePlayer.Play()
}
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
}
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 {
if p.audioPlayer == nil {
return nil
}
if !ebiten.IsKeyPressed(ebiten.KeyS) {
keyState[ebiten.KeyS] = 0
return nil
}
keyState[ebiten.KeyS]&#43;&#43;
if keyState[ebiten.KeyS] != 1 {
return nil
}
if p.audioPlayer.IsPlaying() {
return p.audioPlayer.Pause()
}
return p.audioPlayer.Play()
if p.audioPlayer == nil {
return nil
}
if !ebiten.IsKeyPressed(ebiten.KeyS) {
keyState[ebiten.KeyS] = 0
return nil
}
keyState[ebiten.KeyS]&#43;&#43;
if keyState[ebiten.KeyS] != 1 {
return nil
}
if p.audioPlayer.IsPlaying() {
return p.audioPlayer.Pause()
}
return p.audioPlayer.Play()
}
func (p *Player) updateBar() error {
if p.audioPlayer == nil {
return nil
}
if !ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) {
mouseButtonState[ebiten.MouseButtonLeft] = 0
return nil
}
mouseButtonState[ebiten.MouseButtonLeft]&#43;&#43;
if mouseButtonState[ebiten.MouseButtonLeft] != 1 {
return nil
}
x, y := ebiten.CursorPosition()
bx, by, bw, bh := playerBarRect()
const padding = 4
if y &lt; by-padding || by&#43;bh&#43;padding &lt;= y {
return nil
}
if x &lt; bx || bx&#43;bw &lt;= x {
return nil
}
pos := time.Duration(x-bx) * p.total / time.Duration(bw)
return p.audioPlayer.Seek(pos)
if p.audioPlayer == nil {
return nil
}
if !ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) {
mouseButtonState[ebiten.MouseButtonLeft] = 0
return nil
}
mouseButtonState[ebiten.MouseButtonLeft]&#43;&#43;
if mouseButtonState[ebiten.MouseButtonLeft] != 1 {
return nil
}
x, y := ebiten.CursorPosition()
bx, by, bw, bh := playerBarRect()
const padding = 4
if y &lt; by-padding || by&#43;bh&#43;padding &lt;= y {
return nil
}
if x &lt; bx || bx&#43;bw &lt;= x {
return nil
}
pos := time.Duration(x-bx) * p.total / time.Duration(bw)
return p.audioPlayer.Seek(pos)
}
func (p *Player) close() error {
return p.audioPlayer.Close()
return p.audioPlayer.Close()
}
func update(screen *ebiten.Image) error {
if musicPlayer == nil {
select {
case musicPlayer = &lt;-musicCh:
default:
if musicPlayer == nil {
select {
case musicPlayer = &lt;-musicCh:
default:
}
}
}
if seBytes == nil {
select {
case seBytes = &lt;-seCh:
default:
if seBytes == nil {
select {
case seBytes = &lt;-seCh:
default:
}
}
}
if musicPlayer != nil {
if err := musicPlayer.updateBar(); err != nil {
return err
if musicPlayer != nil {
if err := musicPlayer.updateBar(); 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
}
}
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.GeoM.Translate(float64(cx), float64(cy))
if err := screen.DrawImage(playerCurrentImage, op); err != nil {
return err
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()
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 P to play SE
Press Z or X to change volume of the music
%s`, ebiten.CurrentFPS(), currentTimeStr)
if musicPlayer == nil {
msg &#43;= &#34;\nNow Loading...&#34;
}
if err := ebitenutil.DebugPrint(screen, msg); err != nil {
return err
}
if err := audioContext.Update(); err != nil {
return err
}
return nil
if musicPlayer == nil {
msg &#43;= &#34;\nNow Loading...&#34;
}
if err := ebitenutil.DebugPrint(screen, msg); err != nil {
return err
}
if err := audioContext.Update(); err != nil {
return err
}
return nil
}
func main() {
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)
wavF, err := ebitenutil.OpenFile(&#34;_resources/audio/jab.wav&#34;)
if err != nil {
log.Fatal(err)
return
log.Fatal(err)
}
b, err := ioutil.ReadAll(s)
oggF, err := ebitenutil.OpenFile(&#34;_resources/audio/ragtime.ogg&#34;)
if err != nil {
log.Fatal(err)
return
log.Fatal(err)
}
seCh &lt;- b
close(seCh)
}()
go func() {
s, err := vorbis.Decode(audioContext, oggF)
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)
return
log.Fatal(err)
}
p, err := audio.NewPlayer(audioContext, s)
if err != nil {
log.Fatal(err)
return
go func() {
s, err := wav.Decode(audioContext, wavF)
if err != nil {
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{
audioPlayer: p,
total: time.Second * time.Duration(s.Size()) / bytesPerSample / sampleRate,
if musicPlayer != nil {
if err := musicPlayer.close(); err != nil {
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>

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -27,31 +27,31 @@
package main
import (
&#34;fmt&#34;
&#34;image/color&#34;
&#34;log&#34;
&#34;math&#34;
&#34;fmt&#34;
&#34;image/color&#34;
&#34;log&#34;
&#34;math&#34;
&#34;github.com/hajimehoshi/ebiten&#34;
&#34;github.com/hajimehoshi/ebiten/audio&#34;
&#34;github.com/hajimehoshi/ebiten/ebitenutil&#34;
&#34;github.com/hajimehoshi/ebiten/examples/common&#34;
&#34;github.com/hajimehoshi/ebiten&#34;
&#34;github.com/hajimehoshi/ebiten/audio&#34;
&#34;github.com/hajimehoshi/ebiten/ebitenutil&#34;
&#34;github.com/hajimehoshi/ebiten/examples/common&#34;
)
const (
screenWidth = 320
screenHeight = 240
sampleRate = 44100
screenWidth = 320
screenHeight = 240
sampleRate = 44100
)
var audioContext *audio.Context
func init() {
var err error
audioContext, err = audio.NewContext(sampleRate)
if err != nil {
log.Fatal(err)
}
var err error
audioContext, err = audio.NewContext(sampleRate)
if err != nil {
log.Fatal(err)
}
}
var pcm = make([]float64, 4*sampleRate)
@ -59,203 +59,203 @@ var pcm = make([]float64, 4*sampleRate)
const baseFreq = 220
func init() {
s := float64(sampleRate)
amp := []float64{1.0, 0.8, 0.6, 0.4, 0.2}
x := []float64{4.0, 2.0, 1.0, 0.5, 0.25}
for i := 0; i &lt; len(pcm); i&#43;&#43; {
v := 0.0
twoPiF := 2.0 * math.Pi * baseFreq
for j := 0; j &lt; len(amp); j&#43;&#43; {
a := amp[j] * math.Exp(-5*float64(i)/(x[j]*s))
v &#43;= a * math.Sin(float64(i)*twoPiF*float64(j&#43;1)/s)
s := float64(sampleRate)
amp := []float64{1.0, 0.8, 0.6, 0.4, 0.2}
x := []float64{4.0, 2.0, 1.0, 0.5, 0.25}
for i := 0; i &lt; len(pcm); i&#43;&#43; {
v := 0.0
twoPiF := 2.0 * math.Pi * baseFreq
for j := 0; j &lt; len(amp); j&#43;&#43; {
a := amp[j] * math.Exp(-5*float64(i)/(x[j]*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 (
noteCache = map[int][]byte{}
noteCache = map[int][]byte{}
)
func toBytes(l, r []int16) []byte {
if len(l) != len(r) {
panic(&#34;len(l) must equal to len(r)&#34;)
}
b := make([]byte, len(l)*4)
for i := range l {
b[4*i] = byte(l[i])
b[4*i&#43;1] = byte(l[i] &gt;&gt; 8)
b[4*i&#43;2] = byte(r[i])
b[4*i&#43;3] = byte(r[i] &gt;&gt; 8)
}
return b
if len(l) != len(r) {
panic(&#34;len(l) must equal to len(r)&#34;)
}
b := make([]byte, len(l)*4)
for i := range l {
b[4*i] = byte(l[i])
b[4*i&#43;1] = byte(l[i] &gt;&gt; 8)
b[4*i&#43;2] = byte(r[i])
b[4*i&#43;3] = byte(r[i] &gt;&gt; 8)
}
return b
}
func addNote(freq float64, vol float64) error {
// TODO: Call Close method of *audio.Player.
// However, this works without Close because Close is automatically called when GC
// collects a *audio.Player object.
f := int(freq)
if n, ok := noteCache[f]; ok {
// TODO: Call Close method of *audio.Player.
// However, this works without Close because Close is automatically called when GC
// collects a *audio.Player object.
f := int(freq)
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)
if err != nil {
return err
return err
}
if err := p.Play(); err != nil {
return err
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)
if err != nil {
return err
}
if err := p.Play(); err != nil {
return err
}
return nil
}
var keys = []ebiten.Key{
ebiten.KeyQ,
ebiten.KeyA,
ebiten.KeyW,
ebiten.KeyS,
ebiten.KeyD,
ebiten.KeyR,
ebiten.KeyF,
ebiten.KeyT,
ebiten.KeyG,
ebiten.KeyH,
ebiten.KeyU,
ebiten.KeyJ,
ebiten.KeyI,
ebiten.KeyK,
ebiten.KeyO,
ebiten.KeyL,
ebiten.KeyQ,
ebiten.KeyA,
ebiten.KeyW,
ebiten.KeyS,
ebiten.KeyD,
ebiten.KeyR,
ebiten.KeyF,
ebiten.KeyT,
ebiten.KeyG,
ebiten.KeyH,
ebiten.KeyU,
ebiten.KeyJ,
ebiten.KeyI,
ebiten.KeyK,
ebiten.KeyO,
ebiten.KeyL,
}
var keyStates = map[ebiten.Key]int{}
func init() {
for _, key := range keys {
keyStates[key] = 0
}
for _, key := range keys {
keyStates[key] = 0
}
}
func updateInput() {
for _, key := range keys {
if !ebiten.IsKeyPressed(key) {
keyStates[key] = 0
continue
for _, key := range keys {
if !ebiten.IsKeyPressed(key) {
keyStates[key] = 0
continue
}
keyStates[key]&#43;&#43;
}
keyStates[key]&#43;&#43;
}
}
var (
imagePiano *ebiten.Image
imagePiano *ebiten.Image
)
func init() {
var err error
imageEmpty, err := ebiten.NewImage(16, 16, ebiten.FilterNearest)
if err != nil {
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)
var err error
imageEmpty, err := ebiten.NewImage(16, 16, ebiten.FilterNearest)
if 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)
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 {
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;}
for i, k := range blackKeys {
if k == &#34;&#34; {
continue
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 {
if k == &#34;&#34; {
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 {
updateInput()
for i, key := range keys {
if keyStates[key] != 1 {
continue
updateInput()
for i, key := range keys {
if keyStates[key] != 1 {
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 {
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 {
return err
}
if err := ebitenutil.DebugPrint(screen, fmt.Sprintf(&#34;FPS: %0.2f&#34;, ebiten.CurrentFPS())); err != nil {
return err
}
if err := audioContext.Update(); err != nil {
return err
}
return nil
if err := audioContext.Update(); err != nil {
return err
}
return nil
}
func main() {
if err := ebiten.Run(update, screenWidth, screenHeight, 2, &#34;Piano (Ebiten Demo)&#34;); err != nil {
log.Fatal(err)
}
if err := ebiten.Run(update, screenWidth, screenHeight, 2, &#34;Piano (Ebiten Demo)&#34;); err != nil {
log.Fatal(err)
}
}
</code></pre>

View File

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

View File

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

View File

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