⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 goboy.cpp

📁 gameboy 模拟器的源代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	{
	}


extern "C" void unzip()
	{
	}

extern "C" void ev_poll()
	{
	// Update screen and check for events
	if (!vid_toggle_enable())
		{
		goBoy->UpdateScreen();
		}
	goBoy->GetEvents();
	}


extern "C" void ev_poll_paused()
	{
	goBoy->Redraw(ETrue);
	goBoy->GetEvents();
	}

extern "C" void begin_paused()
	{
	goBoy->OptionsMenuL();
	}


extern "C" void signal()
	{
	}

extern "C" void pcm()
	{
	}


extern "C" void sys_sleep()
	{
	}

extern "C" void sys_elapsed()
	{
	}

extern "C" void sys_timer()
	{
	}

extern "C" void sys_checkdir()
	{
	}

extern "C" void sys_initpath()
	{
	}

extern "C" void sys_sanitize()
	{
	}



extern "C" void pcm_exports()
	{
	}

extern "C" void pcm_close()
	{
	}


extern "C" void vid_preinit()
	{
	}



extern "C" void vid_begin()
	{
	}

extern "C" void vid_end()
	{
	}

extern "C" void vid_setpal(int i, int r, int g, int b)
	{
	}


extern "C" void vid_settitle(char *title)
	{
	}


extern "C" void vid_exports()
	{
	}


extern "C" void joy_exports()
	{
	}

extern "C" void gb_vidset(unsigned char* aPtr)
	{
	goBoy->SetVidPtr(aPtr);
	}


extern "C" void gb_openfile(char* aName)
	{
	goBoy->OpenFile(aName);
	}

extern "C" void gb_closefile()
	{
	goBoy->CloseFile();
	}

extern "C" void gb_readfile(unsigned char* aDest, int aSize)
	{
	goBoy->ReadL(aDest, aSize);
	}

extern "C" int gb_filesize(char* aName)
	{
	return goBoy->FileSize(aName);
	}

extern "C" void gb_note(char* text)
	{
	TBuf<128> buf;
	while ((*text) != 0)
		{
		buf.Append(*text);
		text++;
		}
	}

extern "C" void gb_notenum(char* text, TInt num)
	{
	TBuf<128> buf;
	while ((*text) != 0)
		{
		buf.Append(*text);
		text++;
		}
	buf.AppendFormat(_L(":%d"), num);
	}


extern "C" void* gb_malloc(int aSize)
	{
	void* ptr =  User::Alloc(aSize);
	if (ptr == 0)
		{
		CAknGlobalNote* note = CAknGlobalNote::NewLC();
		note->ShowNoteL(EAknGlobalInformationNote, KNoMemoryNote);
		CleanupStack::PopAndDestroy();
		User::Leave(KErrNoMemory);
		}
	return ptr;
	}

extern "C" void gb_free(void *aPtr)
	{
	delete aPtr;
	}


extern "C" char* gb_getbatteryfilename()
	{
	HBufC* nameBuf = HBufC::NewLC(256);
	TPtr name = nameBuf->Des();
	goBoy->AppendSaveGameDirectory(name);
	goBoy->AppendNameOfGame(name);

	name.Append(_L(".bt"));

	TInt len = name.Length();
	char* buffer = (char*)User::Alloc(len + 1);
	for (TInt ii=0; ii<len; ii++)
		{
		buffer[ii] = name[ii];
		}
	buffer[ii] = 0;
	CleanupStack::PopAndDestroy();	// nameBuf

	return (char*)buffer;
	}

extern "C" char* gb_buf()
	{
	return loadbuf;
	}

extern "C" void gb_setbuttons(int aSettings)
	{
	goBoy->iSwapButtons = aSettings;
	}

extern "C" int gb_getbuttons()
	{
	return goBoy->iSwapButtons;
	}
	
extern "C" void gb_submitsound(char* aBuffer, int aSize)
	{
	TBool terminated = iGBSound->SubmitAudio(aBuffer, aSize);
	if (terminated)
		{
		iServerUi->CreateNoteL(_L("Sound has stopped"), _L("Please register to"));
		iServerUi->AddListItem(_L("enjoy sound support"));
		iServerUi->Activate(ETrue);
		}
	}

extern "C" void gb_leave(int aLeave)
	{
	User::Leave(aLeave);
	}


LOCAL_C void StartL();

void CGoBoy::SetDefaultKeys()
	{
	iKeySelect[0] = EStdKeyUpArrow;
	iKeySelect[1] = EStdKeyDownArrow;
	iKeySelect[2] = EStdKeyLeftArrow;
	iKeySelect[3] = EStdKeyRightArrow;
	iKeySelect[4] = EStdKeyDevice3;
	iKeySelect[5] = '5';
	}



