Reduced memory usage of BigLetter library

This commit is contained in:
Igor Timofeev 2019-02-15 10:27:08 +03:00
parent 0eb990a87f
commit 0e3acc1e95
2 changed files with 612 additions and 607 deletions

View File

@ -1,27 +1,36 @@
{ {
label = "Beer", label = "Companion Cube",
tooltip = "Cold wheat beer... mmmmm",
shapes = { shapes = {
{ 0, 0, 2, 7, 1, 5, texture = "minecraft:blocks/log_spruce" }, -- Center
{ 1, 0, 1, 6, 1, 6, texture = "minecraft:blocks/log_spruce" }, {2, 2,2, 14, 14, 14, texture = "wool_colored_gray"},
{ 2, 0, 0, 5, 1, 7, texture = "minecraft:blocks/log_spruce" },
{ 0, 1, 2, 1, 7, 5, texture = "minecraft:blocks/log_spruce" }, -- Corners
{ 1, 1, 1, 2, 7, 2, texture = "minecraft:blocks/log_spruce" }, { 0, 0, 0, 5, 5, 5, texture = "wool_colored_silver"},
{ 1, 1, 5, 2, 7, 6, texture = "minecraft:blocks/log_spruce" }, { 0, 0, 11, 5, 5, 16, texture = "wool_colored_silver"},
{ 2, 1, 0, 5, 7, 1, texture = "minecraft:blocks/log_spruce" }, { 0, 11, 0, 5, 16, 5, texture = "wool_colored_silver"},
{ 2, 1, 6, 5, 7, 7, texture = "minecraft:blocks/log_spruce" }, { 0, 11, 11, 5, 16, 16, texture = "wool_colored_silver"},
{ 5, 1, 1, 6, 7, 2, texture = "minecraft:blocks/log_spruce" }, { 11, 0, 0, 16, 5, 5, texture = "wool_colored_silver"},
{ 5, 1, 5, 6, 7, 6, texture = "minecraft:blocks/log_spruce" }, { 11, 0, 11, 16, 5, 16, texture = "wool_colored_silver"},
{ 6, 1, 2, 7, 7, 5, texture = "minecraft:blocks/log_spruce" }, { 11, 11, 0, 16, 16, 5, texture = "wool_colored_silver"},
{ 7, 1, 3, 9, 2, 4, texture = "minecraft:blocks/stone" }, { 11, 11, 11, 16, 16, 16, texture = "wool_colored_silver"},
{ 5, 2, 0, 6, 8, 1, texture = "minecraft:blocks/snow", tint = 16771749 },
{ 6, 2, 1, 7, 8, 2, texture = "minecraft:blocks/snow", tint = 16771749 }, -- Edges
{ 8, 2, 3, 9, 6, 4, texture = "minecraft:blocks/stone" }, { 1, 6, 1, 3, 10, 3, texture = "wool_colored_silver"},
{ 7, 5, 3, 9, 6, 4, texture = "minecraft:blocks/stone" }, { 1, 6, 13, 3, 10, 15, texture = "wool_colored_silver"},
{ 1, 6, 2, 6, 7, 5, texture = "minecraft:blocks/snow", tint = 16771749 }, { 13, 6, 1, 15, 10, 3, texture = "wool_colored_silver"},
{ 2, 6, 1, 5, 8, 5, texture = "minecraft:blocks/snow", tint = 16771749 }, { 13, 6, 13, 15, 10, 15, texture = "wool_colored_silver"},
{ 2, 6, 5, 5, 7, 6, texture = "minecraft:blocks/snow", tint = 16771749 }, { 6, 1, 1, 10, 3, 3, texture = "wool_colored_silver"},
{ 3, 7, 0, 6, 8, 5, texture = "minecraft:blocks/snow", tint = 16771749 }, { 6, 1, 13, 10, 3, 15, texture = "wool_colored_silver"},
{ 3, 8, 1, 6, 9, 4, texture = "minecraft:blocks/snow", tint = 16771749 } { 6, 13, 1, 10, 15, 3, texture = "wool_colored_silver"},
{ 6, 13, 13, 10, 15, 15, texture = "wool_colored_silver"},
{ 1, 1, 6, 3, 3, 10, texture = "wool_colored_silver"},
{ 13, 1, 6, 15, 3, 10, texture = "wool_colored_silver"},
{ 1, 13, 6, 3, 15, 10, texture = "wool_colored_silver"},
{ 13, 13, 6, 15, 15, 10, texture = "wool_colored_silver"},
-- Centers
{ 1, 6, 6, 15, 10, 10, texture = "wool_colored_magenta"},
{ 6, 6, 1, 10, 10, 15, texture = "wool_colored_magenta"},
{ 6, 1, 6, 10, 15, 10, texture = "wool_colored_magenta"}
} }
} }

