示例一:Game Engine Part 1

更加完善游戏的基本流程

Graphics添加了以下几个方法,beginScene和endScene提高绘图,showBackbuffer去掉了clear方法

    // Reset the graphics device.
HRESULT reset(); // get functions
// Return direct3d.
LP_3D get3D() { return direct3d; } // Return device3d.
LP_3DDEVICE get3Ddevice() { return device3d; } // Return handle to device context (window).
HDC getDC() { return GetDC(hwnd); } // Test for lost device
HRESULT getDeviceState(); //=============================================================================
// Inline functions for speed. How much more speed? It depends on the game and
// computer. Improvements of 3 or 4 percent have been observed.
//============================================================================= // Set color used to clear screen
void setBackColor(COLOR_ARGB c) {backColor = c;} //=============================================================================
// Clear backbuffer and BeginScene()
//=============================================================================
HRESULT beginScene()
{
result = E_FAIL;
if(device3d == NULL)
return result;
// clear backbuffer to backColor
device3d->Clear(0, NULL, D3DCLEAR_TARGET, backColor, 1.0F, 0);
result = device3d->BeginScene(); // begin scene for drawing
return result;
} //=============================================================================
// EndScene()
//=============================================================================
HRESULT endScene()
{
result = E_FAIL;
if(device3d)
result = device3d->EndScene();
return result;
}

IDirect3DDevice9::TestCooperativeLevel。因此在设备丢失之后,你应该停止整个游戏循环,而通过反复调用

IDirect3DDevice9::TestCooperativeLevel判断设备是否可用。

//=============================================================================
// Test for lost device
//=============================================================================
HRESULT Graphics::getDeviceState()
{
result = E_FAIL; // default to fail, replace on success
if (device3d == NULL)
return result;
result = device3d->TestCooperativeLevel();
return result;
}

Game类

现在Graphics类属于Game类包装

Game类主要流程:

1.初始化

//=============================================================================
// Initializes the game
// throws GameError on error
//=============================================================================
void Game::initialize(HWND hw)
{
hwnd = hw; // save window handle // initialize graphics
graphics = new Graphics();
// throws GameError
graphics->initialize(hwnd, GAME_WIDTH, GAME_HEIGHT, FULLSCREEN); // initialize input, do not capture mouse
input->initialize(hwnd, false); // throws GameError // attempt to set up high resolution timer
if(QueryPerformanceFrequency(&timerFreq) == false)
throw(GameError(gameErrorNS::FATAL_ERROR, "Error initializing high resolution timer")); QueryPerformanceCounter(&timeStart); // get starting time initialized = true;
}

2.messageHandler方法处理消息流程,由Input类接入

3.renderGame

属于Game呈现的主流程,子类重写render方法

//=============================================================================
// Render game items
//=============================================================================
void Game::renderGame()
{
//start rendering
if (SUCCEEDED(graphics->beginScene()))
{
// render is a pure virtual function that must be provided in the
// inheriting class.
render(); // call render in derived class //stop rendering
graphics->endScene();
}
handleLostGraphicsDevice(); //display the back buffer on the screen
graphics->showBackbuffer();
}

4.子类Spacewar继承自Game

// Programming 2D Games
// Copyright (c) 2011 by:
// Charles Kelly
// Game Engine Part 1
// Chapter 4 spacewar.cpp v1.0
// Spacewar is the class we create. #include "spaceWar.h" //=============================================================================
// Constructor
//=============================================================================
Spacewar::Spacewar()
{} //=============================================================================
// Destructor
//=============================================================================
Spacewar::~Spacewar()
{
releaseAll(); // call onLostDevice() for every graphics item
} //=============================================================================
// Initializes the game
// Throws GameError on error
//=============================================================================
void Spacewar::initialize(HWND hwnd)
{
Game::initialize(hwnd); // throws GameError return;
} //=============================================================================
// Update all game items
//=============================================================================
void Spacewar::update()
{} //=============================================================================
// Artificial Intelligence
//=============================================================================
void Spacewar::ai()
{} //=============================================================================
// Handle collisions
//=============================================================================
void Spacewar::collisions()
{} //=============================================================================
// Render game items
//=============================================================================
void Spacewar::render()
{} //=============================================================================
// The graphics device was lost.
// Release all reserved video memory so graphics device may be reset.
//=============================================================================
void Spacewar::releaseAll()
{
Game::releaseAll();
return;
} //=============================================================================
// The grahics device has been reset.
// Recreate all surfaces.
//=============================================================================
void Spacewar::resetAll()
{
Game::resetAll();
return;
}

以上是一个基本游戏的一个主流程

// Game pointer
Spacewar *game = NULL;
HWND hwnd = NULL; //=============================================================================
// Starting point for a Windows application
//=============================================================================
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
// Check for memory leak if debug build
#if defined(DEBUG) | defined(_DEBUG)
_CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
#endif MSG msg; // Create the game, sets up message handler
game = new Spacewar; // Create the window
if (!CreateMainWindow(hwnd, hInstance, nCmdShow))
return 1; try{
game->initialize(hwnd); // throws GameError // main message loop
int done = 0;
while (!done)
{
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
game->run(hwnd); // run the game loop
}
SAFE_DELETE (game); // free memory before exit
return msg.wParam;
}
catch(const GameError &err)
{
game->deleteAll();
DestroyWindow(hwnd);
MessageBox(NULL, err.getMessage(), "Error", MB_OK);
}
catch(...)
{
game->deleteAll();
DestroyWindow(hwnd);
MessageBox(NULL, "Unknown error occured in game.", "Error", MB_OK);
} SAFE_DELETE (game); // free memory before exit
return 0;
} //=============================================================================
// window event callback function
//=============================================================================
LRESULT WINAPI WinProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
return (game->messageHandler(hwnd, msg, wParam, lParam));
}

