Lesson 2: 走向全屏
From:
http://www.directxtutorial.com/tutorial9/b-direct3dbasics/dx9b2.aspx#still
Translated By----Double One(王大寶) dfghj77777@gmail.com
Finished At 20101215
筆者:自第一篇發出以來,已過一月。最近忙,翻譯有些耽擱,接下來我會保證一周至少一篇 ,謝謝大家的支持。
課程概述
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.
其實全屏化很容易,改幾行代碼就好,來死狗(Let’s Go)。
在這一課中我們將介紹兩件事情。首先,我們將研究怎樣改變你的屏幕分辨率以及為什么要這么做。其次呢,我們捎帶介紹一下使窗口進入全屏模式的機制原理。
設置屏幕尺寸
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.
在你的DirectX或者游戲編程經歷里你一定會碰到許多函數和結構體管你要屏幕尺寸,是的,這也許并不困難。可是當你要更改分辨率時麻煩就來了,尤其是你非得在運行的時候這么干。現在,哥將給你介紹一個簡單的方法來規范你的程序的屏幕大小,這個方法非常簡單,但是非常的實用,而且,它只要九——百——九——十——八,還猶豫什么,趕緊往下看吧。
First, we must add two directives to the top of our program. These represent the screen width and the screen height.
首先,我們在程序開頭添加兩個指令。這些代表了屏幕的寬度和屏幕的高度。
1
// define the screen resolution
2
#define SCREEN_WIDTH 800
3
#define SCREEN_HEIGHT 600
4
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 one, although we will come across another in a minute. Do this to the code (changes in bold):
接下來,來到聲明窗口寬度和高度的地方。本教程到目前為止,只有一個聲明的地方,注意粗體那行:
1
hWnd = CreateWindowEx(NULL,
2
L"WindowClass",
3
L"Our Direct3D Program",
4
WS_OVERLAPPEDWINDOW,
5
300, 300,
6
SCREEN_WIDTH, SCREEN_HEIGHT, // 設置新的尺寸
7
NULL,
8
NULL,
9
hInstance,
10
NULL);
11
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
|
When changing to full screen you are doing several things. First, your are telling Windows not to apply any of the standard Windows borders to your window. Second, you are telling Windows to have your window overlap all other things on the screen, including the start menu. Third, you are telling DirectX to change the resolution of the monitor to your set preference. Finally, although less importantly, you are telling Windows to leave the window background color up to you.
The first two of these are handled by changing some CreateWindowEx() parameters. The changes we need to make are shown here.
切換到全屏的時候你要做這么幾件事兒。首先,你告訴Windows不要添加任何標準的Windows窗口邊框。其次,你告訴不要把其他Windows窗口蓋在我們的全屏游戲上,包括開始菜單。再次,你告訴DirectX的把顯示器的分辨率設置成我們要的值。最后,盡管這條不是那么重要,你得告訴Windows離開窗口時的背景色。
前兩個處理通過改變一些CreateWindowEx()的參數。改動如下。
1
hWnd = CreateWindowEx(NULL,
2
L"WindowClass",
3
L"Our Direct3D Program",
4
WS_EX_TOPMOST | WS_POPUP, // fullscreen values
5
0, 0, // the starting x and y positions should be 0
6
SCREEN_WIDTH, SCREEN_HEIGHT,
7
NULL,
8
NULL,
9
hInstance,
10
NULL);
11
Here we set the starting x and y positions to 0. We also changed the previous parameter to "WS_EX_TOPMOST | WS_POPUP". The WS_EX_TOPMOST is self-explanatory, and makes the window overlap everything else. The WS_POPUP is less self-explanatory, but what it does is tell Windows to remove all borders of any kind, including the rounded-edge top that you see in Windows XP.
在這里,我們設置x和y的初始位置為0。我們還改變了第四個參數,將其設置為了“WS_EX_TOPMOST | WS_POPUP”。 WS_EX_TOPMOST顧名思義就是讓我們的全屏程序在桌面最頂層。WS_POPUP的作用是告訴Windows以消除任何形式的所有邊界,包括圓形的邊緣等窗口邊框,總之就是你所看到過的全屏游戲那樣。
There is also a member of the WINDOWCLASSEX struct that we need to take out. This leaves the background color untouched, which means it won't be visible as window for a second or two before the game starts (important to making your game look professional).
還有一個WINDOWCLASSEX結構的成員,我們需要把它拿掉。它的作用是設置窗口背景色不,而這會使游戲全屏時屏幕閃爍幾下(把它拿掉會讓您的游戲看起來專業得多)。
1
wc.hbrBackground = NULL;//(HBRUSH)COLOR_WINDOW;
Next, we have to tell DirectX about our new screen resolution. We do this by making a few changes to the d3dpp struct we built in the last lesson. Let's look at what they are before we see what they do.
下一步,我們來告訴DirectX一個新的屏幕分辨率。我們通過對上一課中創建的d3dpp結構體進行一些小小的改變來完成這項工作。下面讓我們看看具體代碼吧
1
D3DPRESENT_PARAMETERS d3dpp; // create a struct to hold various device information
2
3
ZeroMemory(&d3dpp, sizeof(d3dpp)); // clear out the struct for use
4
d3dpp.Windowed = FALSE; // program fullscreen, not windowed
5
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; // discard old frames
6
d3dpp.hDeviceWindow = hWnd; // set the window to be used by Direct3D
7
d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8; // set the back buffer format to 32-bit
8
d3dpp.BackBufferWidth = SCREEN_WIDTH; // set the width of the buffer
9
d3dpp.BackBufferHeight = SCREEN_HEIGHT; // set the height of the buffer
10
Let's examine these new back buffer related variables.
下面我來解釋一下這些新的后臺緩沖相關變量
d3dpp.BackBufferFormat
This member is used to tell Direct3D what kind of pixels should be displayed. There are six types that can be used here, but two of them are older types (16-bit) and not generally used anymore. There are several 32-bit types that we can use. We'll use the D3DFMT_X8R8G8B8. See the table for a description along with some other values than can be used here (definitely not all of them).
這個成員變量是用來告訴Direct3D什么樣的像素會被顯示。可分為六種 ,但其中兩大類型太老了(16位),一般不到。有4個32位的類型,我們可以使用。本課將使用D3DFMT_X8R8G8B8。想知道具體這個四個類型,可以看看下面的表。
[Table 2.2 - Some Back Buffer Formats]
值
|
描述
|
D3DFMT_A8R8G8B8
|
This is a 32-Bit pixel format, with 256 levels (8 bits) of red, green, blue and alpha (semi-transparency).
這是一個32位的像素格式。有256級(8bit)的紅,綠,藍以及透明通道。
|
D3DFMT_X8R8G8B8
|
This is similar to A8R8G8B8, with the one difference being that it does not support alpha, even though there are 8 bits to represent this.
這個和上一個很類似,唯一的不同就是這個不支持透明
|
D3DFMT_A2R10G10B10
|
This is a 32-Bit pixel format, with only two bits of alpha, but 10 bits (1024 levels) of each red, green and blue.
還是一個32位的像素格式。2bit的透明通道,10bit(1024級)的紅綠藍通道。
|
D3DFMT_A16B16G16R16
|
64-BIT COLOR! If you have the capability to run 64-bit color, I'd recommend playing around with this to see how it works. 64bit的!哦這太棒了。如果你可以運行64位的顏色,我強烈推薦你使用這個 This value has 16 bits for each component (65536 levels compared to the current measly 256!)
紅綠藍加透明各占16bit(65536級)。
|
BackBufferWidth and BackBufferHeight
These two members indicate the width and height of the back buffer. Painfully simple.
這兩個成員變量表明后備緩沖區的寬度和高度。相當簡單
最終成品
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.
所以,其實沒改動多少。讓我們看一看整個代碼,并對比上一課什么是不同的,什么是一樣的。


