From bba20e8b8c65f1c9cb5b8fa17c4091d5f9b7c986 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sun, 28 Jan 2018 22:39:54 +0900 Subject: [PATCH] examples: Add comments --- examples/highdpi/main.go | 2 +- examples/hsv/main.go | 41 ++++++++++++++++++++++++---------------- examples/hue/main.go | 6 ++++++ examples/life/main.go | 19 +++++++++---------- 4 files changed, 41 insertions(+), 27 deletions(-) diff --git a/examples/highdpi/main.go b/examples/highdpi/main.go index ba9e3ba06..22321e4e7 100644 --- a/examples/highdpi/main.go +++ b/examples/highdpi/main.go @@ -33,7 +33,7 @@ var ( ) func init() { - // licensed under Public Domain + // Licensed under Public Domain // https://commons.wikimedia.org/wiki/File:As08-16-2593.jpg const url = "https://upload.wikimedia.org/wikipedia/commons/1/1f/As08-16-2593.jpg" diff --git a/examples/hsv/main.go b/examples/hsv/main.go index 7c1f57547..a3e759ba7 100644 --- a/examples/hsv/main.go +++ b/examples/hsv/main.go @@ -32,15 +32,17 @@ const ( ) var ( - hueInt = 0 - saturationInt = 128 - valueInt = 128 - inverted = false + hue128 = 0 + saturation128 = 128 + value128 = 128 + + inverted = false prevPressedI = false gophersImage *ebiten.Image ) +// clamp clamps v to the range [min, max]. func clamp(v, min, max int) int { if min > max { panic("min must <= max") @@ -55,25 +57,30 @@ func clamp(v, min, max int) int { } func update(screen *ebiten.Image) error { + // Adjust HSV values along with the user's input. if ebiten.IsKeyPressed(ebiten.KeyQ) { - hueInt-- + hue128-- } if ebiten.IsKeyPressed(ebiten.KeyW) { - hueInt++ + hue128++ } if ebiten.IsKeyPressed(ebiten.KeyA) { - saturationInt-- + saturation128-- } if ebiten.IsKeyPressed(ebiten.KeyS) { - saturationInt++ + saturation128++ } if ebiten.IsKeyPressed(ebiten.KeyZ) { - valueInt-- + value128-- } if ebiten.IsKeyPressed(ebiten.KeyX) { - valueInt++ + value128++ } + hue128 = clamp(hue128, -256, 256) + saturation128 = clamp(saturation128, 0, 256) + value128 = clamp(value128, 0, 256) + pressedI := ebiten.IsKeyPressed(ebiten.KeyI) if pressedI && !prevPressedI { inverted = !inverted @@ -83,18 +90,19 @@ func update(screen *ebiten.Image) error { if ebiten.IsRunningSlowly() { return nil } - hueInt = clamp(hueInt, -256, 256) - saturationInt = clamp(saturationInt, 0, 256) - valueInt = clamp(valueInt, 0, 256) + // Center the image on the screen. w, h := gophersImage.Size() op := &ebiten.DrawImageOptions{} op.GeoM.Translate(float64(screenWidth-w)/2, float64(screenHeight-h)/2) - hue := float64(hueInt) * 2 * math.Pi / 128 - saturation := float64(saturationInt) / 128 - value := float64(valueInt) / 128 + // Change HSV. + hue := float64(hue128) * 2 * math.Pi / 128 + saturation := float64(saturation128) / 128 + value := float64(value128) / 128 op.ColorM.ChangeHSV(hue, saturation, value) + + // Invert the color. if inverted { op.ColorM.Scale(-1, -1, -1, 1) op.ColorM.Translate(1, 1, 1, 0) @@ -102,6 +110,7 @@ func update(screen *ebiten.Image) error { screen.DrawImage(gophersImage, op) + // Draw the text of the current status. msgInverted := "false" if inverted { msgInverted = "true" diff --git a/examples/hue/main.go b/examples/hue/main.go index 614e3adc0..e6e5d89df 100644 --- a/examples/hue/main.go +++ b/examples/hue/main.go @@ -37,13 +37,19 @@ var ( func update(screen *ebiten.Image) error { count++ + if ebiten.IsRunningSlowly() { return nil } + + // Center the image on the screen. w, h := gophersImage.Size() op := &ebiten.DrawImageOptions{} op.GeoM.Translate(float64(screenWidth-w)/2, float64(screenHeight-h)/2) + + // Rotate the hue. op.ColorM.RotateHue(float64(count%360) * 2 * math.Pi / 360) + screen.DrawImage(gophersImage, op) return nil } diff --git a/examples/life/main.go b/examples/life/main.go index 161b71a9b..514777ebd 100644 --- a/examples/life/main.go +++ b/examples/life/main.go @@ -33,13 +33,13 @@ import ( "github.com/hajimehoshi/ebiten" ) -// World represents the game state +// World represents the game state. type World struct { area [][]bool rnd *rand.Rand } -// NewWorld creates a new world +// NewWorld creates a new world. func NewWorld(width, height int) *World { world := World{ area: makeArea(width, height), @@ -48,7 +48,7 @@ func NewWorld(width, height int) *World { return &world } -// RandomSeed inits world with a random state +// RandomSeed inits world with a random state. func (w *World) RandomSeed(limit int) { height := len(w.area) width := len(w.area[0]) @@ -59,14 +59,13 @@ func (w *World) RandomSeed(limit int) { } } -// Progress game state by one tick -func (w *World) Progress() { +// Update game state by one tick. +func (w *World) Update() { height := len(w.area) width := len(w.area[0]) next := makeArea(width, height) for y := 0; y < height; y++ { for x := 0; x < width; x++ { - pop := neighbourCount(w.area, x, y) switch { case pop < 2: @@ -95,7 +94,7 @@ func (w *World) Progress() { } // DrawImage paints current game state -func (w *World) DrawImage(pix []uint8) { +func (w *World) DrawImage(pix []byte) { height := len(w.area) width := len(w.area[0]) for y := 0; y < height; y++ { @@ -116,7 +115,7 @@ func (w *World) DrawImage(pix []uint8) { } } -// neighbourCount calculates the Moore neighborhood of x, y +// neighbourCount calculates the Moore neighborhood of (x, y) func neighbourCount(a [][]bool, x, y int) int { height := len(a) width := len(a[0]) @@ -163,11 +162,11 @@ const ( var ( world = NewWorld(screenWidth, screenHeight) - pixels = make([]uint8, screenWidth*screenHeight*4) + pixels = make([]byte, screenWidth*screenHeight*4) ) func update(screen *ebiten.Image) error { - world.Progress() + world.Update() if ebiten.IsRunningSlowly() { return nil }