Magic_Game/Easy2D/easy2d.h

267 lines
4.4 KiB
C
Raw Normal View History

2017-09-10 23:56:52 +08:00
/******************************************************
2017-09-29 17:57:17 +08:00
* Easy2D Game Engine
2017-09-10 23:56:52 +08:00
*
* Website: http://www.easy2d.cn
2017-10-12 02:44:44 +08:00
* Source Code: https://gitee.com/werelone/Easy2D
2017-09-10 23:56:52 +08:00
******************************************************/
#pragma once
#ifndef __cplusplus
#error Easy2D is only for C++
#endif
2017-09-18 23:59:08 +08:00
#if _MSC_VER < 1900
#error Do Visual Studio 2015/2017 specific stuff
2017-09-12 12:53:34 +08:00
#endif
2017-09-10 23:56:52 +08:00
2017-10-12 02:44:44 +08:00
// Modify the following defines if you have to target a platform prior to the ones specified below.
// Refer to MSDN for the latest info on corresponding values for different platforms.
#ifndef WINVER // Allow use of features specific to Windows 7 or later.
#define WINVER 0x0700 // Change this to the appropriate value to target other versions of Windows.
#endif
2017-09-29 17:30:37 +08:00
2017-10-12 02:44:44 +08:00
#ifndef _WIN32_WINNT // Allow use of features specific to Windows 7 or later.
#define _WIN32_WINNT 0x0700 // Change this to the appropriate value to target other versions of Windows.
#endif
2017-09-10 23:56:52 +08:00
2017-10-12 02:44:44 +08:00
#ifndef UNICODE
#define UNICODE
2017-09-10 23:56:52 +08:00
#endif
2017-10-12 02:44:44 +08:00
// Exclude rarely-used items from Windows headers.
#define WIN32_LEAN_AND_MEAN
// Windows Header Files:
#include <windows.h>
2017-09-29 17:30:37 +08:00
2017-10-12 02:44:44 +08:00
// C RunTime Header Files:
#include <wchar.h>
2017-09-29 17:30:37 +08:00
2017-10-12 02:44:44 +08:00
#include "emacros.h"
#include "ecommon.h"
2017-09-29 17:30:37 +08:00
2017-10-12 02:44:44 +08:00
#if defined(UNICODE) && (_DEBUG)
#pragma comment(lib, "Easy2Ddw.lib")
#elif !defined(UNICODE) && (_DEBUG)
#pragma comment(lib, "Easy2Dd.lib")
#elif defined(UNICODE)
#pragma comment(lib, "Easy2Dw.lib")
#elif !defined(UNICODE)
#pragma comment(lib, "Easy2D.lib")
#endif
2017-09-29 17:30:37 +08:00
2017-10-12 02:44:44 +08:00
2017-09-29 17:30:37 +08:00
// Classes Declare
2017-10-12 02:44:44 +08:00
namespace e2d
2017-09-29 17:30:37 +08:00
{
2017-10-12 02:44:44 +08:00
class EApp;
2017-09-29 17:30:37 +08:00
class Scene;
class Object;
}
// Classes
2017-10-12 02:44:44 +08:00
namespace e2d
2017-09-29 17:30:37 +08:00
{
2017-10-12 02:44:44 +08:00
class EApp
2017-09-29 17:30:37 +08:00
{
public:
2017-10-12 02:44:44 +08:00
EApp();
~EApp();
2017-09-29 17:30:37 +08:00
2017-10-12 02:44:44 +08:00
// <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>ʵ<EFBFBD><CAB5>
static EApp * get();
// Register the window class and call methods for instantiating drawing resources
bool init(
EString title,
ESize size,
bool bShowConsole = false
);
// Register the window class and call methods for instantiating drawing resources
bool init(
EString title,
UINT32 width,
UINT32 height,
bool bShowConsole = false
);
2017-09-29 17:30:37 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2017-10-12 02:44:44 +08:00
void run();
2017-09-29 17:30:37 +08:00
// <20>޸Ĵ<DEB8><C4B4>ڴ<EFBFBD>С
2017-10-12 02:44:44 +08:00
void setWindowSize(
int width,
int height
);
2017-09-29 17:30:37 +08:00
// <20>޸Ĵ<DEB8><C4B4>ڴ<EFBFBD>С
2017-10-12 02:44:44 +08:00
void setWindowSize(
ESize size
);
2017-09-29 17:30:37 +08:00
// <20><><EFBFBD>ô<EFBFBD><C3B4>ڱ<EFBFBD><DAB1><EFBFBD>
2017-10-12 02:44:44 +08:00
void setWindowTitle(
EString title
);
2017-09-29 17:30:37 +08:00
// <20><>ȡ<EFBFBD><C8A1><EFBFBD>ڱ<EFBFBD><DAB1><EFBFBD>
2017-10-12 02:44:44 +08:00
EString getTitle();
2017-09-29 17:30:37 +08:00
// <20><>ȡ<EFBFBD><C8A1><EFBFBD>ڿ<EFBFBD><DABF><EFBFBD>
2017-10-12 02:44:44 +08:00
int getWidth();
2017-09-29 17:30:37 +08:00
// <20><>ȡ<EFBFBD><C8A1><EFBFBD>ڸ߶<DAB8>
2017-10-12 02:44:44 +08:00
int getHeight();
2017-09-29 17:30:37 +08:00
// <20>л<EFBFBD><D0BB><EFBFBD><EFBFBD><EFBFBD>
2017-10-12 02:44:44 +08:00
void enterScene(
Scene * scene,
bool save = true
);
2017-09-29 17:30:37 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>
2017-10-12 02:44:44 +08:00
void backScene();
// <20><><EFBFBD>ձ<EFBFBD><D5B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>г<EFBFBD><D0B3><EFBFBD>
void clearScene();
// <20><>ȡ<EFBFBD><C8A1>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD>
Scene * getCurrentScene();
// <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD>ڼ<EFBFBD><DABC><EFBFBD><EFBFBD>еij<D0B5><C4B3><EFBFBD>
Scene * getLoadingScene();
2017-09-29 17:30:37 +08:00
// <20><><EFBFBD><EFBFBD> AppName
2017-10-12 02:44:44 +08:00
void setAppName(
EString appname
);
2017-09-29 17:30:37 +08:00
// <20><>ȡ AppName
2017-10-12 02:44:44 +08:00
EString getAppName();
2017-09-29 17:30:37 +08:00
// <20>޸Ĵ<DEB8><C4B4>ڱ<EFBFBD><DAB1><EFBFBD>ɫ
2017-10-12 02:44:44 +08:00
void setBkColor(
EColor::Enum color
);
2017-09-29 17:30:37 +08:00
2017-10-12 02:44:44 +08:00
// <20>ͷ<EFBFBD><CDB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD>Դ
void free();
// <20>رմ<D8B1><D5B4><EFBFBD>
void close();
// <20><>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD>
void show();
// <20><>ֹ<EFBFBD><D6B9><EFBFBD><EFBFBD>
void quit();
// <20><>ֹ<EFBFBD><D6B9><EFBFBD><EFBFBD>
void end();
2017-09-29 17:30:37 +08:00
protected:
2017-10-12 02:44:44 +08:00
// Initialize device-independent resources.
HRESULT CreateDeviceIndependentResources();
// Initialize device-dependent resources.
HRESULT CreateDeviceResources();
// Release device-dependent resource.
void DiscardDeviceResources();
2017-09-29 17:30:37 +08:00
void _mainLoop();
2017-10-12 02:44:44 +08:00
void _onControl();
// Draw content.
bool _onRender();
2017-09-29 17:30:37 +08:00
void _enterNextScene();
2017-10-12 02:44:44 +08:00
// Resize the render target.
void _onResize(
UINT width,
UINT height
);
// The windows procedure.
static LRESULT CALLBACK WndProc(
HWND hWnd,
UINT message,
WPARAM wParam,
LPARAM lParam
);
protected:
bool m_bRunning;
bool m_bSaveScene;
EString m_sTitle;
EString m_sAppName;
EColor::Enum m_ClearColor;
Scene * m_pCurrentScene;
Scene * m_pNextScene;
Scene * m_pLoadingScene;
2017-09-29 17:30:37 +08:00
};
class Scene
{
2017-10-12 02:44:44 +08:00
friend EApp;
2017-09-29 17:30:37 +08:00
public:
Scene();
~Scene();
2017-10-06 16:40:10 +08:00
// <20><>д<EFBFBD><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
virtual void init();
2017-09-29 17:30:37 +08:00
// <20><>д<EFBFBD><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڽ<EFBFBD><DABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD>Զ<EFBFBD>ִ<EFBFBD><D6B4>
virtual void onEnter();
// <20><>д<EFBFBD><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EBBFAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD>Զ<EFBFBD>ִ<EFBFBD><D6B4>
virtual void onExit();
// <20><><EFBFBD><EFBFBD><EFBFBD>ӳ<EFBFBD>Ա<EFBFBD><D4B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
void add(Node * child, int zOrder = 0);
// ɾ<><C9BE><EFBFBD>ӳ<EFBFBD>Ա
bool del(Node * child);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӳ<EFBFBD>Ա
void clearAllChildren();
protected:
2017-10-12 02:44:44 +08:00
//std::vector<Node*> m_vChildren;
2017-09-29 17:30:37 +08:00
protected:
void _exec();
void _onDraw();
};
class Object
{
friend FreePool;
2017-09-29 17:30:37 +08:00
public:
Object();
virtual ~Object();
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2017-09-29 17:30:37 +08:00
void retain();
// <20>ͷ<EFBFBD><CDB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2017-09-29 17:30:37 +08:00
void release();
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><D4B6>ͷ<EFBFBD><CDB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
void autoRelease();
2017-09-29 17:30:37 +08:00
protected:
int m_nRefCount;
bool m_bAutoRelease;
2017-09-29 17:30:37 +08:00
};
} // End of easy2d namespace
// Functions Declare
2017-10-12 02:44:44 +08:00
using namespace e2d;