9/12/09 1 Lecture 6: Introduction to XNA, Game Loops, and C# Gotchas Prof. Aaron Lanterman School of Electrical and Computer Engineering Georgia Institute of Technology 2 Part 1: Introduction to XNA 3 Dungeon Quest • Developed in 4 days at the 2007 GDC at the XNA contest • By Benjamin Nitschke and Christoph Rienaecker Screenshot from exdream.no-ip.info/blog/2007/07/31/DungeonQuestUpdatedWithSourceCodeNow.aspx9/12/09 2 5 Torpex’s “Schizoid” (on Xbox Live Arcade) http://www.gametrailers.com/player/28542.html Screenshot from http://screenshots.teamxbox.com/screen/68599/Schizoid/ 6 XNA GS Framework • Built on Microsoft’s .NET – Makes MS comfortable with letting “ordinary folks” program on the Xbox 360 • C# is standard language for XNA development – But in theory could use Managed C++, VB.NET, etc. on the PC • Xbox 360 uses .NET Compact Framework – Some stuff available in .NET on the PC is missing! – Garbage collector on 360 isn’t as smart as on the PC – Caused the Schizoid team some trouble, as well as one semester of CS4455 7 Is managed code too slow for games? • Vertigo Software ported Quake II to Managed C++, got 85% performance of the original C code – Should expect to do better if you have the .NET Common Language Runtime in mind from the beginning • Xbox 360 – GPU: 337 million transistors – CPU: 165 million transistors 8 XNA GS graphics • XNA is built on top of DirectX 9 – Not built on MDX or Managed DirectX • DirectX 9 has a fixed function pipeline, but XNA doesn’t! – Everything done with shaders – There is a BasicEffect to get you started From XNA Team Blog, “What is the XNA Framework,” blogs.msdn.com/xna/archive/2006/08/25/724607.aspx9/12/09 3 9 Why no fixed-function pipeline? (1) • Programmable pipeline is the future – Neither Direct3D 10 or Xbox 360 have fixed-function pipeline • Early adopters and customers said cross-platform goal more important than fixed-function pipeline From XNA Team Blog, “What is the XNA Framework,” blogs.msdn.com/xna/archive/2006/08/25/724607.aspx In Microsoft’s own words (paraphrased): 10 Why no fixed-function pipeline? (2) • Fear is someone would start and finish their game using the fixed-function APIs, and then get dozens of errors when they tried to compile it on the Xbox 360 • Better to know your code works on both right from the beginning From XNA Team Blog, “What is the XNA Framework,” blogs.msdn.com/xna/archive/2006/08/25/724607.aspx In Microsoft’s own words (paraphrased): 11 Some convenient things about XNA • Don’t need to mess with Win32-ish boilerplate (opening a window, etc.) • Easy interfacing with the Xbox 360 controller (for both Windows and Xbox 360) • Storage (“saved games”) unified between Windows and Xbox 360 – On Xbox 360, have to associate data with a user profile, put on hard drive or memory card, etc. – XNA “emulates” this on Windows From XNA Team Blog, “What is the XNA Framework,” blogs.msdn.com/xna/archive/2006/08/25/724607.aspx 12 Hello Bluescreen public class SampleGame : Game {!""" private GraphicsComponent graphics;!""!""" public SampleGame() {!""""""" this.graphics = new GraphicsComponent();!""""""" this.GameComponents.Add(graphics);!""" }!""!""" protected override void Update() { }!""!""" protected override void Draw() {!""""""" this.graphics.GraphicsDevice.Clear(Color.Blue);!""""""" this.graphics.GraphicsDevice.Present();!""" }!""!""" static void Main(string[] args) {!""""""" using (SampleGame game = new SampleGame()) {!""""""""""" game.Run();!""""""" }!"" }! From XNA Team Blog, “What is the XNA Framework,” blogs.msdn.com/xna/archive/2006/08/25/724607.aspx9/12/09 4 13 Careful if you’re on Windows x64 • XNA normally targets “AnyCPU” • Will break when you try to run on x64 machines, since x64 versions XNA framework dlls don’t exist (yet – maybe never?) • Workaround: Change target to x86 14 Caveats about Xbox 360 development • Many TVs cutoff 5-10% of the pixels around the edge – Keep text & important info away from there • Xbox 360 handles post processing and render targets a little differently than the PC Info from Alistair Wallis, “Microsoft XNA: A Primer,” interview with Benjamin Nitschke www.gamecareerguide.com/features/328/microsofts_xna_a_.php?page=4 15 Dream Build Play contest • See http://www.dreambuildplay.com • This year’s contest already over… • …but keep on the lookout for the 2010 Dream Build Play contest! 16 XNA Community Games • See http://creators.xna.com • Join the XNA Creator’s Club – The XNA CC memberships students get free from DreamSpark will let you run games on the 360, but may not let you take part in Community Games • Upload your game, rate content (violence, etc.) • Peer review – confirm content ratings, check quality • Can sell your game to Xbox 360 users!9/12/09 5 17 Example: A Fading Melody XNA CG sales (March 31, 2009) 18 From from http://www.gamasutra.com/php-bin/news_index.php?story=22970 Part 2: Game Loops 19 Credit to where it is due • Koen Witters – Thinking about game loops • Shawn Hargreaves – Details about XNA’s game loop • Side note: next few slides on game loops contain rough pseudocode 209/12/09 6 Simplest game loop (1) running = true; while(running) { update(); draw(); } http://dewitters.koonsolo.com/gameloop.html • What could possibly go wrong? bad_guy.x += 1; • Draw() has things like Simplest game loop (2) http://dewitters.koonsolo.com/gameloop.html • Game runs faster on faster hardware, slower on slower hardware • Less of a problem if hardware is well-defined; Apple II+, Commodore 64, game console • Try an original Mac game on a Mac II: too fast! • Big problem on PCs/Macs with varying speed • Can still be a problem if update time varies from iteration to iteration (i.e. varying number of bad guys) – See Defender and Robotron: 2084 FPS dependent on constant GS (1) http://dewitters.koonsolo.com/gameloop.html running = true; seconds_per_frame = 1/60; while(running) { update(); draw(); if (seconds_per_frame_not_elapsed_yet) wait(remaining_time); else { oooops! We are running behind! } } • What could possibly go wrong? FPS dependent on constant GS (2)
View Full Document