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

📄 aepgb.java

📁 一个用java写成的gb模拟器的源代码。
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/**
 * this source file released under the GNU Public Licence.
 * see the accompanying copyright.txt for more information
 * Copyright (C) 2000-2001 Ben Mazur
 * modified by retroK, XTale and baka0815 2004 http://aepgb.aep-emu.de/
 */
import java.applet.Applet;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FileDialog;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class AEPgb extends Applet
	implements KeyListener, WindowListener, ActionListener, ItemListener, Runnable {

	public static boolean applet;
	public JFrame frame;
	public Container container;
	public Thread cpuThread;

	public PgbMemory mem;
	public PgbCpu cpu;
	public PgbCart cart;
	public PgbVideo video;
	public PgbJoypad joy;
	public PgbNetplay net;

	public PgbVideoOutput vidout;
	public PgbDirectVideoOutput directvidout;

	public String curfile;
	public String curpath;

	public PgbMenuBar menubar;

	public AEPgb(){
		frame = new JFrame("AEPgb " + PgbSettings.version);
		container = frame.getContentPane();

		// load settings, if possible
		PgbSettings.load();

		// initialize...
		curfile = "";
		curpath = "";

		// init menu bar
		menubar = new PgbMenuBar(this, this);

		// init emulation
		cart = new PgbCart();
		video = new PgbCachedVideo();
		joy = new PgbJoypad();
		net = new PgbNetplay();

		mem = new PgbMemory(cart, video, joy, net);
		cpu = new PgbCpu(mem);

		vidout = getPgbVideoOutput(video);

		// setup frame stuff
		frame.setMenuBar(menubar);
		frame.setResizable(false);

		frame.addWindowListener(this);
		frame.addKeyListener(this);
		frame.addKeyListener(joy);
		//addFocusListener(this);
	}

	public static void main(String[] args) {

		System.out.println("Pgb" + " (c) 2000-2001 Ben Mazur");
		System.out.println(
			"AEPgb Version " + PgbSettings.version + " (c) 2004 retroK, XTale, baka0815");

		AEPgb pgb = new AEPgb();
		pgb.parseCommandLine(args);
		pgb.go();
	}
	
	public void start() {
		applet = true;
		Thread p = new Thread(this);
		addKeyListener(this);
		//runningAsApplet = true;
		System.out.println(
			"AEPgb Version "
				+ PgbSettings.version
				+ " (c) 2004 retroK & XTale(applet)");
		this.requestFocus();
		vidout = getPgbVideoOutput(video);
		cpuThread = new Thread(this);
		//conformToSettings();
		
		//frame.pack();
		vidout.setGraphics();
		//frame.setVisible(true);
		PgbSettings.paused = true;
		PgbSettings.active = true;
		cart.loadApplet("file.gb",this);
		
		if (cart.loaded()) {
			reset();
			unpause();
		}
		System.out.println("starting cputhread");
		p.start();
		cpuThread.start();
	}

	public void go() {
		cpuThread = new Thread(this);
		conformToSettings(); // once

		frame.pack();
		vidout.setGraphics();
		frame.setVisible(true);

		PgbSettings.paused = true;
		PgbSettings.active = true;

		cart.load(curpath, curfile);
		if (cart.loaded()) {
			setSystem(PgbSettings.desiredsystem);
			PgbSettings.gamestring = cart.getName();
			reset();
			unpause();
		}

		cpuThread.start();
	}

	/**
	 * Shuts down the emulator, killing off all threads and
	 * saving the battery and settings, if needed.
	 */
	public void shutdown() {
		cpuThread = null;
		PgbSettings.save(frame);

		cart.saveBattery(PgbSettings.savepath, curfile);

		System.exit(0);
	}

	/**
	 * Pauses the cpu emulation process.
	 */
	public synchronized void pause() {
		if (!PgbSettings.paused) {
			PgbSettings.paused = true;
		}
		menubar.cpu_paused.setState(PgbSettings.paused);
	}

	/**
	 * Unpauses the cpu emulation process.
	 */
	public synchronized void unpause() {
		if (PgbSettings.paused) {
			PgbSettings.paused = false;
			notifyAll();
		}
		menubar.cpu_paused.setState(PgbSettings.paused);
	}

	/**
	 * Resets all aspects of emulation.
	 */
	public synchronized void reset() {
		pause();

		video.reset();
		vidout.reset();
		cart.reset();
		joy.reset();
		mem.reset();
		cpu.reset();

		unpause();
	}

	public void parseCommandLine(String[] args) {
		if (args.length > 0) {

			// file is last argument
			curfile = args[args.length - 1];
		}
	}

	public PgbVideoOutput getPgbVideoOutput(PgbVideo video) {
		// check if they can use directx
		if (applet) {
			return new PgbDirectVideoOutput(video,this);
		}
		if (PgbSettings.videooutput == PgbSettings.VIDOUT_DIRECTX
			&& !PgbSettings.usedirectx) {
			PgbSettings.videooutput = PgbSettings.VIDOUT_DEFAULT;
		}
		switch (PgbSettings.videooutput) {
			case PgbSettings.VIDOUT_DEFAULT :
				return new PgbIndexedVideoOutput(video);
			case PgbSettings.VIDOUT_DIRECT :
				return new PgbDirectVideoOutput(video);
			case PgbSettings.VIDOUT_INDEXED :
			default :
				return new PgbIndexedVideoOutput(video);
		}
	}

	public void conformToSettings() {
		setSgbBorder(PgbSettings.sgbborder);
		setColorMute(PgbSettings.colormute);
		frame.setLocation(PgbSettings.winloc);
		setDebugLevel(PgbSettings.debuglevel);
		setFrameskip(PgbSettings.frameskip);
		setPriority(PgbSettings.priority);
		setVideoOutput(PgbSettings.videooutput);
		setSystem(PgbSettings.desiredsystem);

		setLcdSize(PgbSettings.lcdsize);
	}

	void setPriority(int priority) {
		PgbSettings.priority = priority;

		menubar.priority_low.setState(priority == Thread.MIN_PRIORITY);
		menubar.priority_normal.setState(priority == Thread.NORM_PRIORITY);
		menubar.priority_high.setState(
			priority == ((Thread.NORM_PRIORITY + Thread.MAX_PRIORITY) / 2));
		menubar.priority_max.setState(priority == Thread.MAX_PRIORITY);

		cpuThread.setPriority(priority);
	}

	// by retroK
	void setSound(boolean usesound) {
		PgbSettings.usesound = usesound;
		menubar.sound.setState(usesound == true);
		mem.soundChip.channel1Enable = menubar.sound.getState();
		menubar.soundChannel1Enable.setState(menubar.sound.getState());
		mem.soundChip.channel2Enable = menubar.sound.getState();
		menubar.soundChannel2Enable.setState(menubar.sound.getState());
		mem.soundChip.channel3Enable = menubar.sound.getState();
		menubar.soundChannel3Enable.setState(menubar.sound.getState());
		mem.soundChip.channel4Enable = menubar.sound.getState();
		menubar.soundChannel4Enable.setState(menubar.sound.getState());
	}

	void setChannelEnable() {
		if (menubar.soundChannel1Enable.getState()
			|| menubar.soundChannel2Enable.getState()
			|| menubar.soundChannel3Enable.getState()
			|| menubar.soundChannel4Enable.getState()) {
			menubar.sound.setState(true);
		}
		mem.soundChip.channel1Enable = menubar.soundChannel1Enable.getState();
		mem.soundChip.channel2Enable = menubar.soundChannel2Enable.getState();
		mem.soundChip.channel3Enable = menubar.soundChannel3Enable.getState();
		mem.soundChip.channel4Enable = menubar.soundChannel4Enable.getState();
	}

	void setSoundFreq(int frequency) {
		if (frequency == 11025) {
			menubar.soundFreq11.setState(true);
			menubar.soundFreq22.setState(false);
			menubar.soundFreq44.setState(false);
		}
		if (frequency == 22050) {
			menubar.soundFreq22.setState(true);
			menubar.soundFreq11.setState(false);
			menubar.soundFreq44.setState(false);
		}
		if (frequency == 44100) {
			menubar.soundFreq44.setState(true);
			menubar.soundFreq11.setState(false);
			menubar.soundFreq22.setState(false);
		}

		mem.soundChip.setSampleRate(frequency);
	}

	void setBufferLength(int bufferlength) {
		if (bufferlength == 200) {
			menubar.soundBuffer200.setState(true);
			menubar.soundBuffer300.setState(false);
			menubar.soundBuffer400.setState(false);
		}
		if (bufferlength == 300) {
			menubar.soundBuffer300.setState(true);
			menubar.soundBuffer200.setState(false);
			menubar.soundBuffer400.setState(false);
		}
		if (bufferlength == 400) {
			menubar.soundBuffer400.setState(true);
			menubar.soundBuffer200.setState(false);
			menubar.soundBuffer300.setState(false);

		}
		mem.soundChip.setBufferLength(bufferlength);
	}

	// end by retroK

	void setLcdSize(int size) {
		// grr, due to a bug, this will not work on a window
		// until it is visible and has been resized...

		Dimension lcdsize;
		PgbSettings.lcdsize = size;

		menubar.size_1.setState(size == 1);
		menubar.size_2.setState(size == 2);
		menubar.size_3.setState(size == 3);
		menubar.size_4.setState(size == 4);

		if (PgbSettings.sgbborder) {
			lcdsize = new Dimension(256 * size, 224 * size);
		} else {
			lcdsize = new Dimension(160 * size, 144 * size);
		}
		vidout.setSize(lcdsize);

		frame.setSize(
			frame.getInsets().right + lcdsize.width + frame.getInsets().left,
			frame.getInsets().top + lcdsize.height + frame.getInsets().bottom);
	}

	void setVideoOutput(int videooutput) {
		// check if they can use directx
		if (videooutput == PgbSettings.VIDOUT_DIRECTX
			&& !PgbSettings.usedirectx) {
			videooutput = PgbSettings.VIDOUT_DEFAULT;
		}
		// then set up the menus as normal
		PgbSettings.videooutput = videooutput;
		if (videooutput == PgbSettings.VIDOUT_DEFAULT) {
			menubar.vidout_default.setState(true);
			if (PgbSettings.usedirectx) {
				menubar.vidout_directx.setState(true);
				menubar.vidout_indexed.setState(false);
				menubar.vidout_direct.setState(false);
			} else {
				menubar.vidout_directx.setState(false);
				menubar.vidout_indexed.setState(true);
				menubar.vidout_direct.setState(false);
			}
		} else {
			menubar.vidout_default.setState(
				videooutput == PgbSettings.VIDOUT_DEFAULT);
			menubar.vidout_directx.setState(
				videooutput == PgbSettings.VIDOUT_DIRECTX);
			menubar.vidout_indexed.setState(
				videooutput == PgbSettings.VIDOUT_INDEXED);
			menubar.vidout_direct.setState(
				videooutput == PgbSettings.VIDOUT_DIRECT);
		}
		pause();

		PgbVideoOutput oldVidout = vidout;

		// add new video
		PgbVideoOutput newVidout = getPgbVideoOutput(video);
		newVidout.addKeyListener(this);
		newVidout.addKeyListener(joy);

		// remove old video, add new one
		// old vidout, please die peacefully...
		frame.remove(oldVidout);
		frame.getContentPane().add(newVidout);
		frame.validate();

		if (frame.isVisible()) {
			newVidout.reset();
			newVidout.requestFocus();
		}

		this.vidout = newVidout;

		unpause();
	}

	void setColorMute(boolean muted) {
		PgbSettings.colormute = muted;
		menubar.color_mute.setState(muted == true);
	}

	void setSgbBorder(boolean border) {
		PgbSettings.sgbborder = border;
		menubar.sgb_border.setState(border == true);
	}

	void setFrameskip(int skip) {
		PgbSettings.frameskip = skip;

		menubar.auto_wait.setState(PgbSettings.autowait);
		menubar.fs_0.setState(skip == 0);
		menubar.fs_1.setState(skip == 1);

⌨️ 快捷键说明

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