mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-11-10 04:57:26 +01:00
Remove internal/colormcache
This commit is contained in:
parent
256adfff9d
commit
87c8625692
@ -20,7 +20,6 @@ import (
|
||||
"math"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/v2"
|
||||
"github.com/hajimehoshi/ebiten/v2/internal/colormcache"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -32,11 +31,17 @@ func init() {
|
||||
emptyImage.Fill(color.White)
|
||||
}
|
||||
|
||||
func colorToScale(clr color.Color) (float64, float64, float64, float64) {
|
||||
cr, cg, cb, ca := clr.RGBA()
|
||||
if ca == 0 {
|
||||
return 0, 0, 0, 0
|
||||
}
|
||||
return float64(cr) / float64(ca), float64(cg) / float64(ca), float64(cb) / float64(ca), float64(ca) / 0xffff
|
||||
}
|
||||
|
||||
// DrawLine draws a line segment on the given destination dst.
|
||||
//
|
||||
// DrawLine is intended to be used mainly for debugging or prototyping purpose.
|
||||
//
|
||||
// DrawLine is not concurrent-safe.
|
||||
func DrawLine(dst *ebiten.Image, x1, y1, x2, y2 float64, clr color.Color) {
|
||||
length := math.Hypot(x2-x1, y2-y1)
|
||||
|
||||
@ -44,7 +49,7 @@ func DrawLine(dst *ebiten.Image, x1, y1, x2, y2 float64, clr color.Color) {
|
||||
op.GeoM.Scale(length, 1)
|
||||
op.GeoM.Rotate(math.Atan2(y2-y1, x2-x1))
|
||||
op.GeoM.Translate(x1, y1)
|
||||
op.ColorM = colormcache.ColorToColorM(clr)
|
||||
op.ColorM.Scale(colorToScale(clr))
|
||||
// Filter must be 'nearest' filter (default).
|
||||
// Linear filtering would make edges blurred.
|
||||
dst.DrawImage(emptySubImage, op)
|
||||
@ -53,13 +58,11 @@ func DrawLine(dst *ebiten.Image, x1, y1, x2, y2 float64, clr color.Color) {
|
||||
// DrawRect draws a rectangle on the given destination dst.
|
||||
//
|
||||
// DrawRect is intended to be used mainly for debugging or prototyping purpose.
|
||||
//
|
||||
// DrawRect is not concurrent-safe.
|
||||
func DrawRect(dst *ebiten.Image, x, y, width, height float64, clr color.Color) {
|
||||
op := &ebiten.DrawImageOptions{}
|
||||
op.GeoM.Scale(width, height)
|
||||
op.GeoM.Translate(x, y)
|
||||
op.ColorM = colormcache.ColorToColorM(clr)
|
||||
op.ColorM.Scale(colorToScale(clr))
|
||||
// Filter must be 'nearest' filter (default).
|
||||
// Linear filtering would make edges blurred.
|
||||
dst.DrawImage(emptyImage.SubImage(image.Rect(1, 1, 2, 2)).(*ebiten.Image), op)
|
||||
|
@ -1,96 +0,0 @@
|
||||
// Copyright 2020 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.
|
||||
|
||||
package colormcache
|
||||
|
||||
import (
|
||||
"image/color"
|
||||
"math"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/v2"
|
||||
)
|
||||
|
||||
var (
|
||||
monotonicClock int64
|
||||
)
|
||||
|
||||
func now() int64 {
|
||||
monotonicClock++
|
||||
return monotonicClock
|
||||
}
|
||||
|
||||
const (
|
||||
cacheLimit = 512 // This is an arbitrary number.
|
||||
)
|
||||
|
||||
type colorMCacheKey uint32
|
||||
|
||||
type colorMCacheEntry struct {
|
||||
m ebiten.ColorM
|
||||
atime int64
|
||||
}
|
||||
|
||||
var (
|
||||
colorMCache = map[colorMCacheKey]*colorMCacheEntry{}
|
||||
emptyColorM ebiten.ColorM
|
||||
)
|
||||
|
||||
func init() {
|
||||
emptyColorM.Scale(0, 0, 0, 0)
|
||||
}
|
||||
|
||||
func ColorToColorM(clr color.Color) ebiten.ColorM {
|
||||
// RGBA() is in [0 - 0xffff]. Adjust them in [0 - 0xff].
|
||||
cr, cg, cb, ca := clr.RGBA()
|
||||
cr /= 0x101
|
||||
cg /= 0x101
|
||||
cb /= 0x101
|
||||
ca /= 0x101
|
||||
if ca == 0 {
|
||||
return emptyColorM
|
||||
}
|
||||
|
||||
key := colorMCacheKey(uint32(cr) | (uint32(cg) << 8) | (uint32(cb) << 16) | (uint32(ca) << 24))
|
||||
e, ok := colorMCache[key]
|
||||
if ok {
|
||||
e.atime = now()
|
||||
return e.m
|
||||
}
|
||||
|
||||
if len(colorMCache) > cacheLimit {
|
||||
oldest := int64(math.MaxInt64)
|
||||
oldestKey := colorMCacheKey(0)
|
||||
for key, c := range colorMCache {
|
||||
if c.atime < oldest {
|
||||
oldestKey = key
|
||||
oldest = c.atime
|
||||
}
|
||||
}
|
||||
delete(colorMCache, oldestKey)
|
||||
}
|
||||
|
||||
cm := ebiten.ColorM{}
|
||||
rf := float64(cr) / float64(ca)
|
||||
gf := float64(cg) / float64(ca)
|
||||
bf := float64(cb) / float64(ca)
|
||||
af := float64(ca) / 0xff
|
||||
cm.Scale(rf, gf, bf, af)
|
||||
e = &colorMCacheEntry{
|
||||
m: cm,
|
||||
atime: now(),
|
||||
}
|
||||
colorMCache[key] = e
|
||||
|
||||
return e.m
|
||||
}
|
Loading…
Reference in New Issue
Block a user