Log In  

If you port your PICO-8 game to any European languages other than English, you might want to include some character accents, which doesn't work by default since PICO-8 only supports ASCII characters (plus some special ones in the unused range between ASCII and ISO-8859-1).

My work here is based off of Zep's post from a couple weeks ago about Latin accent printing (by including special characters in front of plain ASCII characters to indicate accents). My code goes a step further by offering a way to save your strings with the real accent characters included, and then encode them so they can be printed properly. This way your text is a bit more readable in the source file.

I slightly modified Zep's original print function by changing the : control character to @ since I needed : in my printed text. The general rule, if you want to add new encoded characters to this system, is to pick a control character that won't be needed in the actual printed text.

FUNCTIONS:

  • print_with_accents is the modified version of Zep's Latin accent string printing function. It does not print strings with Latin accent characters. It prints strings that have been processed already by encode_accents.
  • encode_accents accepts a string and returns the "encoded" version made up of plain ASCII characters.
  • encode_table takes a table full of string values (can be nested in subtables) and converts all the strings using encode_accents. It does this in-place and doesn't return any value.

HANDLING ADDITIONAL ACCENT CHARACTERS:

A bunch of cases are included in encode_accents to cover all the standard French accents, but if you need to handle another case (like the ñ in Spanish) you can include it manually! For accents that haven't already been handled for any letters, you'll want to also include a new row in the dat table for print_with_accents

ACCOUNTING FOR SPECIAL PICO-8 CHARACTERS:

The printer generally assumes that printed characters will take up 4 pixels of horizontal space, but the special upper range PICO-8 characters (e.g. 🅾️, ❎) actually take up 8 pixels, so we have to account for this in print_with_accents. If you need to include any of those characters in your printed text, make sure to add that check in the code.

HOW TO USE FEWER TOKENS:

If you're worried about the number of tokens you would need to include encode_accents and encode_table, you can use those functions in combination with a table serializer like pico8-table-string to encode your table at build time in a separate Lua script.

function print_with_accents(str,x,y,col)
 local dat={
  ["#"] = {",",  0, 2},
  ["^"] = {"^",  0,-3},
  ["`"] = {"`", -1,-3},
  ["|"] = {",",  1,-6},
  ["@"] = {"\"", 0,-3}
 }
 local p = 1
 while p <= #str do
  local c=sub(str,p,p)
  if dat[c] then
   print(
    dat[c][1],
    x + dat[c][2],
    y + dat[c][3],
    col
   )
   p += 1
   c = sub(str,p,p)
  end
  print(c, x, y, col)
  x += 4 p += 1
  if (
   c == '🅾️' or
   c == '❎' or
   c == '♪'
  ) then
   x += 4
  end
 end
end

function encode_accents(str)
 local new_str = ""
 local i = 0
 while i <= #str do
  -- two byte compare string
  local c = sub(str,i,i+1)
  -- one byte default
  local e = sub(str,i,i)
  -- cedille ¸
  if c == "ç" then
   e="#c"
  -- aigu ˊ
  elseif c == "é" then
   e="|e"
  -- circonflexe ˆ
  elseif c == "â" then
   e="^a"
  elseif c == "ê" then
   e="^e"
  elseif c == "î" then
   e="^i"
  elseif c == "ô" then
   e="^o"
  elseif c == "û" then
   e="^u"
  -- grave ˋ
  elseif c == "à" then
   e="`a"
  elseif c == "è" then
   e="`e"
  elseif c == "ì" then
   e="`i"
  elseif c == "ò" then
   e="`o"
  elseif c == "ù" then
   e="`u"
  -- tréma ¨
  elseif c == "ë" then
   e="@e"
  elseif c == "ï" then
   e="@i"
  elseif c == "ü" then
   e='@u'
  end
  new_str=new_str..e
  if e ~= sub(str,i,i) then
    i = i + 1
  end
  i = i + 1
 end
 return new_str
end

function encode_table(table)
 for k,v in pairs(table) do
  if type(v) == "table" then
   encode_table(v)
  else
   table[k]=encode_accents(v)
  end
 end
end
P#64966 2019-06-03 03:49 ( Edited 2019-06-03 03:54)


[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-03-29 13:15:58 | 0.005s | Q:9