Making your game fullscreen is easy, but requires changing a few details of the
program, as well as adding a couple lines of code.
In this lesson we will cover two things. First, we will go over how to globalize
your screen resolution and why you would do this. Second, we'll cover the
mechanics of making a window go into fullscreen mode and back again.
Throughout your DirectX experience and in game programming you will come across
many functions and structs that demand to know your screen size. This can
become a hassle when you decide to change the resolution later, and especially when
you decide to change it during run-time. For right now, we will cover a simple
method to standardize your screen size across your program.
First, we must add two directives to the top of our program. These represent
the screen width and the screen height.
// define the screen resolution
#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 600
The next step is to go through your program to where you indicate the width and
height of your window. Up to this point in the tutorial, you only have two,
although we will come across another in a minute. Do this to the code (changes
in bold):
hWnd = CreateWindowEx(NULL,
L"WindowClass",
L"Our Direct3D Program",
WS_OVERLAPPEDWINDOW,
300, 300,
SCREEN_WIDTH, SCREEN_HEIGHT, // set window to new resolution
NULL,
NULL,
hInstance,
NULL);
And this:
viewport.TopLeftX = 0; // set the left to 0
viewport.TopLeftY = 0; // set the top to 0
viewport.Width = SCREEN_WIDTH; // set
the width to the window's width
viewport.Height = SCREEN_HEIGHT; //
set the height to the window's height
In a later lesson we will cover how to maintain screen size throughout your game
after changing it during runtime.
There are specific resolutions that are available
on most PCs, the most common
of which can be seen in this table.
[Table 2.1 - Common Screen Resolutions]
|
Resolution |
Pixels |
Widescreen |
|
800 x 600 |
480,000 |
No |
|
1024 x 768 |
786,432 |
No |
|
1152 x 864 |
995,328 |
No |
|
1280 x 1024 |
1,310,720 |
No |
|
1600 x 1200 |
1,920,000 |
No |
|
1440 x 900 |
1,296,000 |
Yes |
|
1680 x 1050 |
1,764,000 |
Yes |
|
1920 x 1080 |
2,073,600 |
Yes |
|
1920 x 1200 |
2,304,000 |
Yes |
|
[Close Table] |
[See Full Table] |
|
While almost all major games are played in fullscreen mode, many games include the
ability to switch between fullscreen and windowed mode. While we want fullscreen
by default, we also want the user to have the ability to switch easily between one
and the other. This is always done using the Alt-Enter keys.
When upgrading to full screen, there are several things we need to do:
1. Modify the window to have no background.
2. Set the back buffer to a specific size.
3. Set DirectX to automatically switch when Alt-Enter is used.
4. Modify the cleanD3D() function to turn off fullscreen when closing.
To remove the window's background, all we need to do is comment one of the members of the WINDOWCLASSEX struct that was used to set the background color. Doing
this leaves the background color untouched, which means it won't be visible as a
window
for a second or two before the game starts (important to making your game look professional).
// wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
Next, we have to tell DirectX about our new screen resolution. We do this
by making a few changes to the scd struct we built in the last lesson. Let's
look at what they are before we see what they do.
void initD3D(HWND hWnd)
{
DXGI_SWAP_CHAIN_DESC scd; // create a struct to hold
various swap chain information
ZeroMemory(&scd, sizeof(DXGI_SWAP_CHAIN_DESC)); //
clear out the struct for use
scd.BufferCount = 1; // create two buffers, one for the
front, one for the back
scd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; //
use 32-bit color
scd.BufferDesc.Width = SCREEN_WIDTH; // set the back
buffer width
scd.BufferDesc.Height = SCREEN_HEIGHT; // set the back
buffer height
scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
// tell how the chain is to be used
scd.OutputWindow = hWnd; // set the window to be used
by Direct3D
scd.SampleDesc.Count = 1; // set the level of multi-sampling
scd.SampleDesc.Quality = 0; // set the quality of multi-sampling
scd.Windowed = TRUE; // set to windowed or full-screen
mode
// ...
This step is quite
simple. All we need to do is add a flag to the scd struct. The rest is handled for us. The DXGI_SWAP_CHAIN_DESC struct actually has a
'Flags' member, which we can use for this.
void initD3D(HWND hWnd)
{
DXGI_SWAP_CHAIN_DESC scd; // create a struct to hold
various swap chain information
ZeroMemory(&scd, sizeof(DXGI_SWAP_CHAIN_DESC)); //
clear out the struct for use
scd.BufferCount = 1; // create two buffers, one for the
front, one for the back
scd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; //
use 32-bit color
scd.BufferDesc.Width = SCREEN_WIDTH; // set the back
buffer width
scd.BufferDesc.Height = SCREEN_HEIGHT; // set the back
buffer height
scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; //
tell how the chain is to be used
scd.OutputWindow = hWnd; // set the window to be used
by Direct3D
scd.SampleDesc.Count = 1; // set the level of multi-sampling
scd.SampleDesc.Quality = 0; // set the quality of multi-sampling
scd.Windowed = TRUE; // set to windowed or full-screen
mode
scd.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;
// allow full-screen switching
// ...
Direct3D is actually incapable of closing when in fullscreen mode. This is
due to certain threading issues that occur behind the scenes. To correctly
close down, we must make sure that we are in windowed mode. We can use the
function SetFullscreenState() to do this. Here's what the new cleanD3D() function
will look like:
// this is the function that cleans up Direct3D and COM
void cleanD3D(void)
{
swapchain->SetFullscreenState(FALSE, NULL); //
switch to windowed mode
swapchain->Release(); // close and release
the swap chain
rtv->Release(); // close and release the render target
view
device->Release(); // close and release the 3D device
}
The first parameter of the function is which state you wish to switch to.
FALSE indicates windowed mode, while TRUE indicates fullscreen mode.
The second parameter is a feature which could allow you to select which video adapter
to use. This is useful for specific cases where you use multiple monitors.
However, for almost every game, you could just set this to NULL and it will choose
the correct adapter.
So, there isn't much to change. Let's take a look at the whole picture and
see what is different and what is the same.
[
Show Code]
// include the basic windows header files and the Direct3D header files
#include <windows.h>
#include <windowsx.h>
#include <d3d10.h>
#include <d3dx10.h>
// include the Direct3D Library file
#pragma comment (lib, "d3d10.lib")
#pragma comment (lib, "d3dx10.lib")
// define the screen resolution
#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 600
// global declarations
ID3D10Device* device; // the pointer to our Direct3D device interface
ID3D10RenderTargetView* rtv; // the pointer to the render target view
IDXGISwapChain* swapchain; // the pointer to the swap chain class
// function prototypes
void initD3D(HWND hWnd); // sets up and initializes Direct3D
void render_frame(void); // renders a single frame
void cleanD3D(void); // closes Direct3D and releases memory
// the WindowProc function prototype
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
// the entry point for any Windows program
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
HWND hWnd;
WNDCLASSEX wc;
ZeroMemory(&wc, sizeof(WNDCLASSEX));
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
// wc.hbrBackground = (HBRUSH)COLOR_WINDOW; // no
longer
needed
wc.lpszClassName = L"WindowClass";
RegisterClassEx(&wc);
hWnd = CreateWindowEx(NULL,
L"WindowClass",
L"Our Direct3D Program",
WS_OVERLAPPEDWINDOW,
0, 0,
SCREEN_WIDTH, SCREEN_HEIGHT,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hWnd, nCmdShow);
// set up and initialize Direct3D
initD3D(hWnd);
// enter the main loop:
MSG msg;
while(TRUE)
{
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
if(msg.message == WM_QUIT)
break;
}
render_frame();
}
// clean up DirectX and COM
cleanD3D();
return msg.wParam;
}
// this is the main message handler for the program
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
} break;
}
return DefWindowProc (hWnd, message, wParam, lParam);
}
// this function initializes and prepares Direct3D for use
void initD3D(HWND hWnd)
{
DXGI_SWAP_CHAIN_DESC scd; // create a struct to hold
various swap chain information
ZeroMemory(&scd, sizeof(DXGI_SWAP_CHAIN_DESC)); //
clear out the struct for use
scd.BufferCount = 1; // create two buffers, one for the
front, one for the back
scd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; //
use 32-bit color
scd.BufferDesc.Width = SCREEN_WIDTH; // set the back
buffer width
scd.BufferDesc.Height = SCREEN_HEIGHT; // set the back
buffer height
scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
// tell how the chain is to be used
scd.OutputWindow = hWnd; // set the window to be used
by Direct3D
scd.SampleDesc.Count = 1; // set the level of multi-sampling
scd.SampleDesc.Quality = 0; // set the quality of multi-sampling
scd.Windowed = TRUE; // set to windowed or full-screen
mode at startup
scd.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;
// allow full-screen switching
// create a device class and swap chain class using the information
in the scd struct
D3D10CreateDeviceAndSwapChain(NULL,
D3D10_DRIVER_TYPE_HARDWARE,
NULL,
0,
D3D10_SDK_VERSION,
&scd,
&swapchain,
&device);
// get the address of the back buffer and use it to create the render
target
ID3D10Texture2D* pBackBuffer;
swapchain->GetBuffer(0, __uuidof(ID3D10Texture2D), (LPVOID*)&pBackBuffer);
device->CreateRenderTargetView(pBackBuffer, NULL, &rtv);
pBackBuffer->Release();
// set the back buffer as the render target
device->OMSetRenderTargets(1, &rtv, NULL);
D3D10_VIEWPORT viewport; // create a struct to hold the
viewport data
ZeroMemory(&viewport, sizeof(D3D10_VIEWPORT)); //
clear out the struct for use
viewport.TopLeftX = 0; // set the left to 0
viewport.TopLeftY = 0; // set the top to 0
viewport.Width = SCREEN_WIDTH; // set
the width to the window's width
viewport.Height = SCREEN_HEIGHT; // set the height to the window's
height
device->RSSetViewports(1, &viewport); // set the
viewport
}
// this is the function used to render a single frame
void render_frame(void)
{
// clear the window to a deep blue
device->ClearRenderTargetView(rtv, D3DXCOLOR(0.0f, 0.2f, 0.4f,
1.0f));
// do 3D rendering on the back buffer here
// display the rendered frame
swapchain->Present(0, 0);
}
// this is the function that cleans up Direct3D and COM
void cleanD3D(void)
{
swapchain->SetFullscreenState(FALSE, NULL); //
switch to windowed mode
swapchain->Release(); // close and release
the swap chain
rtv->Release(); // close and release the render target
view
device->Release(); // close and release the 3D device
}
There isn't really a point in me showing a screenshot of this program's result,
because it would just be a blue rectangle. Your program should look like that:
a blue rectangle with a mouse pointer in it.
Great! We now have a simple app that can correctly switch to full screen and
back. It's a simple upgrade, but it's necessary for an actual game.
1.
Change the resolution of the program until you are familiar with the various
resolutions selectable.
When you're ready to go on, let's hit the next lesson!
Next Lesson: An Overview of the Third Dimension
GO! GO! GO!
Liked the Lesson? Hated the lesson? Give your feedback.
© 2006-2011 DirectXTutorial.com. All Rights Reserved.