Easy2D v1.0.3

This commit is contained in:
werelone 2017-09-18 23:59:08 +08:00
parent 9dc0b67544
commit 740201fded
15 changed files with 258 additions and 70 deletions

View File

@ -153,10 +153,18 @@ void App::_initGraph()
void App::_mainLoop()
{
// 进入下一场景
// 下一场景指针不为空时,切换场景
if (m_nextScene)
{
// 执行当前场景的 onExit 函数
if (m_currentScene)
{
m_currentScene->onExit();
}
// 进入下一场景
_enterNextScene();
// 执行当前场景的 onEnter 函数
m_currentScene->onEnter();
}
// śĎŃÔľąÇ°łĄž°ˇÇżŐ
assert(m_currentScene);
@ -252,6 +260,17 @@ void App::backScene()
m_bSaveScene = false;
}
void App::clearScene()
{
// 清空场景栈
while (m_sceneStack.size())
{
auto temp = m_sceneStack.top();
SAFE_DELETE(temp);
m_sceneStack.pop();
}
}
void App::setBkColor(COLORREF color)
{
setbkcolor(color);

View File

@ -344,6 +344,7 @@
<ClCompile Include="Node\Button\ImageButton.cpp" />
<ClCompile Include="Node\Button\TextButton.cpp" />
<ClCompile Include="Node\Image.cpp" />
<ClCompile Include="Node\Layer.cpp" />
<ClCompile Include="Node\MouseNode.cpp" />
<ClCompile Include="Node\Node.cpp" />
<ClCompile Include="Node\Shape\Circle.cpp" />

View File

@ -114,6 +114,9 @@
<ClCompile Include="Node\Button\TextButton.cpp">
<Filter>源文件\Node\Button</Filter>
</ClCompile>
<ClCompile Include="Node\Layer.cpp">
<Filter>源文件\Node</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="easy2d.h">

View File

@ -1,7 +1,7 @@
#include "easy2d.h"
// FreePool 释放池的实现机制:
/// Object 类中的引用计数m_nRef引用计数保证了指针的使用安全
/// Object 类中的引用计数m_nRef保证了指针的使用安全
/// 它记录了对象被使用的次数,当计数为 0 时FreePool 会自动释放这个对象
/// 所有的 Object 对象都应在被使用时(例如 Text 添加到了场景中)
/// 调用 retain 函数保证该对象不被删除,并在不再使用时调用 release 函数

View File

@ -5,21 +5,6 @@
// 按键监听回调函数的容器
static std::vector<KeyMsg*> s_vKeyMsg;
// 虚拟键值的容器
static std::vector<VK_KEY> s_vKeys = {
KeyMsg::A, KeyMsg::B, KeyMsg::C, KeyMsg::D, KeyMsg::E, KeyMsg::F, KeyMsg::G, KeyMsg::H, KeyMsg::I, KeyMsg::J,
KeyMsg::K, KeyMsg::L, KeyMsg::M, KeyMsg::N, KeyMsg::O, KeyMsg::P, KeyMsg::Q, KeyMsg::R, KeyMsg::S, KeyMsg::T,
KeyMsg::U, KeyMsg::V, KeyMsg::W, KeyMsg::X, KeyMsg::Y, KeyMsg::Z,
KeyMsg::NUM_1, KeyMsg::NUM_2, KeyMsg::NUM_3, KeyMsg::NUM_4, KeyMsg::NUM_5,
KeyMsg::NUM_6, KeyMsg::NUM_7, KeyMsg::NUM_8, KeyMsg::NUM_9, KeyMsg::NUM_0,
KeyMsg::NUMPAD_1, KeyMsg::NUMPAD_2, KeyMsg::NUMPAD_3, KeyMsg::NUMPAD_4, KeyMsg::NUMPAD_5,
KeyMsg::NUMPAD_6, KeyMsg::NUMPAD_7, KeyMsg::NUMPAD_8, KeyMsg::NUMPAD_9, KeyMsg::NUMPAD_0,
KeyMsg::Enter, KeyMsg::Space, KeyMsg::Up, KeyMsg::Down, KeyMsg::Left, KeyMsg::Right, KeyMsg::Esc,
KeyMsg::Decimal, KeyMsg::Shift, KeyMsg::LShift, KeyMsg::RShift, KeyMsg::Ctrl, KeyMsg::LCtrl, KeyMsg::RCtrl,
KeyMsg::F1, KeyMsg::F2, KeyMsg::F3, KeyMsg::F4, KeyMsg::F5, KeyMsg::F6,
KeyMsg::F7, KeyMsg::F8, KeyMsg::F9, KeyMsg::F10, KeyMsg::F11, KeyMsg::F12
};
// 虚拟键值的定义
const VK_KEY KeyMsg::A = 'A';
const VK_KEY KeyMsg::B = 'B';
@ -69,7 +54,6 @@ const VK_KEY KeyMsg::NUMPAD_8 = VK_NUMPAD8;
const VK_KEY KeyMsg::NUMPAD_9 = VK_NUMPAD9;
const VK_KEY KeyMsg::Enter = VK_RETURN;
const VK_KEY KeyMsg::Space = VK_SPACE;
const VK_KEY KeyMsg::Decimal = VK_DECIMAL;
const VK_KEY KeyMsg::Ctrl = VK_CONTROL;
const VK_KEY KeyMsg::LCtrl = VK_LCONTROL;
const VK_KEY KeyMsg::RCtrl = VK_RCONTROL;
@ -94,6 +78,8 @@ const VK_KEY KeyMsg::F10 = VK_F10;
const VK_KEY KeyMsg::F11 = VK_F11;
const VK_KEY KeyMsg::F12 = VK_F12;
static VK_KEY convert(int ascii);
KeyMsg::KeyMsg(tstring name, const KEY_CALLBACK & callback)
{
m_sName = name;
@ -111,17 +97,13 @@ void KeyMsg::onKbHit(VK_KEY key)
void KeyMsg::__exec()
{
if (_kbhit()) // 获取键盘消息
if (_kbhit()) // 检测有无按键消息
{
for (VK_KEY key : s_vKeys) // 循环遍历所有的虚拟键值
VK_KEY key = convert(_getch()); // 获取键盘消息
for (auto k : s_vKeyMsg) // 分发该消息
{
if (GetAsyncKeyState(key) & 0x8000) // 判断该键是否按下
{
for (auto k : s_vKeyMsg) // 分发该按键消息
{
k->onKbHit(key); // 执行按键回调函数
}
}
k->onKbHit(key); // 执行按键回调函数
}
}
}
@ -134,7 +116,7 @@ void KeyMsg::addListener(tstring name, const KEY_CALLBACK & callback)
s_vKeyMsg.push_back(key);
}
bool KeyMsg::delListener(tstring name)
bool KeyMsg::deleteListener(tstring name)
{
// 创建迭代器
std::vector<KeyMsg*>::iterator iter;
@ -169,4 +151,60 @@ bool KeyMsg::isKeyDown(VK_KEY key)
{
// 获取 key 的按下情况
return (GetAsyncKeyState(key) & 0x8000);
}
VK_KEY convert(int ascii)
{
if (ascii >= 'a' && ascii <= 'z' || ascii >= 'A' && ascii <= 'Z')
{
return VK_KEY(ascii);
}
else if (ascii >= '0' && ascii <= '9')
{
return VK_KEY(ascii);
}
else if (ascii == 0x0D || ascii == 0x20 || ascii == 0x1B)
{
return VK_KEY(ascii);
}
else if (ascii == 0 || ascii == 0xE0)
{
switch (_getch())
{
case 72:
return KeyMsg::Up;
case 75:
return KeyMsg::Left;
case 77:
return KeyMsg::Right;
case 80:
return KeyMsg::Down;
case 59:
return KeyMsg::F1;
case 60:
return KeyMsg::F2;
case 61:
return KeyMsg::F3;
case 62:
return KeyMsg::F4;
case 63:
return KeyMsg::F5;
case 64:
return KeyMsg::F6;
case 65:
return KeyMsg::F7;
case 66:
return KeyMsg::F8;
case 67:
return KeyMsg::F9;
case 133:
return KeyMsg::F10;
case 134:
return KeyMsg::F11;
default:
return 0;
}
}
return 0;
}

View File

@ -54,7 +54,7 @@ void MouseMsg::addListener(tstring name, const MOUSE_CALLBACK & callback)
s_vMouseMsg.push_back(mouse);
}
bool MouseMsg::delListener(tstring name)
bool MouseMsg::deleteListener(tstring name)
{
// 创建迭代器
std::vector<MouseMsg*>::iterator iter;

View File

@ -11,6 +11,12 @@ ImageButton::ImageButton() :
m_nHeight = 0;
}
ImageButton::ImageButton(LPCTSTR image) :
ImageButton()
{
setNormal(new Image(image)); // 设置按钮在正常状态时的图片
}
ImageButton::ImageButton(Image * image) :
ImageButton()
{

View File

@ -11,6 +11,12 @@ TextButton::TextButton() :
m_nHeight = 0;
}
TextButton::TextButton(tstring text) :
TextButton()
{
setNormal(new Text(text)); // 设置按钮在正常状态时的文字
}
TextButton::TextButton(Text * text) :
TextButton()
{

View File

@ -14,7 +14,7 @@ Image::Image(LPCTSTR ImageFile, int x, int y, int width, int height) :
m_fScaleX(1),
m_fScaleY(1)
{
setImageFile(ImageFile, x, y, width, height); // 设置图片资源和裁剪大小
setImage(ImageFile, x, y, width, height); // 设置图片资源和裁剪大小
}
Image::~Image()
@ -52,7 +52,7 @@ float Image::getScaleY() const
return m_fScaleY;
}
bool Image::setImageFile(LPCTSTR ImageFile, int x, int y, int width, int height)
bool Image::setImage(LPCTSTR ImageFile)
{
//判断图片路径是否存在
if (!PathFileExists(ImageFile))
@ -67,7 +67,7 @@ bool Image::setImageFile(LPCTSTR ImageFile, int x, int y, int width, int height)
// 加载图片
m_Image.Load(ImageFile);
// 加载失败
if (m_Image.IsNull())
if (m_Image.IsNull())
{
return false;
}
@ -80,14 +80,25 @@ bool Image::setImageFile(LPCTSTR ImageFile, int x, int y, int width, int height)
m_Image.AlphaBlend(GetImageHDC(), 15, 30);
}
// 设置目标矩形(即绘制到窗口的位置和大小)
m_rDest.SetRect(0, 0, m_Image.GetWidth(), m_Image.GetHeight());
m_rDest.SetRect(m_nX, m_nY, m_nX + m_Image.GetWidth(), m_nY + m_Image.GetHeight());
m_rSrc.SetRect(0, 0, m_Image.GetWidth(), m_Image.GetHeight());
return true;
}
bool Image::setImage(LPCTSTR ImageFile, int x, int y, int width, int height)
{
if (!setImage(ImageFile))
{
return false;
}
// 裁剪图片大小
crop(x, y, width, height);
return true;
}
bool Image::setImageRes(LPCTSTR pResName, int x, int y, int width, int height)
bool Image::setImageFromRes(LPCTSTR pResName)
{
// 从资源加载图片(不支持 PNG
m_Image.LoadFromResource(GetModuleHandle(NULL), pResName);
@ -96,10 +107,19 @@ bool Image::setImageRes(LPCTSTR pResName, int x, int y, int width, int height)
{
return false;
}
// 重置缩放属性
m_fScaleX = 0, m_fScaleY = 0;
// 设置目标矩形(即绘制到窗口的位置和大小)
m_rDest.SetRect(0, 0, m_Image.GetWidth(), m_Image.GetHeight());
m_rDest.SetRect(m_nX, m_nY, m_nX + m_Image.GetWidth(), m_nY + m_Image.GetHeight());
m_rSrc.SetRect(0, 0, m_Image.GetWidth(), m_Image.GetHeight());
return true;
}
bool Image::setImageFromRes(LPCTSTR pResName, int x, int y, int width, int height)
{
if (!setImageFromRes(pResName))
{
return false;
}
// 裁剪图片大小
crop(x, y, width, height);
@ -131,8 +151,8 @@ void Image::stretch(int width, int height)
void Image::scale(float scaleX, float scaleY)
{
m_fScaleX = min(max(scaleX, 0), 1.0f);
m_fScaleY = min(max(scaleY, 0), 1.0f);
m_fScaleX = max(scaleX, 0);
m_fScaleY = max(scaleY, 0);
m_rDest.SetRect(
m_nX, m_nY,
m_nX + int(m_Image.GetWidth() * scaleX),

28
Easy2D/Node/Layer.cpp Normal file
View File

@ -0,0 +1,28 @@
#include "..\easy2d.h"
Layer::Layer() :
m_bBlock(true)
{
}
Layer::~Layer()
{
}
int Layer::getBlock() const
{
return m_bBlock;
}
void Layer::setBlock(bool block)
{
m_bBlock = block;
}
bool Layer::_exec(bool active)
{
BatchNode::_exec(active);
// 若图层阻塞消息,则永远取得画面焦点
return m_bBlock;
}

View File

@ -18,11 +18,17 @@ MouseNode::~MouseNode()
bool MouseNode::_exec(bool active)
{
// 若画面已取得焦点,或 display 属性为 false退出函数
if (!active || !m_bDisplay)
// 若 display 属性为 false退出函数
if (!m_bDisplay)
{
return false;
}
// 若画面已取得焦点,重置按钮属性并退出
if (!active)
{
reset();
return false;
}
// 判断节点当前的状态
// 若节点未取得焦点,则重新判断节点状态
if (!m_bTarget)
@ -38,8 +44,8 @@ bool MouseNode::_exec(bool active)
m_bTarget = true; // 取得焦点标记
_setStatus(SELECTED); // 状态设为 SELECTED
}
// 若节点阻塞鼠标消息,则取得画面焦点
if (!m_bBlock) return true;
// 若节点阻塞鼠标消息,则取得画面焦点
if (m_bBlock) return true;
}
else
{
@ -58,8 +64,8 @@ bool MouseNode::_exec(bool active)
}
reset(); // 恢复默认状态
}
// 若节点阻塞鼠标消息,则取得画面焦点
if (!m_bBlock) return true;
// 若节点阻塞鼠标消息,则取得画面焦点
if (m_bBlock) return true;
}
return false;
}

