1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-11-19 10:56:07 -05:00

Create tiles cache on region loading (#200)

* tile choice algo
* cycle region files
* switcher limited by preset
* create tile caches befor rendering
* remove keyLocked
This commit is contained in:
Averrin 2019-11-17 14:09:54 +01:00 committed by Tim Sarbin
parent 43e76230cf
commit 5eafa9cc0b
3 changed files with 82 additions and 52 deletions

View File

@ -96,8 +96,6 @@ type MapEngineTest struct {
fileIndex int fileIndex int
regionSpec RegionSpec regionSpec RegionSpec
filesCount int filesCount int
keyLocked bool
} }
func CreateMapEngineTest( func CreateMapEngineTest(
@ -113,10 +111,9 @@ func CreateMapEngineTest(
sceneProvider: sceneProvider, sceneProvider: sceneProvider,
currentRegion: currentRegion, currentRegion: currentRegion,
levelPreset: levelPreset, levelPreset: levelPreset,
fileIndex: -1, fileIndex: 0,
regionSpec: RegionSpec{}, regionSpec: RegionSpec{},
filesCount: 0, filesCount: 0,
keyLocked: false,
} }
result.gameState = d2core.CreateTestGameState() result.gameState = d2core.CreateTestGameState()
return result return result
@ -245,54 +242,40 @@ func (v *MapEngineTest) Update(tickTime float64) {
os.Exit(0) os.Exit(0)
} }
if !v.keyLocked { if inpututil.IsKeyJustPressed(ebiten.KeyN) && ctrlPressed {
v.fileIndex = increment(v.fileIndex, 0, v.filesCount-1)
if v.uiManager.KeyPressed(ebiten.KeyN) && ctrlPressed { v.sceneProvider.SetNextScene(v)
v.fileIndex = increment(v.fileIndex, 0, v.filesCount-1) return
v.keyLocked = true
v.sceneProvider.SetNextScene(v)
return
}
if v.uiManager.KeyPressed(ebiten.KeyP) && ctrlPressed {
v.fileIndex = decrement(v.fileIndex, 0, v.filesCount-1)
v.keyLocked = true
v.sceneProvider.SetNextScene(v)
return
}
if v.uiManager.KeyPressed(ebiten.KeyN) && shiftPressed {
v.levelPreset = increment(v.levelPreset, v.regionSpec.startPresetIndex, v.regionSpec.endPresetIndex)
v.keyLocked = true
v.sceneProvider.SetNextScene(v)
return
}
if v.uiManager.KeyPressed(ebiten.KeyP) && shiftPressed {
v.levelPreset = decrement(v.levelPreset, v.regionSpec.startPresetIndex, v.regionSpec.endPresetIndex)
v.keyLocked = true
v.sceneProvider.SetNextScene(v)
return
}
if v.uiManager.KeyPressed(ebiten.KeyN) {
v.currentRegion = increment(v.currentRegion, 0, len(regions))
v.keyLocked = true
v.sceneProvider.SetNextScene(v)
return
}
if v.uiManager.KeyPressed(ebiten.KeyP) {
v.currentRegion = decrement(v.currentRegion, 0, len(regions))
v.keyLocked = true
v.sceneProvider.SetNextScene(v)
return
}
} }
//FIXME: do it better if inpututil.IsKeyJustPressed(ebiten.KeyP) && ctrlPressed {
if !v.uiManager.KeyPressed(ebiten.KeyP) && !v.uiManager.KeyPressed(ebiten.KeyN) { v.fileIndex = decrement(v.fileIndex, 0, v.filesCount-1)
v.keyLocked = false v.sceneProvider.SetNextScene(v)
return
}
if inpututil.IsKeyJustPressed(ebiten.KeyN) && shiftPressed {
v.levelPreset = increment(v.levelPreset, v.regionSpec.startPresetIndex, v.regionSpec.endPresetIndex)
v.sceneProvider.SetNextScene(v)
return
}
if inpututil.IsKeyJustPressed(ebiten.KeyP) && shiftPressed {
v.levelPreset = decrement(v.levelPreset, v.regionSpec.startPresetIndex, v.regionSpec.endPresetIndex)
v.sceneProvider.SetNextScene(v)
return
}
if inpututil.IsKeyJustPressed(ebiten.KeyN) {
v.currentRegion = increment(v.currentRegion, 0, len(regions))
v.sceneProvider.SetNextScene(v)
return
}
if inpututil.IsKeyJustPressed(ebiten.KeyP) {
v.currentRegion = decrement(v.currentRegion, 0, len(regions))
v.sceneProvider.SetNextScene(v)
return
} }
} }

View File

