mirror of
https://github.com/OpenDiablo2/OpenDiablo2
synced 2024-09-29 06:36:30 -04:00
7b60882ba3
* lint updates for /d2astar * png issue * debug package * load bmp properly * debug draft * Vector float64 (#565) * Fixed nil pointer in Copy() * Position added Added Floor() and String() methods to Vector. Also added Position which declares an embedded Vector2 and returns various forms of it. * d2vector.Vector2 renamed to d2vector.BigFloat * vector.go renamed to big_float.go * Float64 stub and more renaming * Vector value getters * Separate vector types with initial methods. * Divide and lint warnings. * Distance and Length. * Scale, Abs and Negate. * CompareFloat64Fuzzy delta direction reversed. * Refactor vector_test.go. * Renamed Approx methods. * Distance and Length. * Distance and Length. * Removed BigFloat and Vector, renamed Float64 to Vector, simplified tests. * Angle, SignedAngle and other small functions. * Receiver rename. * SingedAngle and test fixed * Rotate. * SetLength. * Cross. * NinetyAnti and NinetyClock. * Lerp and Clamp. * Reflect and ReflectSurface. * Cardinal convenience functions. * Comments. * Panic on NaN and Inf in Position. * Lint warnings and comments. * D2map lint warnings (#566) * Comments and newlines in engine.go * Comments and newlines in object.go * Comments and newlines in animated_entity.go * Comments and newlines in missile.go * Comments and newlines in npc.go * Comments and newlines in player.go * Removed object.go (incorrectly merged it in during rebase). * Comments and newlines in renderer.go. * Comments and newlines in map_entity.go. * Comments and newlines in walk_mesh.go. * Comments and newlines in viewport.go and tile_cache.go. * Comments and newlines in stamp.go and wilderness_tile_types.go. * Comments and newlines in everything else. * removing inventory grid test for now because it causes builds to fail on github (#570) * Refactored d2enum (#567) * Refactored animation mode enum * More d2enum changes * Refactored tile enum * Refactored weapon class enum * Refactored more enums * Refactored item event enum * Fixed init_functions animation mode * Removed string2enum from MonsterAnimationMode * Refactored ItemStatCost description * Last enum lint errors * Regenerated monster stringer file * compressed image * Replaced restruct with StreamReader (#574) * Fixed bug in palette.go (#575) * Javascript console commands (#572) * Allow the execution of JS from the terminal when hosting a local game or playing a single game Signed-off-by: William Claude <w.claude@thebeat.co> * Reorganise imports on edited files Signed-off-by: William Claude <w.claude@thebeat.co> * Remove Reset Signed-off-by: William Claude <w.claude@thebeat.co> * Add inventory.txt loader, use the records (#573) * adding resource entry for inventory.txt * adding loader for inventory.txt * adding call to inventory.txt loader in d2app * d2game now uses the inventory.txt records for making the inventory panel * removed comments * remove unused files * update go.mod * lint updates for /d2astar * png issue * update go.mod Co-authored-by: danhale-git <36298392+danhale-git@users.noreply.github.com> Co-authored-by: dk <dknuth0101@gmail.com> Co-authored-by: Intyre <Intyre@gmail.com> Co-authored-by: William <1004323+aziule@users.noreply.github.com>
270 lines
6.8 KiB
Go
270 lines
6.8 KiB
Go
package ebiten
|
|
|
|
import (
|
|
"fmt"
|
|
"image"
|
|
"image/color"
|
|
"math"
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2DebugUtil"
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum"
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
|
|
"github.com/hajimehoshi/ebiten"
|
|
"github.com/hajimehoshi/ebiten/ebitenutil"
|
|
)
|
|
|
|
const cacheLimit = 512
|
|
|
|
type colorMCacheKey uint32
|
|
|
|
type colorMCacheEntry struct {
|
|
colorMatrix ebiten.ColorM
|
|
atime int64
|
|
}
|
|
|
|
type ebitenSurface struct {
|
|
stateStack []surfaceState
|
|
stateCurrent surfaceState
|
|
image *ebiten.Image
|
|
colorMCache map[colorMCacheKey]*colorMCacheEntry
|
|
monotonicClock int64
|
|
}
|
|
|
|
func createEbitenSurface(img *ebiten.Image, currentState ...surfaceState) *ebitenSurface {
|
|
state := surfaceState{effect: d2enum.DrawEffectNone}
|
|
if len(currentState) > 0 {
|
|
state = currentState[0]
|
|
}
|
|
|
|
return &ebitenSurface{
|
|
image: img,
|
|
stateCurrent: state,
|
|
colorMCache: make(map[colorMCacheKey]*colorMCacheEntry),
|
|
}
|
|
}
|
|
|
|
func (s *ebitenSurface) PushTranslation(x, y int) {
|
|
s.stateStack = append(s.stateStack, s.stateCurrent)
|
|
s.stateCurrent.x += x
|
|
s.stateCurrent.y += y
|
|
}
|
|
|
|
func (s *ebitenSurface) PushEffect(effect d2enum.DrawEffect) {
|
|
s.stateStack = append(s.stateStack, s.stateCurrent)
|
|
s.stateCurrent.effect = effect
|
|
}
|
|
|
|
func (s *ebitenSurface) PushFilter(filter d2enum.Filter) {
|
|
s.stateStack = append(s.stateStack, s.stateCurrent)
|
|
s.stateCurrent.filter = d2ToEbitenFilter(filter)
|
|
}
|
|
|
|
func (s *ebitenSurface) PushColor(color color.Color) {
|
|
s.stateStack = append(s.stateStack, s.stateCurrent)
|
|
s.stateCurrent.color = color
|
|
}
|
|
|
|
func (s *ebitenSurface) PushBrightness(brightness float64) {
|
|
s.stateStack = append(s.stateStack, s.stateCurrent)
|
|
s.stateCurrent.brightness = brightness
|
|
}
|
|
|
|
func (s *ebitenSurface) Pop() {
|
|
count := len(s.stateStack)
|
|
if count == 0 {
|
|
panic("empty stack")
|
|
}
|
|
|
|
s.stateCurrent = s.stateStack[count-1]
|
|
s.stateStack = s.stateStack[:count-1]
|
|
}
|
|
|
|
func (s *ebitenSurface) PopN(n int) {
|
|
for i := 0; i < n; i++ {
|
|
s.Pop()
|
|
}
|
|
}
|
|
|
|
func (s *ebitenSurface) Render(sfc d2interface.Surface) error {
|
|
opts := &ebiten.DrawImageOptions{}
|
|
opts.GeoM.Translate(float64(s.stateCurrent.x), float64(s.stateCurrent.y))
|
|
opts.Filter = s.stateCurrent.filter
|
|
|
|
if s.stateCurrent.color != nil {
|
|
opts.ColorM = s.colorToColorM(s.stateCurrent.color)
|
|
}
|
|
|
|
if s.stateCurrent.brightness != 0 {
|
|
opts.ColorM.ChangeHSV(0, 1, s.stateCurrent.brightness)
|
|
}
|
|
|
|
// Are these correct? who even knows
|
|
switch s.stateCurrent.effect {
|
|
case d2enum.DrawEffectPctTransparency25:
|
|
opts.ColorM.Translate(0, 0, 0, -0.25)
|
|
case d2enum.DrawEffectPctTransparency50:
|
|
opts.ColorM.Translate(0, 0, 0, -0.50)
|
|
case d2enum.DrawEffectPctTransparency75:
|
|
opts.ColorM.Translate(0, 0, 0, -0.75)
|
|
case d2enum.DrawEffectModulate:
|
|
opts.CompositeMode = ebiten.CompositeModeLighter
|
|
// TODO: idk what to do when ebiten doesn't exactly match, pick closest?
|
|
case d2enum.DrawEffectBurn:
|
|
case d2enum.DrawEffectNormal:
|
|
case d2enum.DrawEffectMod2XTrans:
|
|
case d2enum.DrawEffectMod2X:
|
|
case d2enum.DrawEffectNone:
|
|
opts.CompositeMode = ebiten.CompositeModeSourceOver
|
|
}
|
|
|
|
var img = sfc.(*ebitenSurface).image
|
|
|
|
return s.image.DrawImage(img, opts)
|
|
}
|
|
|
|
// Renders the section of the animation frame enclosed by bounds
|
|
func (s *ebitenSurface) RenderSection(sfc d2interface.Surface, bound image.Rectangle) error {
|
|
opts := &ebiten.DrawImageOptions{}
|
|
opts.GeoM.Translate(float64(s.stateCurrent.x), float64(s.stateCurrent.y))
|
|
opts.Filter = s.stateCurrent.filter
|
|
|
|
if s.stateCurrent.color != nil {
|
|
opts.ColorM = s.colorToColorM(s.stateCurrent.color)
|
|
}
|
|
|
|
if s.stateCurrent.brightness != 0 {
|
|
opts.ColorM.ChangeHSV(0, 1, s.stateCurrent.brightness)
|
|
}
|
|
|
|
// Are these correct? who even knows
|
|
switch s.stateCurrent.effect {
|
|
case d2enum.DrawEffectPctTransparency25:
|
|
opts.ColorM.Translate(0, 0, 0, -0.25)
|
|
case d2enum.DrawEffectPctTransparency50:
|
|
opts.ColorM.Translate(0, 0, 0, -0.50)
|
|
case d2enum.DrawEffectPctTransparency75:
|
|
opts.ColorM.Translate(0, 0, 0, -0.75)
|
|
case d2enum.DrawEffectModulate:
|
|
opts.CompositeMode = ebiten.CompositeModeLighter
|
|
// TODO: idk what to do when ebiten doesn't exactly match, pick closest?
|
|
case d2enum.DrawEffectBurn:
|
|
case d2enum.DrawEffectNormal:
|
|
case d2enum.DrawEffectMod2XTrans:
|
|
case d2enum.DrawEffectMod2X:
|
|
case d2enum.DrawEffectNone:
|
|
opts.CompositeMode = ebiten.CompositeModeSourceOver
|
|
}
|
|
|
|
var img = sfc.(*ebitenSurface).image
|
|
|
|
return s.image.DrawImage(img.SubImage(bound).(*ebiten.Image), opts)
|
|
}
|
|
|
|
func (s *ebitenSurface) DrawText(format string, params ...interface{}) {
|
|
d2DebugUtil.D2DebugPrintAt(s.image, fmt.Sprintf(format, params...), s.stateCurrent.x, s.stateCurrent.y)
|
|
}
|
|
|
|
func (s *ebitenSurface) DrawLine(x, y int, color color.Color) {
|
|
ebitenutil.DrawLine(
|
|
s.image,
|
|
float64(s.stateCurrent.x),
|
|
float64(s.stateCurrent.y),
|
|
float64(s.stateCurrent.x+x),
|
|
float64(s.stateCurrent.y+y),
|
|
color,
|
|
)
|
|
}
|
|
|
|
func (s *ebitenSurface) DrawRect(width, height int, color color.Color) {
|
|
ebitenutil.DrawRect(
|
|
s.image,
|
|
float64(s.stateCurrent.x),
|
|
float64(s.stateCurrent.y),
|
|
float64(width),
|
|
float64(height),
|
|
color,
|
|
)
|
|
}
|
|
|
|
func (s *ebitenSurface) Clear(color color.Color) error {
|
|
return s.image.Fill(color)
|
|
}
|
|
|
|
func (s *ebitenSurface) GetSize() (int, int) {
|
|
return s.image.Size()
|
|
}
|
|
|
|
func (s *ebitenSurface) GetDepth() int {
|
|
return len(s.stateStack)
|
|
}
|
|
|
|
func (s *ebitenSurface) ReplacePixels(pixels []byte) error {
|
|
return s.image.ReplacePixels(pixels)
|
|
}
|
|
|
|
func (s *ebitenSurface) Screenshot() *image.RGBA {
|
|
width, height := s.GetSize()
|
|
bounds := image.Rectangle{Min: image.Point{X: 0, Y: 0}, Max: image.Point{X: width, Y: height}}
|
|
rgba := image.NewRGBA(bounds)
|
|
|
|
for y := 0; y < height; y++ {
|
|
for x := 0; x < width; x++ {
|
|
rgba.Set(x, y, s.image.At(x, y))
|
|
}
|
|
}
|
|
|
|
return rgba
|
|
}
|
|
|
|
func (s *ebitenSurface) now() int64 {
|
|
s.monotonicClock++
|
|
return s.monotonicClock
|
|
}
|
|
|
|
// colorToColorM converts a normal color to a color matrix
|
|
func (s *ebitenSurface) colorToColorM(clr color.Color) ebiten.ColorM {
|
|
// RGBA() is in [0 - 0xffff]. Adjust them in [0 - 0xff].
|
|
cr, cg, cb, ca := clr.RGBA()
|
|
cr >>= 8
|
|
cg >>= 8
|
|
cb >>= 8
|
|
ca >>= 8
|
|
|
|
if ca == 0 {
|
|
emptyColorM := ebiten.ColorM{}
|
|
emptyColorM.Scale(0, 0, 0, 0)
|
|
return emptyColorM
|
|
}
|
|
key := colorMCacheKey(cr | (cg << 8) | (cb << 16) | (ca << 24))
|
|
e, ok := s.colorMCache[key]
|
|
if ok {
|
|
e.atime = s.now()
|
|
return e.colorMatrix
|
|
}
|
|
if len(s.colorMCache) > cacheLimit {
|
|
oldest := int64(math.MaxInt64)
|
|
oldestKey := colorMCacheKey(0)
|
|
for key, c := range s.colorMCache {
|
|
if c.atime < oldest {
|
|
oldestKey = key
|
|
oldest = c.atime
|
|
}
|
|
}
|
|
delete(s.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{
|
|
colorMatrix: cm,
|
|
atime: s.now(),
|
|
}
|
|
s.colorMCache[key] = e
|
|
|
|
return e.colorMatrix
|
|
}
|