Merge pull request #3 from Emu-gators/sphang

Sphang
This commit is contained in:
Artur Bekker 2022-11-25 13:00:23 -05:00 committed by GitHub
commit f39792da76
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 227 additions and 11 deletions

42
emugator_log_sphang.txt Normal file
View File

@ -0,0 +1,42 @@
Emugators Log - Steven Phang
11/15/2022
If a lua script is ran without emu.frameadvance(), the whole program hangs, not just the game that is currently being played. For an unknwn reason, the text that is supposed to be displayed with the Emugators_HelloWorld.lua script is only displayed when a ROM is loaded. It disapears after the ROM is closed as well... I spoke with Carsten, and we discussed the idea of using a ROM to represent the DnD GUI. Automatically load this ROM at startup, and this would work nicely. Problem - I dont know how I would go about making the ROM... Regardless, I want to figure out what mechanism is responsible for clearing the screen and pausing the lua script on startup/when a ROM is closed. This may be the key to running the script without a ROM...
This is the mechanism for clearing the screen when the ROM is closed...
See fceu.cpp > FCEU_CloseGame(void)
//clear screen when game is closed
extern uint8 *XBuf;
if (XBuf)
memset(XBuf, 0, 256 * 256);
lua-engine.cpp is the most important file in our universe. It containes the bindings
interesting... lua_yeild and lua_resume in ldo.c may be useful
search emugatordebug comment for things that maybe should be removed for release
steps to implement lua callable c++ function...
add new library (needs to be registered) or simply register new function with existing library (ex. emulib) contained in lua-engine.cpp
ITS RUNNING BUT THERE IS NO FRAME TO ADVANCE!!!!!
emu.frameadvance causes the lua script to yield to main until another emulator frame is processed. Then, something hands control back to the script. But if no game is running (No ROM Loaded), main just updates the display then sleeps for a little bit. Question of the day is What is normally handing control back to the script? It has been staring me in the face all day...FCEU_LuaFrameBoundary()
adding FCEU_LuaFrameBoundary(); to main before it sleeps will allow the lua script to keep running. So we could have a function like emu.frameadvanceAlways() or something that sets a flag that enables FCEU_LuaFrameBoundary() to be called in main (since we dont really want this behavior for ALL lus scripts)... OR maybe add an emu.updateDisplay() function that will allow the display to refresh without yeilding control to main. I think I prefer this option...
*Note: the script still cant draw images on the GUI unless a ROM is loaded with this current method...why?
TODO: figure out how to get draw functions reflected on GUI with no ROM loaded. Come up with better function names, maybe plan out the organization a bit better. Add unload ROM function.
11/18/2022
Looks like the control is not properly being handed back to the lua scrip as I thought it was. I think there is another flag that is checked that is preventing it from returning.
Indeed there was, it works now!frameAdvanceWaiting must be set to true to indicate a thread is waiting to run after a frame is emulated.
11/20/2022
I am going to fix this graphics bug today. I suspect that the buffer I am editing and the buffer that is actually being drawn are two seperate entities, which is causing it to look like my changes to the display buffer are not going through.
From wikipedia "Bit blit (also written BITBLT, BIT BLT, BitBLT, Bit BLT, Bit Blt etc., which stands for bit block transfer) is a data operation commonly used in computer graphics in which several bitmaps are combined into one using a boolean function.[1]"... So that's what the blit function called in FCEUD_Update is doing.
The call to RedrawWindow() after a game is closed in main() has the flags RDW_ERASE and RDW_INVALIDATE. The erase flag clears the window, and the invalidate flag seems to prevent the window from being edited (drawn over). The window must be in a "validated" state for the lua gui to show up. Aditionally, it appears that the screen must be cleared manually after the lua gui is drawn if no frame is emulated.
11/21/2022
I added a function to clear the screen to fceu.cpp. This is not necessary while a game is running (presumeably because game frame data overwrites what was previously there), but is necessary for drawing lua gui with nothing running. With this addition, this aspect of the project (being able to run lua + draw to the gui without a ROM loaded) is functional...but only if a ROM is first opened then closed. Even though I am drawing a new window when lua is loaded, it does not seem to work if loading a script before a game is initially loaded. Again, the script IS running properly... I need to investigate loading a ROM as well as how the main window is being drawn at launch...

