Skip to content

Commit

Permalink
Fixed texture atlas. Added GC debugging. (OpenDiablo2#140)
Browse files Browse the repository at this point in the history
  • Loading branch information
essial authored Nov 11, 2019
1 parent 9b8ce0c commit 352ff81
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 18 deletions.
7 changes: 7 additions & 0 deletions d2core/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,14 @@ func (v Engine) Draw(screen *ebiten.Image) {
}
if v.showFPS == true {
ebitenutil.DebugPrintAt(screen, "vsync:"+strconv.FormatBool(ebiten.IsVsyncEnabled())+"\nFPS:"+strconv.Itoa(int(ebiten.CurrentFPS())), 5, 565)
var m runtime.MemStats
runtime.ReadMemStats(&m)
ebitenutil.DebugPrintAt(screen, "Alloc "+strconv.FormatInt(int64(m.Alloc)/1024/1024, 10), 700, 0)
ebitenutil.DebugPrintAt(screen, "Pause "+strconv.FormatInt(int64(m.PauseTotalNs/1024/1024), 10), 700, 10)
ebitenutil.DebugPrintAt(screen, "HeapSys "+strconv.FormatInt(int64(m.HeapSys/1024/1024), 10), 700, 20)
ebitenutil.DebugPrintAt(screen, "NumGC "+strconv.FormatInt(int64(m.NumGC), 10), 700, 30)
}

}

// SetNextScene tells the engine what scene to load on the next update cycle
Expand Down
58 changes: 40 additions & 18 deletions d2render/sprite.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ type SpriteFrame struct {
FrameData []byte
Image *ebiten.Image
cached bool
atlasX int
atlasY int
}

// CreateSprite creates an instance of a sprite
Expand Down Expand Up @@ -143,36 +145,56 @@ func CreateSprite(data []byte, palette d2datadict.PaletteRec) Sprite {
}(i)
}
wg.Wait()
totalWidth := 0
totalHeight := 0
frame := 0
curMaxWidth := 0
atlasWidth := 0
atlasHeight := 0
curX := 0
curY := 0
const maxHeight = 8192
for d := 0; d < int(result.Directions); d++ {
curMaxWidth := 0
for f := 0; f < int(result.FramesPerDirection); f++ {
if curY+int(result.Frames[frame].Height) >= maxHeight {
curX += curMaxWidth
curY = 0
curMaxWidth = 0
if curX >= maxHeight {
log.Fatal("Ran out of texture atlas space!")
}
}
result.Frames[frame].atlasX = curX
result.Frames[frame].atlasY = curY
curMaxWidth = int(d2helper.Max(uint32(curMaxWidth), result.Frames[frame].Width))
totalHeight += int(result.Frames[frame].Height)
atlasWidth = int(d2helper.Max(uint32(atlasWidth), uint32(curX)+result.Frames[frame].Width))
atlasHeight = int(d2helper.Max(uint32(atlasHeight), uint32(curY)+result.Frames[frame].Height))
curY += int(result.Frames[frame].Height)
frame++
}
totalWidth += curMaxWidth
}
result.atlas, _ = ebiten.NewImage(totalWidth, totalHeight, ebiten.FilterNearest)
result.atlasBytes = make([]byte, totalWidth*totalHeight*4)
p2 := 1
for p2 < atlasWidth {
p2 <<= 1
}
atlasWidth = p2
p2 = 1
for p2 < atlasHeight {
p2 <<= 1
}
atlasHeight = p2
result.atlas, _ = ebiten.NewImage(atlasWidth, atlasHeight, ebiten.FilterNearest)
result.atlasBytes = make([]byte, atlasWidth*atlasHeight*4)
frame = 0
curX := 0
curY := 0
for d := 0; d < int(result.Directions); d++ {
curMaxWidth := 0
for f := 0; f < int(result.FramesPerDirection); f++ {
curMaxWidth = int(d2helper.Max(uint32(curMaxWidth), result.Frames[frame].Width))
result.Frames[frame].Image = result.atlas.SubImage(image.Rect(curX, curY, curX+int(result.Frames[frame].Width), curY+int(result.Frames[frame].Height))).(*ebiten.Image)
f := &result.Frames[frame]
f.Image = result.atlas.SubImage(image.Rect(f.atlasX, f.atlasY, f.atlasX+int(f.Width), f.atlasY+int(f.Height))).(*ebiten.Image)
curY += int(result.Frames[frame].Height)
frame++
}
curX += curMaxWidth
curY = 0
}
result.valid = true
return result

}

func (v Sprite) IsValid() bool {
Expand All @@ -183,14 +205,13 @@ func (v *Sprite) cacheFrame(frame int) {
if v.Frames[frame].cached {
return
}
r := v.Frames[frame].Image.Bounds().Min
curX := r.X
curY := r.Y
totalWidth := v.atlas.Bounds().Max.X
ox := v.Frames[frame].atlasX
oy := v.Frames[frame].atlasY
for y := 0; y < int(v.Frames[frame].Height); y++ {
for x := 0; x < int(v.Frames[frame].Width); x++ {
pix := (x + (y * int(v.Frames[frame].Width))) * 4
idx := (curX + x + ((curY + y) * totalWidth)) * 4
idx := (ox + x + ((oy + y) * totalWidth)) * 4
v.atlasBytes[idx] = v.Frames[frame].FrameData[pix]
v.atlasBytes[idx+1] = v.Frames[frame].FrameData[pix+1]
v.atlasBytes[idx+2] = v.Frames[frame].FrameData[pix+2]
Expand All @@ -201,6 +222,7 @@ func (v *Sprite) cacheFrame(frame int) {
log.Panic(err.Error())
}
v.Frames[frame].cached = true
v.Frames[frame].FrameData = []byte{}
}

// GetSize returns the size of the sprite
Expand Down

0 comments on commit 352ff81

Please sign in to comment.