Exemplo n.º 1
0
        /// <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);
        }