View File

@ -0,0 +1,73 @@
--Written by Steven Phang
--EmuGators
--This script is a draft of the realistic disk loading system.
require 'lfs' --Lua File System, useful for searching file directories
local roms = {}
local framecount = 0
function loadRom(index)
--Load ROM file into emulator
emu.loadrom(roms[index].path)
emu.message('Successfully Loaded ' .. roms[index].name)
end
function unloadRom()
--Load ROM file into emulator
emu.unloadROM()
emu.message('Disk Ejected')
end
function SearchForROMS(filepath)
local count = 1;
for file in lfs.dir[filepath] do
if lfs.attributes(file,"mode") == "file" then
roms[count] = {}
roms[count].index = count
roms[count].path = file
local truncLoc = strfind (filename, '.')
local friendlyName = strsub(filename, 1, truncLoc)
roms[count].name = friendlyName
count = count + 1
end
end
end
--On startup
movie.load("loadingScreen.fm2")
local dummyPath = "dummy.fds"
local isLoadingScreen = true
unloadRom(dummyPath)
while true do
--TODO: Implement Logic to detect when ROM should be loaded (via GUI event)
--TODO: On 'Disk dragged to Console Event' toggle isLoadingScreen variable
-- and perform corresponding actions
--Poll for GUI Event here, update isLoadingScreen if it occurs, then...
if isLoadingScreen --no disk loaded, play animation
if !movie.active() then
movie.play()
end
frameCount = frameCount + 1
if framecount == movie.length() then
framecount = 0
movie.replay()
end
else --disk loaded, stop animation
if !movie.active() then
movie.stop()
movie.close()
end
loadRom(index)
end
emu.frameadvance()
end

View File

@ -0,0 +1,14 @@
emu.print("Go Gators!")
local y = 0
while(true) do
gui.drawtext(0, y, "Hello World")
if(y < 256) then
y = y + 1
else
y = 0
end
print(y)
emugator.yieldwithflag(); -- call this if you want the script to run without emulation (game running)
--emu.frameadvance()
end

View File

@ -919,6 +919,7 @@ int main(int argc,char *argv[])
if (PauseAfterLoad) FCEUI_ToggleEmulationPause();
SetAutoFirePattern(AFon, AFoff);
UpdateCheckedMenuItems();
doloopy:
UpdateFCEUWindow();
if(GameInfo)
@ -963,10 +964,26 @@ doloopy:
//xbsave = NULL;
RedrawWindow(hAppWnd,0,0,RDW_ERASE|RDW_INVALIDATE);
}
else
UpdateRawInputAndHotkeys();
else if (luaYieldFlag) {
FCEUI_ResetPalette();
while (luaYieldFlag && !exiting && !GameInfo) {
luaYieldFlag = false;
UpdateFCEUWindow();
UpdateRawInputAndHotkeys();
uint8* gfx = 0;
FCEUI_AdvanceNoFrame(&gfx);
FCEUD_BlitScreen(gfx);
Sleep(25);
}
}
else {
UpdateRawInputAndHotkeys();
}
Sleep(50);
if(!exiting)
if (!exiting)
goto doloopy;
DriverKill();

View File