@ -15,8 +15,8 @@ import (
"github.com/OpenDiablo2/OpenDiablo2/d2core" "github.com/OpenDiablo2/OpenDiablo2/d2core"
"github.com/OpenDiablo2/OpenDiablo2/d2audio"
"github.com/OpenDiablo2/D2Shared/d2common" "github.com/OpenDiablo2/D2Shared/d2common"
"github.com/OpenDiablo2/OpenDiablo2/d2audio"
"github.com/hajimehoshi/ebiten" "github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/ebitenutil" "github.com/hajimehoshi/ebiten/ebitenutil"
) )
@ -73,6 +73,7 @@ func (v *Engine) GenerateMap(regionType d2enum.RegionIdType, levelPreset int, fi
for i, _ := range v.regions { for i, _ := range v.regions {
v.GenTiles(&v.regions[i]) v.GenTiles(&v.regions[i])
v.GenTilesCache(&v.regions[i])
} }
} }
@ -100,6 +101,7 @@ func (v *Engine) GenerateAct1Overworld() {
for i, _ := range v.regions { for i, _ := range v.regions {
v.GenTiles(&v.regions[i]) v.GenTiles(&v.regions[i])
v.GenTilesCache(&v.regions[i])
} }
sx, sy := d2helper.IsoToScreen(int(region.StartX), int(region.StartY), 0, 0) sx, sy := d2helper.IsoToScreen(int(region.StartX), int(region.StartY), 0, 0)
@ -144,6 +146,40 @@ func (v *Engine) GenTiles(region *EngineRegion) {
} }
} }
func (v *Engine) GenTilesCache(region *EngineRegion) {
n := 0
for _, t := range region.Tiles {
if t.tileY < len(region.Region.DS1.Tiles) && t.tileX < len(region.Region.DS1.Tiles[t.tileY]) {
tile := region.Region.DS1.Tiles[t.tileY][t.tileX]
for i := range tile.Floors {
if tile.Floors[i].Hidden || tile.Floors[i].Prop1 == 0 {
continue
}
tileCacheIndex := fmt.Sprintf("%v-%v-%v-%v", t.tileY, t.tileX, tile.Floors[i].MainIndex, tile.Floors[i].SubIndex)
region.Region.FloorCache[tileCacheIndex] = region.Region.generateFloorCache(tile.Floors[i])
n++
}
for i, shadow := range tile.Shadows {
if tile.Shadows[i].Hidden || tile.Shadows[i].Prop1 == 0 {
continue
}
tileCacheIndex := fmt.Sprintf("%v-%v-%v-%v", t.tileY, t.tileX, shadow.MainIndex, shadow.SubIndex)
region.Region.ShadowCache[tileCacheIndex] = region.Region.generateShadowCache(shadow)
n++
}
for i, wall := range tile.Walls {
if tile.Walls[i].Hidden {
continue
}
tileCacheIndex := fmt.Sprintf("%v-%v-%v-%v-%v", t.tileY, t.tileX, wall.MainIndex, wall.SubIndex, wall.Orientation)
region.Region.WallCache[tileCacheIndex] = region.Region.generateWallCache(wall)
n++
}
}
}
fmt.Printf("generated: %v cached tiles\n", n)
}
func (v *Engine) RenderRegion(region EngineRegion, target *ebiten.Image) { func (v *Engine) RenderRegion(region EngineRegion, target *ebiten.Image) {
for tileIdx := range region.Tiles { for tileIdx := range region.Tiles {
sx, sy := d2helper.IsoToScreen(region.Tiles[tileIdx].tileX+region.Rect.Left, region.Tiles[tileIdx].tileY+region.Rect.Top, int(v.OffsetX), int(v.OffsetY)) sx, sy := d2helper.IsoToScreen(region.Tiles[tileIdx].tileX+region.Rect.Left, region.Tiles[tileIdx].tileY+region.Rect.Top, int(v.OffsetX), int(v.OffsetY))
@ -237,7 +273,7 @@ func (v *Engine) RenderPass3(region *Region, offX, offY, x, y int, target *ebite
func (v *Engine) DrawTileLines(region *Region, offX, offY, x, y int, target *ebiten.Image) { func (v *Engine) DrawTileLines(region *Region, offX, offY, x, y int, target *ebiten.Image) {
if v.ShowTiles > 0 { if v.ShowTiles > 0 {
subtileColor := color.RGBA{255, 100, 100, 140} subtileColor := color.RGBA{80, 80, 255, 100}
tileColor := color.RGBA{255, 255, 255, 255} tileColor := color.RGBA{255, 255, 255, 255}
ebitenutil.DrawLine(target, float64(offX)+v.OffsetX, float64(offY)+v.OffsetY, float64(offX)+v.OffsetX+80, float64(offY)+v.OffsetY+40, tileColor) ebitenutil.DrawLine(target, float64(offX)+v.OffsetX, float64(offY)+v.OffsetY, float64(offX)+v.OffsetX+80, float64(offY)+v.OffsetY+40, tileColor)
@ -255,6 +291,14 @@ func (v *Engine) DrawTileLines(region *Region, offX, offY, x, y int, target *ebi
ebitenutil.DrawLine(target, float64(offX+x)+v.OffsetX, float64(offY+y)+v.OffsetY, ebitenutil.DrawLine(target, float64(offX+x)+v.OffsetX, float64(offY+y)+v.OffsetY,
float64(offX+x)+v.OffsetX-80, float64(offY+y)+v.OffsetY+40, subtileColor) float64(offX+x)+v.OffsetX-80, float64(offY+y)+v.OffsetY+40, subtileColor)
} }
tile := region.DS1.Tiles[y][x]
for i := range tile.Floors {
floorSpec := fmt.Sprintf("f: %v-%v", tile.Floors[i].MainIndex, tile.Floors[i].SubIndex)
ebitenutil.DebugPrintAt(target, floorSpec, offX+int(v.OffsetX)-20, offY+int(v.OffsetY)+10+((i+1)*14))
}
// wallSpec := fmt.Sprintf("w: %v-%v", tile.Walls[0].MainIndex, tile.Walls[0].SubIndex)
// ebitenutil.DebugPrintAt(target, wallSpec, offX+int(v.OffsetX)-20, offY+int(v.OffsetY)+34)
} }
} }
} }

View File

@ -206,6 +206,7 @@ func (v *Region) renderFloor(tile d2ds1.FloorShadowRecord, offsetX, offsetY int,
tileCacheIndex := fmt.Sprintf("%v-%v-%v-%v", tileY, tileX, tile.MainIndex, tile.SubIndex) tileCacheIndex := fmt.Sprintf("%v-%v-%v-%v", tileY, tileX, tile.MainIndex, tile.SubIndex)
tileCache, exists := v.FloorCache[tileCacheIndex] tileCache, exists := v.FloorCache[tileCacheIndex]
if !exists { if !exists {
log.Printf("floor cache missed: %v of %v", tileCacheIndex, len(v.FloorCache))
v.FloorCache[tileCacheIndex] = v.generateFloorCache(tile) v.FloorCache[tileCacheIndex] = v.generateFloorCache(tile)
tileCache = v.FloorCache[tileCacheIndex] tileCache = v.FloorCache[tileCacheIndex]
if tileCache == nil { if tileCache == nil {
@ -227,6 +228,7 @@ func (v *Region) renderWall(tile d2ds1.WallRecord, offsetX, offsetY int, target
tileCacheIndex := fmt.Sprintf("%v-%v-%v-%v-%v", tileY, tileX, tile.MainIndex, tile.SubIndex, tile.Orientation) tileCacheIndex := fmt.Sprintf("%v-%v-%v-%v-%v", tileY, tileX, tile.MainIndex, tile.SubIndex, tile.Orientation)
tileCache, exists := v.WallCache[tileCacheIndex] tileCache, exists := v.WallCache[tileCacheIndex]
if !exists { if !exists {
log.Println("wall cache missed")
v.WallCache[tileCacheIndex] = v.generateWallCache(tile) v.WallCache[tileCacheIndex] = v.generateWallCache(tile)
if v.WallCache[tileCacheIndex] == nil { if v.WallCache[tileCacheIndex] == nil {
log.Println("Could not generate wall: " + tileCacheIndex) log.Println("Could not generate wall: " + tileCacheIndex)
@ -247,6 +249,7 @@ func (v *Region) renderShadow(tile d2ds1.FloorShadowRecord, offsetX, offsetY int
tileCacheIndex := fmt.Sprintf("%v-%v-%v-%v", tileY, tileX, tile.MainIndex, tile.SubIndex) tileCacheIndex := fmt.Sprintf("%v-%v-%v-%v", tileY, tileX, tile.MainIndex, tile.SubIndex)
tileCache, exists := v.ShadowCache[tileCacheIndex] tileCache, exists := v.ShadowCache[tileCacheIndex]
if !exists { if !exists {
log.Println("shadow cache missed")
v.ShadowCache[tileCacheIndex] = v.generateShadowCache(tile) v.ShadowCache[tileCacheIndex] = v.generateShadowCache(tile)
tileCache = v.ShadowCache[tileCacheIndex] tileCache = v.ShadowCache[tileCacheIndex]
if tileCache == nil { if tileCache == nil {