2D游戏引擎制作:添加游戏类并修改XML读取类
2018-03-11 21:02
537 查看
添加游戏类并修改XML读取类
前言
两篇连在一起感觉没什么话可以说了。正文
这个游戏类,可以代表整个游戏,从游戏生成到结束,都由它控制,内容应该会比较多,笔者一点一点添加功能,第一步就是把前文生成游戏窗体的部分添加到游戏类里。定义如下:
class LLGame { public: LLGame(); ~LLGame(); //初始化 void Init(); //游戏启动 void Start(); private: //初始化窗体 void InitWindow(); //初始化数据 void InitData(); //加载配置 void LoadConfig(); //保存配置 void SaveConfig(); wstring currentPath; GameConfig gameConfig; LLGameWindow* gameWindow; };
具体实现,实现代码里使用了一些帮助类,帮助类的代码在更下面:
#include "LLGame.h" LLGame::LLGame() { } LLGame::~LLGame() { if (gameWindow) { delete gameWindow; gameWindow = nullptr; } } void LLGame::Init() { currentPath = SystemHelper::GetCurrentPath(); LoadConfig(); InitWindow(); } void LLGame::Start() { if (gameWindow) { gameWindow->Run(); } } void LLGame::LoadConfig() { LLXMLDocument xmlDocument; if(xmlDocument.LoadXMLFromFile(currentPath + L"\\" + L"Game.xml")) { LLXMLNode* rootNode=xmlDocument.GetRootNode(); gameConfig.gameName = rootNode->GetProperty(L"gameName")->GetValue(); gameConfig.width = rootNode->GetProperty(L"width")->GetValueInt(); gameConfig.height = rootNode->GetProperty(L"height")->GetValueInt(); gameConfig.fullScreen = rootNode->GetProperty(L"fullScreen")->GetValueBool(); gameConfig.canMultiGame = rootNode->GetProperty(L"canMultiGame")->GetValueBool(); } else { MessageHelper::ShowMessage(L"配置文件加载失败!"); } } void LLGame::SaveConfig() { LLXMLDocument xmlDocument; LLXMLNode* node = new LLXMLNode(L"Game"); node->AddProperty(new LLXMLProperty(L"gameName", gameConfig.gameName)); node->AddProperty(new LLXMLProperty(L"width", to_wstring(gameConfig.width))); node->AddProperty(new LLXMLProperty(L"height", to_wstring(gameConfig.height))); node->AddProperty(new LLXMLProperty(L"fullScreen", to_wstring(gameConfig.fullScreen))); node->AddProperty(new LLXMLProperty(L"canMultiGame", to_wstring(gameConfig.canMultiGame))) a962 ; xmlDocument.SetRootNode(node); if (!xmlDocument.SaveXMLToFile(currentPath + L"\\" + L"Game.xml")) { MessageHelper::ShowMessage(L"配置文件保存失败!"); } } void LLGame::InitWindow() { if (!gameConfig.canMultiGame) { HANDLE hMutex = CreateMutex(NULL, TRUE, gameConfig.gameName.c_str()); if (GetLastError() == ERROR_ALREADY_EXISTS) { HWND hWnd = FindWindow(LLGameWindow::className.c_str(), gameConfig.gameName.c_str()); SetForegroundWindow(hWnd); if (hMutex) { ReleaseMutex(hMutex); } return; } } gameWindow = new LLGameWindow(); int screenWidth = SystemHelper::GetScreenWidth(); int screenHeight = SystemHelper::GetScreenHeight(); if (gameConfig.fullScreen) { gameWindow->SetPosition(0, 0); gameWindow->SetSize(screenWidth, screenHeight); gameWindow->SetTitle(gameConfig.gameName); } else { gameWindow->SetPosition((screenWidth - gameConfig.width) / 2, (screenHeight - gameConfig.height) / 2); gameWindow->SetSize(gameConfig.width, gameConfig.height); gameWindow->SetTitle(gameConfig.gameName); } } void LLGame::InitData() { }
帮助类:
#include <windows.h> #include <string> using namespace std; class MessageHelper { public: //弹出提示框。 static void ShowMessage(wstring message) { MessageBox(NULL, message.c_str(), L"", NULL); } }; class SystemHelper { public: //获得屏幕宽度。 static int GetScreenWidth() { return GetSystemMetrics(SM_CXSCREEN); } //获得屏幕高度。 static int GetScreenHeight() { return GetSystemMetrics(SM_CYSCREEN); } //获得当前路径,是指.exe文件所在的目录。 static wstring GetCurrentPath() { wchar_t currentPath[MAX_PATH + 1] = { 0 }; GetModuleFileName(NULL, currentPath, MAX_PATH); (wcsrchr(currentPath, L'\\'))[0] = 0; return currentPath; } };
现在在main方法中删掉其他添加:
LLGame llGame; llGame.Init(); llGame.Start();
同时保证Game.xml文件同exe文件在同一目录下,就可以有配置文件控制窗体大小,标题等信息。
Game.xml文件内容如下:
<Game width='1000' height='500' fullScreen='false' gameName='helloworld' canMultiGame="false"/>
这样就可以生成宽度为1000,高度为500,标题为helloworld的唯一运行窗体了。
还有一点改动,LLGameWindow类中生成窗体所需的hInstance改为由方法GetModuleHandle(0)获得,不再由main方法层层传递了。
在LLGameWindow类InitWindow里将设置窗体类名(之前是以字符串形式写在程序里)改为wc.lpszClassName = className.c_str();CreateWindow方法第一个参数也改成className.c_str()。
另外添加窗体最大化和最小化方法(现在还用不到):
void SetMaximize(){ShowWindow(hWnd, SW_MAXIMIZE);} void SetMinimize(){ShowWindow(hWnd, SW_MINIMIZE);}
再对LLXML类进行修改:
添加XML属性的新构造函数:
LLXMLProperty::LLXMLProperty(wstring name, wstring value) { this->name = name; this->value = value; }
添加XML节点类的析构函数,节点析构时将属性的子节点一起删除:
LLXMLNode::~LLXMLNode() { for (auto propertyPair : propertyMap) { delete propertyPair.second; } for (auto node : childNodeList) { delete node; } }
XML文档类析构,加载节点时将根节点删除。
LLXMLDocument::~LLXMLDocument() { delete rootNode; } LLXMLDocument::LoadXMLFromFile(wstring filePath, FileEncode fileEncode) { if (rootNode) { delete rootNode; rootNode = nullptr; } }
添加设置根节点方法:
void LLXMLDocument::SetRootNode(LLXMLNode* rootNode) { if (this->rootNode) { delete this->rootNode; this->rootNode = nullptr; } this->rootNode = rootNode; }
结束语
今天想说的都在上篇说完了,,对了多问一个问题,专业类图书和兴趣类图书区别在哪,小说算兴趣类吗,某个国家语言教程算兴趣类吗?
相关文章推荐
- Unity2d 5.1 弱联网游戏制作 (一)场景创建、添加组件、精灵移动、主摄像机跟随
- 2D游戏引擎制作:窗体封装和唯一窗体运行
- 【Unity 3D 游戏引擎】使用 2DToolkit 插件 制作2D精灵动画
- 2D游戏引擎(九)——添加背景支持
- 2d游戏引擎(二)——向引擎添加位图类
- 2D游戏引擎制作:读取XML文件 3
- 2D游戏引擎制作:前言
- 2D游戏引擎制作:读取XML文件 1
- 2D游戏引擎(六)——添加子画面管理器
- 《Genesis-3D开源游戏引擎--横版格斗游戏制作教程01: 资源导入》
- 2D游戏引擎Allegro 系列教程 新的框架
- Nokia发布Symbian平台上C++语言的2D游戏引擎(含源代码)
- 国外 2D 游戏引擎相关站点
- Android游戏引擎《Rokon》学习笔记一:为你的项目添加Rokon框架
- 【Cocos2D-X 游戏引擎】初窥门径(1) 制作一个动态的精灵
- 使用C# + Managed DIrectX 制作 2D 游戏使用C# + Managed DIrectX 制作 2D 游戏
- 非典型2D游戏引擎 Orx 源码阅读笔记(4) 用C实现的基本容器(List,HashTable,Tree)
- Java制作桌面弹球下载版 使用如鹏游戏引擎制作 包含2个精灵球同时弹动
- Strive 2D游戏引擎 -- 序
- OPhone平台2D游戏引擎实现——概述及框架(一)