<!DOCTYPE html>
<html lang="en">
    <head>
        <title>GBA Rocks</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
    </head>
    <body>
        <!-- Screen of the GBA.js -->
        <canvas id="screen" width="480" height="320"></canvas>

        <!-- Start Controls -->
        <div id="controls">
			<!-- Start App controls -->
			<h4>App Controls</h4>
			<div id="preload">
				<button id="select"> Select ROM file </button>
				<input id="loader" type="file" accept=".gba" />
				<button id="select-savegame-btn">Upload Savegame</button>
				<input id="saveloader" type="file" />
			</div>
			<!-- End App controls -->
			<br>
			<!-- Start ingame controls -->
			<h4>In-game controls</h4>
			<div id="ingame" class="hidden">
				<button id="pause">Pause game</button>
				<button id="reset-btn">Reset</button>
				<button id="download-savegame">Download Savegame File</button>

				<div id="sound">
					<p>Audio enabled</p>
					<input type="checkbox" id="audio-enabled-checkbox" checked="checked" />
					<p>Change sound level</p>
					<input id="volume-level-slider" type="range" min="0" max="1" value="1" step="any" />
				</div>
			</div>
			<!-- End ingame controls -->
		</div>
		<!-- End Controls -->
		 

	<script src="js/util.js"></script>
	<script src="js/core.js"></script>
	<script src="js/arm.js"></script>
	<script src="js/thumb.js"></script>
	<script src="js/mmu.js"></script>
	<script src="js/io.js"></script>
	<script src="js/audio.js"></script>
	<script src="js/video.js"></script>
	<script src="js/video/proxy.js"></script>
	<script src="js/video/software.js"></script>
	<script src="js/irq.js"></script>
	<script src="js/keypad.js"></script>
	<script src="js/sio.js"></script>
	<script src="js/savedata.js"></script>
	<script src="js/gpio.js"></script>
	<script src="js/gba.js"></script>
	<!-- 
		This file is optional as it only is a function to load the ROM 
		But the function loadRom needs to exist !
	-->
	<script src="resources/xhr.js"></script>


	<!-- Start APP Scripts -->
	<script>
		var gba;
		var runCommands = [];

		// Setup the emulator
		try {
			gba = new GameBoyAdvance();
			gba.keypad.eatInput = true;

			gba.setLogger(function (level, error) {
				console.error(error);
				
				gba.pause();
				
				var screen = document.getElementById('screen');
				
				if (screen.getAttribute('class') == 'dead') {
					console.log('We appear to have crashed multiple times without reseting.');
					return;
				}


				// Show error image in the emulator screen
				// The image can be retrieven from the repository
				var crash = document.createElement('img');
				crash.setAttribute('id', 'crash');
				crash.setAttribute('src', 'resources/crash.png');
				screen.parentElement.insertBefore(crash, screen);
				screen.setAttribute('class', 'dead');
			});
		} catch (exception) {
			gba = null;
		}

		// Initialize emulator once the browser loads
		window.onload = function () {
			if (gba && FileReader) {
				var canvas = document.getElementById('screen');
				gba.setCanvas(canvas);

				gba.logLevel = gba.LOG_ERROR;

				// Load the BIOS file of GBA (change the path according to yours)
				loadRom('resources/bios.bin', function (bios) {
					gba.setBios(bios);
				});

				if (!gba.audio.context) {
					// Remove the sound box if sound isn't available
					var soundbox = document.getElementById('sound');
					soundbox.parentElement.removeChild(soundbox);
				}

			} else {
				var dead = document.getElementById('controls');
				dead.parentElement.removeChild(dead);
			}
		}

		function fadeOut(id, nextId, kill) {
			var e = document.getElementById(id);
			var e2 = document.getElementById(nextId);
			if (!e) {
				return;
			}

			var removeSelf = function () {
				if (kill) {
					e.parentElement.removeChild(e);
				} else {
					e.setAttribute('class', 'dead');
					e.removeEventListener('webkitTransitionEnd', removeSelf);
					e.removeEventListener('oTransitionEnd', removeSelf);
					e.removeEventListener('transitionend', removeSelf);
				}
				if (e2) {
					e2.setAttribute('class', 'hidden');
					setTimeout(function () {
						e2.removeAttribute('class');
					}, 0);
				}
			}

			e.addEventListener('webkitTransitionEnd', removeSelf, false);
			e.addEventListener('oTransitionEnd', removeSelf, false);
			e.addEventListener('transitionend', removeSelf, false);
			e.setAttribute('class', 'hidden');
		}

		/**
		 * Starts the emulator with the given ROM file
		 * 
		 * @param file 
		 */
		function run(file) {
			var dead = document.getElementById('loader');

			dead.value = '';
			
			var load = document.getElementById('select');
			load.textContent = 'Loading...';
			load.removeAttribute('onclick');
			
			var pause = document.getElementById('pause');
			pause.textContent = "PAUSE";
			
			gba.loadRomFromFile(file, function (result) {
				if (result) {
					for (var i = 0; i < runCommands.length; ++i) {
						runCommands[i]();
					}

					runCommands = [];
					fadeOut('preload', 'ingame');
					fadeOut('instructions', null, true);
					gba.runStable();
				} else {
					load.textContent = 'FAILED';

					setTimeout(function () {
						load.textContent = 'SELECT';
						
						load.onclick = function () {
							document.getElementById('loader').click();
						};

					}, 3000);
				}
			});
		}

		/**
		 * Resets the emulator
		 * 
		 */
		function reset() {
			gba.pause();
			gba.reset();

			var load = document.getElementById('select');
			
			load.textContent = 'SELECT';

			var crash = document.getElementById('crash');

			if (crash) {
				var context = gba.targetCanvas.getContext('2d');
				context.clearRect(0, 0, 480, 320);
				gba.video.drawCallback();
				crash.parentElement.removeChild(crash);
				var canvas = document.getElementById('screen');
				canvas.removeAttribute('class');
			} else {
				lcdFade(gba.context, gba.targetCanvas.getContext('2d'), gba.video.drawCallback);
			}

			load.onclick = function () {
				document.getElementById('loader').click();
			};

			fadeOut('ingame', 'preload');

			// Clear the ROM
			gba.rom = null;
		}

		/**
		 * Stores the savefile data in the emulator.
		 * 
		 * @param file 
		 */
		function uploadSavedataPending(file) {
			runCommands.push(function () { 
				gba.loadSavedataFromFile(file) 
			});
		}

		/**
		 * Toggles the state of the game
		 */
		function togglePause() {
			var e = document.getElementById('pause');

			if (gba.paused) {
				gba.runStable();
				e.textContent = "PAUSE";
			} else {
				gba.pause();
				e.textContent = "UNPAUSE";
			}
		}

		/**
		 * From a canvas context, creates an LCD animation that fades the content away.
		 * 
		 * @param context 
		 * @param target 
		 * @param callback 
		 */
		function lcdFade(context, target, callback) {
			var i = 0;

			var drawInterval = setInterval(function () {
				i++;

				var pixelData = context.getImageData(0, 0, 240, 160);

				for (var y = 0; y < 160; ++y) {
					for (var x = 0; x < 240; ++x) {
						var xDiff = Math.abs(x - 120);
						var yDiff = Math.abs(y - 80) * 0.8;
						var xFactor = (120 - i - xDiff) / 120;
						var yFactor = (80 - i - ((y & 1) * 10) - yDiff + Math.pow(xDiff, 1 / 2)) / 80;
						pixelData.data[(x + y * 240) * 4 + 3] *= Math.pow(xFactor, 1 / 3) * Math.pow(yFactor, 1 / 2);
					}
				}
				
				context.putImageData(pixelData, 0, 0);

				target.clearRect(0, 0, 480, 320);

				if (i > 40) {
					clearInterval(drawInterval);
				} else {
					callback();
				}
			}, 50);
		}

		/**
		 * Set the volume of the emulator.
		 * 
		 * @param value 
		 */
		function setVolume(value) {
			gba.audio.masterVolume = Math.pow(2, value) - 1;
		}
	</script>
	<!-- End APP Scripts -->

	<!-- Start Events Scripts -->
	<script>
		// If clicked, simulate click on the File Select input to load a ROM
		document.getElementById("select").addEventListener("click", function(){
			document.getElementById("loader").click();
		}, false);
		
		// Run the emulator with the loaded ROM
		document.getElementById("loader").addEventListener("change", function(){
			var ROM = this.files[0];
 			run(ROM);
		}, false);

		// If clicked, simulate click on the File Select Input to load the savegame file
		document.getElementById("select-savegame-btn").addEventListener("click", function(){
			document.getElementById('saveloader').click();
		}, false);

		// Load the savegame to the emulator
		document.getElementById("saveloader").addEventListener("change", function(){
			var SAVEGAME = this.files[0];
			uploadSavedataPending(SAVEGAME);
		}, false); 

		// Pause/Resume game
		document.getElementById("pause").addEventListener("click", function(){
			 togglePause();
		}, false);
		
		// Reset game
		document.getElementById("reset-btn").addEventListener("click", function(){
			 reset();
		}, false);

		// Download the savegamefile
		document.getElementById("download-savegame").addEventListener("click", function(){
			 gba.downloadSavedata();
		}, false);
		
		// Mute/Unmute emulator
		document.getElementById("audio-enabled-checkbox").addEventListener("change", function(){
			gba.audio.masterEnable = this.checked;
		}, false);

		// Handle volume level slider
		document.getElementById("volume-level-slider").addEventListener("change", function(){
			var volumeLevel = this.value;
			setVolume(volumeLevel);
		}, false);
		document.getElementById("volume-level-slider").addEventListener("input", function(){
			var volumeLevel = this.value;
			setVolume(volumeLevel);
		}, false); 

		// In order to pause/resume the game when the user changes the website tab in the browser
		// add the 2 following listeners to the window !
		// 
		// This feature is problematic/tricky to handle, so you can make it better if you need to
		window.onblur = function () {
			if(gba.hasRom()){
				var e = document.getElementById('pause');

				if (!gba.paused) {
					gba.pause();
					e.textContent = "UNPAUSE";

					console.log("Window Focused: the game has been paused");
				}
			}
		};

		window.onfocus = function () {
			if(gba.hasRom()){
				var e = document.getElementById('pause');

				if (gba.paused) {
					gba.runStable();
					e.textContent = "PAUSE";

					console.log("Window Focused: the game has been resumed");
				}
			}
		};
	</script>
	<!-- End Events Scripts -->
