示例一: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. js async await 终极异步解决方案

    既然有了promise 为什么还要有async await ? 当然是promise 也不是完美的异步解决方案,而 async await 的写法看起来更加简单且容易理解. 回顾 Promise Pr ...

  2. PHP回调函数及匿名函数概念与用法详解

    1.回调函数 PHP的回调函数其实和C.Java等语言的回调函数的作用是一模一样的,都是在主线程执行的过程中,突然跳去执行设置的回调函数: 回调函数执行完毕之后,再回到主线程处理接下来的流程 而在ph ...

  3. .NetCore下使用Prometheus实现系统监控和警报 (一)介绍【译】

    [译]原文https://prometheus.io/docs/introduction/overview 什么是Prometheus? Prometheus是一个开源系统监控和警报工具包,最初起源于 ...

  4. json多态序列化

    https://blog.csdn.net/java_huashan/article/details/46428971 https://blog.csdn.net/bruce128/article/d ...

  5. 【LOJ】#2513. 「BJOI2018」治疗之雨

    题解 具体就是列一个未知数方程\(dp[i]\)表示有\(i\)滴血的时候期望多少轮 \(dp[i] = 1 + \sum_{j = 1}^{i + 1} a_{i,j}dp[j]\) \(dp[n] ...

  6. 【BZOJ】4560: [JLoi2016]字符串覆盖

    题解 先用kmp求出来一个ed[i][j]表示在母串的第i位是第j个子串的结尾 考虑状压一个二进制位表示这个子串覆盖过没有 对于最大值,记一个dp[S][i]表示子串的使用状况为S,当前为母串的第i位 ...

  7. CDM中添加Hive服务时Gateway是什么?

    参考这里http://grokbase.com/t/cloudera/scm-users/12aayq5cyh/what-is-gateway-in-cloudera-manager 实际上Gatew ...

  8. 【Java】 大话数据结构(6) 栈的顺序与链式存储

    本文根据<大话数据结构>一书,实现了Java版的栈的顺序存储结构.两栈共享空间.栈的链式存储机构. 栈:限定仅在表尾进行插入和删除操作的线性表. 栈的插入(进栈)和删除(出栈)操作如下图所 ...

  9. CentOS和Windows下配置MySQL远程访问的教程

    CentOS和Windows下配置MySQL远程访问的教程   一.前言 由于实验在云服务器上跑的结果不是很理想.所以,现在切换到局域网服务器.因此,需要重新配置 Windows 服务器和 CentO ...

  10. 洛谷P4742 [Wind Festival]Running In The Sky [Tarjan缩点,DAGDP]

    题目传送门 Running In The Sky 格式难调,题面就不放了. 分析: 一句话题意:给定一张带点权的有向图,求最长点权路径及该路径上的最大点权. 很明显的$DAGDP$,因此需要缩点,将该 ...