Programming 2D Games 读书笔记(第四章)的更多相关文章

  1. Programming 2D Games 读书笔记(第五章)

      http://www.programming2dgames.com/chapter5.htm 示例一:Planet 真正示例的开始,首先是载入2张图片 1.Graphics添加了2个方法 load ...

  2. Programming 2D Games 读书笔记(第六章)

      http://www.programming2dgames.com/chapter6.htm 示例一:Bounce 边界碰撞测试 velocity为移动的速度, 超过右边界,velocity.x为 ...

  3. Programming 2D Games 读书笔记(第三章)

      示例一:DirectX Window Graphics类用于初始化Direct 3D 主流程: 仅需要粗体部分 try{ // Create Graphics object graphics = ...

  4. Programming 2D Games 读书笔记(第二章)

      本意还是想了解DirectX的,由于网上拿不到书的pdf文档,幸好有作者的源代码示例,想完整的看一下,基本的游戏需要的点. 下面直接以代码为例,仅用于帮助自身理解 http://www.progr ...

  5. 《Linux内核设计与实现》第八周读书笔记——第四章 进程调度

    <Linux内核设计与实现>第八周读书笔记——第四章 进程调度 第4章 进程调度35 调度程序负责决定将哪个进程投入运行,何时运行以及运行多长时间,进程调度程序可看做在可运行态进程之间分配 ...

  6. 《Linux内核设计与实现》 第八周读书笔记 第四章 进程调度

    20135307 张嘉琪 第八周读书笔记 第四章 进程调度 调度程序负责决定将哪个进程投入运行,何时运行以及运行多长时间,进程调度程序可看做在可运行态进程之间分配有限的处理器时间资源的内核子系统.只有 ...

  7. 《Linux内核分析》读书笔记(四章)

    <Linux内核分析>读书笔记(四章) 标签(空格分隔): 20135328陈都 第四章 进程调度 调度程序负责决定将哪个进程投入运行,何时运行以及运行多长时间,进程调度程序可看做在可运行 ...

  8. 4 Visual Effects 视觉效果 读书笔记 第四章

    4   Visual Effects    视觉效果        读书笔记 第四章 Well, circles and ovals are good, but how about drawing r ...

  9. 《Linux内核设计与实现》读书笔记 第四章 进程调度

    第四章进程调度 进程调度程序可看做在可运行太进程之间分配有限的处理器时间资源的内核子系统.调度程序是多任务操作系统的基础.通过调度程序的合理调度,系统资源才能最大限度地发挥作用,多进程才会有并发执行的 ...

随机推荐

  1. CentOS7 关闭防火墙和selinux

    本文将简单介绍在CentOS7上如何临时和永久关闭防火墙和selinux. 关闭防火墙 # 查看防火墙状态 [root@localhost ~]# systemctl status firewalld ...

  2. Anaconda+django写出第一个web app(八)

    今天来实现网站的登入和登出功能. 首先我们需要在urls.py中添加路径,注意此处的路径和在导航栏中设置的文字路径保持一致: from django.urls import path from . i ...

  3. python爬虫:抓取下载电影文件,合并ts文件为完整视频

    目标网站:https://www.88ys.cc/vod-play-id-58547-src-1-num-1.html 反贪风暴4 对电影进行分析 我们发现,电影是按片段一点点加载出来的,我们分别抓取 ...

  4. 转:VMWare服务器虚拟化--转自CSDN

    http://blog.csdn.net/kkfloat/article/category/1249845/3

  5. curl wget 不验证证书进行https请求【转】

    $ wget 'https://x.x.x.x/get_ips' --no-check-certificate $ curl 'https://x.x.x.x/get_ips' -k 转自 curl ...

  6. 【论文阅读】Learning Spatial Regularization with Image-level Supervisions for Multi-label Image Classification

    转载请注明出处:https://www.cnblogs.com/White-xzx/ 原文地址:https://arxiv.org/abs/1702.05891 Caffe-code:https:// ...

  7. app后端设计-- 数据库分表

    当项目上线后,随着用户的增长,有些数据表的规模会以几何级增长,当数据达到一定规模的时候(例如100万条),查询,读取性能就下降得很厉害,这时,我们就要考虑分表. 更新表数据时会导致索引更新,当单表数据 ...

  8. CVE-2010-0249 IE8 UAF漏洞分析

    CVE-2010-0249 [CNNVD]Microsoft Internet Explorer非法事件操作内存破坏漏洞(CNNVD-201001-153) Microsoft Internet Ex ...

  9. 6. 缓存 - 《APS.NET本质论》

    CaChe是ASP.NET中唯一可以根据服务器使用情况,动态管理内存使用的状态管理方案.我们通过每个缓存数据的键值字符串来区分缓存的数据. 简单案例来说.将数据从数据库/文件取出放在服务器内存中,后来 ...

  10. 【LOJ】#2888. 「APIO2015」巴邻旁之桥 Palembang Bridges

    题解 发现我们选择一座桥会选择力\(\frac{s + t}{2}\)较近的一座桥 然后我们只需要按照\(s + t\)排序,然后枚举断点,左边取所有s和t的中位数,右边同理 动态求中位数用平衡树维护 ...