image: Remove image.DrawFilledRect (#142)

This commit is contained in:
Hajime Hoshi 2016-02-06 17:45:08 +09:00
parent 580effba8e
commit 85279e8422
4 changed files with 37 additions and 61 deletions

View File

@ -26,6 +26,7 @@ import (
)
var (
imageEmpty *ebiten.Image
imageGameBG *ebiten.Image
imageWindows *ebiten.Image
imageGameover *ebiten.Image
@ -67,6 +68,12 @@ func linesTextBoxPosition() (x, y int) {
func init() {
var err error
// Empty
imageEmpty, err = ebiten.NewImage(16, 16, ebiten.FilterNearest)
if err != nil {
panic(err)
}
imageEmpty.Fill(color.White)
// Background
imageGameBG, _, err = ebitenutil.NewImageFromFile("images/gophers.jpg", ebiten.FilterLinear)
if err != nil {
@ -121,7 +128,12 @@ func init() {
}
func drawWindow(r *ebiten.Image, x, y, width, height int) error {
return r.DrawFilledRect(x, y, width, height, color.NRGBA{0, 0, 0, 0xc0})
op := &ebiten.DrawImageOptions{}
w, h := imageEmpty.Size()
op.GeoM.Scale(float64(width)/float64(w), float64(height)/float64(h))
op.GeoM.Translate(float64(x), float64(y))
op.ColorM.Scale(0, 0, 0, 0.75)
return r.DrawImage(imageEmpty, op)
}
var fontColor = color.NRGBA{0x40, 0x40, 0xff, 0xff}

View File

@ -128,11 +128,18 @@ func updateInput() {
}
}
var pianoImage *ebiten.Image
var (
imagePiano *ebiten.Image
)
func init() {
var err error
pianoImage, err = ebiten.NewImage(screenWidth, screenHeight, ebiten.FilterNearest)
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)
}
@ -142,8 +149,13 @@ func init() {
for i, k := range whiteKeys {
x := i*width + 36
height := 112
pianoImage.DrawFilledRect(x, y, width-1, height, color.White)
common.ArcadeFont.DrawText(pianoImage, k, x+8, y+height-16, 1, color.Black)
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"}
@ -153,8 +165,13 @@ func init() {
}
x := i*width + 24
height := 64
pianoImage.DrawFilledRect(x, y, width-1, height, color.Black)
common.ArcadeFont.DrawText(pianoImage, k, x+8, y+height-16, 1, color.White)
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)
}
}
@ -168,7 +185,7 @@ func update(screen *ebiten.Image) error {
}
screen.Fill(color.RGBA{0x80, 0x80, 0xc0, 0xff})
screen.DrawImage(pianoImage, nil)
screen.DrawImage(imagePiano, nil)
ebitenutil.DebugPrint(screen, fmt.Sprintf("FPS: %0.2f", ebiten.CurrentFPS()))
return nil

View File

@ -1,48 +0,0 @@
// Copyright 2015 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.
package main
import (
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/exp/shape"
"image/color"
"log"
//"math"
)
const (
screenWidth = 320
screenHeight = 240
)
func update(screen *ebiten.Image) error {
for i := 0; i < 6; i++ {
screen.DrawRect(2*i, 2*i, 100, 100, color.NRGBA{0x80, 0x80, 0xff, 0x80})
}
screen.DrawFilledRect(10, 10, 100, 100, color.NRGBA{0x80, 0x80, 0xff, 0x80})
screen.DrawFilledRect(20, 20, 100, 100, color.NRGBA{0x80, 0x80, 0xff, 0x80})
screen.DrawLine(130, 0, 140, 100, color.NRGBA{0xff, 0x80, 0x80, 0x80})
screen.DrawLine(140, 0, 150, 100, color.NRGBA{0xff, 0x80, 0x80, 0x80})
shape.DrawEllipse(screen, 0, 0, 200, 200, color.NRGBA{0x80, 0xff, 0x80, 0x80})
//shape.DrawArc(screen, 0, 0, 50, 50, 0, math.Pi/2, color.NRGBA{0xff, 0x80, 0x80, 0x80})
return nil
}
func main() {
if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Shapes (Ebiten Demo)"); err != nil {
log.Fatal(err)
}
}

View File

@ -116,11 +116,6 @@ func (i *Image) DrawRect(x, y, width, height int, clr color.Color) error {
return i.DrawLines(&rectsAsLines{&rect{x, y, width, height, clr}})
}
// DrawFilledRect draws a filled rectangle.
func (i *Image) DrawFilledRect(x, y, width, height int, clr color.Color) error {
return i.DrawFilledRects(&rect{x, y, width, height, clr})
}
// DrawFilledRects draws filled rectangles on the image.
func (i *Image) DrawFilledRects(rects Rects) (err error) {
i.pixels = nil