summaryrefslogtreecommitdiff
path: root/wpspec/wpspec/GamePage.xaml.cs
blob: a6bfd295ffc204400b6900318e20ff7467046e1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// This file is part of the wpspec Spectrum emulator.
//
// wpspec is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// wpspec is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Noddybox.Emulation.  If not, see <http://www.gnu.org/licenses/>.
//
// Copyright (c) 2012 Ian Cowburn
//
using System;
using System.Windows;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using wpspec.Spectrum;
using Noddybox.Emulation.Xna.Keyboard;
using Microsoft.Xna.Framework.Input.Touch;

namespace wpspec
{
    public partial class GamePage : PhoneApplicationPage
    {
        #region Private Data

        private ContentManager contentManager;
        private GameTimer timer;
        private SpriteBatch spriteBatch;
        private Texture2D screen;
        private Vector2 location;
        private Rectangle locationZoomed;
        private Texture2D keyboardImage;
        private KeyboardDriver<SpectrumKeySymbol> keyboard;

        #endregion

        #region Constructors

        /// <summary>
        /// Default constructor
        /// </summary>
        public GamePage()
        {
            InitializeComponent();

            // Get the content manager from the application
            //
            contentManager = (Application.Current as App).Content;

            // Create a timer for this page.
            //
            timer = new GameTimer();
            timer.UpdateInterval = TimeSpan.FromMilliseconds(20);
            timer.Update += OnUpdate;
            timer.Draw += OnDraw;
        }

        #endregion

        #region Overrides

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            // Set the sharing mode of the graphics device to turn on XNA rendering
            //
            SharedGraphicsDeviceManager.Current.GraphicsDevice.SetSharingMode(true);

            // Create a new SpriteBatch, which can be used to draw textures.
            //
            spriteBatch = new SpriteBatch(SharedGraphicsDeviceManager.Current.GraphicsDevice);

            // Create screen object
            //
            screen = new Texture2D
                            (SharedGraphicsDeviceManager.Current.GraphicsDevice,
                             Shared.Spectrum.Width,
                             Shared.Spectrum.Height,
                             false, Shared.Spectrum.ScreenFormat);

            // Start the timer
            //
            timer.Start();

            // Initialise locations for normal and zoomed display
            //
            location = new Vector2(SharedGraphicsDeviceManager.Current.GraphicsDevice.DisplayMode.Width / 2 - Shared.Spectrum.Width / 2, 10);
            locationZoomed = new Rectangle(SharedGraphicsDeviceManager.Current.GraphicsDevice.DisplayMode.Width / 2 - Shared.Spectrum.Width * 100 / 140, 10,
                                            Shared.Spectrum.Height * 100 / 70, Shared.Spectrum.Width * 100 / 70);

            // Reset settings
            //
            Shared.Spectrum.ResetKeyboard();

            // Create a keyboard driver
            //
            keyboardImage = contentManager.Load<Texture2D>("keyboard");

            keyboard = new KeyboardDriver<SpectrumKeySymbol>
                                (SharedGraphicsDeviceManager.Current.GraphicsDevice,
                                keyboardImage,
                                new Vector2(0, 560),
                                Shared.SpectrumKeyboard);

            keyboard.KeyEvent += HandleKeyboardEvent;
            keyboard.TouchEvent += HandleTouchEvent;

            base.OnNavigatedTo(e);
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            // Stop the timer
            //
            timer.Stop();

            // Set the sharing mode of the graphics device to turn off XNA rendering
            //
            SharedGraphicsDeviceManager.Current.GraphicsDevice.SetSharingMode(false);

            screen.Dispose();
            screen = null;

            keyboard.KeyEvent -= HandleKeyboardEvent;
            keyboard.TouchEvent -= HandleTouchEvent;
            keyboard = null;

            base.OnNavigatedFrom(e);
        }

        #endregion

        #region Private members

        /// <summary>
        /// Run the emulation.
        /// </summary>
        private void OnUpdate(object sender, GameTimerEventArgs e)
        {
            keyboard.Update();
            Shared.Spectrum.Run();
        }

        /// <summary>
        /// Updates the display.
        /// </summary>
        private void OnDraw(object sender, GameTimerEventArgs e)
        {
            SharedGraphicsDeviceManager.Current.GraphicsDevice.Clear(Color.Black);

            screen.SetData(Shared.Spectrum.Screen);

            spriteBatch.Begin();

            if (Shared.IsZoomed)
            {
                spriteBatch.Draw(screen, locationZoomed, Color.White);
            }
            else
            {
                spriteBatch.Draw(screen, location, Color.White);
            }

            keyboard.Draw(spriteBatch);
            spriteBatch.End();
            SharedGraphicsDeviceManager.Current.GraphicsDevice.Textures[0] = null;
        }

        #endregion

        #region Keyboard driver events

        private void HandleKeyboardEvent(object sender, KeyboardDriver<SpectrumKeySymbol>.KeyPressEventArgs e)
        {
            if (e.Pressed)
            {
                Shared.Spectrum.KeyPressed(e.Key);
            }
            else
            {
                Shared.Spectrum.KeyReleased(e.Key);
            }
        }

        private void HandleTouchEvent(object sender, KeyboardDriver<SpectrumKeySymbol>.TouchLocationEventArgs e)
        {
            if (e.Location.State == TouchLocationState.Released && e.Location.Position.Y < 400)
            {
                Shared.IsZoomed = !Shared.IsZoomed;
            }
        }

        #endregion
    }
}