Fixed some broken functions, added Lua test script to tools

Note I only plan to add files to the tools folder that are helpful for quickly testing the implemented Lua functions. (they aren't used in the build.)
This commit is contained in:
NPO 2024-10-16 19:36:22 -04:00
parent 487b80c06a
commit 7283efcb03
5 changed files with 203 additions and 7 deletions

View File

@ -258,6 +258,7 @@ public:
bool doAudioSync;
melonDS::u32 getInputMask(){return inputMask;}
std::map<melonDS::u8,bool> KeyboardMask; //For Lua Scripts
private:
std::unique_ptr<melonDS::Savestate> backupState;

View File

@ -71,6 +71,10 @@ void EmuInstance::inputInit()
hotkeyMask = 0;
lastHotkeyMask = 0;
for (int i=0;i<256;i++){
KeyboardMask[i]=false;
}
joystick = nullptr;
controller = nullptr;
hasRumble = false;
@ -218,6 +222,11 @@ int getEventKeyVal(QKeyEvent* event)
void EmuInstance::onKeyPress(QKeyEvent* event)
{
if (event->key()<256)
KeyboardMask[event->key()]=true;
if ((event->key()&0x01000000)>0 && (event->key()&0xff)<0x41) //special keys (there is probably a less messy way to write this...)
KeyboardMask[0xff - (event->key()&0xff)]=true;
int keyHK = getEventKeyVal(event);
int keyKP = keyHK;
keyStrokes.push_back(keyHK);
@ -235,6 +244,11 @@ void EmuInstance::onKeyPress(QKeyEvent* event)
void EmuInstance::onKeyRelease(QKeyEvent* event)
{
if (event->key()<256)
KeyboardMask[event->key()]=false;
if ((event->key()&0x01000000)>0 && (event->key()&0xff)<0x41)//special keys
KeyboardMask[0xff - (event->key()&0xff)]=false;
int keyHK = getEventKeyVal(event);
int keyKP = keyHK;
if (event->modifiers() != Qt::KeypadModifier)

View File

@ -361,18 +361,20 @@ AddLuaFunction(Lua_Reads32,Reads32);
int Lua_NDSTapDown(lua_State* L)
{
LuaBundle* bundle = get_bundle(L);
melonDS::NDS* nds = bundle->getEmuInstance()->getNDS();
int x = luaL_checkinteger(L,1);
int y = luaL_checkinteger(L,2);
//TODO
//NDS::TouchScreen(x,y);
nds->TouchScreen(x,y);
return 0;
}
AddLuaFunction(Lua_NDSTapDown,NDSTapDown);
int Lua_NDSTapUp(lua_State* L)
{
//TODO
//NDS::ReleaseScreen();
LuaBundle* bundle = get_bundle(L);
melonDS::NDS* nds = bundle->getEmuInstance()->getNDS();
nds->ReleaseScreen();
return 0;
}
AddLuaFunction(Lua_NDSTapUp,NDSTapUp);
@ -424,6 +426,19 @@ int Lua_getMouse(lua_State* L)
}
AddLuaFunction(Lua_getMouse,GetMouse);
int Lua_KeyboardMask(lua_State* L)
{
LuaBundle* bundle = get_bundle(L);
lua_createtable(L,0,256);
for (int i=0;i<256;i++){
lua_pushboolean(L,bundle->getEmuInstance()->KeyboardMask[i]);
lua_seti(L,-2,i);
}
return 1;
}
AddLuaFunction(Lua_KeyboardMask,KeyboardMask);
/*--------------------------------------------------------------------------------------------------
Front-end lua function definitions
--------------------------------------------------------------------------------------------------*/
@ -472,7 +487,7 @@ int Lua_Flip(lua_State* L)
}
AddLuaFunction(Lua_Flip,Flip);
//text(int x, int y, string message, [u32 color = 'black'], [int fontsize = 9], [string fontfamily = Franklin Gothic Medium])
//Text(int x, int y, string message, [u32 color = 'black'], [int fontsize = 9], [string fontfamily = Franklin Gothic Medium])
int Lua_text(lua_State* L)
{
LuaBundle* bundle = get_bundle(L);
@ -538,6 +553,21 @@ int Lua_fillrect(lua_State* L)
}
AddLuaFunction(Lua_fillrect,FillRect);
int Lua_Ellipse(lua_State* L)
{
LuaBundle* bundle = get_bundle(L);
melonDS::u32 color = luaL_checknumber(L,5);
int x = luaL_checknumber(L,1);
int y = luaL_checknumber(L,2);
int width = luaL_checknumber(L,3);
int height = luaL_checknumber(L,4);
QPainter painter(bundle->luaCanvas->imageBuffer);
painter.setPen(color);
painter.drawEllipse(x,y,width,height);
return 0;
}
AddLuaFunction(Lua_Ellipse,Ellipse);
int Lua_keystrokes(lua_State* L)
{
LuaBundle* bundle = get_bundle(L);
@ -548,7 +578,6 @@ int Lua_keystrokes(lua_State* L)
lua_seti(L,-2,i);
}
bundle->getEmuInstance()->keyStrokes.clear();
lua_createtable(L,0,1);
return 1;
}
AddLuaFunction(Lua_keystrokes,Keys);
@ -590,7 +619,6 @@ AddLuaFunction(Lua_clearImageHash,ClearHash);
int Lua_getJoy(lua_State* L)
{
//TODO:
LuaBundle* bundle = get_bundle(L);
melonDS::u32 buttonMask=bundle->getEmuInstance()->getInputMask();//current button state.
const char* keys[12] =

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.1 KiB

View File

@ -0,0 +1,153 @@
-- Simple Script to test most of the different Lua Functions for MelonDS
-- Written by NPO197
MelonClear()
MelonPrint("This text Should be cleared")
MelonClear()
MelonPrint("Running Test...")
u32Data = Readu32(0x00000000)
MelonPrint(string.format("DataZero: %x",u32Data))
NDSTapDown(0,0)
NDSTapUp()
--StateSave("SaveState_Auto")
--StateLoad("SaveState_Auto")
canvas = MakeCanvas(0,0,500,500)
SetCanvas(canvas)
ClearOverlay()
FillRect(0,0,55,11,0xffffffff)
Text(0,9,"Test Message")
Line(0,10,55,10,0x00ff00ff)
Rect(0,0,55,11,0xff00ff00)
DrawImage("Lua-Logo_128x128.png",0,60)
Text(0,200,"WASD to move \"lua Stylus\", Q to tap screen",0xffffff)
--ClearHash()
Flip()
-------- Main Loop ----------
typed = ""
keys = {}
joys = {}
--protected ints -> string
function pInts2Str(ints)
str = ""
for _,i in pairs(ints) do
if pcall(string.char,i) then
str = str..string.char(i)
else
MelonPrint("NonAscii:"..i)
typed = ""
end
end
return str
end
textFunctions = {
--MousePosText
[1] = function()
mouse = GetMouse()
return "MousePos:"..mouse.X..","..mouse.Y
end,
--MouseButtonText
[2] = function()
mouse = GetMouse()
str = ""
for k,v in pairs(mouse) do
if k~="X" and k~="Y" and v then
str = str..k
end
end
return "MouseBtn:"..str
end,
--KeysText
[3] = function()
keys = Keys()
temp = pInts2Str(keys)
typed = typed..temp
return "Keys:"..typed
end,
--JoyText
[4] = function()
joys = GetJoy()
str = ""
for k,v in pairs(joys) do
if v then
str = str..k
end
end
return "Joy:"..str
end
}
function TextLoop()
SetCanvas(textCanvas)
ClearOverlay()
y = 0
for _,tfunct in ipairs(textFunctions) do
y = y+10
Text(0,y,tfunct(),0xffffff)
end
Flip()
end
Stylus = {
x = 0,
y = 0,
}
function Stylus:Loop()
move = {
--Key = {dx,dy}
["W"] = {0,-1},
["A"] = {-1,0},
["S"] = {0,1},
["D"] = {1,0}
}
mask = KeyboardMask()
for tkey,dir in pairs(move) do
if mask[string.byte(tkey)] then
self.x=self.x+dir[1]
self.y=self.y+dir[2]
end
end
if mask[string.byte("Q")] then
NDSTapDown(self.x,self.y)
else
NDSTapUp()
end
SetCanvas(vstylusCanvas)
ClearOverlay()
Ellipse(self.x-5,self.y-5,10,10,0xffffffff)
Ellipse(self.x-2,self.y-2,4,4,0x00000000)
Flip()
end
textCanvas = MakeCanvas(0,12,500,100)
vstylusCanvas = MakeCanvas(0,0,256,192,1) -- bottom screen
function _Update()
TextLoop()
Stylus:Loop()
end