Programming 2D Games 读书笔记(第三章)
示例一:DirectX Window
Graphics类用于初始化Direct 3D

主流程:
仅需要粗体部分
try{
// Create Graphics object
graphics = new Graphics;
// Initialize Graphics, throws GameError
graphics->initialize(hwnd, GAME_WIDTH, GAME_HEIGHT, FULLSCREEN);
// main message loop
int done = 0;
while (!done)
{
// PeekMessage,non-blocking method for checking for Windows messages.
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
// look for quit message
if (msg.message == WM_QUIT)
done = 1;
// decode and pass messages on to WinProc
TranslateMessage(&msg);
DispatchMessage(&msg);
} else
graphics->showBackbuffer();
}
SAFE_DELETE(graphics); // free memory before exit
return msg.wParam;
}
catch(const GameError &err)
{
MessageBox(NULL, err.getMessage(), "Error", MB_OK);
}
- Direct3DCreate9创建IDirect3D9
- GetDeviceCaps测试是否支持硬件顶点支持
- initD3Dpp初始化D3DPRESENT_PARAMETERS参数
//=============================================================================
// Initialize D3D presentation parameters
//=============================================================================
void Graphics::initD3Dpp()
{
try{
ZeroMemory(&d3dpp, sizeof(d3dpp)); // fill the structure with 0
// fill in the parameters we need
d3dpp.BackBufferWidth = width;
d3dpp.BackBufferHeight = height;
if(fullscreen) // if fullscreen
d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8; // 24 bit color
else
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN; // use desktop setting
d3dpp.BackBufferCount = 1;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.hDeviceWindow = hwnd;
d3dpp.Windowed = (!fullscreen);
d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
} catch(...)
{
throw(GameError(gameErrorNS::FATAL_ERROR,
"Error initializing D3D presentation parameters")); }
} - CreateDevice
- showBackbuffer
//=============================================================================
// Display the backbuffer
//=============================================================================
HRESULT Graphics::showBackbuffer()
{
result = E_FAIL; // default to fail, replace on success
// (this function will be moved in later versions)
// Clear the backbuffer to lime green
device3d->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,255,0), 0.0f, 0); // Display backbuffer to screen
result = device3d->Present(NULL, NULL, NULL, NULL); return result;
}
//=============================================================================
// Initialize DirectX graphics
// throws GameError on error
//=============================================================================
void Graphics::initialize(HWND hw, int w, int h, bool full)
{
hwnd = hw;
width = w;
height = h;
fullscreen = full; //initialize Direct3D
direct3d = Direct3DCreate9(D3D_SDK_VERSION);
if (direct3d == NULL)
throw(GameError(gameErrorNS::FATAL_ERROR, "Error initializing Direct3D")); initD3Dpp(); // init D3D presentation parameters // determine if graphics card supports harware texturing and lighting and vertex shaders
D3DCAPS9 caps;
DWORD behavior;
result = direct3d->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &caps);
// If device doesn't support HW T&L or doesn't support 1.1 vertex
// shaders in hardware, then switch to software vertex processing.
if( (caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT) == 0 ||
caps.VertexShaderVersion < D3DVS_VERSION(1,1) )
behavior = D3DCREATE_SOFTWARE_VERTEXPROCESSING; // use software only processing
else
behavior = D3DCREATE_HARDWARE_VERTEXPROCESSING; // use hardware only processing //create Direct3D device
result = direct3d->CreateDevice(
D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
hwnd,
behavior,
&d3dpp,
&device3d); if (FAILED(result))
throw(GameError(gameErrorNS::FATAL_ERROR, "Error creating Direct3D device")); }
示例二:DirectX Full Screen
见initD3Dpp方法的Windowed属性
参考:http://www.cnblogs.com/Clingingboy/archive/2012/07/27/2611651.html
示例三:DirectX Device Capabilities
获取指定显卡支持的显示模式
全屏时检测
- GetAdapterModeCount:Returns the number of display modes available on this adapter.
- EnumAdapterModes:
Queries the device to determine whether the specified adapter supports the requested format and display mode. This method could be used in a loop to enumerate all the available adapter modes.
bool Graphics::isAdapterCompatible()
{
UINT modes = direct3d->GetAdapterModeCount(D3DADAPTER_DEFAULT,
d3dpp.BackBufferFormat);
for(UINT i=0; i<modes; i++)
{
result = direct3d->EnumAdapterModes( D3DADAPTER_DEFAULT,
d3dpp.BackBufferFormat,
i, &pMode);
if( pMode.Height == d3dpp.BackBufferHeight &&
pMode.Width == d3dpp.BackBufferWidth &&
pMode.RefreshRate >= d3dpp.FullScreen_RefreshRateInHz)
return true;
}
return false;
}
Programming 2D Games 读书笔记(第三章)的更多相关文章
- Programming 2D Games 读书笔记(第四章)
示例一:Game Engine Part 1 更加完善游戏的基本流程 Graphics添加了以下几个方法,beginScene和endScene提高绘图,showBackbuffer去掉了clea ...
- Programming 2D Games 读书笔记(第五章)
http://www.programming2dgames.com/chapter5.htm 示例一:Planet 真正示例的开始,首先是载入2张图片 1.Graphics添加了2个方法 load ...
- Programming 2D Games 读书笔记(第六章)
http://www.programming2dgames.com/chapter6.htm 示例一:Bounce 边界碰撞测试 velocity为移动的速度, 超过右边界,velocity.x为 ...
- Programming 2D Games 读书笔记(第二章)
本意还是想了解DirectX的,由于网上拿不到书的pdf文档,幸好有作者的源代码示例,想完整的看一下,基本的游戏需要的点. 下面直接以代码为例,仅用于帮助自身理解 http://www.progr ...
- 《Linux内核设计与分析》第六周读书笔记——第三章
<Linux内核设计与实现>第六周读书笔记——第三章 20135301张忻估算学习时间:共2.5小时读书:2.0代码:0作业:0博客:0.5实际学习时间:共3.0小时读书:2.0代码:0作 ...
- 《Linux内核设计与实现》读书笔记 第三章 进程管理
第三章进程管理 进程是Unix操作系统抽象概念中最基本的一种.我们拥有操作系统就是为了运行用户程序,因此,进程管理就是所有操作系统的心脏所在. 3.1进程 概念: 进程:处于执行期的程序.但不仅局限于 ...
- 《CSS3实战》读书笔记 第三章:选择器:样式实现的标记
第三章:选择器:样式实现的标记 选择器的魔力在于,让你完全实现对网页样式的掌控.不同的选择器可以用在不同的情况下使用.总之把握的原则是:规范的编码,根据合理地使用选择器,比去背选择器的定义有价值的多. ...
- 《linux内核设计与实现》读书笔记第三章
第3章 进程管理 3.1 进程 1.进程 进程就是处于执行期的程序. 进程包括: 可执行程序代码 打开的文件 挂起的信号 内核内部数据 处理器状态 一个或多个具有内存映射的内存地址空间 一个或多个执行 ...
- 《R语言实战》读书笔记--第三章 图形初阶(二)
3.4添加文本.自定义坐标轴和图例 很多作图函数可以设置坐标轴和文本标注.比如标题.副标题.坐标轴标签.坐标轴范围等.需要注意的是并不是所有的绘图函数都有上述的参数,需要进行验证.可以将一些默认的参数 ...
随机推荐
- Nessus扫描策略
本篇将简单介绍下Nessus的扫描策略设置.选用plugins及如何使用定制的策略来进行扫描任务. Step 1: 启动Nessus服务 root@kali:~# /etc/init.d/nessus ...
- 数链剖分(树的统计Count )
题目链接:https://cn.vjudge.net/contest/279350#problem/C 具体思路:单点更新,区间查询,查询的时候有两种操作,查询区间最大值和区间和. 注意点:在查询的时 ...
- 让浏览器重新下载css文件,解决不刷新缓存的问题
网站页面源代码中的css文件和js文件后面带一个问号,后面跟着一连串数字或字符,问号起不到实际作用,仅能当作后缀,如果用问号加参数的方法,可以添加版本号等信息 它的作用有:1.作为版本号,让自己方便记 ...
- 使用 scm-manager 搭建 git/svn 代码管理仓库(二)
主要介绍scm的配置. 1.配置为在Windows服务中启动scm-manager的启动方式有多种,可以在DOS(即命令行CMD模式)中启动,也可以在Windows服务中启动. 下面我们采用Windo ...
- 【ARTS】01_05_左耳听风-20181210~1216
ARTS: Algrothm: leetcode算法题目 Review: 阅读并且点评一篇英文技术文章 Tip/Techni: 学习一个技术技巧 Share: 分享一篇有观点和思考的技术文章 Algo ...
- Longest Words
Given a dictionary, find all of the longest words in the dictionary. Example Given { "dog" ...
- 高可用的并行MySQL数据同步及分布式
首先聊聊MySQL的数据分布式,目前最为常用的就是Replication(复制)技术.基于此技术外延开来有很多中架构,分类归结为如下: 1.树状结构(Master,Backup-Master ...
- Entity Framework 6.1.0 Tools for Visual Studio 2012 & 2013
http://www.microsoft.com/en-us/download/confirmation.aspx?id=40762
- Go语言Windows 10开发环境搭建:Eclipse+GoClipse
Intel Core i5-8250U,Windows 10家庭中文版,go version go1.11 windows/amd64, Eclipse IDE for C/C++ Developer ...
- Python_oldboy_自动化运维之路_面向对象2(十)
本节内容: 面向对象程序设计的由来 什么是面向对象的程序设计及为什么要有它 类和对象 继承与派生 多的态与多态性 封装 静态方法和类方法 面向对象的软件开发 反射 类的特殊成员方法 异常处理 1.面向 ...