Homebrew [Release] Lua Player Plus 3DS (lpp-3ds) - LUA interpreter for 3DS

  • Thread starter Thread starter Rinnegatamante
  • Start date Start date
  • Views Views 187,241
  • Replies Replies 1,199
  • Likes Likes 35
Here's my full .lua:
Code:
while true do
     -- updating screens
     Screen.waitVblankStart()
     Screen.refresh()

     Screen.flip()

     -- Variable initializing
     if (init == nil) then
       fontsize = 16
       mchr = 45
       curline = 0
       mlines = 13
       marginx = 10
       marginy = 10
       posline = marginy
      
       init = true
     end
    
     -- Font definitions
     defaultfont = Font.load("monof55.ttf") -- use mono-spaced fixed width fonts
     textcolor = Color.new(255,255,255)
     Font.setPixelSizes(defaultfont,fontsize)
      
     -- Wait for buttons
     if (Controls.check(Controls.read(),KEY_HOME)) then System.exit() end

     if (Controls.check(Controls.read(),KEY_A)) then newline("testing") end

     -- Functions

     -- Text processing
     function newline (text)
       strsize = string.len(text)
      
       if (strsize >= mchr) then
         fullline = string.sub(text,0,42)
         splitchr = fullline:find"%s$" or fullline:find"%s%S-$" or mchr
        
         newtext = string.sub(text,0,splitchr)
         addline(newtext)
        
         nexttext = string.sub(text,splitchr+1)
         newline(nexttext)
        
       else
         addline (text)
       end

     end

        
     function addline(val)
       --if (curline > mlines) then clear() end
       curline = curline+1
       Font.print(defaultfont, marginx, posline, val, textcolor, BOTTOM_SCREEN)
       posline = posline + fontsize
     end

end

It's not working as intended btw. So I'm doing some experiments.

How can I be sure I'm purging everything? If I don't know that maybe I'm not purging anything.
Anyway, I'm using the provided demos and documentation to help me.
 
Here's my full .lua:
Code:
while true do
     -- updating screens
     Screen.waitVblankStart()
     Screen.refresh()

     Screen.flip()

     -- Variable initializing
     if (init == nil) then
       fontsize = 16
       mchr = 45
       curline = 0
       mlines = 13
       marginx = 10
       marginy = 10
       posline = marginy
     
       init = true
     end
   
     -- Font definitions
     defaultfont = Font.load("monof55.ttf") -- use mono-spaced fixed width fonts
     textcolor = Color.new(255,255,255)
     Font.setPixelSizes(defaultfont,fontsize)
     
     -- Wait for buttons
     if (Controls.check(Controls.read(),KEY_HOME)) then System.exit() end

     if (Controls.check(Controls.read(),KEY_A)) then newline("testing") end

     -- Functions

     -- Text processing
     function newline (text)
       strsize = string.len(text)
     
       if (strsize >= mchr) then
         fullline = string.sub(text,0,42)
         splitchr = fullline:find"%s$" or fullline:find"%s%S-$" or mchr
       
         newtext = string.sub(text,0,splitchr)
         addline(newtext)
       
         nexttext = string.sub(text,splitchr+1)
         newline(nexttext)
       
       else
         addline (text)
       end

     end

       
     function addline(val)
       --if (curline > mlines) then clear() end
       curline = curline+1
       Font.print(defaultfont, marginx, posline, val, textcolor, BOTTOM_SCREEN)
       posline = posline + fontsize
     end

end

It's not working as intended btw. So I'm doing some experiments.

How can I be sure I'm purging everything? If I don't know that maybe I'm not purging anything.
Anyway, I'm using the provided demos and documentation to help me.

You're not unloading Font while exiting causing 2 problems:
1) Handles not correctly closed leading to no more handles to access SD contents.
2) Memory leak.
 
Is there an easy way of drawing images with transparencies? Or would I need to check each pixel for a "magic" pixel and print each pixel separately?
 
Sorry to bother you again with lua problems Rinnegatamante, but here goes:

I want to print a text to the screen with a function to be triggered when I press the A key. Alright, this part I can get done with
Code:
  pad = Controls.read()
   if (Controls.check(pad,KEY_A)) and not (Controls.check(oldpad,KEY_A)) then
and the function working outside the main loop.