@ -196,6 +196,7 @@ string gettingstartedhelp = "Gettingstarted";//Getting Started
void SetMainWindowText()
{
string str = FCEU_NAME_AND_VERSION;
if (newppu)
str.append(" (New PPU)");
if (GameInfo)
@ -1166,6 +1167,7 @@ bool ALoad(const char *nameo, char* innerFilename, bool silent)
updateGameDependentMenus();
updateGameDependentMenusDebugger();
EmulationPaused = oldPaused;
return true;
}
@ -2702,7 +2704,7 @@ void ByebyeWindow()
DestroyWindow(hAppWnd);
}
/// reates the main window.
/// Creates the main window.
/// @return Flag that indicates failure (0) or success (1)
int CreateMainWindow()
{
@ -2746,6 +2748,7 @@ int CreateMainWindow()
updateGameDependentMenusDebugger();
if (MainWindow_wndx==-32000) MainWindow_wndx=0; //Just in case
if (MainWindow_wndy==-32000) MainWindow_wndy=0;
hAppWnd = CreateWindowEx(
0,
"FCEUXWindowClass",

View File

@ -236,6 +236,13 @@ static void FCEU_CloseGame(void)
}
}
void FCEU_ClearScreen(void) {
//clear screen when game is closed
extern uint8* XBuf;
if (XBuf)
memset(XBuf, 0, 256 * 256);
}
uint64 timestampbase;
@ -254,6 +261,7 @@ static int RWWrap = 0;
//mbg merge 7/18/06 docs
//bit0 indicates whether emulation is paused
//bit1 indicates whether emulation is in frame step mode
bool luaYieldFlag = false;
int EmulationPaused = 0;
bool frameAdvanceRequested=false;
int frameAdvance_Delay_count = 0;
@ -470,7 +478,7 @@ FCEUGI *FCEUI_LoadGameVirtual(const char *name, int OverwriteVidMode, bool silen
if (fp->archiveFilename != "")
GameInfo->archiveFilename = strdup(fp->archiveFilename.c_str());
GameInfo->archiveCount = fp->archiveCount;
GameInfo->soundchan = 0;
GameInfo->soundrate = 0;
GameInfo->name = 0;
@ -479,7 +487,7 @@ FCEUGI *FCEUI_LoadGameVirtual(const char *name, int OverwriteVidMode, bool silen
GameInfo->input[0] = GameInfo->input[1] = SI_UNSET;
GameInfo->inputfc = SIFC_UNSET;
GameInfo->cspecial = SIS_NONE;
//try to load each different format
bool FCEUXLoad(const char *name, FCEUFILE * fp);
@ -548,7 +556,7 @@ FCEUGI *FCEUI_LoadGameVirtual(const char *name, int OverwriteVidMode, bool silen
}
if (GameInfo->type != GIT_NSF && !disableAutoLSCheats)
FCEU_LoadGameCheats(0);
//FCEU_LoadGameCheats(0);
if (AutoResumePlay)
{
@ -871,6 +879,17 @@ void FCEUI_Emulate(uint8 **pXBuf, int32 **SoundBuf, int32 *SoundBufSize, int ski
ProcessSubtitles();
}
void FCEUI_AdvanceNoFrame(uint8** pXBuf) {
FCEU_ClearScreen();
FCEU_LuaFrameBoundary();
FCEU_DrawLuaGui();
*pXBuf = XBuf;
}
void FCEUI_ResetPalette(void) {
FCEU_ResetPalette();
}
void FCEUI_CloseGame(void) {
if (!FCEU_IsValidUI(FCEUI_CLOSEGAME))
return;

View File

@ -72,6 +72,8 @@ extern uint8 qtaintramreg;
extern uint8 *RAM; //shared memory modifications
extern int EmulationPaused;
//Lua-engine (emugators)
extern bool luaYieldFlag;
extern int frameAdvance_Delay;
extern int RAMInitOption;
@ -148,6 +150,12 @@ void FCEU_TogglePPU();
void SetNESDeemph_OldHacky(uint8 d, int force);
void DrawTextTrans(uint8 *dest, uint32 width, uint8 *textmsg, uint8 fgcolor);
void FCEU_PutImage(void);
// - emugator
void FCEUI_AdvanceNoFrame(uint8** pXBuf);
void FCEU_ClearScreen(void);
void FCEUI_ResetPalette(void);
#ifdef FRAMESKIP
void FCEU_PutImageDummy(void);
#endif

View File

@ -940,9 +940,9 @@ int iNESLoad(const char *name, FCEUFILE *fp, int OverwriteVidMode) {
trainerpoo = NULL;
ExtraNTARAM = NULL;
return LOADER_HANDLED_ERROR;
init_ok:
GameInfo->mappernum = MapperNo;
FCEU_LoadGameSave(&iNESCart);

View File

@ -5442,6 +5442,17 @@ static int cdl_resetcdlog(lua_State *L)
return 0;
}
static int emugator_yieldwithflag(lua_State* L)
{
frameAdvanceWaiting = TRUE;
luaYieldFlag = true;
return lua_yield(L, 0);
// It's actually rather disappointing...
}
static int doPopup(lua_State *L, const char* deftype, const char* deficon) {
@ -6284,6 +6295,12 @@ static const struct luaL_reg cdloglib[] = {
{ NULL,NULL }
};
static const struct luaL_reg emugatorlib[] = {
{"yieldwithflag", emugator_yieldwithflag},
{NULL,NULL}
};
void CallExitFunction()
{
if (!L)
@ -6381,6 +6398,7 @@ void FCEU_LuaFrameBoundary()
*
* Returns true on success, false on failure.
*/
int FCEU_LoadLuaCode(const char *filename, const char *arg)
{
if (!DemandLua())
@ -6446,6 +6464,9 @@ int FCEU_LoadLuaCode(const char *filename, const char *arg)
luaL_register(L, "bit", bit_funcs); // LuaBitOp library
lua_settop(L, 0);
luaL_register(L, "emugator", emugatorlib); // LuaBitOp library
lua_settop(L, 0);
// register a few utility functions outside of libraries (in the global namespace)
lua_register(L, "print", print);
lua_register(L, "gethash", gethash),

View File

@ -71,6 +71,7 @@ static pal *default_palette[8]=
static void CalculatePalette(void);
static void ChoosePalette(void);
static void WritePalette(void);
static void UseDefaultPalette(void);
//points to the actually selected current palette
pal *palo = NULL;
@ -500,6 +501,11 @@ void FCEU_ResetPalette(void)
ChoosePalette();
WritePalette();
}
else if (luaYieldFlag) {
UseDefaultPalette();
WritePalette();
}
}
static void ChoosePalette(void)
@ -552,6 +558,12 @@ static void ChoosePalette(void)
}
}
static void UseDefaultPalette(void) {
palo = default_palette[default_palette_selection];
//need to calcualte a deemph on the fly.. sorry. maybe support otherwise later
ApplyDeemphasisComplete(palo);
}
void WritePalette(void)
{
int x;

View File

@ -69,7 +69,7 @@
#define FCEU_VERSION_MINOR_DECODE(x) ( (x / 100) % 100 )
#define FCEU_VERSION_PATCH_DECODE(x) (x % 100)
#define FCEU_VERSION_STRING "2.6.5" FCEU_SUBVERSION_STRING FCEU_FEATURE_STRING FCEU_COMPILER
#define FCEU_VERSION_STRING "2.6.5 (Emugators)" FCEU_SUBVERSION_STRING FCEU_FEATURE_STRING FCEU_COMPILER
#define FCEU_NAME_AND_VERSION FCEU_NAME " " FCEU_VERSION_STRING
#endif

View File

@ -383,6 +383,11 @@ void FCEU_PutImage(void)
} else DrawMessage(false);
}
void FCEU_DrawLuaGui(void) {
FCEU_LuaGui(XBuf);
}
void snapAVI()
{
//Update AVI
@ -487,7 +492,7 @@ static int WritePNGChunk(FILE *fp, uint32 size, const char *type, uint8 *data)
return 1;
}
uint32 GetScreenPixel(int x, int y, bool usebackup) {
uint32 GetScreenPixel(int x, int y, bool usebackup) {
uint8 r,g,b;

View File

@ -7,6 +7,8 @@ int SaveSnapshot(char[]);
void ResetScreenshotsCounter();
uint32 GetScreenPixel(int x, int y, bool usebackup);
int GetScreenPixelPalette(int x, int y, bool usebackup);
void FCEU_DrawLuaGui(void);
//in case we need more flags in the future we can change the size here
//bit0 : monochrome bit