mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-02-02 22:14:29 +01:00
doc: Add and remove examples
This commit is contained in:
parent
4c2048d77d
commit
e6fbbc9ea9
@ -167,13 +167,16 @@ func versions() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var examples = []example{
|
var examples = []example{
|
||||||
|
{Name: "alphablending"},
|
||||||
{Name: "hue"},
|
{Name: "hue"},
|
||||||
|
{Name: "gamepad"},
|
||||||
{Name: "keyboard"},
|
{Name: "keyboard"},
|
||||||
{Name: "mosaic"},
|
{Name: "mosaic"},
|
||||||
|
{Name: "noise"},
|
||||||
{Name: "paint"},
|
{Name: "paint"},
|
||||||
{Name: "perspective"},
|
{Name: "perspective"},
|
||||||
{Name: "piano"},
|
|
||||||
{Name: "rotate"},
|
{Name: "rotate"},
|
||||||
|
{Name: "sprites"},
|
||||||
{Name: "blocks"},
|
{Name: "blocks"},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
39
_docs/public/examples/alphablending.content.html
Normal file
39
_docs/public/examples/alphablending.content.html
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<!--
|
||||||
|
Copyright 2014 Hajime Hoshi
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
-->
|
||||||
|
<script>
|
||||||
|
'use strict';
|
||||||
|
window.addEventListener('load', function() {
|
||||||
|
function isProduction() {
|
||||||
|
var l = window.top.location;
|
||||||
|
if (l.hash === '#_production') {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (l.hostname === 'localhost' || l.hostname === '127.0.0.1') {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
var s = document.createElement('script');
|
||||||
|
var src = 'alphablending.js';
|
||||||
|
if (isProduction()) {
|
||||||
|
src = '//hajimehoshi.github.io/ebiten.pagestorage/master/' + src;
|
||||||
|
}
|
||||||
|
s.src = src;
|
||||||
|
document.body.appendChild(s);
|
||||||
|
});
|
||||||
|
</script>
|
116
_docs/public/examples/alphablending.html
Normal file
116
_docs/public/examples/alphablending.html
Normal file
@ -0,0 +1,116 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<!--
|
||||||
|
Copyright 2014 Hajime Hoshi
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
-->
|
||||||
|
<link rel="shortcut icon" href="../favicon.png" type="image/png" >
|
||||||
|
<link rel="icon" href="../favicon.png" type="image/png" >
|
||||||
|
<title>Ebiten example - alphablending</title>
|
||||||
|
<style>
|
||||||
|
body {
|
||||||
|
font-family: sans-serif;
|
||||||
|
}
|
||||||
|
iframe {
|
||||||
|
border-color: #999;
|
||||||
|
border-style: solid;
|
||||||
|
border-width: 1px;
|
||||||
|
overflow: hidden;
|
||||||
|
}
|
||||||
|
pre {
|
||||||
|
background: #eee;
|
||||||
|
padding: 1em;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
<nav><a href="..">Ebiten</a></nav>
|
||||||
|
|
||||||
|
<h1>Ebiten example - alphablending</h1>
|
||||||
|
<iframe src="alphablending.content.html" width="640" height="480"></iframe>
|
||||||
|
<pre><code>package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/hajimehoshi/ebiten"
|
||||||
|
"github.com/hajimehoshi/ebiten/ebitenutil"
|
||||||
|
"image/color"
|
||||||
|
_ "image/png"
|
||||||
|
"log"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
screenWidth = 320
|
||||||
|
screenHeight = 240
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
count int
|
||||||
|
tmpRenderTarget *ebiten.Image
|
||||||
|
ebitenImage *ebiten.Image
|
||||||
|
saved bool
|
||||||
|
)
|
||||||
|
|
||||||
|
func update(screen *ebiten.Image) error {
|
||||||
|
count++
|
||||||
|
count %= 600
|
||||||
|
diff := float64(count) * 0.2
|
||||||
|
switch {
|
||||||
|
case 480 < count:
|
||||||
|
diff = 0
|
||||||
|
case 240 < count:
|
||||||
|
diff = float64(480-count) * 0.2
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := tmpRenderTarget.Clear(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
op := &ebiten.DrawImageOptions{}
|
||||||
|
op.GeoM.Translate(15+float64(i)*diff, 20)
|
||||||
|
op.ColorM.Scale(1.0, 1.0, 1.0, 0.5)
|
||||||
|
if err := tmpRenderTarget.DrawImage(ebitenImage, op); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
screen.Fill(color.NRGBA{0x00, 0x00, 0x80, 0xff})
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
op := &ebiten.DrawImageOptions{}
|
||||||
|
op.GeoM.Translate(0, float64(i)*diff)
|
||||||
|
if err := screen.DrawImage(tmpRenderTarget, op); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var err error
|
||||||
|
ebitenImage, _, err = ebitenutil.NewImageFromFile("images/ebiten.png", ebiten.FilterNearest)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
tmpRenderTarget, err = ebiten.NewImage(screenWidth, screenHeight, ebiten.FilterNearest)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
//update := update
|
||||||
|
//f, _ := os.Create("out.gif")
|
||||||
|
//update = ebitenutil.RecordScreenAsGIF(update, f, 100)
|
||||||
|
if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Alpha Blending (Ebiten Demo)"); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</code></pre>
|
||||||
|
|
||||||
|
|
||||||
|
<footer>© 2014 Hajime Hoshi</footer>
|
39
_docs/public/examples/gamepad.content.html
Normal file
39
_docs/public/examples/gamepad.content.html
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<!--
|
||||||
|
Copyright 2014 Hajime Hoshi
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
-->
|
||||||
|
<script>
|
||||||
|
'use strict';
|
||||||
|
window.addEventListener('load', function() {
|
||||||
|
function isProduction() {
|
||||||
|
var l = window.top.location;
|
||||||
|
if (l.hash === '#_production') {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (l.hostname === 'localhost' || l.hostname === '127.0.0.1') {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
var s = document.createElement('script');
|
||||||
|
var src = 'gamepad.js';
|
||||||
|
if (isProduction()) {
|
||||||
|
src = '//hajimehoshi.github.io/ebiten.pagestorage/master/' + src;
|
||||||
|
}
|
||||||
|
s.src = src;
|
||||||
|
document.body.appendChild(s);
|
||||||
|
});
|
||||||
|
</script>
|
92
_docs/public/examples/gamepad.html
Normal file
92
_docs/public/examples/gamepad.html
Normal file
@ -0,0 +1,92 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<!--
|
||||||
|
Copyright 2014 Hajime Hoshi
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
-->
|
||||||
|
<link rel="shortcut icon" href="../favicon.png" type="image/png" >
|
||||||
|
<link rel="icon" href="../favicon.png" type="image/png" >
|
||||||
|
<title>Ebiten example - gamepad</title>
|
||||||
|
<style>
|
||||||
|
body {
|
||||||
|
font-family: sans-serif;
|
||||||
|
}
|
||||||
|
iframe {
|
||||||
|
border-color: #999;
|
||||||
|
border-style: solid;
|
||||||
|
border-width: 1px;
|
||||||
|
overflow: hidden;
|
||||||
|
}
|
||||||
|
pre {
|
||||||
|
background: #eee;
|
||||||
|
padding: 1em;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
<nav><a href="..">Ebiten</a></nav>
|
||||||
|
|
||||||
|
<h1>Ebiten example - gamepad</h1>
|
||||||
|
<iframe src="gamepad.content.html" width="640" height="480"></iframe>
|
||||||
|
<pre><code>package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/hajimehoshi/ebiten"
|
||||||
|
"github.com/hajimehoshi/ebiten/ebitenutil"
|
||||||
|
"log"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
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{}
|
||||||
|
|
||||||
|
maxAxis := ebiten.GamepadAxisNum(gamepadID)
|
||||||
|
for a := 0; a < maxAxis; a++ {
|
||||||
|
v := ebiten.GamepadAxis(gamepadID, a)
|
||||||
|
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
|
||||||
|
Axes:
|
||||||
|
{{.Axes}}
|
||||||
|
Pressed Buttons: {{.Buttons}}`
|
||||||
|
str = strings.Replace(str, "{{.Axes}}", strings.Join(axes, "\n "), -1)
|
||||||
|
str = strings.Replace(str, "{{.Buttons}}", strings.Join(pressedButtons, ", "), -1)
|
||||||
|
ebitenutil.DebugPrint(screen, str)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Gamepad (Ebiten Demo)"); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</code></pre>
|
||||||
|
|
||||||
|
|
||||||
|
<footer>© 2014 Hajime Hoshi</footer>
|
@ -29,7 +29,7 @@ window.addEventListener('load', function() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var s = document.createElement('script');
|
var s = document.createElement('script');
|
||||||
var src = 'piano.js';
|
var src = 'noise.js';
|
||||||
if (isProduction()) {
|
if (isProduction()) {
|
||||||
src = '//hajimehoshi.github.io/ebiten.pagestorage/master/' + src;
|
src = '//hajimehoshi.github.io/ebiten.pagestorage/master/' + src;
|
||||||
}
|
}
|
97
_docs/public/examples/noise.html
Normal file
97
_docs/public/examples/noise.html
Normal file
@ -0,0 +1,97 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<!--
|
||||||
|
Copyright 2014 Hajime Hoshi
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
-->
|
||||||
|
<link rel="shortcut icon" href="../favicon.png" type="image/png" >
|
||||||
|
<link rel="icon" href="../favicon.png" type="image/png" >
|
||||||
|
<title>Ebiten example - noise</title>
|
||||||
|
<style>
|
||||||
|
body {
|
||||||
|
font-family: sans-serif;
|
||||||
|
}
|
||||||
|
iframe {
|
||||||
|
border-color: #999;
|
||||||
|
border-style: solid;
|
||||||
|
border-width: 1px;
|
||||||
|
overflow: hidden;
|
||||||
|
}
|
||||||
|
pre {
|
||||||
|
background: #eee;
|
||||||
|
padding: 1em;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
<nav><a href="..">Ebiten</a></nav>
|
||||||
|
|
||||||
|
<h1>Ebiten example - noise</h1>
|
||||||
|
<iframe src="noise.content.html" width="640" height="480"></iframe>
|
||||||
|
<pre><code>package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/hajimehoshi/ebiten"
|
||||||
|
"github.com/hajimehoshi/ebiten/ebitenutil"
|
||||||
|
"image"
|
||||||
|
"log"
|
||||||
|
//"math/rand"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
screenWidth = 320
|
||||||
|
screenHeight = 240
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
noiseImage *image.RGBA
|
||||||
|
)
|
||||||
|
|
||||||
|
type rand struct {
|
||||||
|
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 << 11)
|
||||||
|
r.x, r.y, r.z = r.y, r.z, r.w
|
||||||
|
r.w = (r.w ^ (r.w >> 19)) ^ (t ^ (t >> 8))
|
||||||
|
return r.w
|
||||||
|
}
|
||||||
|
|
||||||
|
var randInstance = &rand{12345678, 4185243, 776511, 45411}
|
||||||
|
|
||||||
|
func update(screen *ebiten.Image) error {
|
||||||
|
const l = screenWidth * screenHeight
|
||||||
|
for i := 0; i < l; i++ {
|
||||||
|
x := randInstance.next()
|
||||||
|
noiseImage.Pix[4*i] = uint8(x >> 24)
|
||||||
|
noiseImage.Pix[4*i+1] = uint8(x >> 16)
|
||||||
|
noiseImage.Pix[4*i+2] = uint8(x >> 8)
|
||||||
|
noiseImage.Pix[4*i+3] = 0xff
|
||||||
|
}
|
||||||
|
screen.ReplacePixels(noiseImage.Pix)
|
||||||
|
ebitenutil.DebugPrint(screen, fmt.Sprintf("FPS: %f", ebiten.CurrentFPS()))
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
noiseImage = image.NewRGBA(image.Rect(0, 0, screenWidth, screenHeight))
|
||||||
|
if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Noise (Ebiten Demo)"); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</code></pre>
|
||||||
|
|
||||||
|
|
||||||
|
<footer>© 2014 Hajime Hoshi</footer>
|
@ -1,253 +0,0 @@
|
|||||||
<!DOCTYPE html>
|
|
||||||
<!--
|
|
||||||
Copyright 2014 Hajime Hoshi
|
|
||||||
|
|
||||||
Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
you may not use this file except in compliance with the License.
|
|
||||||
You may obtain a copy of the License at
|
|
||||||
|
|
||||||
http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
|
|
||||||
Unless required by applicable law or agreed to in writing, software
|
|
||||||
distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
See the License for the specific language governing permissions and
|
|
||||||
limitations under the License.
|
|
||||||
-->
|
|
||||||
<link rel="shortcut icon" href="../favicon.png" type="image/png" >
|
|
||||||
<link rel="icon" href="../favicon.png" type="image/png" >
|
|
||||||
<title>Ebiten example - piano</title>
|
|
||||||
<style>
|
|
||||||
body {
|
|
||||||
font-family: sans-serif;
|
|
||||||
}
|
|
||||||
iframe {
|
|
||||||
border-color: #999;
|
|
||||||
border-style: solid;
|
|
||||||
border-width: 1px;
|
|
||||||
overflow: hidden;
|
|
||||||
}
|
|
||||||
pre {
|
|
||||||
background: #eee;
|
|
||||||
padding: 1em;
|
|
||||||
}
|
|
||||||
</style>
|
|
||||||
<nav><a href="..">Ebiten</a></nav>
|
|
||||||
|
|
||||||
<h1>Ebiten example - piano</h1>
|
|
||||||
<iframe src="piano.content.html" width="640" height="480"></iframe>
|
|
||||||
<pre><code>package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"fmt"
|
|
||||||
"image/color"
|
|
||||||
"log"
|
|
||||||
"math"
|
|
||||||
|
|
||||||
"github.com/hajimehoshi/ebiten"
|
|
||||||
"github.com/hajimehoshi/ebiten/ebitenutil"
|
|
||||||
"github.com/hajimehoshi/ebiten/examples/common"
|
|
||||||
"github.com/hajimehoshi/ebiten/exp/audio"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
screenWidth = 320
|
|
||||||
screenHeight = 240
|
|
||||||
sampleRate = 44100
|
|
||||||
)
|
|
||||||
|
|
||||||
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 < len(pcm); i++ {
|
|
||||||
v := 0.0
|
|
||||||
twoPiF := 2.0 * math.Pi * baseFreq
|
|
||||||
for j := 0; j < len(amp); j++ {
|
|
||||||
a := amp[j] * math.Exp(-5*float64(i)/(x[j]*s))
|
|
||||||
v += a * math.Sin(float64(i)*twoPiF*float64(j+1)/s)
|
|
||||||
}
|
|
||||||
pcm[i] = v / 5.0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var (
|
|
||||||
noteCache = map[int][]byte{}
|
|
||||||
)
|
|
||||||
|
|
||||||
func toBytes(l, r []int16) []byte {
|
|
||||||
if len(l) != len(r) {
|
|
||||||
panic("len(l) must equal to len(r)")
|
|
||||||
}
|
|
||||||
b := make([]byte, len(l)*4)
|
|
||||||
for i, _ := range l {
|
|
||||||
b[4*i] = byte(l[i])
|
|
||||||
b[4*i+1] = byte(l[i] >> 8)
|
|
||||||
b[4*i+2] = byte(r[i])
|
|
||||||
b[4*i+3] = byte(r[i] >> 8)
|
|
||||||
}
|
|
||||||
return b
|
|
||||||
}
|
|
||||||
|
|
||||||
type stream struct {
|
|
||||||
*bytes.Reader
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *stream) Close() error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func addNote(freq float64, vol float64) error {
|
|
||||||
f := int(freq)
|
|
||||||
if n, ok := noteCache[f]; ok {
|
|
||||||
p, err := audio.NewPlayer(&stream{bytes.NewReader(n)}, sampleRate)
|
|
||||||
if err == audio.ErrTooManyPlayers {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
p.Play()
|
|
||||||
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.NewPlayer(&stream{bytes.NewReader(n)}, sampleRate)
|
|
||||||
if err == audio.ErrTooManyPlayers {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
p.Play()
|
|
||||||
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,
|
|
||||||
}
|
|
||||||
|
|
||||||
var keyStates = map[ebiten.Key]int{}
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
for _, key := range keys {
|
|
||||||
keyStates[key] = 0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func updateInput() {
|
|
||||||
for _, key := range keys {
|
|
||||||
if !ebiten.IsKeyPressed(key) {
|
|
||||||
keyStates[key] = 0
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
keyStates[key]++
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var (
|
|
||||||
imagePiano *ebiten.Image
|
|
||||||
)
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
var err error
|
|
||||||
imageEmpty, err := ebiten.NewImage(16, 16, ebiten.FilterNearest)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
imageEmpty.Fill(color.White)
|
|
||||||
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)
|
|
||||||
imagePiano.DrawImage(imageEmpty, op)
|
|
||||||
common.ArcadeFont.DrawText(imagePiano, k, x+8, y+height-16, 1, color.Black)
|
|
||||||
}
|
|
||||||
|
|
||||||
blackKeys := []string{"Q", "W", "", "R", "T", "", "U", "I", "O"}
|
|
||||||
for i, k := range blackKeys {
|
|
||||||
if k == "" {
|
|
||||||
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)
|
|
||||||
imagePiano.DrawImage(imageEmpty, op)
|
|
||||||
common.ArcadeFont.DrawText(imagePiano, k, x+8, y+height-16, 1, color.White)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func update(screen *ebiten.Image) error {
|
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
screen.Fill(color.RGBA{0x80, 0x80, 0xc0, 0xff})
|
|
||||||
screen.DrawImage(imagePiano, nil)
|
|
||||||
|
|
||||||
ebitenutil.DebugPrint(screen, fmt.Sprintf("FPS: %0.2f", ebiten.CurrentFPS()))
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Piano (Ebiten Demo)"); err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
</code></pre>
|
|
||||||
|
|
||||||
|
|
||||||
<footer>© 2014 Hajime Hoshi</footer>
|
|
39
_docs/public/examples/sprites.content.html
Normal file
39
_docs/public/examples/sprites.content.html
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<!--
|
||||||
|
Copyright 2014 Hajime Hoshi
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
-->
|
||||||
|
<script>
|
||||||
|
'use strict';
|
||||||
|
window.addEventListener('load', function() {
|
||||||
|
function isProduction() {
|
||||||
|
var l = window.top.location;
|
||||||
|
if (l.hash === '#_production') {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (l.hostname === 'localhost' || l.hostname === '127.0.0.1') {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
var s = document.createElement('script');
|
||||||
|
var src = 'sprites.js';
|
||||||
|
if (isProduction()) {
|
||||||
|
src = '//hajimehoshi.github.io/ebiten.pagestorage/master/' + src;
|
||||||
|
}
|
||||||
|
s.src = src;
|
||||||
|
document.body.appendChild(s);
|
||||||
|
});
|
||||||
|
</script>
|
183
_docs/public/examples/sprites.html
Normal file
183
_docs/public/examples/sprites.html
Normal file
@ -0,0 +1,183 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<!--
|
||||||
|
Copyright 2014 Hajime Hoshi
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
-->
|
||||||
|
<link rel="shortcut icon" href="../favicon.png" type="image/png" >
|
||||||
|
<link rel="icon" href="../favicon.png" type="image/png" >
|
||||||
|
<title>Ebiten example - sprites</title>
|
||||||
|
<style>
|
||||||
|
body {
|
||||||
|
font-family: sans-serif;
|
||||||
|
}
|
||||||
|
iframe {
|
||||||
|
border-color: #999;
|
||||||
|
border-style: solid;
|
||||||
|
border-width: 1px;
|
||||||
|
overflow: hidden;
|
||||||
|
}
|
||||||
|
pre {
|
||||||
|
background: #eee;
|
||||||
|
padding: 1em;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
<nav><a href="..">Ebiten</a></nav>
|
||||||
|
|
||||||
|
<h1>Ebiten example - sprites</h1>
|
||||||
|
<iframe src="sprites.content.html" width="640" height="480"></iframe>
|
||||||
|
<pre><code>package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/hajimehoshi/ebiten"
|
||||||
|
"github.com/hajimehoshi/ebiten/ebitenutil"
|
||||||
|
_ "image/png"
|
||||||
|
"log"
|
||||||
|
"math/rand"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
screenWidth = 320
|
||||||
|
screenHeight = 240
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ebitenImage *ebiten.Image
|
||||||
|
ebitenImageWidth = 0
|
||||||
|
ebitenImageHeight = 0
|
||||||
|
)
|
||||||
|
|
||||||
|
type Sprite struct {
|
||||||
|
image *ebiten.Image
|
||||||
|
x int
|
||||||
|
y int
|
||||||
|
vx int
|
||||||
|
vy int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Sprite) Update() {
|
||||||
|
s.x += s.vx
|
||||||
|
s.y += s.vy
|
||||||
|
if s.x < 0 {
|
||||||
|
s.x = -s.x
|
||||||
|
s.vx = -s.vx
|
||||||
|
}
|
||||||
|
if s.y < 0 {
|
||||||
|
s.y = -s.y
|
||||||
|
s.vy = -s.vy
|
||||||
|
}
|
||||||
|
w, h := s.image.Size()
|
||||||
|
if screenWidth <= s.x+w {
|
||||||
|
s.x = 2*(screenWidth-w) - s.x
|
||||||
|
s.vx = -s.vx
|
||||||
|
}
|
||||||
|
if screenHeight <= s.y+h {
|
||||||
|
s.y = 2*(screenHeight-h) - s.y
|
||||||
|
s.vy = -s.vy
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type Sprites struct {
|
||||||
|
sprites []*Sprite
|
||||||
|
num int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s Sprites) Update() {
|
||||||
|
for _, sprite := range s.sprites {
|
||||||
|
sprite.Update()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s Sprites) Len() int {
|
||||||
|
return s.num
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s Sprites) Dst(i int) (x0, y0, x1, y1 int) {
|
||||||
|
if s.num <= i {
|
||||||
|
return 0, 0, 0, 0
|
||||||
|
}
|
||||||
|
ss := s.sprites[i]
|
||||||
|
return ss.x, ss.y, ss.x + ebitenImageWidth, ss.y + ebitenImageHeight
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s Sprites) Src(i int) (x0, y0, x1, y1 int) {
|
||||||
|
if s.num <= i {
|
||||||
|
return 0, 0, 0, 0
|
||||||
|
}
|
||||||
|
return 0, 0, ebitenImageWidth, ebitenImageHeight
|
||||||
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
MinSprites = 0
|
||||||
|
MaxSprites = 10000
|
||||||
|
)
|
||||||
|
|
||||||
|
var sprites = &Sprites{make([]*Sprite, MaxSprites), 500}
|
||||||
|
|
||||||
|
func update(screen *ebiten.Image) error {
|
||||||
|
if ebiten.IsKeyPressed(ebiten.KeyLeft) {
|
||||||
|
sprites.num -= 20
|
||||||
|
if sprites.num < MinSprites {
|
||||||
|
sprites.num = MinSprites
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ebiten.IsKeyPressed(ebiten.KeyRight) {
|
||||||
|
sprites.num += 20
|
||||||
|
if MaxSprites < sprites.num {
|
||||||
|
sprites.num = MaxSprites
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sprites.Update()
|
||||||
|
|
||||||
|
op := &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 <- or -> to change the number of sprites`, ebiten.CurrentFPS(), sprites.Len())
|
||||||
|
ebitenutil.DebugPrint(screen, msg)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var err error
|
||||||
|
ebitenImage, _, err = ebitenutil.NewImageFromFile("images/ebiten.png", 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] = &Sprite{
|
||||||
|
image: ebitenImage,
|
||||||
|
x: x,
|
||||||
|
y: y,
|
||||||
|
vx: vx,
|
||||||
|
vy: vy,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Sprites (Ebiten Demo)"); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</code></pre>
|
||||||
|
|
||||||
|
|
||||||
|
<footer>© 2014 Hajime Hoshi</footer>
|
@ -62,20 +62,26 @@ pre {
|
|||||||
<h2>Example</h2>
|
<h2>Example</h2>
|
||||||
<p>
|
<p>
|
||||||
|
|
||||||
|
<a href="examples/alphablending.html"><img src="images/examples/alphablending.png" width="320" height="240" alt="Ebiten example: alphablending" class="example"></a>
|
||||||
|
|
||||||
<a href="examples/hue.html"><img src="images/examples/hue.png" width="320" height="240" alt="Ebiten example: hue" class="example"></a>
|
<a href="examples/hue.html"><img src="images/examples/hue.png" width="320" height="240" alt="Ebiten example: hue" class="example"></a>
|
||||||
|
|
||||||
|
<a href="examples/gamepad.html"><img src="images/examples/gamepad.png" width="320" height="240" alt="Ebiten example: gamepad" class="example"></a>
|
||||||
|
|
||||||
<a href="examples/keyboard.html"><img src="images/examples/keyboard.png" width="320" height="240" alt="Ebiten example: keyboard" class="example"></a>
|
<a href="examples/keyboard.html"><img src="images/examples/keyboard.png" width="320" height="240" alt="Ebiten example: keyboard" class="example"></a>
|
||||||
|
|
||||||
<a href="examples/mosaic.html"><img src="images/examples/mosaic.png" width="320" height="240" alt="Ebiten example: mosaic" class="example"></a>
|
<a href="examples/mosaic.html"><img src="images/examples/mosaic.png" width="320" height="240" alt="Ebiten example: mosaic" class="example"></a>
|
||||||
|
|
||||||
|
<a href="examples/noise.html"><img src="images/examples/noise.png" width="320" height="240" alt="Ebiten example: noise" class="example"></a>
|
||||||
|
|
||||||
<a href="examples/paint.html"><img src="images/examples/paint.png" width="320" height="240" alt="Ebiten example: paint" class="example"></a>
|
<a href="examples/paint.html"><img src="images/examples/paint.png" width="320" height="240" alt="Ebiten example: paint" class="example"></a>
|
||||||
|
|
||||||
<a href="examples/perspective.html"><img src="images/examples/perspective.png" width="320" height="240" alt="Ebiten example: perspective" class="example"></a>
|
<a href="examples/perspective.html"><img src="images/examples/perspective.png" width="320" height="240" alt="Ebiten example: perspective" class="example"></a>
|
||||||
|
|
||||||
<a href="examples/piano.html"><img src="images/examples/piano.png" width="320" height="240" alt="Ebiten example: piano" class="example"></a>
|
|
||||||
|
|
||||||
<a href="examples/rotate.html"><img src="images/examples/rotate.png" width="320" height="240" alt="Ebiten example: rotate" class="example"></a>
|
<a href="examples/rotate.html"><img src="images/examples/rotate.png" width="320" height="240" alt="Ebiten example: rotate" class="example"></a>
|
||||||
|
|
||||||
|
<a href="examples/sprites.html"><img src="images/examples/sprites.png" width="320" height="240" alt="Ebiten example: sprites" class="example"></a>
|
||||||
|
|
||||||
<a href="examples/blocks.html"><img src="images/examples/blocks.png" width="256" height="240" alt="Ebiten example: blocks" class="example"></a>
|
<a href="examples/blocks.html"><img src="images/examples/blocks.png" width="256" height="240" alt="Ebiten example: blocks" class="example"></a>
|
||||||
|
|
||||||
</p>
|
</p>
|
||||||
|
Loading…
Reference in New Issue
Block a user