From 6d2692b0db1b5a06c3ba20034f41ac82ebcdf758 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sun, 5 Feb 2017 05:19:14 +0900 Subject: [PATCH] Add examples/edge to check #315 --- examples/edge/main.go | 85 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 examples/edge/main.go diff --git a/examples/edge/main.go b/examples/edge/main.go new file mode 100644 index 000000000..4fb6937c9 --- /dev/null +++ b/examples/edge/main.go @@ -0,0 +1,85 @@ +// Copyright 2017 The Ebiten Authors +// +// 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. + +// +build example + +package main + +import ( + "image/color" + "log" + "math" + + "github.com/hajimehoshi/ebiten" +) + +const ( + screenWidth = 320 + screenHeight = 240 +) + +var ( + colorImage *ebiten.Image + colorImageWidth = 64 + colorImageHeight = 64 + angle = 0 +) + +func update(screen *ebiten.Image) error { + angle++ + angle %= 360 + if err := screen.Fill(color.White); err != nil { + return err + } + op := &ebiten.DrawImageOptions{} + op.GeoM.Translate(-float64(colorImageWidth)/2, -float64(colorImageHeight)/2) + op.GeoM.Rotate(float64(angle) * math.Pi / 180) + op.GeoM.Translate(screenWidth/2, screenHeight/2) + if err := screen.DrawImage(colorImage, op); err != nil { + return err + } + return nil +} + +func main() { + var err error + colorImage, err = ebiten.NewImage(colorImageWidth, colorImageHeight, ebiten.FilterLinear) + if err != nil { + log.Fatal(err) + } + pixels := make([]uint8, 4*colorImageWidth*colorImageHeight) + for j := 0; j < colorImageHeight; j++ { + for i := 0; i < colorImageWidth; i++ { + idx := 4 * (i + j*colorImageWidth) + switch { + case j < colorImageHeight/2: + pixels[idx] = 0xff + pixels[idx+1] = 0 + pixels[idx+2] = 0 + pixels[idx+3] = 0xff + default: + pixels[idx] = 0 + pixels[idx+1] = 0xff + pixels[idx+2] = 0 + pixels[idx+3] = 0xff + } + } + } + if err := colorImage.ReplacePixels(pixels); err != nil { + log.Fatal(err) + } + if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Edge (Ebiten Demo)"); err != nil { + log.Fatal(err) + } +}