void CGoBoy::OptionsMenuL()
	{
	iServerUi->CreateList(EOptionsMenu, _L("Select option"));
	iServerUi->AddListItem(_L("Save"));
	iServerUi->AddListItem(_L("Load..."));
	iServerUi->AddListItem(_L("Delete..."));

	switch (iSoundMode)
		{
		case 0:
		iServerUi->AddListItem(_L("Sound : OFF")); break;
		case 1:
		iServerUi->AddListItem(_L("Sound : ON")); break;
		case 2:
		iServerUi->AddListItem(_L("Sound : LOUD")); break;
		}
	iServerUi->AddListItem(_L("Select Keys"));
	iServerUi->AddListItem(_L("Swap Buttons"));
	iServerUi->AddListItem(_L("Exit"));
	iServerUi->Activate(ETrue);
	}

void CGoBoy::LoadGameMenuL(TBool aDelete)
	{
	set_paused(1);
	if (aDelete)
		{
		iServerUi->CreateList(EDeleteGameMenu, _L("Delete Game..."));
		}
	else
		{
		iServerUi->CreateList(ELoadGameMenu, _L("Load Game..."));
		}
	TFileName name;
	goBoy->AppendSaveGameDirectory(name);
	TFileName game;
	goBoy->AppendNameOfGame(game);
	game.Append(_L(".???"));

	RDir dir;
	dir.Open(TheFs, name, 0);
	TEntry entry;
	while (dir.Read(entry) == KErrNone)
		{
		if (entry.iName.Match(game) == 0)
			{
			TBuf<80> listItem;
			listItem.Append(entry.iName.Mid(entry.iName.Length()-3));
			TBuf<40> fmt;
			_LIT(KFormatTxt," %H%:1%T %/0%4%/1%5%/3");
			entry.iModified.FormatL(fmt, KFormatTxt);
			listItem.Append(fmt);
			iServerUi->AddListItem(listItem);
			}
		}
	dir.Close();
	iServerUi->Activate(ETrue);
	}


void CGoBoy::ListItemSelection(TInt aListId, TInt aIndex)
	{
	if (aListId == EOptionsMenu)
		{
		set_paused(0);
		switch (aIndex)
			{
			case -1:
				break;
			case EOptionSound:
				iSoundMode++;
				if (iSoundMode == 2)
					{
					iSoundMode = 0;
					}
				SetSoundMode();
				SaveSettingsL();
				break;
			case EOptionSave:
				ProcessSaveL();
				break;
			case EOptionLoad:
				LoadGameMenuL();
				break;
			case EDeleteGameMenu:
				LoadGameMenuL(ETrue);
				break;
			case EOptionKeys:
				SelectKeysL(0);
				break;
			case EOptionSwap:
				iSwapButtons = !iSwapButtons;
				SaveSettingsL();
				break;
			case EOptionExit:
				exit_emulator();
				break;
			}
		}
	if (aListId == ELoadGameMenu)
		{
		set_paused(0);
		if (aIndex != -1)
			{
			LoadGameIndex(aIndex, EFalse);
			}
		}
	if (aListId == EDeleteGameMenu)
		{
		set_paused(0);
		if (aIndex != -1)
			{
			LoadGameIndex(aIndex, ETrue);
			}
		}
	}

void CGoBoy::LoadGameIndex(TInt aIndex, TBool aDelete)
	{
	TFileName name;
	goBoy->AppendSaveGameDirectory(name);


	TFileName game;
	goBoy->AppendNameOfGame(game);
	game.Append(_L(".???"));

	TFileName fullname;
	goBoy->AppendSaveGameDirectory(fullname);
	goBoy->AppendNameOfGame(fullname);
	fullname.Append('.');


	RDir dir;
	dir.Open(TheFs, name, 0);
	TEntry entry;
	while (dir.Read(entry) == KErrNone)
		{
		if (entry.iName.Match(game) == 0)
			{
			TBuf<80> listItem;
			listItem.Append(entry.iName.Mid(entry.iName.Length()-3));
			if (aIndex == 0)
				{
				fullname.Append(listItem);
				break;
				}
			aIndex--;
			}
		}
	dir.Close();




		if (aDelete)
			{
			TheFs.Delete(fullname);
			TBuf<80> buffer(_L("Game "));
			TParsePtrC parse(fullname);
			buffer.Append(parse.Ext().Mid(1));
			buffer.Append(_L(" deleted"));
			ShowNoteL(_L("Information"), buffer);
			}
		else
			{
			char fName[256];
			TInt len = fullname.Length();
			for (TInt ii=0; ii<len; ii++)
				{
				fName[ii] = fullname[ii];
				}
			fName[ii] = 0;

			doLoadState(fName);
			}


	}

void CGoBoy::SelectKeysL(TInt aKeyNo)
	{
	TBuf<16> keyName;
	switch (aKeyNo)
		{
		case 0: keyName.Copy(_L("Up")); break;
		case 1: keyName.Copy(_L("Down")); break;
		case 2: keyName.Copy(_L("Left")); break;
		case 3: keyName.Copy(_L("Right")); break;
		case 4: keyName.Copy(_L("Button A")); break;
		case 5: keyName.Copy(_L("Button B")); break;
		}
	iSelectKeyNo = aKeyNo;

	iServerUi->CreateKeySelectQueryL(ESelectKeyQuery, _L("Select Key for..."), keyName);
	iServerUi->Activate(ETrue);
	set_paused(1);
	}


