Rendering complex 3D scenes is by definition "complex." While it is true that the vast majority of games being released currently use 3D graphics, there are still plenty of game types out there where the "fancy" graphics aren't necessary. It seems that more often than not these days, games that should be written in 2D are still written in 3D, just because you "have to." Given that Direct3D can render complex 3D scenes, it should be no surprise that it is also quite capable of rendering a fully two-dimensional scene. Creating a full screen device is not that much different from creating a windowed device. The same basic principles apply; there are just a few extra properties that will need to be filled out. Create a windows forms application that can be used to render the 2D graphics. You will need to add the using clauses, declare the device variable, and set the windows style before the application, like has been done in previous chapters. Once the project has been created, you will need to add two constants for the full screen size: public const int ScreenWidth = 800; public const int ScreenHeight = 600; These constants will represent the width and height of the full screen window that will be used for rendering. With the constants declared, you can now add the updated initialization method found in Listing 16.1. Listing 16.1 Initializing Your Full Screen Devicepublic void InitializeGraphics() { // Set our presentation parameters PresentParameters presentParams = new PresentParameters(); presentParams.SwapEffect = SwapEffect.Discard; // Start up full screen Format current = Manager.Adapters[0].CurrentDisplayMode.Format; if (Manager.CheckDeviceType(0, DeviceType.Hardware, current, current, false)) { // Perfect, this is valid presentParams.Windowed = false; presentParams.BackBufferFormat = current; presentParams.BackBufferCount = 1; presentParams.BackBufferWidth = ScreenWidth; presentParams.BackBufferHeight = ScreenHeight; } else { presentParams.Windowed = true; } // Create our device device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams); } The beginning of this method should appear familiar. You'll notice that there is no depth buffer being declared for this device. Since the only objects being rendered will be 2D, and thus at the same depth, keeping the depth buffer provides no benefit. In actuality it's a performance hit, since the depth check will be performed for each sprite. It's normally a safe assumption that the current display mode format of the default adapter is a valid format for a full screen device. Before blindly assuming this is the case, you should perform the check done here by calling the CheckDeviceType method. If this device type is supported, update the presentation parameters to specify a full screen device. Naturally the windowed property must be false, since the device will run full screen, not in a window. The full screen properties are all maintained by the back buffer properties in your presentation parameters structure. The width and height of the back buffer should be set to the full screen size you wish to use, so the constants that were declared earlier are used here. The format is set to the default adapters display mode that was stored and checked to be supported.
If for some reason the check fails, you don't want the sample to just quit, so you can revert to windowed mode by setting the appropriate member in your presentation parameters structure. Once this structure has been filled out, you can actually create the device.
Since you will be rendering your scene in full screen mode, you will need an easy way to quit the application. Using the Escape key is a common way to quit, so add the following override to your code: protected override void OnKeyUp(KeyEventArgs e) { if (e.KeyCode == Keys.Escape) // Quit this.Close(); base.OnKeyUp (e); } |