</body>
</html> 

HTML Online Editor & Compiler

Write, Run & Share HTML code online using OneCompiler's HTML online Code editor for free. It's one of the robust, feature-rich online Code editor for HTML language, running on the latest version HTML5. Getting started with the OneCompiler's HTML compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as HTML. You can also specify the stylesheet information in styles.css tab and scripts information in scripts.js tab and start coding.

About HTML

HTML(Hyper Text Markup language) is the standard markup language for Web pages, was created by Berners-Lee in the year 1991. Almost every web page over internet might be using HTML.

Syntax help

Fundamentals

  • Any HTML document must start with document declaration <!DOCTYPE html>
  • HTML documents begin with <html> and ends with </html>
  • Headings are defined with <h1> to <h6> where <h1> is the highest important heading and <h6> is the least important sub-heading.
  • Paragrahs are defined in <p>..</p> tag.
  • Links are defined in <a> tag.

    Example:

    <a href="https://onecompiler.com/html">HTML online compiler</a>
    
  • Images are defined in <img> tag, where src attribute consists of image name.
  • Buttons are defined in <button>..</button> tag
  • Lists are defined in <ul> for unordered/bullet list and <ol> for ordered/number list, and the list items are defined in <li>.

HTML Elements and Attributes

  • HTML element is everything present from start tag to end tag.
  • The text present between start and end tag is called HTML element content.
  • Anything can be a tagname but it's preferred to put the meaningful title to the content present as tag name.
  • Do not forget the end tag.
  • Elements with no content are called empty elements.
  • Elements can have attributes which provides additional information about the element.
  • In the below example, href is an attribute and a is the tag name.

    Example:

    <a href="https://onecompiler.com/html">HTML online compiler</a>
    

CSS

CSS(cascading style sheets) describes how HTML elements will look on the web page like color, font-style, font-size, background color etc.

Example:

Below is a sample style sheet which displays heading in green and in Candara font with padding space of 25px.

body{
  padding: 25px;
}
.title {
	color: #228B22;
	font-family: Candara;
}

HTML Tables

  • HTML Tables are defined in <table> tag.
  • Table row should be defined in <tr> tag
  • Table header should be defined in <th> tag
  • Table data should be defined in <td> tag
  • Table caption should be defined in <caption> tag

HTML-Javascript

  • Javascript is used in HTML pages to make them more interactive.
  • <script> is the tag used to write scripts in HTML
  • You can either reference a external script or write script code in this tag.

Example

<script src="script.js"></script>