mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-26 02:42:02 +01:00
doc: Change indent size 4 -> 8
This commit is contained in:
parent
69dafff704
commit
12920a79da
@ -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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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>
|
||||||
|
|
||||||
|
@ -27,78 +27,78 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"image/color"
|
"image/color"
|
||||||
_ "image/png"
|
_ "image/png"
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
"github.com/hajimehoshi/ebiten"
|
"github.com/hajimehoshi/ebiten"
|
||||||
"github.com/hajimehoshi/ebiten/ebitenutil"
|
"github.com/hajimehoshi/ebiten/ebitenutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
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 + 15)
|
x := int(float64(i%10)*p.diff + 15)
|
||||||
y := int(float64(i/10)*p.diff + 20)
|
y := int(float64(i/10)*p.diff + 20)
|
||||||
w, h := ebitenImage.Size()
|
w, h := ebitenImage.Size()
|
||||||
return x, y, x + w, y + h
|
return x, y, x + w, y + 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++
|
count++
|
||||||
count %= ebiten.FPS * 10
|
count %= ebiten.FPS * 10
|
||||||
diff := float64(count) * 0.2
|
diff := float64(count) * 0.2
|
||||||
switch {
|
switch {
|
||||||
case 480 < count:
|
case 480 < count:
|
||||||
diff = 0
|
diff = 0
|
||||||
case 240 < count:
|
case 240 < 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 := &ebiten.DrawImageOptions{}
|
op := &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 = &imageParts{
|
op.ImageParts = &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("_resources/images/ebiten.png", ebiten.FilterNearest)
|
ebitenImage, _, err = ebitenutil.NewImageFromFile("_resources/images/ebiten.png", ebiten.FilterNearest)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Alpha Blending (Ebiten Demo)"); err != nil {
|
if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Alpha Blending (Ebiten Demo)"); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</code></pre>
|
</code></pre>
|
||||||
|
|
||||||
|
@ -27,287 +27,287 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"image/color"
|
"image/color"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/hajimehoshi/ebiten"
|
"github.com/hajimehoshi/ebiten"
|
||||||
"github.com/hajimehoshi/ebiten/audio"
|
"github.com/hajimehoshi/ebiten/audio"
|
||||||
"github.com/hajimehoshi/ebiten/audio/vorbis"
|
"github.com/hajimehoshi/ebiten/audio/vorbis"
|
||||||
"github.com/hajimehoshi/ebiten/audio/wav"
|
"github.com/hajimehoshi/ebiten/audio/wav"
|
||||||
"github.com/hajimehoshi/ebiten/ebitenutil"
|
"github.com/hajimehoshi/ebiten/ebitenutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
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(&color.RGBA{0x80, 0x80, 0x80, 0xff}); err != nil {
|
if err := playerBarImage.Fill(&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(&color.RGBA{0xff, 0xff, 0xff, 0xff}); err != nil {
|
if err := playerCurrentImage.Fill(&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]++
|
keyState[ebiten.KeyP]++
|
||||||
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++
|
||||||
|
}
|
||||||
|
if volume128 < 0 {
|
||||||
|
volume128 = 0
|
||||||
|
}
|
||||||
|
if 128 < 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++
|
|
||||||
}
|
|
||||||
if volume128 < 0 {
|
|
||||||
volume128 = 0
|
|
||||||
}
|
|
||||||
if 128 < 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]++
|
keyState[ebiten.KeyS]++
|
||||||
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]++
|
mouseButtonState[ebiten.MouseButtonLeft]++
|
||||||
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 < by-padding || by+bh+padding <= y {
|
if y < by-padding || by+bh+padding <= y {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if x < bx || bx+bw <= x {
|
if x < bx || bx+bw <= 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 = <-musicCh:
|
case musicPlayer = <-musicCh:
|
||||||
default:
|
default:
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
if seBytes == nil {
|
||||||
if seBytes == nil {
|
select {
|
||||||
select {
|
case seBytes = <-seCh:
|
||||||
case seBytes = <-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 := &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 := "00:00"
|
|
||||||
if musicPlayer != nil {
|
|
||||||
c := musicPlayer.audioPlayer.Current()
|
|
||||||
|
|
||||||
// Current Time
|
|
||||||
m := (c / time.Minute) % 100
|
|
||||||
s := (c / time.Second) % 60
|
|
||||||
currentTimeStr = fmt.Sprintf("%02d:%02d", m, s)
|
|
||||||
|
|
||||||
// Bar
|
|
||||||
cw, ch := playerCurrentImage.Size()
|
|
||||||
cx := int(time.Duration(w)*c/musicPlayer.total) + x - cw/2
|
|
||||||
cy := y - (ch-h)/2
|
|
||||||
op := &ebiten.DrawImageOptions{}
|
op := &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 := "00:00"
|
||||||
|
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("%02d:%02d", m, s)
|
||||||
|
|
||||||
|
// Bar
|
||||||
|
cw, ch := playerCurrentImage.Size()
|
||||||
|
cx := int(time.Duration(w)*c/musicPlayer.total) + x - cw/2
|
||||||
|
cy := y - (ch-h)/2
|
||||||
|
op := &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 += "\nNow Loading..."
|
msg += "\nNow Loading..."
|
||||||
}
|
}
|
||||||
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("_resources/audio/jab.wav")
|
wavF, err := ebitenutil.OpenFile("_resources/audio/jab.wav")
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
oggF, err := ebitenutil.OpenFile("_resources/audio/ragtime.ogg")
|
|
||||||
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("_resources/audio/ragtime.ogg")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
seCh <- 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 <- 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 <- &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, "Audio (Ebiten Demo)"); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
musicCh <- &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, "Audio (Ebiten Demo)"); err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
if musicPlayer != nil {
|
|
||||||
if err := musicPlayer.close(); err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
</code></pre>
|
</code></pre>
|
||||||
|
|
||||||
|
@ -27,95 +27,95 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"image"
|
"image"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
"github.com/golang/freetype/truetype"
|
"github.com/golang/freetype/truetype"
|
||||||
"github.com/hajimehoshi/ebiten"
|
"github.com/hajimehoshi/ebiten"
|
||||||
"github.com/hajimehoshi/ebiten/ebitenutil"
|
"github.com/hajimehoshi/ebiten/ebitenutil"
|
||||||
"golang.org/x/image/font"
|
"golang.org/x/image/font"
|
||||||
"golang.org/x/image/math/fixed"
|
"golang.org/x/image/math/fixed"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
screenWidth = 640
|
screenWidth = 640
|
||||||
screenHeight = 480
|
screenHeight = 480
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
textImage *ebiten.Image
|
textImage *ebiten.Image
|
||||||
)
|
)
|
||||||
|
|
||||||
var text = []string{
|
var text = []string{
|
||||||
"The quick brown fox jumps over the lazy dog.",
|
"The quick brown fox jumps over the lazy dog.",
|
||||||
"",
|
"",
|
||||||
// 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.
|
||||||
"隴西の李徴は博学才穎、天宝の末年、",
|
"隴西の李徴は博学才穎、天宝の末年、",
|
||||||
"若くして名を虎榜に連ね、",
|
"若くして名を虎榜に連ね、",
|
||||||
"ついで江南尉に補せられたが、",
|
"ついで江南尉に補せられたが、",
|
||||||
"性、狷介、自ら恃むところ頗厚く、",
|
"性、狷介、自ら恃むところ頗厚く、",
|
||||||
"賤吏に甘んずるを潔しとしなかった。",
|
"賤吏に甘んずるを潔しとしなかった。",
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseFont() error {
|
func parseFont() error {
|
||||||
f, err := ebitenutil.OpenFile("_resources/fonts/mplus-1p-regular.ttf")
|
f, err := ebitenutil.OpenFile("_resources/fonts/mplus-1p-regular.ttf")
|
||||||
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 := &font.Drawer{
|
d := &font.Drawer{
|
||||||
Dst: dst,
|
Dst: dst,
|
||||||
Src: image.White,
|
Src: image.White,
|
||||||
Face: truetype.NewFace(tt, &truetype.Options{
|
Face: truetype.NewFace(tt, &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 += size
|
y += 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, &ebiten.DrawImageOptions{}); err != nil {
|
if err := screen.DrawImage(textImage, &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, "Font (Ebiten Demo)"); err != nil {
|
if err := ebiten.Run(update, screenWidth, screenHeight, 1, "Font (Ebiten Demo)"); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</code></pre>
|
</code></pre>
|
||||||
|
|
||||||
|
@ -27,55 +27,55 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/hajimehoshi/ebiten"
|
"github.com/hajimehoshi/ebiten"
|
||||||
"github.com/hajimehoshi/ebiten/ebitenutil"
|
"github.com/hajimehoshi/ebiten/ebitenutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
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 < maxAxis; a++ {
|
for a := 0; a < maxAxis; a++ {
|
||||||
v := ebiten.GamepadAxis(gamepadID, a)
|
v := ebiten.GamepadAxis(gamepadID, a)
|
||||||
axes = append(axes, fmt.Sprintf("%d: %0.6f", a, v))
|
axes = append(axes, fmt.Sprintf("%d: %0.6f", a, v))
|
||||||
}
|
|
||||||
|
|
||||||
maxButton := ebiten.GamepadButton(ebiten.GamepadButtonNum(gamepadID))
|
|
||||||
for b := ebiten.GamepadButton(gamepadID); b < maxButton; b++ {
|
|
||||||
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 < maxButton; b++ {
|
||||||
|
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, "{{.Axes}}", strings.Join(axes, "\n "), -1)
|
str = strings.Replace(str, "{{.Axes}}", strings.Join(axes, "\n "), -1)
|
||||||
str = strings.Replace(str, "{{.Buttons}}", strings.Join(pressedButtons, ", "), -1)
|
str = strings.Replace(str, "{{.Buttons}}", strings.Join(pressedButtons, ", "), -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, "Gamepad (Ebiten Demo)"); err != nil {
|
if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Gamepad (Ebiten Demo)"); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</code></pre>
|
</code></pre>
|
||||||
|
|
||||||
|
@ -27,92 +27,92 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
_ "image/jpeg"
|
_ "image/jpeg"
|
||||||
"log"
|
"log"
|
||||||
"math"
|
"math"
|
||||||
|
|
||||||
"github.com/hajimehoshi/ebiten"
|
"github.com/hajimehoshi/ebiten"
|
||||||
"github.com/hajimehoshi/ebiten/ebitenutil"
|
"github.com/hajimehoshi/ebiten/ebitenutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
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 > max {
|
if min > max {
|
||||||
panic("min must <= max")
|
panic("min must <= max")
|
||||||
}
|
}
|
||||||
if v < min {
|
if v < min {
|
||||||
return min
|
return min
|
||||||
}
|
}
|
||||||
if max < v {
|
if max < 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++
|
hueInt++
|
||||||
}
|
}
|
||||||
if ebiten.IsKeyPressed(ebiten.KeyA) {
|
if ebiten.IsKeyPressed(ebiten.KeyA) {
|
||||||
saturationInt--
|
saturationInt--
|
||||||
}
|
}
|
||||||
if ebiten.IsKeyPressed(ebiten.KeyS) {
|
if ebiten.IsKeyPressed(ebiten.KeyS) {
|
||||||
saturationInt++
|
saturationInt++
|
||||||
}
|
}
|
||||||
if ebiten.IsKeyPressed(ebiten.KeyZ) {
|
if ebiten.IsKeyPressed(ebiten.KeyZ) {
|
||||||
valueInt--
|
valueInt--
|
||||||
}
|
}
|
||||||
if ebiten.IsKeyPressed(ebiten.KeyX) {
|
if ebiten.IsKeyPressed(ebiten.KeyX) {
|
||||||
valueInt++
|
valueInt++
|
||||||
}
|
}
|
||||||
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 := &ebiten.DrawImageOptions{}
|
op := &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("_resources/images/gophers.jpg", ebiten.FilterNearest)
|
gophersImage, _, err = ebitenutil.NewImageFromFile("_resources/images/gophers.jpg", ebiten.FilterNearest)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
if err := ebiten.Run(update, screenWidth, screenHeight, 2, "HSV (Ebiten Demo)"); err != nil {
|
if err := ebiten.Run(update, screenWidth, screenHeight, 2, "HSV (Ebiten Demo)"); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</code></pre>
|
</code></pre>
|
||||||
|
|
||||||
|
@ -27,45 +27,45 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
_ "image/jpeg"
|
_ "image/jpeg"
|
||||||
"log"
|
"log"
|
||||||
"math"
|
"math"
|
||||||
|
|
||||||
"github.com/hajimehoshi/ebiten"
|
"github.com/hajimehoshi/ebiten"
|
||||||
"github.com/hajimehoshi/ebiten/ebitenutil"
|
"github.com/hajimehoshi/ebiten/ebitenutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
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++
|
count++
|
||||||
w, h := gophersImage.Size()
|
w, h := gophersImage.Size()
|
||||||
op := &ebiten.DrawImageOptions{}
|
op := &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("_resources/images/gophers.jpg", ebiten.FilterNearest)
|
gophersImage, _, err = ebitenutil.NewImageFromFile("_resources/images/gophers.jpg", ebiten.FilterNearest)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Hue (Ebiten Demo)"); err != nil {
|
if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Hue (Ebiten Demo)"); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</code></pre>
|
</code></pre>
|
||||||
|
|
||||||
|
@ -27,116 +27,116 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"github.com/hajimehoshi/ebiten"
|
"github.com/hajimehoshi/ebiten"
|
||||||
"github.com/hajimehoshi/ebiten/ebitenutil"
|
"github.com/hajimehoshi/ebiten/ebitenutil"
|
||||||
"github.com/hajimehoshi/ebiten/examples/keyboard/keyboard"
|
"github.com/hajimehoshi/ebiten/examples/keyboard/keyboard"
|
||||||
)
|
)
|
||||||
|
|
||||||
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("_resources/images/keyboard/keyboard.png", ebiten.FilterNearest)
|
keyboardImage, _, err = ebitenutil.NewImageFromFile("_resources/images/keyboard/keyboard.png", 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: "BS",
|
ebiten.KeyBackspace: "BS",
|
||||||
ebiten.KeyComma: ",",
|
ebiten.KeyComma: ",",
|
||||||
ebiten.KeyDelete: "Del",
|
ebiten.KeyDelete: "Del",
|
||||||
ebiten.KeyEnter: "Enter",
|
ebiten.KeyEnter: "Enter",
|
||||||
ebiten.KeyEscape: "Esc",
|
ebiten.KeyEscape: "Esc",
|
||||||
ebiten.KeyPeriod: ".",
|
ebiten.KeyPeriod: ".",
|
||||||
ebiten.KeySpace: "Space",
|
ebiten.KeySpace: "Space",
|
||||||
ebiten.KeyTab: "Tab",
|
ebiten.KeyTab: "Tab",
|
||||||
|
|
||||||
// Arrows
|
// Arrows
|
||||||
ebiten.KeyDown: "Down",
|
ebiten.KeyDown: "Down",
|
||||||
ebiten.KeyLeft: "Left",
|
ebiten.KeyLeft: "Left",
|
||||||
ebiten.KeyRight: "Right",
|
ebiten.KeyRight: "Right",
|
||||||
ebiten.KeyUp: "Up",
|
ebiten.KeyUp: "Up",
|
||||||
|
|
||||||
// Mods
|
// Mods
|
||||||
ebiten.KeyShift: "Shift",
|
ebiten.KeyShift: "Shift",
|
||||||
ebiten.KeyControl: "Ctrl",
|
ebiten.KeyControl: "Ctrl",
|
||||||
ebiten.KeyAlt: "Alt",
|
ebiten.KeyAlt: "Alt",
|
||||||
}
|
}
|
||||||
|
|
||||||
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 := &ebiten.DrawImageOptions{}
|
op := &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 <= 9; i++ {
|
for i := 0; i <= 9; i++ {
|
||||||
if ebiten.IsKeyPressed(ebiten.Key(i) + ebiten.Key0) {
|
if ebiten.IsKeyPressed(ebiten.Key(i) + ebiten.Key0) {
|
||||||
pressed = append(pressed, string(i+'0'))
|
pressed = append(pressed, string(i+'0'))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
for c := 'A'; c <= 'Z'; c++ {
|
||||||
for c := 'A'; c <= 'Z'; c++ {
|
if ebiten.IsKeyPressed(ebiten.Key(c) - 'A' + ebiten.KeyA) {
|
||||||
if ebiten.IsKeyPressed(ebiten.Key(c) - 'A' + ebiten.KeyA) {
|
pressed = append(pressed, string(c))
|
||||||
pressed = append(pressed, string(c))
|
}
|
||||||
}
|
}
|
||||||
}
|
for i := 1; i <= 12; i++ {
|
||||||
for i := 1; i <= 12; i++ {
|
if ebiten.IsKeyPressed(ebiten.Key(i) + ebiten.KeyF1 - 1) {
|
||||||
if ebiten.IsKeyPressed(ebiten.Key(i) + ebiten.KeyF1 - 1) {
|
pressed = append(pressed, "F"+strconv.Itoa(i))
|
||||||
pressed = append(pressed, "F"+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 = &ebiten.DrawImageOptions{
|
op = &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, "Keyboard (Ebiten Demo)"); err != nil {
|
if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Keyboard (Ebiten Demo)"); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</code></pre>
|
</code></pre>
|
||||||
|
|
||||||
|
@ -27,129 +27,129 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"image"
|
"image"
|
||||||
"image/color"
|
"image/color"
|
||||||
_ "image/jpeg"
|
_ "image/jpeg"
|
||||||
"log"
|
"log"
|
||||||
"math"
|
"math"
|
||||||
|
|
||||||
"github.com/hajimehoshi/ebiten"
|
"github.com/hajimehoshi/ebiten"
|
||||||
"github.com/hajimehoshi/ebiten/ebitenutil"
|
"github.com/hajimehoshi/ebiten/ebitenutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
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 += spotLightVX
|
spotLightX += spotLightVX
|
||||||
spotLightY += spotLightVY
|
spotLightY += spotLightVY
|
||||||
if spotLightX < 0 {
|
if spotLightX < 0 {
|
||||||
spotLightX = -spotLightX
|
spotLightX = -spotLightX
|
||||||
spotLightVX = -spotLightVX
|
spotLightVX = -spotLightVX
|
||||||
}
|
}
|
||||||
if spotLightY < 0 {
|
if spotLightY < 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 < spotLightX {
|
if maxX < spotLightX {
|
||||||
spotLightX = -spotLightX + 2*maxX
|
spotLightX = -spotLightX + 2*maxX
|
||||||
spotLightVX = -spotLightVX
|
spotLightVX = -spotLightVX
|
||||||
}
|
}
|
||||||
if maxY < spotLightY {
|
if maxY < spotLightY {
|
||||||
spotLightY = -spotLightY + 2*maxY
|
spotLightY = -spotLightY + 2*maxY
|
||||||
spotLightVY = -spotLightVY
|
spotLightVY = -spotLightVY
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := maskImage.Clear(); err != nil {
|
if err := maskImage.Clear(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
op := &ebiten.DrawImageOptions{}
|
op := &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 = &ebiten.DrawImageOptions{}
|
op = &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, &ebiten.DrawImageOptions{}); err != nil {
|
if err := screen.DrawImage(gophersImage, &ebiten.DrawImageOptions{}); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := screen.DrawImage(maskImage, &ebiten.DrawImageOptions{}); err != nil {
|
if err := screen.DrawImage(maskImage, &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 < b {
|
if a < b {
|
||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
return a
|
return a
|
||||||
}
|
}
|
||||||
|
|
||||||
func min(a, b int) int {
|
func min(a, b int) int {
|
||||||
if a < b {
|
if a < b {
|
||||||
return a
|
return a
|
||||||
}
|
}
|
||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var err error
|
var err error
|
||||||
gophersImage, _, err = ebitenutil.NewImageFromFile("_resources/images/gophers.jpg", ebiten.FilterNearest)
|
gophersImage, _, err = ebitenutil.NewImageFromFile("_resources/images/gophers.jpg", ebiten.FilterNearest)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
fiveyearsImage, _, err = ebitenutil.NewImageFromFile("_resources/images/fiveyears.jpg", ebiten.FilterNearest)
|
fiveyearsImage, _, err = ebitenutil.NewImageFromFile("_resources/images/fiveyears.jpg", 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 < as.Y; j++ {
|
for j := 0; j < as.Y; j++ {
|
||||||
for i := 0; i < as.X; i++ {
|
for i := 0; i < as.X; i++ {
|
||||||
r := as.X / 2
|
r := as.X / 2
|
||||||
d := math.Sqrt(float64((i-r)*(i-r) + (j-r)*(j-r)))
|
d := math.Sqrt(float64((i-r)*(i-r) + (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, "Masking (Ebiten Demo)"); 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, "Masking (Ebiten Demo)"); err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
</code></pre>
|
</code></pre>
|
||||||
|
|
||||||
|
@ -27,53 +27,53 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
_ "image/jpeg"
|
_ "image/jpeg"
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
"github.com/hajimehoshi/ebiten"
|
"github.com/hajimehoshi/ebiten"
|
||||||
"github.com/hajimehoshi/ebiten/ebitenutil"
|
"github.com/hajimehoshi/ebiten/ebitenutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
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 := &ebiten.DrawImageOptions{}
|
op := &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 = &ebiten.DrawImageOptions{}
|
op = &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("_resources/images/gophers.jpg", ebiten.FilterNearest)
|
gophersImage, _, err = ebitenutil.NewImageFromFile("_resources/images/gophers.jpg", 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, "Mosaic (Ebiten Demo)"); err != nil {
|
if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Mosaic (Ebiten Demo)"); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</code></pre>
|
</code></pre>
|
||||||
|
|
||||||
|
@ -27,61 +27,61 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"image"
|
"image"
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
"github.com/hajimehoshi/ebiten"
|
"github.com/hajimehoshi/ebiten"
|
||||||
"github.com/hajimehoshi/ebiten/ebitenutil"
|
"github.com/hajimehoshi/ebiten/ebitenutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
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 << 11)
|
t := r.x ^ (r.x << 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 >> 19)) ^ (t ^ (t >> 8))
|
r.w = (r.w ^ (r.w >> 19)) ^ (t ^ (t >> 8))
|
||||||
return r.w
|
return r.w
|
||||||
}
|
}
|
||||||
|
|
||||||
var randInstance = &rand{12345678, 4185243, 776511, 45411}
|
var randInstance = &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 < l; i++ {
|
for i := 0; i < l; i++ {
|
||||||
x := randInstance.next()
|
x := randInstance.next()
|
||||||
noiseImage.Pix[4*i] = uint8(x >> 24)
|
noiseImage.Pix[4*i] = uint8(x >> 24)
|
||||||
noiseImage.Pix[4*i+1] = uint8(x >> 16)
|
noiseImage.Pix[4*i+1] = uint8(x >> 16)
|
||||||
noiseImage.Pix[4*i+2] = uint8(x >> 8)
|
noiseImage.Pix[4*i+2] = uint8(x >> 8)
|
||||||
noiseImage.Pix[4*i+3] = 0xff
|
noiseImage.Pix[4*i+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("FPS: %f", ebiten.CurrentFPS())); err != nil {
|
if err := ebitenutil.DebugPrint(screen, fmt.Sprintf("FPS: %f", 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, "Noise (Ebiten Demo)"); err != nil {
|
if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Noise (Ebiten Demo)"); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</code></pre>
|
</code></pre>
|
||||||
|
|
||||||
|
@ -27,103 +27,103 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"image"
|
"image"
|
||||||
"image/color"
|
"image/color"
|
||||||
"log"
|
"log"
|
||||||
"math"
|
"math"
|
||||||
|
|
||||||
"github.com/hajimehoshi/ebiten"
|
"github.com/hajimehoshi/ebiten"
|
||||||
"github.com/hajimehoshi/ebiten/ebitenutil"
|
"github.com/hajimehoshi/ebiten/ebitenutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
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 := &ebiten.DrawImageOptions{}
|
op := &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++
|
||||||
}
|
}
|
||||||
drawn = true
|
|
||||||
}
|
|
||||||
if drawn {
|
|
||||||
count++
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := screen.DrawImage(canvasImage, nil); err != nil {
|
if err := screen.DrawImage(canvasImage, nil); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
msg := fmt.Sprintf("(%d, %d)", mx, my)
|
msg := fmt.Sprintf("(%d, %d)", mx, my)
|
||||||
for _, t := range ebiten.Touches() {
|
for _, t := range ebiten.Touches() {
|
||||||
x, y := t.Position()
|
x, y := t.Position()
|
||||||
msg += fmt.Sprintf("\n(%d, %d) touch %d", x, y, t.ID())
|
msg += fmt.Sprintf("\n(%d, %d) touch %d", 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(&image.Alpha{
|
brushImage, err = ebiten.NewImageFromImage(&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, "Paint (Ebiten Demo)"); err != nil {
|
if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Paint (Ebiten Demo)"); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</code></pre>
|
</code></pre>
|
||||||
|
|
||||||
|
@ -27,66 +27,66 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
_ "image/jpeg"
|
_ "image/jpeg"
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
"github.com/hajimehoshi/ebiten"
|
"github.com/hajimehoshi/ebiten"
|
||||||
"github.com/hajimehoshi/ebiten/ebitenutil"
|
"github.com/hajimehoshi/ebiten/ebitenutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
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 + i*3/4
|
width := w + i*3/4
|
||||||
x := ((h - i) * 3 / 4) / 2
|
x := ((h - i) * 3 / 4) / 2
|
||||||
return x, i, x + width, i + 1
|
return x, i, x + width, i + 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 + 1
|
return 0, i, w, i + 1
|
||||||
}
|
}
|
||||||
|
|
||||||
func update(screen *ebiten.Image) error {
|
func update(screen *ebiten.Image) error {
|
||||||
op := &ebiten.DrawImageOptions{
|
op := &ebiten.DrawImageOptions{
|
||||||
ImageParts: &parts{gophersImage},
|
ImageParts: &parts{gophersImage},
|
||||||
}
|
}
|
||||||
w, h := gophersImage.Size()
|
w, h := gophersImage.Size()
|
||||||
maxWidth := float64(w) + float64(h)*0.75
|
maxWidth := float64(w) + 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("_resources/images/gophers.jpg", ebiten.FilterNearest)
|
gophersImage, _, err = ebitenutil.NewImageFromFile("_resources/images/gophers.jpg", ebiten.FilterNearest)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Perspective (Ebiten Demo)"); err != nil {
|
if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Perspective (Ebiten Demo)"); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</code></pre>
|
</code></pre>
|
||||||
|
|
||||||
|
@ -27,31 +27,31 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"image/color"
|
"image/color"
|
||||||
"log"
|
"log"
|
||||||
"math"
|
"math"
|
||||||
|
|
||||||
"github.com/hajimehoshi/ebiten"
|
"github.com/hajimehoshi/ebiten"
|
||||||
"github.com/hajimehoshi/ebiten/audio"
|
"github.com/hajimehoshi/ebiten/audio"
|
||||||
"github.com/hajimehoshi/ebiten/ebitenutil"
|
"github.com/hajimehoshi/ebiten/ebitenutil"
|
||||||
"github.com/hajimehoshi/ebiten/examples/common"
|
"github.com/hajimehoshi/ebiten/examples/common"
|
||||||
)
|
)
|
||||||
|
|
||||||
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 < len(pcm); i++ {
|
for i := 0; i < len(pcm); i++ {
|
||||||
v := 0.0
|
v := 0.0
|
||||||
twoPiF := 2.0 * math.Pi * baseFreq
|
twoPiF := 2.0 * math.Pi * baseFreq
|
||||||
for j := 0; j < len(amp); j++ {
|
for j := 0; j < len(amp); j++ {
|
||||||
a := amp[j] * math.Exp(-5*float64(i)/(x[j]*s))
|
a := amp[j] * math.Exp(-5*float64(i)/(x[j]*s))
|
||||||
v += a * math.Sin(float64(i)*twoPiF*float64(j+1)/s)
|
v += a * math.Sin(float64(i)*twoPiF*float64(j+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("len(l) must equal to len(r)")
|
panic("len(l) must equal to len(r)")
|
||||||
}
|
}
|
||||||
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+1] = byte(l[i] >> 8)
|
b[4*i+1] = byte(l[i] >> 8)
|
||||||
b[4*i+2] = byte(r[i])
|
b[4*i+2] = byte(r[i])
|
||||||
b[4*i+3] = byte(r[i] >> 8)
|
b[4*i+3] = byte(r[i] >> 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 < len(l); i++ {
|
||||||
|
p := pcm[j]
|
||||||
|
l[i] = int16(p * vol * math.MaxInt16)
|
||||||
|
r[i] = l[i]
|
||||||
|
jj += 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 < len(l); i++ {
|
|
||||||
p := pcm[j]
|
|
||||||
l[i] = int16(p * vol * math.MaxInt16)
|
|
||||||
r[i] = l[i]
|
|
||||||
jj += 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]++
|
||||||
}
|
}
|
||||||
keyStates[key]++
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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{"A", "S", "D", "F", "G", "H", "J", "K", "L"}
|
|
||||||
width := 24
|
|
||||||
y := 48
|
|
||||||
for i, k := range whiteKeys {
|
|
||||||
x := i*width + 36
|
|
||||||
height := 112
|
|
||||||
op := &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+8, y+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{"A", "S", "D", "F", "G", "H", "J", "K", "L"}
|
||||||
|
width := 24
|
||||||
|
y := 48
|
||||||
|
for i, k := range whiteKeys {
|
||||||
|
x := i*width + 36
|
||||||
|
height := 112
|
||||||
|
op := &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+8, y+height-16, 1, color.Black); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
blackKeys := []string{"Q", "W", "", "R", "T", "", "U", "I", "O"}
|
blackKeys := []string{"Q", "W", "", "R", "T", "", "U", "I", "O"}
|
||||||
for i, k := range blackKeys {
|
for i, k := range blackKeys {
|
||||||
if k == "" {
|
if k == "" {
|
||||||
continue
|
continue
|
||||||
|
}
|
||||||
|
x := i*width + 24
|
||||||
|
height := 64
|
||||||
|
op := &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+8, y+height-16, 1, color.White); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
x := i*width + 24
|
|
||||||
height := 64
|
|
||||||
op := &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+8, y+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("FPS: %0.2f", 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("FPS: %0.2f", 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, "Piano (Ebiten Demo)"); err != nil {
|
if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Piano (Ebiten Demo)"); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</code></pre>
|
</code></pre>
|
||||||
|
|
||||||
|
@ -27,46 +27,46 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
_ "image/jpeg"
|
_ "image/jpeg"
|
||||||
"log"
|
"log"
|
||||||
"math"
|
"math"
|
||||||
|
|
||||||
"github.com/hajimehoshi/ebiten"
|
"github.com/hajimehoshi/ebiten"
|
||||||
"github.com/hajimehoshi/ebiten/ebitenutil"
|
"github.com/hajimehoshi/ebiten/ebitenutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
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++
|
count++
|
||||||
w, h := gophersImage.Size()
|
w, h := gophersImage.Size()
|
||||||
op := &ebiten.DrawImageOptions{}
|
op := &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("_resources/images/gophers.jpg", ebiten.FilterNearest)
|
gophersImage, _, err = ebitenutil.NewImageFromFile("_resources/images/gophers.jpg", ebiten.FilterNearest)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Rotate (Ebiten Demo)"); err != nil {
|
if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Rotate (Ebiten Demo)"); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</code></pre>
|
</code></pre>
|
||||||
|
|
||||||
|
@ -27,149 +27,149 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
_ "image/png"
|
_ "image/png"
|
||||||
"log"
|
"log"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
|
|
||||||
"github.com/hajimehoshi/ebiten"
|
"github.com/hajimehoshi/ebiten"
|
||||||
"github.com/hajimehoshi/ebiten/ebitenutil"
|
"github.com/hajimehoshi/ebiten/ebitenutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
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 += s.vx
|
s.x += s.vx
|
||||||
s.y += s.vy
|
s.y += s.vy
|
||||||
if s.x < 0 {
|
if s.x < 0 {
|
||||||
s.x = -s.x
|
s.x = -s.x
|
||||||
s.vx = -s.vx
|
s.vx = -s.vx
|
||||||
}
|
}
|
||||||
if s.y < 0 {
|
if s.y < 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 <= s.x+w {
|
if screenWidth <= s.x+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 <= s.y+h {
|
if screenHeight <= s.y+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 <= i {
|
if s.num <= 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 + ebitenImageWidth, ss.y + ebitenImageHeight
|
return ss.x, ss.y, ss.x + ebitenImageWidth, ss.y + 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 <= i {
|
if s.num <= 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 = &Sprites{make([]*Sprite, MaxSprites), 500}
|
var sprites = &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 < MinSprites {
|
if sprites.num < MinSprites {
|
||||||
sprites.num = MinSprites
|
sprites.num = MinSprites
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
if ebiten.IsKeyPressed(ebiten.KeyRight) {
|
||||||
if ebiten.IsKeyPressed(ebiten.KeyRight) {
|
sprites.num += 20
|
||||||
sprites.num += 20
|
if MaxSprites < sprites.num {
|
||||||
if MaxSprites < sprites.num {
|
sprites.num = MaxSprites
|
||||||
sprites.num = MaxSprites
|
}
|
||||||
}
|
}
|
||||||
}
|
sprites.Update()
|
||||||
sprites.Update()
|
|
||||||
|
|
||||||
if ebiten.IsRunningSlowly() {
|
if ebiten.IsRunningSlowly() {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
op := &ebiten.DrawImageOptions{
|
op := &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 <- or -> to change the number of sprites`, ebiten.CurrentFPS(), sprites.Len())
|
Press <- or -> 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("_resources/images/ebiten.png", ebiten.FilterNearest)
|
ebitenImage, _, err = ebitenutil.NewImageFromFile("_resources/images/ebiten.png", 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] = &Sprite{
|
sprites.sprites[i] = &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, "Sprites (Ebiten Demo)"); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Sprites (Ebiten Demo)"); err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
</code></pre>
|
</code></pre>
|
||||||
|
|
||||||
|
@ -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>
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user