1. Trang chủ
  2. » Công Nghệ Thông Tin

microsoft xna framework edition

430 256 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 430
Dung lượng 5,62 MB

Nội dung

www.it-ebooks.info Microsoft XNA Framework Edition: Programming Windows Phone 7 Charles Petzold A01T656697.indd iA01T656697.indd i 11/30/2010 1:16:46 PM www.it-ebooks.info PUBLISHED BY Microsoft Press A Division of Microsoft Corporation One Microsoft Way Redmond, Washington 98052-6399 Copyright © 2010 by Microsoft Corporation (All) All rights reserved. No part of the contents of this book may be reproduced or transmitted in any form or by any means without the written permission of the publisher. Library of Congress Control Number: 2010941603 ISBN: 978-0-7356-5669-7 Printed and bound in the United States of America. Microsoft Press books are available through booksellers and distributors worldwide. For further infor mation about international editions, contact your local Microsoft Corporation offi ce or contact Microsoft Press International directly at fax (425) 936-7329. Tell us what you think of this book at http://www.microsoft.com/learning/booksurvey. Send comments to mspinput@microsoft.com. Microsoft and the trademarks listed at http://www.microsoft.com/about/legal/en/us/IntellectualProperty/ Trademark/EN-US.aspx are trademarks of the Microsoft group of companies. All other marks are the property of their respective owners. The example companies, organizations, products, domain names, e-mail addresses, logos, people, places, and events depicted herein are fi ctitious. No association with any real company, organization, product, domain name, e-mail address, logo, person, place, or event is intended or should be inferred. This book expresses the author’s views and opinions. The information contained in this book is provided without any express, statutory, or implied warranties. Neither the authors, Microsoft Corporation, nor its resellers, or distributors will be held liable for any damages caused or alleged to be caused either directly or indirectly by this book. Acquisitions Editor: Devon Musgrave Developmental Editor: Devon Musgrave Project Editor: Devon Musgrave Editorial Production: Ashley Schneider, S4Carlisle Publishing Services Technical Reviewer: Per Blomqvist; Technical Review Services provided by Content Master, a member of CM Group, Ltd. Body Part No. X17-35782 A02L656697.indd iiA02L656697.indd ii 11/30/2010 1:22:43 PM11/30/2010 1:22:43 PM www.it-ebooks.info iii Contents at a Glance Part I The Basics 1 Hello, Windows Phone 7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 2 Getting Oriented. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 3 An Introduction to Touch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 4 Bitmaps, Also Known as Textures. . . . . . . . . . . . . . . . . . . . . . . . . . 65 5 Sensors and Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 6 Issues in Application Architecture . . . . . . . . . . . . . . . . . . . . . . . . 107 Part II XNA 7 Principles of Movement. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 8 Textures and Sprites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169 9 Dynamic Textures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193 10 From Gestures to Transforms . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233 11 Touch and Play . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293 12 Tilt and Play . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345 A03C656697.indd iiiA03C656697.indd iii 11/18/2010 5:17:10 PM11/18/2010 5:17:10 PM www.it-ebooks.info A03C656697.indd ivA03C656697.indd iv 11/18/2010 5:17:11 PM11/18/2010 5:17:11 PM www.it-ebooks.info v Table of Contents Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xi Part I The Basics 1 Hello, Windows Phone 7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Targeting Windows Phone 7. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 The Hardware Chassis. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Sensors and Services. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 File | New | Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 A First Silverlight Phone Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 The Standard Silverlight Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Color Themes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Points and Pixels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 The XAP is a ZIP. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 An XNA Program for the Phone . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 2 Getting Oriented. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 Silverlight and Dynamic Layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 Orientation Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 XNA Orientation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 Simple Clocks (Very Simple Clocks) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 3 An Introduction to Touch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 Low-Level Touch Handling in XNA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 The XNA Gesture Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 Low-Level Touch Events in Silverlight . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 The Manipulation Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 Routed Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 Some Odd Behavior? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 What do you think of this book? We want to hear from you! Microsoft is interested in hearing your feedback so we can continually improve our books and learning resources for you. To participate in a brief online survey, please visit: microsoft.com/learning/booksurvey A04T656697.indd vA04T656697.indd v 11/18/2010 5:17:25 PM11/18/2010 5:17:25 PM www.it-ebooks.info vi Table of Contents 4 Bitmaps, Also Known as Textures. . . . . . . . . . . . . . . . . . . . . . . . . . 65 XNA Texture Drawing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 The Silverlight Image Element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 Images Via the Web . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Image and ImageSource . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 Loading Local Bitmaps from Code. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 Capturing from the Camera . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 The Phone’s Photo Library. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 5 Sensors and Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 Accelerometer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 A Simple Bubble Level . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 Geographic Location . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 Using a Map Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 6 Issues in Application Architecture . . . . . . . . . . . . . . . . . . . . . . . . 107 Basic Navigation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 Passing Data to Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 Sharing Data Among Pages. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 Retaining Data across Instances . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 The Multitasking Ideal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 Task Switching on the Phone . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 Page State. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126 Isolated Storage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130 Xna Tombstoning and Settings. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 Testing and Experimentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140 Part II XNA 7 Principles of Movement. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 The Naïve Approach. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 A Brief Review of Vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146 Moving Sprites with Vectors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151 Working with Parametric Equations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 Fiddling with the Transfer Function. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156 Scaling the Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157 Two Text Rotation Programs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161 A04T656697.indd viA04T656697.indd vi 11/18/2010 5:17:26 PM11/18/2010 5:17:26 PM www.it-ebooks.info Table of Contents vii 8 Textures and Sprites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169 The Draw Variants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169 Another Hello Program? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171 Driving Around the Block . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176 Movement Along a Polyline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180 The Elliptical Course . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185 A Generalized Curve Solution. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188 9 Dynamic Textures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193 The Render Target. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193 Preserving Render Target Contents. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202 Drawing Lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206 Manipulating the Pixel Bits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213 The Geometry of Line Drawing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217 Modifying Existing Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229 10 From Gestures to Transforms . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233 Gestures and Properties. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233 Scale and Rotate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237 Matrix Transforms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245 The Pinch Gesture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249 Flick and Inertia. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257 The Mandelbrot Set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260 Pan and Zoom . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271 Game Components. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278 Affi ne and Non-Affi ne Transforms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282 11 Touch and Play . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293 More Game Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293 The PhingerPaint Canvas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299 A Little Tour Through SpinPaint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309 The SpinPaint Code. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311 The Actual Drawing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316 PhreeCell and a Deck of Cards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321 The Playing Field . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322 Play and Replay . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331 A04T656697.indd viiA04T656697.indd vii 11/18/2010 5:17:27 PM11/18/2010 5:17:27 PM www.it-ebooks.info viii Table of Contents 12 Tilt and Play . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345 3D Vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345 A Better Bubble Visualization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347 The Graphical Rendition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 356 Follow the Rolling Ball . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364 Navigating a Maze . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 376 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389 What do you think of this book? We want to hear from you! Microsoft is interested in hearing your feedback so we can continually improve our books and learning resources for you. To participate in a brief online survey, please visit: microsoft.com/learning/booksurvey A04T656697.indd viiiA04T656697.indd viii 11/18/2010 5:17:27 PM11/18/2010 5:17:27 PM www.it-ebooks.info ix Introduction Important This book and Microsoft Silverlight Edition: Programming Windows Phone 7 are fully indexed, print-book versions of a single free, electronic edition titled Programming Windows Phone 7, which you can fi nd on the Microsoft Press blog: http://blogs.msdn.com/b/ microsoft_press/. No changes have been made to the original edition’s text, including references to the color of the original images, which appear black and white in this book. What follows is the Introduction that originally appeared in Programming Windows Phone 7; “Code Samples” is the only section in this Introduction that has been updated. This book is a gift from the Windows Phone 7 team at Microsoft to the programming community, and I am proud to have been a part of it. Within the pages that follow, I show you the basics of writing applications for Windows Phone 7 using the C# programming language with the Silverlight and XNA 2D frameworks. Yes, Programming Windows Phone 7 is truly a free download, but for those readers who still love paper—as I certainly do—this book will also be available (for sale) divided into two fully-indexed print editions: Microsoft Silverlight Edition: Programming Windows Phone 7 and Microsoft XNA Framework Edition: Programming Windows Phone 7 . With the money you’ve saved downloading this book, please buy other books. Despite the plethora of information available online, books are still the best way to learn about programming within a coherent and cohesive tutorial narrative. Every book sale brings a tear of joy to an author’s eye, so please help make them weep overfl owing rivers. In particular, you might want to buy other books to supplement the material in this book. For example, I barely mention Web services in this book, and that’s a serious defi ciency because Web services are likely to become increasingly important in Windows Phone 7 applications. My coverage of XNA is limited to 2D graphics and while I hope to add several 3D chapters in the next edition of this book, I don’t really get into the whole Xbox LIVE community aspect of game development. Nor do I discuss any programming tools beyond Visual Studio—not even Expression Blend. My publisher Microsoft Press has a couple additional Windows Phone 7 books coming soon: Windows Phone 7 Silverlight Development Step by Step by Andy Wigley & Peter Foot offers a more tools-oriented approach. Although Michael Stroh’s Windows Phone 7 Plain & Simple is a guide to using the phone rather than developing for it, I suspect it will give developers some insights and ideas. Moreover, I also hear that my old friend Doug Boling is working hard on a Windows Phone 7 enterprise-programming book that is likely to be considered his masterpiece. Be sure to check out that one. A05I656697.indd ixA05I656697.indd ix 12/2/2010 3:39:19 PM12/2/2010 3:39:19 PM www.it-ebooks.info [...]... x:Class="SilverlightHelloPhone.MainPage" xmlns="http://schemas .microsoft. com/winfx/2006/xaml/presentation" xmlns:x="http://schemas .microsoft. com/winfx/2006/xaml" xmlns:phone="clr-namespace :Microsoft. Phone.Controls;assembly =Microsoft. Phone" xmlns:shell="clr-namespace :Microsoft. Phone.Shell;assembly =Microsoft. Phone" xmlns:d="http://schemas .microsoft. com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"... App.xaml (excerpt) You’ll recognize this file as XML, but more precisely... stand for something like XNA is Not an Acronym”—is Microsoft s game platform supporting both 2D sprite-based and 3D graphics with a traditional game-loop architecture Although XNA is mostly associated with writing games for the Xbox 360 console, developers can also use XNA to target the PC itself, as well as Microsoft s classy audio player, the Zune HD Either Silverlight or XNA would make good sense... content If you do find an error, e-mail Microsoft Press Book Support at mspinput @microsoft. com (Please note that product support for Microsoft software is not offered through this address.) We Want to Hear from You At Microsoft Press, your satisfaction is our top priority, and your feedback our most valuable asset Please tell us what you think of this book at: http://www .microsoft. com/learning/booksurvey... Microsoft Visual Studio 2010 Express for Windows Phone includes XNA Game Studio 4.0 and an on-screen phone emulator, and also integrates with Visual Studio 2010 You can develop visuals and animations for Silverlight applications using Microsoft Expression Blend 3 www.it-ebooks.info 4 Part I The Basics The Silverlight and XNA platforms for Windows Phone 7 share some libraries, and you can use some XNA. .. presents programs for both Microsoft Silverlight and Microsoft XNA that display the text “Hello, Windows Phone 7!” Just to make these programs a little more interesting, I want to display the text in the center of the display The Silverlight program will use the background and foreground colors selected by the user in the Themes section of the phone’s Settings screen In the XNA program, the text will... enhancements XNA is primarily for writing high-performance games For 2D games, you define sprites and backgrounds based around bitmaps; for 3D games you define models in 3D space The action of the game, which includes moving graphical objects around the screen and polling for user input, is synchronized by the built-in XNA game loop The differentiation between Silverlight-based applications and XNA- based... Silverlight territory, however Implementing a list box in XNA might be considered “fun” by some programmers but a torture by many others The first several chapters in this book describe Silverlight and XNA together, and then the book splits into different parts for the two platforms I suspect that some developers will stick with either Silverlight or XNA exclusively and won’t even bother learning the other... of Windows Mobile, that is certainly true But the support of Silverlight, XNA, and C# are not breaks with the past, but a balance of continuity and innovation As young as they are, Silverlight and XNA have already proven themselves as powerful and popular platforms Many skilled programmers are already working with either one framework or the other—probably not so many with both just yet—and they have... included), and developers can use C# to share libraries between their Silverlight and XNA programs as well as programs for other NET environments The Hardware Chassis Developers with experience targeting Windows Mobile devices of the past will find significant changes in Microsoft s strategy for the Windows Phone 7 Microsoft has been extremely proactive in defining the hardware specification, often referred . (for sale) divided into two fully-indexed print editions: Microsoft Silverlight Edition: Programming Windows Phone 7 and Microsoft XNA Framework Edition: Programming Windows Phone 7 . With the. www.it-ebooks.info Microsoft XNA Framework Edition: Programming Windows Phone 7 Charles Petzold A01T656697.indd iA01T656697.indd i 11/30/2010 1:16:46 PM www.it-ebooks.info PUBLISHED BY Microsoft Press A. Microsoft Press International directly at fax (425) 936-7329. Tell us what you think of this book at http://www .microsoft. com/learning/booksurvey. Send comments to mspinput @microsoft. com. Microsoft

Ngày đăng: 24/04/2014, 15:34

TỪ KHÓA LIÊN QUAN