void CGoBoy::KeySelect(TInt aId, TInt aKey)
	{
	if (aId == ESelectKeyQuery)
		{
		iKeySelect[iSelectKeyNo] = aKey;
		if (iSelectKeyNo < 5)
			{
			iSelectKeyNo++;
			SelectKeysL(iSelectKeyNo);
			}
		else
			{
			// Set Keys to emulator
			SaveSettingsL();
			set_paused(0);
			}
		}
	}


void CGoBoy::SetSoundMode()
	{
	switch (iSoundMode)
		{
		case 0:
			iGBSound->SetSoundOn(EFalse);
			break;
		case 1:
			iGBSound->SetSoundOn(ETrue);
			gbsetSoundMode(0);
			break;
		case 2:
			iGBSound->SetSoundOn(ETrue);
			gbsetSoundMode(1);
			break;
		}
	}



void CGoBoy::CbaPressed(TInt aListId, TCbaLabel)
	{
	if (aListId == -1)
		{
		set_paused(0);
		}
	}


void CGoBoy::Redraw(TBool aMenu)
	{
	if (aMenu)
		{

		iServerUi->Draw(iScreen);


	    iWindowGc->Activate(iWsWindow);
		TRect rect = TRect(iWsWindow.Size());
		iWsWindow.Invalidate(rect);
		iWsWindow.BeginRedraw(rect);

		iWindowGc->BitBlt(TPoint(8,16),iScreen);
		iWindowGc->BitBlt(TPoint(0,188), iMenuCba);


		iWsWindow.EndRedraw();
		iWindowGc->Deactivate();
		iWsSession.Flush();

		}
	else
		{
	    iWindowGc->Activate(iWsWindow);
		TRect rect = TRect(iWsWindow.Size());
		iWsWindow.Invalidate(rect);
		iWsWindow.BeginRedraw(rect);

		iWindowGc->BitBlt(TPoint(8,16),iScreen);
		iWindowGc->BitBlt(TPoint(0,188), iCba);


		iWsWindow.EndRedraw();
		iWindowGc->Deactivate();
		iWsSession.Flush();
		}
	}

void CGoBoy::SaveSettingsL()
	{
	TFileName name;
	AppendSaveGameDirectory(name);
	name.Append(_L("GoBoy.ini"));
	RFileWriteStream stream;
	TInt err = stream.Replace(TheFs, name, EFileWrite);
	if (err == KErrNone)
		{
		stream.WriteUint32L(iSoundMode);
		stream.WriteUint32L(iKeySelect[0]);
		stream.WriteUint32L(iKeySelect[1]);
		stream.WriteUint32L(iKeySelect[2]);
		stream.WriteUint32L(iKeySelect[3]);
		stream.WriteUint32L(iKeySelect[4]);
		stream.WriteUint32L(iKeySelect[5]);
		stream.WriteUint32L(iSwapButtons);

		stream.CommitL();
		stream.Close();
		}

	}

void CGoBoy::LoadSettingsL()
	{
	TFileName name;
	AppendSaveGameDirectory(name);
	name.Append(_L("GoBoy.ini"));
	RFileReadStream stream;
	TInt err = stream.Open(TheFs, name, EFileRead);
	if (err == KErrNone)
		{
		iSoundMode = stream.ReadUint32L();
		iKeySelect[0] = stream.ReadUint32L();
		iKeySelect[1] = stream.ReadUint32L();
		iKeySelect[2] = stream.ReadUint32L();
		iKeySelect[3] = stream.ReadUint32L();
		iKeySelect[4] = stream.ReadUint32L();
		iKeySelect[5] = stream.ReadUint32L();
		iSwapButtons = stream.ReadUint32L();
		stream.Close();
		}
	}


GLDEF_C TInt E32Main()
	{	
	__UHEAP_MARK; // mark heap state
    
	CTrapCleanup* TheTrapCleanup=CTrapCleanup::New();
	TRAPD(error, StartL());
    delete TheTrapCleanup;
	
	__UHEAP_MARKEND;
	
	return(error);
	}

#if defined(__WINS__)
GLDEF_C TInt E32Dll(TDllReason)
	{
	return(KErrNone);
	}
#endif


LOCAL_C void StartL()
	{
	// Start by renaming our thread
	RThread thread;
	thread.Rename(_L("GOBOYEXE"));

	RProcess process;
	TFileName commandLine;
	process.CommandLine(commandLine);

	TheFs.Connect();

	goBoy = new(ELeave)CGoBoy();
	goBoy->ConstructL(commandLine);
	goBoy->CreateScreenL();

	TRAPD(err, goBoy->Go());

	delete goBoy;
	TheFs.Close();
	}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -