1 / 21

Behind the Scene Tricks

XNA Utilities. Behind the Scene Tricks. From: http ://rbwhitaker.wikidot.com/utility-tutorials. Changing the Window Size. Changing the window size is extremely easy. It is really only two lines of code.

sumi
Télécharger la présentation

Behind the Scene Tricks

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. XNA Utilities Behind the Scene Tricks From: http://rbwhitaker.wikidot.com/utility-tutorials

  2. Changing the Window Size • Changing the window size is extremely easy. It is really only two lines of code. • Go to the constructor of your game (probably called Game1) and add the following code: graphics.PreferredBackBufferWidth = 100; graphics.PreferredBackBufferHeight = 500; graphics.ApplyChanges(); • You can now run your game and see that your window is the size that you want.

  3. Setting the Window Title • Setting the title of your XNA game window is very simple. • It is just one line of code, which you can put in your main game class's constructor: this.Window.Title = "New Title";

  4. Make Game Window Match Current Monitor Resolution • In a Windows-based game, if you want to run in full screen mode, you will likely want to make the window size be the same size as the monitor resolution. • The GraphicsDevice has a reference to something called the DisplayMode, which knows your resolution. • Use that information to appropriately set the size of your game window, and then go into full screen mode: graphics.PreferredBackBufferWidth = GraphicsDevice.DisplayMode.Width; graphics.PreferredBackBufferHeight = GraphicsDevice.DisplayMode.Height; graphics.IsFullScreen = true; graphics.ApplyChanges();

  5. Closing the Program in Full Screen Mode • Before we put our program into full screen mode, make sure you have a way to exit your game. • If you are running your game on the XBox, the standard game template has already built in the functionality to press the Back button and quit the game. • If you are running on Windows, and you don't have an XBox controller plugged in, you might run into trouble.

  6. Closing the Program in Full Screen Mode • In full screen mode, the little 'X' in the corner won't be around. So, to deal with this small problem, let's add the following code to our Update() method: if(Keyboard.GetState().IsKeyDown(Keys.Escape)) this.Exit(); • If you've looked at the Basic Keyboard Input tutorial, you will know exactly what this does. If not, just put it in - this makes it so you can press Escape and quit the program.

  7. Console Output • There's a lot of beginning XNA game developers who have done a lot of work using programs involving only a console window, i.e., “Hello, World”. • For those of you who are like this, you are probably glad to be doing something that is actually interesting to look at. However, it is nice to have console access to output debug information. • An XNA game doesn't have a console window by default, this you need to enable the console. • For those of you who haven't really used a console window in C# before, we will also take a look at how to use it once we've got it open.

  8. Enabling a Console Window • Getting the console is fairly simple. • First, open the project's properties page. • Go to the Solution Explorer and right click on your project to get the menu below to appear:

  9. Enabling a Console Window • Select Properties from the menu. A new window will open up that contains various properties for the project. This window will look like the one below:

  10. Enabling a Console Window • Select the Application tab if it isn't already. Then, in the Output type: drop down box, change it from 'Windows Application' to 'Console Application'. • Now, when you run your program, a console window will be available. While this is nice for programmers, be sure to turn it off when you release the application to the public, by switching this back to 'Windows Application'. • At this point, you should be able to run your game and see the black console window running in the background.

  11. Printing to the Console Window • Many of you probably already know how to use a console window, now that you have it open. But for those of you who haven't used one in C# before, this next section will show you how. • To write a line of text to the console window, try the following: Console.WriteLine("This will be displayed in the console window."); • If you run your program with this code in it, the text "This will be displayed in the console window." will be displayed in the console window.

  12. Printing to the Console Window • We can also write the contents of a variable to the console window. int number = 3; Console.WriteLine(number); • This will write the number '3' to the console window. • As a final trick, let's try combining both text and our variable and outputting it: int number = 3; Console.WriteLine("The value of the number is " + number + ".");

  13. Using the Output Window Instead • Another alternative is to use the built-in Output Window that Visual C# Express has. The Output Window displays everything that is supposed to be written to the console, and it resides in Visual C# Express, instead of in a black window next to your game. • To open this, go to View > Output Window while your game is running. It will appear as another window, like the Error List, at the bottom of Visual C# Express.

  14. Wireframe Rendering • One common method for drawing objects is to use a wireframe. • This means that the graphics device will only draw the lines that make up an object, rather than all of the faces. • This gives sort of an outline look to the drawing, and in addition to just being cool, has several uses. • This allows for less intensive rendering for debugging purposes.

  15. Using the Output Window Instead • Below is an image which shows wire frames. • In this case, wire frame allows the user to see through their ship to other objects behind them.

  16. Changing Modes • Performing this in XNA is actually very simple. It is a simple matter of changing one variable. • While you are drawing objects (for example in the Draw()method), we need to create a new RasterizerState object, set its FillMode property to the desired mode, and assign it to the graphics device. • As you can see from the screenshot below, you can mix-and-match drawing some images as a wire frame, and others as solid, in whatever order you want. • It may also be worth hanging on to the original RasterizerState so that when you are done with wireframe mode, you can simply switch back.

  17. Changing Modes • This setting in the RasterizerState take on any of the following values: • FillMode.Solid • FillMode.WireFrame • So for example, in your main game class you can use code like below to change your game state: RasterizerStaterasterizerState = new RasterizerState(); rasterizerState.FillMode = FillMode.WireFrame; GraphicsDevice.RasterizerState = rasterizerState;

  18. Changing Modes • You may also want to save your original raster state: RasterizerStateoriginalState = GraphicsDevice.RasterizerState; RasterizerStaterasterizerState = new RasterizerState(); rasterizerState.FillMode = FillMode.WireFrame; GraphicsDevice.RasterizerState = rasterizerState; // Do your wireframe drawing here... GraphicsDevice.RasterizerState = originalState;

  19. Calculating the Frame Rate • If your game is running a little slow, or you are just curious about how many frames per second you are getting, you've come to the right place. Here is an extremely simple way to check the frame rate! • In XNA we can calculate the frame rate of our game with one line of code in the Draw() method: float frameRate = 1 / (float)gameTime.ElapsedGameTime.TotalSeconds;

  20. Seeing the Frame Rate • Obviously, the line of code above isn't going to really do much good if you can't see the frame rate yourself, so here's a couple of suggestions for how to do that if you don't already know how: • Draw it with a Sprite Font. It's pretty simple, and is worth learning about, if you don't know how. I've already made a tutorial for Drawing Text with SpriteFonts that you can look at to do this. • A second valid option would be to write it out to the command line. I've also written a tutorial about making the command line visible in XNA.

  21. Some Other Notes • Similar code can be placed in the Update() method and you can see the number of updates that are being done each second as well. • Default frame rate is 60 frames per second. XNA allows you to set it to as frequent as possible. • Many high end games run with a fixed frame rate around 30 frames per second. If your frame rate is down below 15 or 20 frames per second, the players will begin to notice. • Also, this method doesn't do any sort of averaging, and it might be useful to calculate the average frame rate over a small span of time.

More Related