View File

@ -7,7 +7,8 @@ local lettersInterval = 2
local unknownSymbol = "*" local unknownSymbol = "*"
local spaceWidth = 2 local spaceWidth = 2
local letters = { local function getCharTable(c)
return ({
["0"] = { ["0"] = {
{ 1, 1, 1 }, { 1, 1, 1 },
{ 1, 0, 1 }, { 1, 0, 1 },
@ -544,52 +545,58 @@ local letters = {
{ 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 },
{ 1, 0, 1, 0, 1 }, { 1, 0, 1, 0, 1 },
}, },
} })[c]
end
function bigLetters.draw(x, y, color, symbol, drawWithSymbol) function bigLetters.draw(x, y, color, symbol, drawWithSymbol)
local char
if symbol == " " then if symbol == " " then
return spaceWidth return spaceWidth
elseif not letters[symbol] then
symbol = unknownSymbol
end
for j = 1, #letters[symbol] do
for i = 1, #letters[symbol][j] do
if letters[symbol][j][i] == 1 then
if not drawWithSymbol then
screen.drawRectangle(x + i * 2 - 2, y + (pixelHeight - #letters[symbol]) + j - 1, 2, 1, color, 0xFFFFFF, " ")
else else
screen.drawText(x + i * 2 - 2, y + (pixelHeight - #letters[symbol]) + j - 1, color, "*") char = getCharTable(symbol) or getCharTable(unknownSymbol)
end
for j = 1, #char do
for i = 1, #char[j] do
if char[j][i] == 1 then
if not drawWithSymbol then
screen.drawRectangle(x + i * 2 - 2, y + (pixelHeight - #char) + j - 1, 2, 1, color, 0xFFFFFF, " ")
else
screen.drawText(x + i * 2 - 2, y + (pixelHeight - #char) + j - 1, color, "*")
end end
end end
end end
end end
return #letters[symbol][1] return #char[1]
end end
function bigLetters.drawText(x, y, color, stroka, drawWithSymbol) function bigLetters.drawText(x, y, color, stroka, drawWithSymbol)
checkArg(4, stroka, "string") checkArg(4, stroka, "string")
for i = 1, unicode.len(stroka) do for i = 1, unicode.len(stroka) do
x = x + bigLetters.draw(x, y, color, unicode.sub(stroka, i, i), drawWithSymbol) * 2 + lettersInterval x = x + bigLetters.draw(x, y, color, unicode.sub(stroka, i, i), drawWithSymbol) * 2 + lettersInterval
end end
return x return x
end end
function bigLetters.getTextSize(text) function bigLetters.getTextSize(text)
local width, height = 0, 0 local width, height = 0, 0
local symbol, symbolWidth, symbolHeight local symbol, symbolWidth, symbolHeight, char
for i = 1, unicode.len(text) do for i = 1, unicode.len(text) do
symbol = unicode.sub(text, i, i) symbol = unicode.sub(text, i, i)
if symbol == " " then if symbol == " " then
symbolWidth = spaceWidth symbolWidth = spaceWidth
symbolHeight = 5 symbolHeight = 5
elseif not letters[symbol] then
symbolHeight = #letters[unknownSymbol]
symbolWidth = #letters[unknownSymbol][1]
else else
symbolHeight = #letters[symbol] char = getCharTable(symbol) or getCharTable(unknownSymbol)
symbolWidth = #letters[symbol][1]
symbolHeight = #char
symbolWidth = #char[1]
end end
width = width + symbolWidth * 2 + lettersInterval width = width + symbolWidth * 2 + lettersInterval
@ -600,14 +607,3 @@ function bigLetters.getTextSize(text)
end end
return bigLetters return bigLetters