shapes.go (2066B)
1 // Copyright 2017 The Ebiten Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package ebitenutil 16 17 import ( 18 "image/color" 19 "math" 20 21 "github.com/hajimehoshi/ebiten/v2" 22 "github.com/hajimehoshi/ebiten/v2/internal/colormcache" 23 ) 24 25 var ( 26 emptyImage = ebiten.NewImage(1, 1) 27 ) 28 29 func init() { 30 emptyImage.Fill(color.White) 31 } 32 33 // DrawLine draws a line segment on the given destination dst. 34 // 35 // DrawLine is intended to be used mainly for debugging or prototyping purpose. 36 // 37 // DrawLine is not concurrent-safe. 38 func DrawLine(dst *ebiten.Image, x1, y1, x2, y2 float64, clr color.Color) { 39 ew, eh := emptyImage.Size() 40 length := math.Hypot(x2-x1, y2-y1) 41 42 op := &ebiten.DrawImageOptions{} 43 op.GeoM.Scale(length/float64(ew), 1/float64(eh)) 44 op.GeoM.Rotate(math.Atan2(y2-y1, x2-x1)) 45 op.GeoM.Translate(x1, y1) 46 op.ColorM = colormcache.ColorToColorM(clr) 47 // Filter must be 'nearest' filter (default). 48 // Linear filtering would make edges blurred. 49 dst.DrawImage(emptyImage, op) 50 } 51 52 // DrawRect draws a rectangle on the given destination dst. 53 // 54 // DrawRect is intended to be used mainly for debugging or prototyping purpose. 55 // 56 // DrawRect is not concurrent-safe. 57 func DrawRect(dst *ebiten.Image, x, y, width, height float64, clr color.Color) { 58 ew, eh := emptyImage.Size() 59 60 op := &ebiten.DrawImageOptions{} 61 op.GeoM.Scale(width/float64(ew), height/float64(eh)) 62 op.GeoM.Translate(x, y) 63 op.ColorM = colormcache.ColorToColorM(clr) 64 // Filter must be 'nearest' filter (default). 65 // Linear filtering would make edges blurred. 66 dst.DrawImage(emptyImage, op) 67 }