But when I do that, the text I've added starts flickering onscreen.

However, when I insert some text with debugprint inside the main loop the text works as intended.

What should I do to make this dynamic text behave correctly?

I'm running out of ideas here :/.

I could write everything to an array then reposition everything inside it and print x amount of lines (re-printing even empty lines) for every time the loop cycles (I guess it cycles once per frame?) but that would look terrible. Is this the only way?

Also, what is the cause of the flickering? I was so excited with LPP

Here is my code, once again.
Argh, this flickering is killing me :(.
Code:
-- Variable initializing
-- This time, outside the main loop (lol)
fontsize = 16
mchr = 34
curline = 0
mlines = 13
marginx = 10
marginy = 10
posline = marginy
oldpad = 0
textcolor = Color.new(255,255,255)

-- Functions
-- This time, outside the main loop (lol noob)
function newline (text)
   strsize = string.len(text)

  -- this and the next function are mostly a text position calculator and an automatic text wrapper
   if (strsize >= mchr) then
     fullline = string.sub(text,0,mchr)
     splitchr = fullline:find"%s$" or fullline:find"%s%S-$" or mchr

     newtext = string.sub(text,0,splitchr)
     addline(newtext)

     nexttext = string.sub(text,splitchr+1)
     newline(nexttext)

   else
     addline (text)
   end
end
    
function addline(val)
   curline = curline+1
   if (curline <= mlines) then
   --Font.print(defaultfont, marginx, posline, val, textcolor, BOTTOM_SCREEN) --I ve disabled fonts for now - to avoid the memory leak problem - even then it still persists though :/
   Screen.debugPrint(marginx,posline,val,textcolor,BOTTOM_SCREEN)
   posline = posline + fontsize
   end
end

-- MAIN LOOP YAY
while true do
   Screen.refresh()

   --defaultfont = Font.load("monof55.ttf")
   --Font.setPixelSizes(defaultfont,fontsize)

   pad = Controls.read()
   if (Controls.check(pad,KEY_A)) and not (Controls.check(oldpad,KEY_A)) then
     newline("The quick brown fox jumps over the lazy dog.");
   end

   if (Controls.check(Controls.read(),KEY_HOME)) then
     --Font.unload(defaultfont)
     System.exit()
   end


   --Screen.fillRect(5, 315, 5, 235, Color.new(255,0,0), BOTTOM_SCREEN) --If I draw this rectangle, the text doesn't even appear anymore.

   --Font.unload(defaultfont)
   oldpad = pad

   Screen.flip()
   Screen.waitVblankStart()

end
 
Last edited by phalk,
Sorry to bother you again with lua problems Rinnegatamante, but here goes:

I want to print a text to the screen with a function to be triggered when I press the A key. Alright, this part I can get done with
Code:
  pad = Controls.read()
   if (Controls.check(pad,KEY_A)) and not (Controls.check(oldpad,KEY_A)) then
and the function working outside the main loop.

But when I do that, the text I've added starts flickering onscreen.

However, when I insert some text with debugprint inside the main loop the text works as intended.

What should I do to make this dynamic text behave correctly?

I'm running out of ideas here :/.

I could write everything to an array then reposition everything inside it and print x amount of lines (re-printing even empty lines) for every time the loop cycles (I guess it cycles once per frame?) but that would look terrible. Is this the only way?

Also, what is the cause of the flickering? I was so excited with LPP

Here is my code, once again.
Argh, this flickering is killing me :(.
Code:
-- Variable initializing
-- This time, outside the main loop (lol)
fontsize = 16
mchr = 34
curline = 0
mlines = 13
marginx = 10
marginy = 10
posline = marginy
oldpad = 0
textcolor = Color.new(255,255,255)

-- Functions
-- This time, outside the main loop (lol noob)
function newline (text)
   strsize = string.len(text)

  -- this and the next function are mostly a text position calculator and an automatic text wrapper
   if (strsize >= mchr) then
     fullline = string.sub(text,0,mchr)
     splitchr = fullline:find"%s$" or fullline:find"%s%S-$" or mchr

     newtext = string.sub(text,0,splitchr)
     addline(newtext)

     nexttext = string.sub(text,splitchr+1)
     newline(nexttext)

   else
     addline (text)
   end
end
   
function addline(val)
   curline = curline+1
   if (curline <= mlines) then
   --Font.print(defaultfont, marginx, posline, val, textcolor, BOTTOM_SCREEN) --I ve disabled fonts for now - to avoid the memory leak problem - even then it still persists though :/
   Screen.debugPrint(marginx,posline,val,textcolor,BOTTOM_SCREEN)
   posline = posline + fontsize
   end
end

-- MAIN LOOP YAY
while true do
   Screen.refresh()

   --defaultfont = Font.load("monof55.ttf")
   --Font.setPixelSizes(defaultfont,fontsize)

   pad = Controls.read()
   if (Controls.check(pad,KEY_A)) and not (Controls.check(oldpad,KEY_A)) then
     newline("The quick brown fox jumps over the lazy dog.");
   end

   if (Controls.check(Controls.read(),KEY_HOME)) then
     --Font.unload(defaultfont)
     System.exit()
   end


   --Screen.fillRect(5, 315, 5, 235, Color.new(255,0,0), BOTTOM_SCREEN) --If I draw this rectangle, the text doesn't even appear anymore.

   --Font.unload(defaultfont)
   oldpad = pad

   Screen.flip()
   Screen.waitVblankStart()

end

You should blit twice your stuffs on screen refreshing it between the two calls. You can use this function from Sunshell to simplify the thing: https://github.com/Rinnegatamante/Sunshell/blob/master/LUA/scripts/funcs.lua#L301-L308
 
  • Like
Reactions: phalk
Is there a way to load ogg files faster?

Streaming does not seems to be working for ogg. (I've tried modifying the audio example to make sure my code was not at fault this time).
 
Is there a way to load ogg files faster?

Streaming does not seems to be working for ogg. (I've tried modifying the audio example to make sure my code was not at fault this time).

Streaming works fine, are you sure to had used updateStream in your main loop? You should do an updateStream call like once every 2/3 seconds.
 
Streaming works fine, are you sure to had used updateStream in your main loop? You should do an updateStream call like once every 2/3 seconds.

Yeah, I tried leaving updateStream it in the main loop but it froze, didn't even start playing the song. Maybe my ogg files are messy? (I've used audacity to convert).
I'm gonna try setting up a timer to refresh it only after 2 or 3 seconds. I should try again later, maybe I really did something wrong (again). Was too sleepy yesterday trying to finish the initial feature set of crimson scripter rofl
 
Yeah, I tried leaving updateStream it in the main loop but it froze, didn't even start playing the song. Maybe my ogg files are messy? (I've used audacity to convert).
I'm gonna try setting up a timer to refresh it only after 2 or 3 seconds. I should try again later, maybe I really did something wrong (again). Was too sleepy yesterday trying to finish the initial feature set of crimson scripter rofl

Are you using r4 or nightly build? Cause nightly build has audio streaming bugged and got solved recently with this commit: https://github.com/Rinnegatamante/lpp-3ds/commit/29e67a2abb88bda881317c0de81d72791364ea19
 
I've managed to make it work again using R4 (grr, I hate you, System.currentDirectory()).
However, R4 seems to be more unstable with my .lua than the nightly I was using (random freezing happens sometimes when launching the .3dsx)

Also, I got a problem with streaming ogg audio:
Everytime I print more than one line at a time to the screen using my custom font, the music stutters. It seems like it's a problem with processing power, I don't know.

I use loops to print more than a line at once. It seems the problem lies there, because when I input line by line manually (no looping involved), the stuttering diminishes.

Is there a way to avoid this music stuttering?
 
I was trying to make a keyboard in lpp-3ds (using HBKBLib's keyboard images because the native one is fugly)
I'm having a problem I don't understand. Eeverytime I try to run that, it just freezes on black screen and I have to reboot by holding the power button. If I only use the code Inside the function it works fine though (using r4 .3dsx)
Here's the full index.lua:
EDIT:apparently copy/paste messed up the indentation. sorry!
--attempt at a keyboard in lpp-3ds
--using HBKBLib (by jbr373) keyboard images

white = Color.new(255,255,255)

Screen.debugPrint(5,5,"hello !", white, TOP_SCREEN)
while true do
Screen.refresh()
pad = Controls.read()
if oldPad ~= pad then
if Controls.check(pad,KEY_START) then
System.exit()
elseif Controls.check(pad,KEY_A) then
Screen.clear(TOP_SCREEN)
word = keyboard("What's your name?")
Screen.clear(TOP_SCREEN)
Screen.debugPrint(5,50,"hello "..word.." !", white, TOP_SCREEN)
end
end
end

function keyboard(question)
state = 1
oldstate = 0
X, Y, line, column = 0, 0, 0, 0
oldPos = -1
offX = 32
offY = 40
text = ""
states = { "abc_lower", "abc_upper", "special"}
numbers = {"1","2","3","4","5","6","7","8","9","0"}
line2_abc_lower = {"q","w","e","r","t","y","u","i","o","p"}
line3_abc_lower = {"a","s","d","f","g","h","j","k","l"}
line4_abc_lower = {"z","x","c","v","b","n","m"}
line2_abc_upper = {"Q","W","E","R","T","Y","U","I","O","P"}
line3_abc_upper = {"A","S","D","F","G","H","J","K","L"}
line4_abc_upper = {"W","X","C","V","B","N","M"}
line2_special = {"!",'"',"ยง","$","%","&","/", "(",")","?"}
line3_special = {"*","'","<",">","+","[","]","{","}"}
line4_special = {",",";",".",":","-","_","="}
image = Screen.loadImage(System.currentDirectory().."/hbkb_abc_lower.png")
Screen.debugPrint(5,5,question,white,TOP_SCREEN)
while true do
Screen.refresh()
if oldstate ~= state then
Screen.freeImage(image)
oldstate = state
image = Screen.loadImage(System.currentDirectory().."/hbkb_"..states[state]..".png")
Screen.clear(BOTTOM_SCREEN)
Screen.drawImage(0, 0, image, BOTTOM_SCREEN)
end
X, Y = Controls.readTouch()
if X+Y > 0 then
line = math.floor(Y/offY)+1
column = math.floor(X/offX)+1
else
line = 0
column = 0
end
if oldPos ~= line+column then
oldPos = line+column
if line == 1 then
text = text..numbers[column]
elseif line == 2 then
cLine = _G["line2_"..states[state]]
text = text..cLine[column]
elseif line == 3 then
if column == 10 then
text = string.sub(text,1,-2)
else
cLine = _G["line3_"..states[state]]
text = text..cLine[column]
end
elseif line == 4 then
if column <= 2 then
if state == 3 then state = 1 else state = 3 end
elseif column == 10 then
text = string.sub(text,1,-2)
else
cLine = _G["line4_"..states[state]]
text = text..cLine[column-2]
end
elseif line == 5 then
if column <= 2 then
if state == 2 then state = 1 else state = 2 end
caps = true
elseif column >= 9 then
if state == 2 then state = 1 else state = 2 end
caps = false
else
text = text.." "
end
elseif line == 6 then
if column > 5 then
text = "validate"
else
text = "cancel"
end
end
Screen.clear(TOP_SCREEN)
Screen.debugPrint(5,5,question,white,TOP_SCREEN)
Screen.debugPrint(5,20,text,white,TOP_SCREEN)
end
pad = Controls.read()
if pad ~= oldPad then
oldPad = pad
if Controls.check(pad,KEY_START) then
Screen.freeImage(image)
System.exit()
elseif Controls.check(pad, KEY_B) then
text = string.sub(text, 1, -2)
elseif Controls.check(pad, KEY_A) then
Screen.freeImage(image)
Screen.clear(BOTTOM_SCREEN)
return text
end
end
end
end
 
I've managed to make it work again using R4 (grr, I hate you, System.currentDirectory()).
However, R4 seems to be more unstable with my .lua than the nightly I was using (random freezing happens sometimes when launching the .3dsx)

Also, I got a problem with streaming ogg audio:
Everytime I print more than one line at a time to the screen using my custom font, the music stutters. It seems like it's a problem with processing power, I don't know.

I use loops to print more than a line at once. It seems the problem lies there, because when I input line by line manually (no looping involved), the stuttering diminishes.

Is there a way to avoid this music stuttering?

Try to add a descriptor forcing csnd:SND usage. (There is a pre-builded one in r4 release)

I was trying to make a keyboard in lpp-3ds (using HBKBLib's keyboard images because the native one is fugly)
I'm having a problem I don't understand. Eeverytime I try to run that, it just freezes on black screen and I have to reboot by holding the power button. If I only use the code Inside the function it works fine though (using r4 .3dsx)
Here's the full index.lua:
EDIT:apparently copy/paste messed up the indentation. sorry!
--attempt at a keyboard in lpp-3ds
--using HBKBLib (by jbr373) keyboard images

white = Color.new(255,255,255)

Screen.debugPrint(5,5,"hello !", white, TOP_SCREEN)
while true do
Screen.refresh()
pad = Controls.read()
if oldPad ~= pad then
if Controls.check(pad,KEY_START) then
System.exit()
elseif Controls.check(pad,KEY_A) then
Screen.clear(TOP_SCREEN)
word = keyboard("What's your name?")
Screen.clear(TOP_SCREEN)
Screen.debugPrint(5,50,"hello "..word.." !", white, TOP_SCREEN)
end
end
end

function keyboard(question)
state = 1
oldstate = 0
X, Y, line, column = 0, 0, 0, 0
oldPos = -1
offX = 32
offY = 40
text = ""
states = { "abc_lower", "abc_upper", "special"}
numbers = {"1","2","3","4","5","6","7","8","9","0"}
line2_abc_lower = {"q","w","e","r","t","y","u","i","o","p"}
line3_abc_lower = {"a","s","d","f","g","h","j","k","l"}
line4_abc_lower = {"z","x","c","v","b","n","m"}
line2_abc_upper = {"Q","W","E","R","T","Y","U","I","O","P"}
line3_abc_upper = {"A","S","D","F","G","H","J","K","L"}
line4_abc_upper = {"W","X","C","V","B","N","M"}
line2_special = {"!",'"',"ยง","$","%","&","/", "(",")","?"}
line3_special = {"*","'","<",">","+","[","]","{","}"}
line4_special = {",",";",".",":","-","_","="}
image = Screen.loadImage(System.currentDirectory().."/hbkb_abc_lower.png")
Screen.debugPrint(5,5,question,white,TOP_SCREEN)
while true do
Screen.refresh()
if oldstate ~= state then
Screen.freeImage(image)
oldstate = state
image = Screen.loadImage(System.currentDirectory().."/hbkb_"..states[state]..".png")
Screen.clear(BOTTOM_SCREEN)
Screen.drawImage(0, 0, image, BOTTOM_SCREEN)
end
X, Y = Controls.readTouch()
if X+Y > 0 then
line = math.floor(Y/offY)+1
column = math.floor(X/offX)+1
else
line = 0
column = 0
end
if oldPos ~= line+column then
oldPos = line+column
if line == 1 then
text = text..numbers[column]
elseif line == 2 then
cLine = _G["line2_"..states[state]]
text = text..cLine[column]
elseif line == 3 then
if column == 10 then
text = string.sub(text,1,-2)
else
cLine = _G["line3_"..states[state]]
text = text..cLine[column]
end
elseif line == 4 then
if column <= 2 then
if state == 3 then state = 1 else state = 3 end
elseif column == 10 then
text = string.sub(text,1,-2)
else
cLine = _G["line4_"..states[state]]
text = text..cLine[column-2]
end
elseif line == 5 then
if column <= 2 then
if state == 2 then state = 1 else state = 2 end
caps = true
elseif column >= 9 then
if state == 2 then state = 1 else state = 2 end
caps = false
else
text = text.." "
end
elseif line == 6 then
if column > 5 then
text = "validate"
else
text = "cancel"
end
end
Screen.clear(TOP_SCREEN)
Screen.debugPrint(5,5,question,white,TOP_SCREEN)
Screen.debugPrint(5,20,text,white,TOP_SCREEN)
end
pad = Controls.read()
if pad ~= oldPad then
oldPad = pad
if Controls.check(pad,KEY_START) then
Screen.freeImage(image)
System.exit()
elseif Controls.check(pad, KEY_B) then
text = string.sub(text, 1, -2)
elseif Controls.check(pad, KEY_A) then
Screen.freeImage(image)
Screen.clear(BOTTOM_SCREEN)
return text
end
end
end
end

Problem is probably in your debugPrint. Before calling any kind of printing function (for Screen module) you have to call at least once Screen.refresh()
 

Site & Scene News

Popular threads in this forum