220 likes | 370 Views
YMTIS Workshop Geneva Introduction to XNA Marios Karagiannis. mkaragia @ xboxlive. My name is Marios Karagiannis I am Greek I am not affiliated with Microsoft PhD Candidate @ UNIGE In my last year Gamer Since the Game & Watch era Games Developer
E N D
mkaragia@xboxlive • My name is MariosKaragiannis • I am Greek • I am not affiliated with Microsoft • PhD Candidate @ UNIGE • In my last year • Gamer • Since the Game & Watch era • Games Developer • Made my first game on an8-bit Atari 65 XE (on cassette) • Currently making games for Windows Phone 7 Who am I
All Windows Phone 7 phones must have as a minimum (called Chassis 1): • 4 points multi touch capacitive screens • 480x800 screen resolution • 1GHz CPU • Accelerometer • 256MB Ram – 8GB Storage • DirectX 9 GPU with video acceleration • 5 MP Camera with flash • A-GPS • Compass • Ambient and proximity sensors • 3 hardware keys (Back, Start, Search) WP7 Hardware
What is XNA? • A set of tools specifically designed for game development • Managed runtime environment • For • Windows Phone • XBOX 360 • Windows • (using almost the same code) • Originally for the XBOX it was called Xbox New Architecture) • After XBOX 360 itmeans XNA is Not an Acronym XNA
Let me state this • Developing games with XNA is the best way to develop games in any platform, ever (did I say ever?) • Why? • C# • .NET • Free and great tools (XNA Game Studio 4 and Visual Studio Express 2010) • Great libraries • Amazing documentation Is it any good?
Game loop vs event driven programming • Games play even when you don’t Update Draw XNA for Windows Phone 7
XNA lets you choose how you want timing to work in your game: • Fixed Time Step (default) TargetElapsedTime = TimeSpan.FromSeconds(1/30.0); Time for next update Update Draw NO YESIsRunningSlowly = true Game timing
XNA lets you choose how you want timing to work in your game: • Variable Time Step Be careful to use ElapsedGameTime (more traditional way to develop games) Update Draw Game timing
XNA provides a powerful content pipeline that can import and process: • Images (bmp, dib, hdr, jpg, pfm, png, ppm, tga) • Fonts (converted automatically to spritefonts) • XML • 3D models(fbx) • Music (mp3, wma) • Sounds (wav) • And custom processors (if you so wish) • Assets are processed to xbl files • Some assets can be compressed Content
Although technically not part of the XNA you can easily compile and use the Farseer Physics Engine with your Windows Phone 7 games http://farseerphysics.codeplex.com/ Physics
One Draw method • You can render 3D objects and then 2D objects • Or vice versa • GPU hardwalre image scaler is free to use • The scaler is easy to use: just set graphics.PreferredBackBufferWidth and PreferredBackBufferHeight in your game constructor • The resolution can be anywhere from 240x240 up to a max of 800x480 (or 480x800 if you are making a portrait game) • If you choose a resolution that does not match the screen aspect ratio, it will be automatically letterboxed (black bars along the edges) • Touch input is automatically scaled to match your chosen backbuffer resolution • Scaling is implemented by dedicated hardware, so doesn't cost any GPU • Scaling uses a high quality filter (better looking than GPU bilinear filtering) Drawing (2D and 3D)
Like in Silverlight, events can occur in XNA #if WINDOWS_PHONE PhoneApplicationService.Current.Launching+=Current_Launching; PhoneApplicationService.Current.Deactivated+=Current_Deactivating; PhoneApplicationService.Current.Activated+=Current_Activating; #endif Events
With XNA you have control over • The vibration function • The accelerometer • The touch screen • The camera • But not (yet) • The compass • The light and proximity sensors • Bluetooth Sensors
When you start a new Windows Phone 7 XNA Game everything is setup for you: Your Game class A spriteBatch object to draw sprites Your Graphics Devide Manager object Your Content root Your Initialize function Your LoadContent function Your UnloadContent function Your Update function Your Draw function Start Initialize LoadContent Update Draw UnloadContent Finish Let’s see how easy it is
How to use multi touch • Touch can detect Pressed, Moved, Released events • Is arranged in touch collections with unique IDs // Processtouchevents TouchCollectiontouchCollection = TouchPanel.GetState(); foreach (TouchLocationtl in touchCollection) { if ((tl.State == TouchLocationState.Pressed) || (tl.State == TouchLocationState.Moved)) { // addsparklesbased on the touch location sparkles.Add(new Sparkle(tl.Position.X, tl.Position.Y, tl.Id)); } } A few examples
How to use the accelerometer • Use a ReadingChanged event • Read 3 axis data A few examples
Accelerometer accelSensor; Vector3 accelReading = new Vector3(); public voidAccelerometerReadingChanged(objectsender, AccelerometerReadingEventArgs e) { accelReading.X = (float)e.X; accelReading.Y = (float)e.Y; accelReading.Z = (float)e.Z; } accelSensor = new Accelerometer(); // Add the accelerometereventhandler to the accelerometer sensor. accelSensor.ReadingChanged += new EventHandler<AccelerometerReadingEventArgs>(AccelerometerReadingChanged); // Start the accelerometer try { accelSensor.Start(); accelActive = true; } catch (AccelerometerFailedException e) { // the accelerometercouldn'tbestarted. No fun! accelActive = false; } catch (UnauthorizedAccessException e) { // This exception isthrown in the emulator-whichdoesn't support an accelerometer. accelActive = false; } A few examples
// Start the accelerometer try { accelSensor.Start(); accelActive = true; } catch (AccelerometerFailedException e) { // the accelerometercouldn'tbestarted. No fun! accelActive = false; } catch (UnauthorizedAccessException e) { // This exception isthrown in the emulator-whichdoesn't support an accelerometer. accelActive = false; } if (accelActive) { // accelerate the sparkledepending on accelerometer // action. s.speed.X += accelReading.X * ACCELFACTOR; s.speed.Y += -accelReading.Y * ACCELFACTOR; } A few examples
//When the game is paused or accelerometer data is not needed // Stop the accelerometer if it's active. if (accelActive) { try { accelSensor.Stop(); } catch (AccelerometerFailedException e) { // the accelerometer couldn't be stopped now. } } A few examples
The App Hub http://developer.windowsphone.com/ • Windows Phone 7 Developer Training Kit http://create.msdn.com/en-US/education/catalog/article/wp7_training_kit • Windows Phone 7 Jump Start http://create.msdn.com/en-US/education/catalog/article/wp7_jump_start • Education http://create.msdn.com/en-US/education/ Where to start
MonsterUp My latest game
MonsterUp My latest game