protected override void OnRenderFrame(FrameEventArgs args) { if (_gameboy.GetFrameBufferCount() > 0) { int[] buf = _gameboy.DequeueFrameBuffer(); for (int i = 0; i < buf.Length; i++) { texArr[i] = (byte)buf[i]; } _texture.UpdateTexture(texArr); } else { Console.WriteLine("Missing " + DateTime.Now.Ticks); } GL.Clear(ClearBufferMask.ColorBufferBit); _texture.Use(TextureUnit.Texture0); _shader.Use(); GL.BindVertexArray(_vertexArrayObject); GL.DrawElements(PrimitiveType.Triangles, 6, DrawElementsType.UnsignedInt, 0); SwapBuffers(); base.OnRenderFrame(args); }
/// <summary> /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input, and playing audio. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Update(GameTime gameTime) { if (Keyboard.GetState().IsKeyDown(Keys.Escape)) { Exit(); } if (Keyboard.GetState().IsKeyDown(Keys.Tab)) { _gameboy.Debug(); } if (Keyboard.GetState().IsKeyDown(Keys.F1) && !oldState.IsKeyDown(Keys.F1)) { using (BinaryWriter bw = new BinaryWriter(File.Create("savestate.sav"))) { _gameboy.SaveState(bw); } } if (Keyboard.GetState().IsKeyDown(Keys.F2) && !oldState.IsKeyDown(Keys.F2)) { using (BinaryReader br = new BinaryReader(File.OpenRead("savestate.sav"))) { _gameboy.LoadState(br); } } if (!_debugMode && Keyboard.GetState().IsKeyDown(Keys.F3) && !oldState.IsKeyDown(Keys.F3)) { graphics.IsFullScreen = !graphics.IsFullScreen; if (graphics.IsFullScreen) { graphics.PreferredBackBufferWidth = 1920; graphics.PreferredBackBufferHeight = 1080; } else { graphics.PreferredBackBufferWidth = PPU.SCREEN_WIDTH * 3; graphics.PreferredBackBufferHeight = PPU.SCREEN_HEIGHT * 3; } graphics.ApplyChanges(); gameScale = 2f; while (PPU.SCREEN_WIDTH * gameScale <= graphics.PreferredBackBufferWidth && PPU.SCREEN_HEIGHT * gameScale <= graphics.PreferredBackBufferHeight) { gameScale++; } gameScale -= 1; } var _keyState = Keyboard.GetState(); var _padState = GamePad.GetState(0); float deadzone = .15f; _gameboy.SetInput(Input.Button.Up, _keyState.IsKeyDown(Keys.Up) || _padState.DPad.Up == ButtonState.Pressed || _padState.ThumbSticks.Left.Y > deadzone); _gameboy.SetInput(Input.Button.Down, _keyState.IsKeyDown(Keys.Down) || _padState.DPad.Down == ButtonState.Pressed || _padState.ThumbSticks.Left.Y < -deadzone); _gameboy.SetInput(Input.Button.Left, _keyState.IsKeyDown(Keys.Left) || _padState.DPad.Left == ButtonState.Pressed || _padState.ThumbSticks.Left.X < -deadzone); _gameboy.SetInput(Input.Button.Right, _keyState.IsKeyDown(Keys.Right) || _padState.DPad.Right == ButtonState.Pressed || _padState.ThumbSticks.Left.X > deadzone); _gameboy.SetInput(Input.Button.B, _keyState.IsKeyDown(Keys.A) || _padState.Buttons.X == ButtonState.Pressed); _gameboy.SetInput(Input.Button.A, _keyState.IsKeyDown(Keys.S) || _padState.Buttons.A == ButtonState.Pressed); _gameboy.SetInput(Input.Button.Start, _keyState.IsKeyDown(Keys.Space) || _padState.Buttons.Start == ButtonState.Pressed); _gameboy.SetInput(Input.Button.Select, _keyState.IsKeyDown(Keys.LeftShift) || _padState.Buttons.Back == ButtonState.Pressed); //_gameboy.ExecuteFrame(); if (_gameboy.GetFrameBufferCount() > 0) { int[] frameBuffer = _gameboy.DequeueFrameBuffer(); for (int i = 0; i < frameBuffer.Length; i += 4) { colors[i / 4] = new Color(frameBuffer[i], frameBuffer[i + 1], frameBuffer[i + 2], frameBuffer[i + 3]); } _frame.SetData <Color>(colors); //_frame.SaveAsPng(File.Create("Pictures/pict" + picCount++ + ".png"), PPU.SCREEN_WIDTH, PPU.SCREEN_HEIGHT); if (_debugMode) { // tiles w h int[] ppuTiles = _gameboy.GetTilesBuffer(0); for (int i = 0; i < ppuTiles.Length; i += 4) { tileColors[i / 4] = new Color(ppuTiles[i], ppuTiles[i + 1], ppuTiles[i + 2], ppuTiles[i + 3]); } _tiles.SetData <Color>(tileColors); ppuTiles = _gameboy.GetTilesBuffer(1); for (int i = 0; i < ppuTiles.Length; i += 4) { tileColors[i / 4] = new Color(ppuTiles[i], ppuTiles[i + 1], ppuTiles[i + 2], ppuTiles[i + 3]); } _tiles2.SetData <Color>(tileColors); } } if (_keyState.IsKeyDown(Keys.R) && !oldState.IsKeyDown(Keys.R)) { _gameboy.Reset(false, GetNextTestRom()); //_cpu.Debug(); } if (_keyState.IsKeyDown(Keys.Tab) && !oldState.IsKeyDown(Keys.Tab)) { //_cpu.Debug(); } oldState = _keyState; base.Update(gameTime); }
/// <summary> /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input, and playing audio. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Update(GameTime gameTime) { if (task.IsCompleted) { if (cartridge == null) { cartridge = task.Result; _gameboy.LoadCartridge(cartridge); thread.Start(); } if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) { Close(); Exit(); } dpad.Reset(); start.Reset(); select.Reset(); btnB.Reset(); btnA.Reset(); TouchCollection tc = TouchPanel.GetState(); foreach (TouchLocation tl in tc) { if (dpad.BeingHandled && tl.Id == dpad.TLIndex) { continue; } int mx = (int)tl.Position.X, my = (int)tl.Position.Y; if (tl.State == TouchLocationState.Pressed) { dpad.SetInput(mx, my, tl.Id); } start.HandleInput(mx, my); select.HandleInput(mx, my); btnB.HandleInput(mx, my); btnA.HandleInput(mx, my); } if (dpad.BeingHandled) { bool stillValid = tc.FindById(dpad.TLIndex, out var tl); if (!stillValid || tl.State == TouchLocationState.Released) { dpad.ResetInput(); } else { dpad.HandleInput((int)tl.Position.X, (int)tl.Position.Y); } } float deadzone = .35f; _gameboy.SetInput(Input.Button.Up, dpad.YAxis < -deadzone); _gameboy.SetInput(Input.Button.Down, dpad.YAxis > deadzone); _gameboy.SetInput(Input.Button.Left, dpad.XAxis < -deadzone); _gameboy.SetInput(Input.Button.Right, dpad.XAxis > deadzone); _gameboy.SetInput(Input.Button.Start, start.Down); _gameboy.SetInput(Input.Button.Select, select.Down); _gameboy.SetInput(Input.Button.B, btnB.Down); _gameboy.SetInput(Input.Button.A, btnA.Down); //_gameboy.ExecuteFrame(); if (_gameboy.GetFrameBufferCount() > 0) { int[] frameBuffer = _gameboy.DequeueFrameBuffer(); for (int i = 0; i < frameBuffer.Length; i += 4) { colors[i / 4] = new Color(frameBuffer[i], frameBuffer[i + 1], frameBuffer[i + 2], frameBuffer[i + 3]); } _frame.SetData <Color>(colors); } } base.Update(gameTime); }