新增Config配置类

This commit is contained in:
Nomango 2018-07-04 17:00:21 +08:00
parent 580278e869
commit 2c0f7c5178
24 changed files with 225 additions and 140 deletions

View File

@ -9,13 +9,15 @@ e2d::Game * e2d::Game::_instance = nullptr;
e2d::Game::Game()
: _ended(false)
, _paused(false)
, _initialized(false)
, _config(nullptr)
{
CoInitialize(nullptr);
}
e2d::Game::~Game()
{
GC::release(_config);
CoUninitialize();
}
@ -37,16 +39,10 @@ void e2d::Game::destroyInstance()
void e2d::Game::start(bool cleanup)
{
if (!_initialized)
{
throw Exception(L"开始游戏前未进行初始化");
}
auto gc = GC::getInstance();
auto input = Input::getInstance();
auto window = Window::getInstance();
auto renderer = Renderer::getInstance();
// 初始化场景管理器
SceneManager::__init();
@ -101,7 +97,7 @@ void e2d::Game::pause()
void e2d::Game::resume()
{
if (_initialized && _paused)
if (_paused)
{
Game::reset();
}
@ -110,7 +106,7 @@ void e2d::Game::resume()
void e2d::Game::reset()
{
if (_initialized && !_ended)
if (!_ended)
{
Time::__reset();
ActionManager::__resetAll();
@ -123,6 +119,23 @@ bool e2d::Game::isPaused()
return _paused;
}
void e2d::Game::setConfig(Config * config)
{
GC::release(_config);
_config = config;
GC::retain(_config);
}
e2d::Config * e2d::Game::getConfig()
{
if (!_config)
{
_config = Create<Config>();
GC::retain(_config);
}
return _config;
}
void e2d::Game::quit()
{
_ended = true; // 这个变量将控制游戏是否结束

View File

@ -156,7 +156,7 @@ HWND e2d::Window::__create()
if (SUCCEEDED(hr))
{
// 禁用输入法
this->setTypewritingEnable(false);
this->setTypewritingEnabled(false);
// 禁用控制台关闭按钮
HWND consoleHWnd = ::GetConsoleWindow();
if (consoleHWnd)
@ -336,11 +336,11 @@ void e2d::Window::showConsole(bool show)
}
}
void e2d::Window::setTypewritingEnable(bool enable)
void e2d::Window::setTypewritingEnabled(bool enabled)
{
static HIMC hImc = nullptr;
if (enable)
if (enabled)
{
if (hImc != nullptr)
{

View File

@ -48,7 +48,7 @@ void e2d::CircleCollider::setCircle(Point center, double radius)
void e2d::CircleCollider::_resize()
{
if (_parentNode && _enable)
if (_parentNode && _enabled)
{
double minSide = std::min(_parentNode->getRealWidth(), _parentNode->getRealHeight());
this->setCircle(

View File

@ -7,7 +7,7 @@ e2d::Collider::Collider()
, _color(Color::Red, 0.7)
, _parentNode(nullptr)
, _transformed(nullptr)
, _enable(true)
, _enabled(true)
, _autoResize(false)
{
}
@ -27,9 +27,9 @@ e2d::Color e2d::Collider::getColor() const
return _color;
}
void e2d::Collider::setEnable(bool enable)
void e2d::Collider::setEnabled(bool enabled)
{
_enable = enable;
_enabled = enabled;
}
void e2d::Collider::setVisiable(bool bVisiable)
@ -42,14 +42,14 @@ void e2d::Collider::setColor(Color color)
_color = color;
}
void e2d::Collider::setAutoResize(bool enable)
void e2d::Collider::setAutoResize(bool enabled)
{
_autoResize = enable;
_autoResize = enabled;
}
void e2d::Collider::_render()
{
if (_transformed && _enable)
if (_transformed && _enabled)
{
auto renderer = Renderer::getInstance();
// »ñÈ¡´¿É«»­Ë¢
@ -65,7 +65,7 @@ e2d::Collider::Relation e2d::Collider::getRelationWith(Collider * pCollider) con
{
if (_transformed && pCollider->_transformed)
{
if (_enable && pCollider->_enable)
if (_enabled && pCollider->_enabled)
{
D2D1_GEOMETRY_RELATION relation;
@ -83,7 +83,7 @@ e2d::Collider::Relation e2d::Collider::getRelationWith(Collider * pCollider) con
void e2d::Collider::_transform()
{
if (_parentNode && _enable)
if (_parentNode && _enabled)
{
if (_autoResize)
{

View File

@ -5,7 +5,6 @@
typedef std::pair<UINT, UINT> HashPair;
static std::vector<e2d::Listener*> s_vListeners; // 监听器容器
static bool s_bCollisionEnable = false; // 碰撞触发状态
static e2d::Node * s_pActiveNode = nullptr; // 主动碰撞体
static e2d::Node * s_pPassiveNode = nullptr; // 被动碰撞体
static std::set<HashPair> s_sCollisionList; // 碰撞映射
@ -91,17 +90,6 @@ e2d::Node* e2d::Collision::isCausedBy(const String& name)
return nullptr;
}
void e2d::Collision::setEnable(bool enable)
{
s_bCollisionEnable = enable;
}
bool e2d::Collision::isEnable()
{
return s_bCollisionEnable;
}
void e2d::Collision::__update(Node * active, Node * passive)
{
if (s_vListeners.empty() || Game::getInstance()->isPaused())

View File

@ -48,7 +48,7 @@ void e2d::EllipseCollider::setEllipse(Point center, double radiusX, double radiu
void e2d::EllipseCollider::_resize()
{
if (_parentNode && _enable)
if (_parentNode && _enabled)
{
this->setEllipse(
Point(

View File

@ -40,7 +40,7 @@ void e2d::RectCollider::setRect(double left, double top, double right, double bo
void e2d::RectCollider::_resize()
{
if (_parentNode && _enable)
if (_parentNode && _enabled)
{
this->setRect(0, 0, _parentNode->getRealWidth(), _parentNode->getRealHeight());
}

46
core/Common/Config.cpp Normal file
View File

@ -0,0 +1,46 @@
#include "..\e2dbase.h"
e2d::Config::Config()
: _gameName()
, _nodeDefPivot()
, _collisionEnabled(false)
{
}
e2d::Config::~Config()
{
}
void e2d::Config::setGameName(const String & name)
{
_gameName = name;
}
void e2d::Config::setCollisionEnabled(bool enabled)
{
_collisionEnabled = enabled;
}
void e2d::Config::setNodeDefaultPivot(Point pivot)
{
_nodeDefPivot = Point(
std::min(std::max(pivot.x, 0.0), 1.0),
std::min(std::max(pivot.y, 0.0), 1.0)
);
}
e2d::String e2d::Config::getGameName() const
{
return _gameName;
}
bool e2d::Config::isCollisionEnabled() const
{
return _collisionEnabled;
}
e2d::Point e2d::Config::getNodeDefaultPivot() const
{
return _nodeDefPivot;
}

View File

@ -9,8 +9,8 @@ static std::vector<e2d::Collider*> s_vColliders;
void e2d::ColliderManager::__updateCollider(e2d::Collider * pActiveCollider)
{
// 判断碰撞触发是否打开
if (!Collision::isEnable())
// 判断碰撞监听是否打开
if (!Game::getInstance()->getConfig()->isCollisionEnabled())
return;
Node* pActiveNode = pActiveCollider->_parentNode;

View File

@ -7,7 +7,7 @@
e2d::Button::Button()
: _func(nullptr)
, _state(ButtonState::Normal)
, _enable(true)
, _enabled(true)
, _isSelected(false)
, _normal(nullptr)
, _mouseover(nullptr)
@ -19,7 +19,7 @@ e2d::Button::Button()
e2d::Button::Button(Node * normal, const Function& func)
: _func(nullptr)
, _state(ButtonState::Normal)
, _enable(true)
, _enabled(true)
, _isSelected(false)
, _normal(nullptr)
, _mouseover(nullptr)
@ -33,7 +33,7 @@ e2d::Button::Button(Node * normal, const Function& func)
e2d::Button::Button(Node * normal, Node * selected, const Function& func)
: _func(nullptr)
, _state(ButtonState::Normal)
, _enable(true)
, _enabled(true)
, _isSelected(false)
, _normal(nullptr)
, _mouseover(nullptr)
@ -48,7 +48,7 @@ e2d::Button::Button(Node * normal, Node * selected, const Function& func)
e2d::Button::Button(Node * normal, Node * mouseover, Node * selected, const Function& func)
: _func(nullptr)
, _state(ButtonState::Normal)
, _enable(true)
, _enabled(true)
, _isSelected(false)
, _normal(nullptr)
, _mouseover(nullptr)
@ -64,7 +64,7 @@ e2d::Button::Button(Node * normal, Node * mouseover, Node * selected, const Func
e2d::Button::Button(Node * normal, Node * mouseover, Node * selected, Node * disabled, const Function& func)
: _func(nullptr)
, _state(ButtonState::Normal)
, _enable(true)
, _enabled(true)
, _isSelected(false)
, _normal(nullptr)
, _mouseover(nullptr)
@ -80,7 +80,7 @@ e2d::Button::Button(Node * normal, Node * mouseover, Node * selected, Node * dis
bool e2d::Button::isEnable() const
{
return _enable;
return _enabled;
}
void e2d::Button::setNormal(Node * normal)
@ -161,11 +161,11 @@ void e2d::Button::setDisabled(Node * disabled)
}
}
void e2d::Button::setEnable(bool enable)
void e2d::Button::setEnabled(bool enabled)
{
if (_enable != enable)
if (_enabled != enabled)
{
_enable = enable;
_enabled = enabled;
_updateVisiable();
}
}
@ -183,7 +183,7 @@ void e2d::Button::_fixedUpdate()
auto input = Input::getInstance();
auto window = Window::getInstance();
if (_enable && _visiable && _normal)
if (_enabled && _visiable && _normal)
{
if (input->isRelease(Input::Mouse::Left))
{
@ -226,7 +226,7 @@ void e2d::Button::_fixedUpdate()
_setState(ButtonState::Normal);
}
if (_visiable && !_enable && _normal && _normal->containsPoint(input->getMousePos()))
if (_visiable && !_enabled && _normal && _normal->containsPoint(input->getMousePos()))
{
window->setCursor(Window::Cursor::No);
}
@ -248,7 +248,7 @@ void e2d::Button::_updateVisiable()
SAFE_SET(_selected, setVisiable, false);
SAFE_SET(_disabled, setVisiable, false);
if (_enable)
if (_enabled)
{
if (_state == ButtonState::Selected && _selected)
{

View File

@ -1,12 +1,12 @@
#include "..\e2dnode.h"
e2d::Menu::Menu()
: _enable(true)
: _enabled(true)
{
}
e2d::Menu::Menu(const std::vector<Button*>& buttons)
: _enable(true)
: _enabled(true)
{
for (auto button : buttons)
{
@ -16,7 +16,7 @@ e2d::Menu::Menu(const std::vector<Button*>& buttons)
bool e2d::Menu::isEnable() const
{
return _enable;
return _enabled;
}
size_t e2d::Menu::getButtonCount() const
@ -24,15 +24,15 @@ size_t e2d::Menu::getButtonCount() const
return _buttons.size();
}
void e2d::Menu::setEnable(bool enable)
void e2d::Menu::setEnabled(bool enabled)
{
if (_enable != enable)
if (_enabled != enabled)
{
_enable = enable;
_enabled = enabled;
for (auto button : _buttons)
{
button->setEnable(enable);
button->setEnabled(enabled);
}
}
}
@ -43,7 +43,7 @@ void e2d::Menu::addButton(Button * button)
{
this->addChild(button);
_buttons.push_back(button);
button->setEnable(_enable);
button->setEnabled(_enabled);
}
}
@ -64,7 +64,7 @@ bool e2d::Menu::removeButton(Button * button)
if (_buttons[i] == button)
{
// 移除按钮前,将它启用
button->setEnable(true);
button->setEnabled(true);
_buttons.erase(_buttons.begin() + i);
return true;
}

View File

@ -3,9 +3,6 @@
#include "..\e2daction.h"
#include <algorithm>
// 默认中心点位置
static float s_fDefaultPiovtX = 0;
static float s_fDefaultPiovtY = 0;
static e2d::Collider::Type s_fDefaultColliderType = e2d::Collider::Type::None;
e2d::Node::Node()
@ -19,10 +16,10 @@ e2d::Node::Node()
, _rotation(0)
, _skewAngleX(0)
, _skewAngleY(0)
, _displayOpacity(1.0f)
, _realOpacity(1.0f)
, _pivotX(s_fDefaultPiovtX)
, _pivotY(s_fDefaultPiovtY)
, _displayOpacity(1.f)
, _realOpacity(1.f)
, _pivotX(0.f)
, _pivotY(0.f)
, _initialMatri(D2D1::Matrix3x2F::Identity())
, _finalMatri(D2D1::Matrix3x2F::Identity())
, _visiable(true)
@ -35,6 +32,10 @@ e2d::Node::Node()
, _autoUpdate(true)
, _positionFixed(false)
{
Point defPivot = Game::getInstance()->getConfig()->getNodeDefaultPivot();
_pivotX = float(defPivot.x);
_pivotY = float(defPivot.y);
if (s_fDefaultColliderType != Collider::Type::None)
{
this->setCollider(s_fDefaultColliderType);
@ -911,12 +912,6 @@ void e2d::Node::setAutoUpdate(bool bAutoUpdate)
_autoUpdate = bAutoUpdate;
}
void e2d::Node::setDefaultPiovt(double defaultPiovtX, double defaultPiovtY)
{
s_fDefaultPiovtX = std::min(std::max(float(defaultPiovtX), 0.f), 1.f);
s_fDefaultPiovtY = std::min(std::max(float(defaultPiovtY), 0.f), 1.f);
}
void e2d::Node::setDefaultCollider(Collider::Type type)
{
s_fDefaultColliderType = type;

View File

@ -13,37 +13,6 @@ e2d::String e2d::Path::_dataPath;
std::list<e2d::String> e2d::Path::_paths;
void e2d::Path::setGameFolderName(const String & name)
{
if (name.isEmpty())
return;
// 设置数据的默认保存路径
String localAppDataPath = Path::getLocalAppDataPath();
if (!localAppDataPath.isEmpty())
{
_dataPath = localAppDataPath + L"\\Easy2DGameData\\" << name << L"\\";
if (!Path::exists(_dataPath) && !Path::createFolder(_dataPath))
{
_dataPath = L"";
}
_dataPath << L"Data.ini";
}
// 设置临时文件保存路径
wchar_t path[_MAX_PATH];
if (0 != ::GetTempPath(_MAX_PATH, path))
{
_tempPath << path << L"\\Easy2DGameTemp\\" << name << L"\\";
if (!Path::exists(_tempPath) && !Path::createFolder(_tempPath))
{
_tempPath = L"";
}
}
}
void e2d::Path::addSearchPath(String path)
{
path.replace(L"/", L"\\");
@ -58,8 +27,45 @@ void e2d::Path::addSearchPath(String path)
}
}
e2d::String e2d::Path::getDataPath()
{
if (_dataPath.isEmpty())
{
// 设置数据的保存路径
String localAppDataPath = Path::getLocalAppDataPath();
String gameName = Game::getInstance()->getConfig()->getGameName();
if (!localAppDataPath.isEmpty() && !gameName.isEmpty())
{
_dataPath = localAppDataPath + L"\\Easy2DGameData\\" << gameName << L"\\";
if (!Path::exists(_dataPath) && !Path::createFolder(_dataPath))
{
_dataPath = L"";
}
}
_dataPath << L"Data.ini";
}
return _dataPath;
}
e2d::String e2d::Path::getTempPath()
{
if (_tempPath.isEmpty())
{
// 设置临时文件保存路径
wchar_t path[_MAX_PATH];
String gameName = Game::getInstance()->getConfig()->getGameName();
if (0 != ::GetTempPath(_MAX_PATH, path) && !gameName.isEmpty())
{
_tempPath << path << L"\\Easy2DGameTemp\\" << gameName << L"\\";
if (!Path::exists(_tempPath) && !Path::createFolder(_tempPath))
{
_tempPath = L"";
}
}
}
return _tempPath;
}
@ -144,11 +150,6 @@ e2d::String e2d::Path::extractResource(int resNameId, const String & resType, co
}
}
e2d::String e2d::Path::getDataPath()
{
return _dataPath;
}
e2d::String e2d::Path::getFileExtension(const String& filePath)
{
String fileExtension;

View File

@ -43,6 +43,14 @@ public:
// 游戏是否暂停
bool isPaused();
// ÐÞ¸ÄÓÎÏ·ÅäÖÃ
void setConfig(
Config * config
);
// »ñÈ¡ÓÎÏ·ÅäÖÃ
Config * getConfig();
private:
Game();
@ -53,7 +61,8 @@ private:
private:
bool _ended;
bool _paused;
bool _initialized;
Config* _config;
static Game * _instance;
};
@ -128,8 +137,8 @@ public:
);
// 是否允许响应输入法
void setTypewritingEnable(
bool enable
void setTypewritingEnabled(
bool enabled
);
// 弹出提示窗口

View File

@ -70,14 +70,6 @@ public:
Node * node
);
// 开启或关闭物体碰撞监听功能(默认关闭)
static void setEnable(
bool enable
);
// 是否打开了物体碰撞监听
static bool isEnable();
// 添加碰撞监听
static Listener * addListener(
const Function& func, /* 监听到碰撞时的执行函数 */
@ -175,8 +167,8 @@ public:
Color getColor() const;
// 启用或关闭该碰撞体
virtual void setEnable(
bool enable
virtual void setEnabled(
bool enabled
);
// 设置碰撞体的可见性
@ -191,7 +183,7 @@ public:
// 设置大小跟随
void setAutoResize(
bool enable
bool enabled
);
// 获取 ID2D1Geometry 对象
@ -208,7 +200,7 @@ protected:
virtual void _render();
protected:
bool _enable;
bool _enabled;
bool _visiable;
bool _autoResize;
Color _color;

View File

@ -468,6 +468,46 @@ private:
};
// 游戏配置
class Config :
public Object
{
public:
Config();
virtual ~Config();
// 修改游戏名称
void setGameName(
const String& name
);
// 打开或关闭碰撞监听(默认关闭)
void setCollisionEnabled(
bool enabled
);
// 设置节点的默认中心点位置
void setNodeDefaultPivot(
Point pivot
);
// 获取游戏名称
String getGameName() const;
// 获取碰撞监听状态
bool isCollisionEnabled() const;
// 获取节点的默认中心点位置
Point getNodeDefaultPivot() const;
protected:
String _gameName;
Point _nodeDefPivot;
bool _collisionEnabled;
};
// ͼƬ
class Image :
public Object

View File

@ -385,13 +385,7 @@ public:
// 停止所有动作
virtual void stopAllActions();
// 修改节点的默认中心点位置
static void setDefaultPiovt(
double defaultPiovtX,
double defaultPiovtY
);
// 设置节点的默认碰撞体类型(默认无)
// 设置节点的默认碰撞体类型(默认 None
static void setDefaultCollider(
Collider::Type type
);
@ -775,8 +769,8 @@ public:
bool isEnable() const;
// 设置按钮启用或禁用
void setEnable(
bool enable
void setEnabled(
bool enabled
);
// 设置一般情况下显示的按钮
@ -825,7 +819,7 @@ protected:
Node * _mouseover;
Node * _selected;
Node * _disabled;
bool _enable;
bool _enabled;
bool _isSelected;
ButtonState _state;
Function _func;
@ -955,8 +949,8 @@ public:
size_t getButtonCount() const;
// 设置菜单启用或禁用
void setEnable(
bool enable
void setEnabled(
bool enabled
);
// 添加按钮
@ -973,7 +967,7 @@ public:
const std::vector<Button*>& getAllButtons() const;
protected:
bool _enable;
bool _enabled;
std::vector<Button*> _buttons;
};

View File

@ -454,11 +454,6 @@ class Path
friend class Game;
public:
// 设置游戏数据和临时文件保存路径名称
static void setGameFolderName(
const String& name
);
// Ìí¼Ó×ÊÔ´ËÑË÷·¾¶
static void addSearchPath(
String path

View File

@ -64,6 +64,7 @@
<ClCompile Include="..\..\core\Collider\EllipseCollider.cpp" />
<ClCompile Include="..\..\core\Collider\RectCollider.cpp" />
<ClCompile Include="..\..\core\Common\Color.cpp" />
<ClCompile Include="..\..\core\Common\Config.cpp" />
<ClCompile Include="..\..\core\Common\Font.cpp" />
<ClCompile Include="..\..\core\Common\Function.cpp" />
<ClCompile Include="..\..\core\Common\Image.cpp" />

View File

@ -140,6 +140,9 @@
<ClCompile Include="..\..\core\Common\Color.cpp">
<Filter>Common</Filter>
</ClCompile>
<ClCompile Include="..\..\core\Common\Config.cpp">
<Filter>Common</Filter>
</ClCompile>
<ClCompile Include="..\..\core\Common\Font.cpp">
<Filter>Common</Filter>
</ClCompile>

View File

@ -208,6 +208,7 @@
<ClCompile Include="..\..\core\Collider\EllipseCollider.cpp" />
<ClCompile Include="..\..\core\Collider\RectCollider.cpp" />
<ClCompile Include="..\..\core\Common\Color.cpp" />
<ClCompile Include="..\..\core\Common\Config.cpp" />
<ClCompile Include="..\..\core\Common\Font.cpp" />
<ClCompile Include="..\..\core\Common\Function.cpp" />
<ClCompile Include="..\..\core\Common\Image.cpp" />

View File

@ -140,6 +140,9 @@
<ClCompile Include="..\..\core\Common\Color.cpp">
<Filter>Common</Filter>
</ClCompile>
<ClCompile Include="..\..\core\Common\Config.cpp">
<Filter>Common</Filter>
</ClCompile>
<ClCompile Include="..\..\core\Common\Font.cpp">
<Filter>Common</Filter>
</ClCompile>

View File

@ -227,6 +227,7 @@
<ClCompile Include="..\..\core\Collider\RectCollider.cpp" />
<ClCompile Include="..\..\core\Collider\Collision.cpp" />
<ClCompile Include="..\..\core\Common\Color.cpp" />
<ClCompile Include="..\..\core\Common\Config.cpp" />
<ClCompile Include="..\..\core\Common\Font.cpp" />
<ClCompile Include="..\..\core\Common\Function.cpp" />
<ClCompile Include="..\..\core\Common\Object.cpp" />

View File

@ -243,6 +243,9 @@
<ClCompile Include="..\..\core\Custom\SystemException.cpp">
<Filter>Custom</Filter>
</ClCompile>
<ClCompile Include="..\..\core\Common\Config.cpp">
<Filter>Common</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\core\easy2d.h" />