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):
接下来,来到声明窗口宽度和高度的地方。本教程到目前为止,只有一个声明的地方,注意粗体那行:
1hWnd = CreateWindowEx(NULL,
2 L"WindowClass",
3 L"Our Direct3D Program",
4 WS_OVERLAPPEDWINDOW,
5 300, 300,
6 SCREEN_WIDTH, SCREEN_HEIGHT, // 设置新的尺寸
7NULL,
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()的参数。改动如下。
1hWnd = 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结构体进行一些小小的改变来完成这项工作。下面让我们看看具体代码吧
1D3DPRESENT_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
14LPDIRECT3D9 d3d; // the pointer to our Direct3D interface
15LPDIRECT3DDEVICE9 d3ddev; // the pointer to the device class
16
17// function prototypes
18void initD3D(HWND hWnd); // sets up and initializes Direct3D
19void render_frame(void); // renders a single frame
20void cleanD3D(void); // closes Direct3D and releases memory
21
22// the WindowProc function prototype
23LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
24
25
26// the entry point for any Windows program
27int 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
89LRESULT 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
105void 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
131void 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
147void 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
叫我老王吧 阅读(2357)
评论(0) 编辑 收藏 引用 所属分类:
DierectX 、
C++