1
// include the basic windows header files and the Direct3D header file
2
#include <windows.h>
3
#include <windowsx.h>
4
#include <d3d9.h>
5
6
// define the screen resolution
7
#define SCREEN_WIDTH 800
8
#define SCREEN_HEIGHT 600
9
10
// include the Direct3D Library file
11
#pragma comment (lib, "d3d9.lib")
12
13
// global declarations
14
LPDIRECT3D9 d3d; // the pointer to our Direct3D interface
15
LPDIRECT3DDEVICE9 d3ddev; // the pointer to the device class
16
17
// function prototypes
18
void initD3D(HWND hWnd); // sets up and initializes Direct3D
19
void render_frame(void); // renders a single frame
20
void cleanD3D(void); // closes Direct3D and releases memory
21
22
// the WindowProc function prototype
23
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
24
25
26
// the entry point for any Windows program
27
int WINAPI WinMain(HINSTANCE hInstance,
28
HINSTANCE hPrevInstance,
29
LPSTR lpCmdLine,
30
int nCmdShow)
31

{
32
HWND hWnd;
33
WNDCLASSEX wc;
34
35
ZeroMemory(&wc, sizeof(WNDCLASSEX));
36
37
wc.cbSize = sizeof(WNDCLASSEX);
38
wc.style = CS_HREDRAW | CS_VREDRAW;
39
wc.lpfnWndProc = WindowProc;
40
wc.hInstance = hInstance;
41
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
42
wc.hbrBackground =Null;// (HBRUSH)COLOR_WINDOW; // not needed any more
43
wc.lpszClassName = L"WindowClass";
44
45
RegisterClassEx(&wc);
46
47
hWnd = CreateWindowEx(NULL,
48
L"WindowClass",
49
L"Our Direct3D Program",
50
WS_EX_TOPMOST | WS_POPUP, // fullscreen values
51
0, 0, // the starting x and y positions should be 0
52
SCREEN_WIDTH, SCREEN_HEIGHT, // set the window to 640 x 480
53
NULL,
54
NULL,
55
hInstance,
56
NULL);
57
58
ShowWindow(hWnd, nCmdShow);
59
60
// set up and initialize Direct3D
61
initD3D(hWnd);
62
63
// enter the main loop:
64
65
MSG msg;
66
67
while(TRUE)
68
{
69
while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
70
{
71
TranslateMessage(&msg);
72
DispatchMessage(&msg);
73
}
74
75
if(msg.message == WM_QUIT)
76
break;
77
78
render_frame();
79
}
80
81
// clean up DirectX and COM
82
cleanD3D();
83
84
return msg.wParam;
85
}
86
87
88
// this is the main message handler for the program
89
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
90

