Optimisation of tilemap

This commit is contained in:
Maruno17
2021-01-14 20:11:25 +00:00
parent 386396cdf6
commit 9364a543e7

View File

@@ -112,9 +112,9 @@ class CustomTilemap
@flash = nil
@oxFlash = 0
@oyFlash = 0
@priotiles = []
@priotiles = {}
@priotilesfast = []
@prioautotiles = []
@prioautotiles = {}
@autosprites = []
@framecount = [0,0,0,0,0,0,0,0] # For autotiles
@tilesetChanged = true
@@ -376,7 +376,7 @@ class CustomTilemap
def addTile(tiles,count,xpos,ypos,id)
terrain = @terrain_tags[id]
priority = @priorities[id]
if id>=384
if id >= 384 # Tileset tile
if count>=tiles.length
sprite = CustomTilemapSprite.new(@viewport)
tiles.push(sprite,0)
@@ -390,7 +390,7 @@ class CustomTilemap
sprite.tone = @tone
sprite.color = @color
getRegularTile(sprite,id)
else
else # Autotile
if count>=tiles.length
sprite = CustomTilemapSprite.new(@viewport)
tiles.push(sprite,1)
@@ -491,7 +491,8 @@ class CustomTilemap
end
def refresh_tileset
i = 0; len = @regularTileInfo.length
i = 0
len = @regularTileInfo.length
while i < len
if @regularTileInfo[i]
@regularTileInfo[i].dispose
@@ -513,7 +514,8 @@ class CustomTilemap
id = @map_data[x, y, z]
next if id == 0
next if @priorities[id] == 0 && !PBTerrain.hasReflections?(@terrain_tags[id])
@priotiles.push([x,y,z,id])
@priotiles[[x, y]] = [] if !@priotiles[[x, y]]
@priotiles[[x, y]].push([z, id])
end
end
end
@@ -522,7 +524,8 @@ class CustomTilemap
end
def refresh_autotiles
i = 0; len = @autotileInfo.length
i = 0
len = @autotileInfo.length
while i < len
if @autotileInfo[i]
@autotileInfo[i].dispose
@@ -530,7 +533,8 @@ class CustomTilemap
end
i += 1
end
i = 0; len = @autosprites.length
i = 0
len = @autosprites.length
while i < len
if @autosprites[i]
@autosprites[i].dispose
@@ -558,16 +562,14 @@ class CustomTilemap
else
for y in 0...ysize
for x in 0...xsize
haveautotile = false
for z in 0...zsize
id = @map_data[x, y, z]
next if id==0 || id>=384
next if id == 0 || id >= 384 # Skip non-autotiles
next if @priorities[id] != 0 || PBTerrain.hasReflections?(@terrain_tags[id])
next if @framecount[id / 48 - 1] < 2
haveautotile = true
@prioautotiles[[x, y]] = true
break
end
@prioautotiles.push([x,y]) if haveautotile
end
end
@fullyrefreshedautos = true
@@ -587,8 +589,7 @@ class CustomTilemap
if @layer0clip && @viewport.ox == 0 && @viewport.oy == 0
@layer0.ox = 0
@layer0.oy = 0
@layer0.src_rect.set(ptX.round,ptY.round,
@viewport.rect.width,@viewport.rect.height)
@layer0.src_rect.set(ptX.round, ptY.round, @viewport.rect.width, @viewport.rect.height)
else
@layer0.ox = ptX.round
@layer0.oy = ptY.round
@@ -612,8 +613,8 @@ class CustomTilemap
yStart = @oyLayer0 / theight
yStart = 0 if yStart < 0
xEnd = xStart + (width / twidth) + 1
yEnd = yStart+(height/theight)+1
xEnd = xsize if xEnd > xsize
yEnd = yStart + (height / theight) + 1
yEnd = ysize if yEnd > ysize
return true if xStart >= xEnd || yStart >= yEnd
trans = Color.new(0, 0, 0, 0)
@@ -625,23 +626,19 @@ class CustomTilemap
if !@fullyrefreshedautos
for y in yStart..yEnd
for x in xStart..xEnd
haveautotile = false
for z in zrange
id = mapdata[x, y, z]
next if !id || id<48 || id>=384
next if !id || id < 48 || id >= 384 # Skip non-autotiles
prioid = @priorities[id]
next if prioid != 0 || PBTerrain.hasReflections?(@terrain_tags[id])
fcount = @framecount[id / 48 - 1]
next if !fcount || fcount < 2
if !haveautotile
haveautotile = true
overallcount += 1
xpos = (x * twidth) - @oxLayer0
ypos = (y * theight) - @oyLayer0
bitmap.fill_rect(xpos, ypos, twidth, theight, trans) if overallcount <= 2000
break
end
end
for z in zrange
id = mapdata[x, y, z]
next if !id || id < 48
@@ -651,9 +648,9 @@ class CustomTilemap
xpos = (x * twidth) - @oxLayer0
ypos = (y * theight) - @oyLayer0
count = addTile(@autosprites, count, xpos, ypos, id)
next
elsif id>=384
temprect.set(((id - 384)&7)*@tileSrcWidth,((id - 384)>>3)*@tileSrcHeight,
elsif id >= 384 # Tileset tiles
temprect.set(((id - 384) & 7) * @tileSrcWidth,
((id - 384) >> 3) * @tileSrcHeight,
@tileSrcWidth, @tileSrcHeight)
xpos = (x * twidth) - @oxLayer0
ypos = (y * theight) - @oyLayer0
@@ -662,7 +659,7 @@ class CustomTilemap
else
bitmap.blt(xpos, ypos, @tileset, temprect)
end
else
else # Autotiles
tilebitmap = @autotileInfo[id]
if !tilebitmap
anim = autotileFrame(id)
@@ -683,15 +680,14 @@ class CustomTilemap
if !@priorect || !@priorectautos ||
@priorect[0] != xStart || @priorect[1] != yStart ||
@priorect[2] != xEnd || @priorect[3] != yEnd
@priorectautos = @prioautotiles.find_all { |tile|
x = tile[0]
y = tile[1]
# "next" means "return" here
next !(x<xStart || x>xEnd || y<yStart || y>yEnd)
}
@priorect = [xStart, yStart, xEnd, yEnd]
@priorectautos = []
for y in yStart..yEnd
for x in xStart..xEnd
@priorectautos.push([x, y]) if @prioautotiles[[x, y]]
end
end
end
# echoln ["autos",@priorect,@priorectautos.length,@prioautotiles.length]
for tile in @priorectautos
x = tile[0]
y = tile[1]
@@ -706,15 +702,16 @@ class CustomTilemap
next if !id || id < 48
prioid = @priorities[id]
next if prioid != 0 || PBTerrain.hasReflections?(@terrain_tags[id])
if id>=384
temprect.set(((id - 384)&7)*@tileSrcWidth,((id - 384)>>3)*@tileSrcHeight,
if id >= 384 # Tileset tiles
temprect.set(((id - 384) & 7) * @tileSrcWidth,
((id - 384) >> 3) * @tileSrcHeight,
@tileSrcWidth, @tileSrcHeight)
if @diffsizes
bitmap.stretch_blt(Rect.new(xpos, ypos, twidth, theight), @tileset, temprect)
else
bitmap.blt(xpos, ypos, @tileset, temprect)
end
else
else # Autotiles
tilebitmap = @autotileInfo[id]
if !tilebitmap
anim = autotileFrame(id)
@@ -739,8 +736,7 @@ class CustomTilemap
if @layer0clip
@layer0.ox = 0
@layer0.oy = 0
@layer0.src_rect.set(width>>2,height>>2,
@viewport.rect.width,@viewport.rect.height)
@layer0.src_rect.set(width >> 2, height >> 2, @viewport.rect.width, @viewport.rect.height)
else
@layer0.ox = (width >> 2)
@layer0.oy = (height >> 2)
@@ -753,8 +749,8 @@ class CustomTilemap
yStart = @oyLayer0 / theight
yStart = 0 if yStart < 0
xEnd = xStart + (width / twidth) + 1
yEnd = yStart+(height/theight)+1
xEnd = xsize if xEnd >= xsize
yEnd = yStart + (height / theight) + 1
yEnd = ysize if yEnd >= ysize
if xStart < xEnd && yStart < yEnd
tmprect = Rect.new(0, 0, 0, 0)
@@ -767,15 +763,16 @@ class CustomTilemap
xpos = (x * twidth) - @oxLayer0
id = mapdata[x, y, z]
next if id == 0 || @priorities[id] != 0 || PBTerrain.hasReflections?(@terrain_tags[id])
if id>=384
tmprect.set( ((id - 384)&7)*@tileSrcWidth,((id - 384)>>3)*@tileSrcHeight,
if id >= 384 # Tileset tiles
tmprect.set(((id - 384) & 7) * @tileSrcWidth,
((id - 384) >> 3) * @tileSrcHeight,
@tileSrcWidth, @tileSrcHeight)
if @diffsizes
bitmap.stretch_blt(Rect.new(xpos, ypos, twidth, theight), @tileset, tmprect)
else
bitmap.blt(xpos, ypos, @tileset, tmprect)
end
else
else # Autotiles
frames = @framecount[id / 48 - 1]
if frames <= 1
frame = 0
@@ -807,50 +804,30 @@ class CustomTilemap
xsize = @map_data.xsize
ysize = @map_data.ysize
minX = (@ox / @tileWidth) - 1
minX = 0 if minX<0
minX = xsize-1 if minX>=xsize
minX.clamp(0, xsize - 1)
maxX = ((@ox + @viewport.rect.width) / @tileWidth) + 1
maxX = 0 if maxX<0
maxX = xsize-1 if maxX>=xsize
maxX.clamp(0, xsize - 1)
minY = (@oy / @tileHeight) - 1
minY = 0 if minY<0
minY = ysize-1 if minY>=ysize
minY.clamp(0, ysize - 1)
maxY = ((@oy + @viewport.rect.height) / @tileHeight) + 1
maxY = 0 if maxY<0
maxY = ysize-1 if maxY>=ysize
maxY.clamp(0, ysize - 1)
count = 0
if minX < maxX && minY < maxY
@usedsprites = usesprites || @usedsprites
if @layer0
@layer0.visible = false if usesprites
end
if @fullyrefreshed
@layer0.visible = false if usesprites && @layer0
if !@priotilesrect || !@priotilesfast ||
@priotilesrect[0]!=minX ||
@priotilesrect[1]!=minY ||
@priotilesrect[2]!=maxX ||
@priotilesrect[3]!=maxY
@priotilesfast = @priotiles.find_all { |tile|
x = tile[0]
y = tile[1]
# "next" means "return" here
next !(x<minX || x>maxX || y<minY || y>maxY)
}
@priotilesrect[0] != minX || @priotilesrect[1] != minY ||
@priotilesrect[2] != maxX || @priotilesrect[3] != maxY
@priotilesrect = [minX, minY, maxX, maxY]
@priotilesfast = []
if @fullyrefreshed
for y in minY..maxY
for x in minX..maxX
next if !@priotiles[[x, y]]
@priotiles[[x, y]].each { |tile| @priotilesfast.push([x, y, tile[0], tile[1]]) }
end
# echoln [minX,minY,maxX,maxY,@priotilesfast.length,@priotiles.length]
for prio in @priotilesfast
xpos = (prio[0]*@tileWidth)-@ox
ypos = (prio[1]*@tileHeight)-@oy
count = addTile(@tiles,count,xpos,ypos,prio[3])
end
else
if !@priotilesrect || !@priotilesfast ||
@priotilesrect[0]!=minX ||
@priotilesrect[1]!=minY ||
@priotilesrect[2]!=maxX ||
@priotilesrect[3]!=maxY
@priotilesfast=[]
for z in 0...@map_data.zsize
for y in minY..maxY
for x in minX..maxX
@@ -861,7 +838,7 @@ class CustomTilemap
end
end
end
@priotilesrect = [minX,minY,maxX,maxY]
end
end
for prio in @priotilesfast
xpos = (prio[0] * @tileWidth) - @ox
@@ -869,10 +846,10 @@ class CustomTilemap
count = addTile(@tiles, count, xpos, ypos, prio[3])
end
end
end
if count < @tiles.length
bigchange = (count<=(@tiles.length*2/3)) && (@tiles.length*2/3)>25
j = count; len = @tiles.length
bigchange = (count <= (@tiles.length * 2 / 3)) && @tiles.length > 40
j = count
len = @tiles.length
while j < len
sprite = @tiles[j]
@tiles[j + 1] = -1
@@ -923,15 +900,9 @@ class CustomTilemap
refresh_autotiles
repaintAutotiles
end
if @flashChanged
refresh_flash
end
if @tilesetChanged
refresh_tileset
end
if @flash
@flash.opacity = FlashOpacity[(Graphics.frame_count/2) % 6]
end
refresh_flash if @flashChanged
refresh_tileset if @tilesetChanged
@flash.opacity = FlashOpacity[(Graphics.frame_count / 2) % 6] if @flash
mustrefresh = (@oldOx != @ox || @oldOy != @oy || @tilesetChanged || @autotiles.changed)
if @viewport.ox != @oldViewportOx || @viewport.oy != @oldViewportOy
mustrefresh = true
@@ -939,7 +910,7 @@ class CustomTilemap
@oldViewportOy = @viewport.oy
end
refresh if mustrefresh
if (Graphics.frame_count % Animated_Autotiles_Frames == 0) || @nowshown
if (Graphics.frame_count % Animated_Autotiles_Frames) == 0 || @nowshown
repaintAutotiles
refresh(true)
end