View File

@ -3,7 +3,8 @@
Shape::Shape() :
lineColor(Color::black),
fillColor(Color::white)
fillColor(Color::white),
m_eStyle(SOLID)
{
}
@ -23,15 +24,15 @@ void Shape::_onDraw()
setfillcolor(fillColor);
// 根据形状的样式进行不同的绘制
if (_style == Shape::STYLE::round)
if (m_eStyle == Shape::STYLE::ROUND)
{
roundShape();
}
else if (_style == Shape::STYLE::solid)
else if (m_eStyle == Shape::STYLE::SOLID)
{
solidShape();
}
else if (_style == Shape::STYLE::fill)
else if (m_eStyle == Shape::STYLE::FILL)
{
fillShape();
}
@ -56,3 +57,8 @@ void Shape::setLineColor(COLORREF color)
{
lineColor = color;
}
void Shape::setStyle(STYLE style)
{
m_eStyle = style;
}

View File

@ -35,6 +35,14 @@ void Scene::_onDraw()
}
}
void Scene::onEnter()
{
}
void Scene::onExit()
{
}
void Scene::add(Node * child, int zOrder)
{
// 断言添加的节点非空

View File

@ -14,13 +14,19 @@ const LONG FontWeight::extraBold = 800;
const LONG FontWeight::black = 900;
const LONG FontWeight::heavy = 900;
// ĬÈÏ×ÖÌå
static const FontStyle s_defaultFont(_T(""), 18, FontWeight::normal);
FontStyle::FontStyle()
{
m_font = s_defaultFont.m_font;
setFontFamily(_T(""));
m_font.lfWeight = 18;
m_font.lfHeight = 0;
m_font.lfWidth = 0;
m_font.lfItalic = 0;
m_font.lfUnderline = 0;
m_font.lfStrikeOut = 0;
m_font.lfEscapement = 0;
m_font.lfOrientation = 0;
setQuality(true);
}
FontStyle::FontStyle(LPCTSTR fontfamily, LONG height, LONG weight, LONG width, bool italic, bool underline, bool strikeout, LONG escapement, LONG orientation, bool quality)
@ -43,7 +49,7 @@ FontStyle::~FontStyle()
FontStyle * FontStyle::getDefault()
{
return new FontStyle(s_defaultFont);
return new FontStyle(_T(""), 18, FontWeight::normal);
}
void FontStyle::setHeight(LONG value)

View File

@ -1,5 +1,5 @@
/******************************************************
* Easy2D Game Engine (v1.0.2)
* Easy2D Game Engine (v1.0.3)
* http://www.easy2d.cn
*
* Depends on EasyX (Ver:20170827(beta))
@ -11,8 +11,8 @@
#error Easy2D is only for C++
#endif
#if _MSC_VER < 1600
#error Do Visual Studio 2010/2013/2015/2017 specific stuff
#if _MSC_VER < 1900
#error Do Visual Studio 2015/2017 specific stuff
#endif
@ -72,6 +72,7 @@ class FileUtils;
// 对象
class Object;
class Node;
class Layer;
class BatchNode;
class MouseNode;
class Image;
@ -84,7 +85,6 @@ class TextButton;
class ImageButton;
typedef BatchNode Layer;
typedef unsigned int VK_KEY;
typedef std::function<void()> CLICK_CALLBACK;
typedef std::function<void()> TIMER_CALLBACK;
@ -149,6 +149,8 @@ public:
void enterScene(Scene *scene, bool save = true);
// 返回上一场景
void backScene();
// 清空之前保存的所有场景
void clearScene();
// 修改窗口背景色
void setBkColor(COLORREF color);
// 设置帧率
@ -193,6 +195,10 @@ public:
Scene();
~Scene();
// 重写这个函数,它将在进入这个场景时自动执行
virtual void onEnter();
// 重写这个函数,它将在离开这个场景时自动执行
virtual void onExit();
// 添加子成员到场景
void add(Node * child, int zOrder = 0);
// 删除子成员
@ -233,7 +239,7 @@ public:
// 添加键盘监听
static void addListener(tstring name, const MOUSE_CALLBACK& callback);
// 删除键盘监听
static bool delListener(tstring name);
static bool deleteListener(tstring name);
// 删除所有键盘监听
static void clearAllListener();
@ -289,7 +295,7 @@ public:
// 数字键值
static const VK_KEY NUM_1, NUM_2, NUM_3, NUM_4, NUM_5, NUM_6, NUM_7, NUM_8, NUM_9, NUM_0;
// 小数字键盘值
static const VK_KEY NUMPAD_1, NUMPAD_2, NUMPAD_3, NUMPAD_4, NUMPAD_5, NUMPAD_6, NUMPAD_7, NUMPAD_8, NUMPAD_9, NUMPAD_0, Decimal;
static const VK_KEY NUMPAD_1, NUMPAD_2, NUMPAD_3, NUMPAD_4, NUMPAD_5, NUMPAD_6, NUMPAD_7, NUMPAD_8, NUMPAD_9, NUMPAD_0;
// 控制键值
static const VK_KEY Enter, Space, Up, Down, Left, Right, Esc, Shift, LShift, RShift, Ctrl, LCtrl, RCtrl;
// F 键值
@ -312,7 +318,7 @@ public:
// 添加键盘监听
static void addListener(tstring name, const KEY_CALLBACK& callback);
// 删除键盘监听
static bool delListener(tstring name);
static bool deleteListener(tstring name);
// 删除所有键盘监听
static void clearAllListener();
// 判断键是否被按下按下返回true
@ -614,6 +620,26 @@ public:
};
class Layer :
public virtual BatchNode
{
protected:
bool m_bBlock;
protected:
virtual bool _exec(bool active) override;
public:
Layer();
virtual ~Layer();
// 图层是否阻塞消息
int getBlock() const;
// 设置图层是否阻塞消息
void setBlock(bool block);
};
class Image :
public virtual Node
{
@ -648,23 +674,33 @@ public:
float getScaleY() const;
/**
* (png/bmp/jpg/gif/emf/wmf/ico)
*
* (png/bmp/jpg/gif/emf/wmf/ico)
*
*/
bool setImageFile(LPCTSTR ImageFile, int x = 0, int y = 0, int width = 0, int height = 0);
bool setImage(LPCTSTR ImageFile);
/**
* (png/bmp/jpg/gif/emf/wmf/ico)
*
*
*/
bool setImage(LPCTSTR ImageFile, int x, int y, int width, int height);
/**
* png (bmp/jpg/gif/emf/wmf/ico)
*
*/
bool setImageFromRes(LPCTSTR pResName);
/**
* png (bmp/jpg/gif/emf/wmf/ico)
*
*
*/
bool setImageRes(LPCTSTR pResName, int x = 0, int y = 0, int width = 0, int height = 0);
bool setImageFromRes(LPCTSTR pResName, int x, int y, int width, int height);
// 裁剪图片(裁剪后会恢复 stretch 拉伸)
void crop(int x = 0, int y = 0, int width = 0, int height = 0);
void crop(int x, int y, int width, int height);
// 将图片拉伸到固定宽高
void stretch(int width = 0, int height = 0);
// 按比例拉伸图片0 ~ 1.0f
void scale(float scaleX, float scaleY);
// 按比例拉伸图片
void scale(float scaleX = 1.0f, float scaleY = 1.0f);
// 设置图片位置
void setPos(int x, int y) override;
// 移动图片
@ -827,6 +863,7 @@ protected:
public:
TextButton();
TextButton(tstring text);
TextButton(Text * text);
virtual ~TextButton();
@ -869,6 +906,7 @@ protected:
public:
ImageButton();
ImageButton(LPCTSTR image);
ImageButton(Image * image);
virtual ~ImageButton();
@ -894,8 +932,6 @@ class Shape :
public virtual Node
{
protected:
enum STYLE { round, solid, fill }; // 形状填充样式
STYLE _style;
COLORREF fillColor;
COLORREF lineColor;
@ -905,6 +941,9 @@ protected:
virtual void fillShape() = 0;
virtual void roundShape() = 0;
public:
enum STYLE { ROUND, SOLID, FILL } m_eStyle; // 形状填充样式
public:
Shape();
virtual ~Shape();
@ -917,6 +956,8 @@ public:
void setFillColor(COLORREF color);
// 设置线条颜色
void setLineColor(COLORREF color);
// 设置填充样式
void setStyle(STYLE style);
};