{
91
switch(message)
92
{
93
case WM_DESTROY:
94
{
95
PostQuitMessage(0);
96
return 0;
97
} break;
98
}
99
100
return DefWindowProc (hWnd, message, wParam, lParam);
101
}
102
103
104
// this function initializes and prepares Direct3D for use
105
void initD3D(HWND hWnd)
106

{
107
d3d = Direct3DCreate9(D3D_SDK_VERSION); // create the Direct3D interface
108
109
D3DPRESENT_PARAMETERS d3dpp; // create a struct to hold various device information
110
111
ZeroMemory(&d3dpp, sizeof(d3dpp)); // clear out the struct for use
112
d3dpp.Windowed = FALSE; // program fullscreen, not windowed
113
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; // discard old frames
114
d3dpp.hDeviceWindow = hWnd; // set the window to be used by Direct3D
115
d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8; // set the back buffer format to 32-bit
116
d3dpp.BackBufferWidth = SCREEN_WIDTH; // set the width of the buffer
117
d3dpp.BackBufferHeight = SCREEN_HEIGHT; // set the height of the buffer
118
119
120
// create a device class using this information and the info from the d3dpp stuct
121
d3d->CreateDevice(D3DADAPTER_DEFAULT,
122
D3DDEVTYPE_HAL,
123
hWnd,
124
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
125
&d3dpp,
126
&d3ddev);
127
}
128
129
130
// this is the function used to render a single frame
131
void render_frame(void)
132

{
133
// clear the window to a deep blue
134
d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 40, 100), 1.0f, 0);
135
136
d3ddev->BeginScene(); // begins the 3D scene
137
138
// do 3D rendering on the back buffer here
139
140
d3ddev->EndScene(); // ends the 3D scene
141
142
d3ddev->Present(NULL, NULL, NULL, NULL); // displays the created frame on the screen
143
}
144
145
146
// this is the function that cleans up Direct3D and COM
147
void cleanD3D(void)
148

{
149
d3ddev->Release(); // close and release the 3D device
150
d3d->Release(); // close and release Direct3D
151
}
152
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 the middle.
我沒有做截圖,因為它只是一個藍色的長方形。你的程序應該也看起來像他一樣:藍色的長方形充鼠標指針在中間。
總結
Great! We now have a simple platform on which to build games.
Before you go on, I recommend doing the following exercise to gain familiarity with the code in this program:
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!
太棒了!我們現在有一個簡單的平臺做游戲。
在你接著干之前,我建議做做這幾個練習一遍更好地熟悉Direct3D:
改變程序的分辨率,直到你熟悉各種各樣可選擇的分辨率。
當你準備好了,我們開始下一個教程。
Next Lesson: An Overview of the Third Dimension
下一課:三維的概述
GO! GO! GO!
posted on 2010-12-15 23:54
叫我老王吧 閱讀(2373)
評論(0) 編輯 收藏 引用 所屬分類:
DierectX 、
C++