做挂网站吗,wordpress免费主机空间,制作wordpress模板,app制作器软件下载不多说了直接上代码#xff0c;课程中的架构讲的比较宽泛#xff0c;而且有些方法写完之后并未测试。所以先把代码写完。理解其原理#xff0c;未来使用时候会再此完善此博客。
文件架构#xff1a; Event.h:核心基类
#pragma once
#include../Core.h
#inclu…不多说了直接上代码课程中的架构讲的比较宽泛而且有些方法写完之后并未测试。所以先把代码写完。理解其原理未来使用时候会再此完善此博客。
文件架构 Event.h:核心基类
#pragma once
#include../Core.h
#includestring
#includefunctional
namespace YOTO {// Hazel中的事件当前是阻塞的这意味着当一个事件发生时它立即被分派必须立即处理。//将来一个更好的策略可能是在事件总线中缓冲事件并在更新阶段的“事件”部分处理它们。//事件类型enum class EventType{None 0,WindowClose, WindowResize, WindowFocus, WindowLostFocus, WindowMoved,AppTick, AppUpdate, AppRender,KeyPressed, KeyReleased,MouseButtonPressed, MouseButtonReleased, MouseMoved, MouseScrolled};//事件分类enum EventCategory {None 0,EventCategoryApplication BIT(0),EventCategoryInput BIT(1),EventCategoryKeyboard BIT(2),EventCategoryMouse BIT(3),EventCategoryMouseButton BIT(4)};//定义一些重复的重写的函数只需要传入固定参数就能少些一大部分代码
#define EVENT_CLASS_TYPE(type) static EventType GetStaticType(){return EventType::##type; }\virtual EventType GetEventType() const override {return GetStaticType();}\virtual const char* GetName() const override {return #type;}
#define EVENT_CLASS_CATEGORY(category) virtual int GetCategoryFlags() const override {return category; }/// summary/// 事件基类事件需要继承此类/// /summaryclass Event{public://获得该事件的具体类型virtual EventType GetEventType() const 0;//获得事件的名称virtual const char* GetName() const 0;//获得该事件的大类型virtual int GetCategoryFlags() const 0;//toString方法返回事件名virtual std::string ToString() const { return GetName(); }//判断该事件是否是category类inline bool IsInCategory(EventCategory category) {return GetCategoryFlags() category;}protected://事件是否被处理了bool m_Handled false;};/// summary/// 基于事件类型调度事件的方法暂时没有测试不知道怎么用/// /summaryclass EventDispatcher{template typename Tusing EventFn std::functionbool(T);public:EventDispatcher(Event event):m_Event(event) {}template typename Tbool Dispatch(EventFnT func) {if (m_Event.GetEventType() T::GetStaticType()) {m_Event.m_Handled func(*(T*)m_Event);return true;}return false;}private:Event m_Event;};//暂无测试方便输出inline std::ostream operator(std::ostream os, const Event e){return os e.ToString();}} ApplicationEvent.h:处理appwindow的各种事件
#pragma once
#includeEvent.h
#includesstream
namespace YOTO {class YOTO_API WindowResizeEvent :public Event {public:WindowResizeEvent(unsigned int width, unsigned int height) :m_Width(width), m_Height(height) {}inline unsigned int GetWidth() const { return m_Width; }inline unsigned int GetHeight() const { return m_Height; }std::string ToString() const override {std::stringstream ss;ss 窗口大小改变事件 m_Width , m_Height;return ss.str();}EVENT_CLASS_TYPE(WindowResize)EVENT_CLASS_CATEGORY(EventCategoryApplication)private:unsigned int m_Width, m_Height;};class YOTO_API WindowCloseEvent :public Event{public:WindowCloseEvent(){}EVENT_CLASS_TYPE(WindowClose)EVENT_CLASS_CATEGORY(EventCategoryApplication)};class YOTO_API AppTickEvent :public Event {public:AppTickEvent(){}EVENT_CLASS_TYPE(AppTick)EVENT_CLASS_CATEGORY(EventCategoryApplication)};class YOTO_API AppUpdateEvent :public Event {public:AppUpdateEvent(){}EVENT_CLASS_TYPE(AppUpdate)EVENT_CLASS_CATEGORY(EventCategoryApplication)};class YOTO_API AppRenderEvent :public Event {public:AppRenderEvent() {}EVENT_CLASS_TYPE(AppRender)EVENT_CLASS_CATEGORY(EventCategoryApplication)};
}
KeyEvent.h处理键盘事件
#pragma once
#includeEvent.h
#includesstream
namespace YOTO {class YOTO_API KeyEvent:public Event{public:inline int GetKeyCode() const { return m_KeyCode; }EVENT_CLASS_CATEGORY(EventCategoryKeyboard | EventCategoryInput)protected:KeyEvent(int keycode):m_KeyCode(keycode){}int m_KeyCode;};class YOTO_API KeyPressedEvent :public KeyEvent{public:KeyPressedEvent(int keycode, int repeatCount):KeyEvent(keycode),m_RepeatCount(repeatCount){}inline int GetRepeatCount() const { return m_RepeatCount; }std::string ToString() const override{std::stringstream ss;ss 键盘按下事件 m_KeyCode ( m_RepeatCount 重复);return ss.str();}//static EventType GetStaticType() { return EventType::KeyPressed; }//virtual EventType GetEventType()const override { return GetStaticType(); }//virtual const char* GetName()const override { return KeyPressed; }EVENT_CLASS_TYPE(KeyPressed)private:int m_RepeatCount;};class KeyReleasedEvent:public KeyEvent{public:KeyReleasedEvent(int keycode):KeyEvent(keycode){}std::string ToString()const override {std::stringstream ss;ss 键盘释放事件 m_KeyCode;return ss.str(); }EVENT_CLASS_TYPE(KeyReleased)};} MouseEvent.h:处理鼠标事件
#pragma once
#includeEvent.h
#includesstream
namespace YOTO {class YOTO_API MouseMovedEvent :public Event{public:MouseMovedEvent(float x, float y):m_MouseX(x), m_MouseY(y) {}inline float GetX() const { return m_MouseX; }inline float GetY() const { return m_MouseY; }std::string ToString() const override {std::stringstream ss;ss 鼠标移动事件 m_MouseX , m_MouseY;return ss.str();}EVENT_CLASS_TYPE(MouseMoved)EVENT_CLASS_CATEGORY(EventCategoryMouse |EventCategoryInput )private:float m_MouseX, m_MouseY;};class YOTO_API MouseScrolledEvent :public Event{public:MouseScrolledEvent(float x, float y):m_XOffset(x), m_YOffset(y) {}inline float GetXOffset() const { return m_XOffset; }inline float GetYOffset() const { return m_YOffset; }std::string ToString() const override {std::stringstream ss;ss 鼠标滚动事件 GetXOffset() , GetYOffset();return ss.str();}EVENT_CLASS_TYPE(MouseScrolled)EVENT_CLASS_CATEGORY(EventCategoryMouse | EventCategoryInput)private:float m_XOffset, m_YOffset;};class YOTO_API MouseButtonEvent :public Event{public:inline int GetMouseButton() const { return m_Button; }EVENT_CLASS_CATEGORY(EventCategoryMouse | EventCategoryInput)protected:MouseButtonEvent(int button):m_Button(button){}int m_Button;};class YOTO_API MouseButtonPressedEvent :public MouseButtonEvent{public:MouseButtonPressedEvent(int button) :MouseButtonEvent(button) {}std::string ToString() const override {std::stringstream ss;ss 鼠标按下事件 m_Button;return ss.str();}EVENT_CLASS_TYPE(MouseButtonPressed)};class YOTO_API MouseButtonReleasedEvent :public MouseButtonEvent{public:MouseButtonReleasedEvent(int button):MouseButtonEvent(button) {}std::string ToString() const override {std::stringstream ss;ss 鼠标松开事件 m_Button;return ss.str();}EVENT_CLASS_TYPE(MouseButtonReleased)};
}
说白了就是写了个基类然后写了实现类。
修改
Core.h
#pragma once
//用于dll的宏
#ifdef YT_PLATFORM_WINDOWS
#ifdef YT_BUILD_DLL
#define YOTO_API __declspec(dllexport)
#else
#define YOTO_API __declspec(dllimport) #endif // DEBUG
#else
#error YOTO_ONLY_SUPPORT_WINDOWS
#endif // YOTO_PLATFORM_WINDOWS
#define BIT(x)(1x)
记得加Event.h的头文件
premake5.lua
workspace YOTOEngine -- sln文件名architecture x64 configurations{Debug,Release,Dist}
-- https://github.com/premake/premake-core/wiki/Tokens#value-tokens
-- 组成输出目录:Debug-windows-x86_64
outputdir %{cfg.buildcfg}-%{cfg.system}-%{cfg.architecture}project YOTOEngine --Hazel项目location YOTOEngine--在sln所属文件夹下的Hazel文件夹kind SharedLib--dll动态库language Ctargetdir (bin/ .. outputdir .. /%{prj.name}) -- 输出目录objdir (bin-int/ .. outputdir .. /%{prj.name})-- 中间目录-- 包含的所有h和cpp文件files{%{prj.name}/src/**.h,%{prj.name}/src/**.cpp}-- 包含目录includedirs{%{prj.name}/src,%{prj.name}/vendor/spdlog-1.x/include}-- 如果是window系统filter system:windowscppdialect C17-- On:代码生成的运行库选项是MTD,静态链接MSVCRT.lib库;-- Off:代码生成的运行库选项是MDD,动态链接MSVCRT.dll库;打包后的exe放到另一台电脑上若无这个dll会报错staticruntime On systemversion latest -- windowSDK版本-- 预处理器定义defines{YT_PLATFORM_WINDOWS,YT_BUILD_DLL}-- 编译好后移动Hazel.dll文件到Sandbox文件夹下postbuildcommands{({COPY} %{cfg.buildtarget.relpath} ../bin/ .. outputdir .. /Sandbox)}-- 不同配置下的预定义不同filter configurations:Debugdefines YT_DEBUGsymbols Onfilter configurations:Releasedefines YT_RELEASEoptimize Onfilter configurations:Distdefines YT_DISToptimize Onproject Sandboxlocation Sandboxkind ConsoleApplanguage Ctargetdir (bin/ .. outputdir .. /%{prj.name})objdir (bin-int/ .. outputdir .. /%{prj.name})files{%{prj.name}/src/**.h,%{prj.name}/src/**.cpp}-- 同样包含spdlog头文件includedirs{YOTOEngine/vendor/spdlog-1.x/include,YOTOEngine/src}-- 引用hazellinks{YOTOEngine}filter system:windowscppdialect C17staticruntime Onsystemversion latestdefines{YT_PLATFORM_WINDOWS}filter configurations:Debugdefines YT_DEBUGsymbols Onfilter configurations:Releasedefines YT_RELEASEoptimize Onfilter configurations:Distdefines YT_DISToptimize On测试
Application.cpp
#include Application.h
#includeEvent/ApplicationEvent.h
#includeLog.h
namespace YOTO {Application::Application() {}Application::~Application() {}void Application::Run() {WindowResizeEvent e(1280, 720);if (e.IsInCategory(EventCategoryApplication)) {YT_CORE_TRACE(e);}if (e.IsInCategory(EventCategoryInput)) {YT_CORE_ERROR(e);}while (true){}}
} 预编译头文件
ytpch.h
#pragma once
#includeiostream
#includememory
#includeutility
#includealgorithm
#includefunctional
#includestring
#includevector
#includeunordered_map
#includeunordered_set
#includesstream
#ifdef YT_PLATFORM_WINDOWS
#includeWindows.h
#endif // YT_PLATFORM_WINDOWS
ytpch.cpp
#include ytpch.hpremake5.lua
workspace YOTOEngine -- sln文件名architecture x64 configurations{Debug,Release,Dist}
-- https://github.com/premake/premake-core/wiki/Tokens#value-tokens
-- 组成输出目录:Debug-windows-x86_64
outputdir %{cfg.buildcfg}-%{cfg.system}-%{cfg.architecture}project YOTOEngine --Hazel项目location YOTOEngine--在sln所属文件夹下的Hazel文件夹kind SharedLib--dll动态库language Ctargetdir (bin/ .. outputdir .. /%{prj.name}) -- 输出目录objdir (bin-int/ .. outputdir .. /%{prj.name})-- 中间目录pchheader ytpch.hpchsource YOTOEngine/src/ytpch.cpp-- 包含的所有h和cpp文件files{%{prj.name}/src/**.h,%{prj.name}/src/**.cpp}-- 包含目录includedirs{%{prj.name}/src,%{prj.name}/vendor/spdlog-1.x/include}-- 如果是window系统filter system:windowscppdialect C17-- On:代码生成的运行库选项是MTD,静态链接MSVCRT.lib库;-- Off:代码生成的运行库选项是MDD,动态链接MSVCRT.dll库;打包后的exe放到另一台电脑上若无这个dll会报错staticruntime On systemversion latest -- windowSDK版本-- 预处理器定义defines{YT_PLATFORM_WINDOWS,YT_BUILD_DLL}-- 编译好后移动Hazel.dll文件到Sandbox文件夹下postbuildcommands{({COPY} %{cfg.buildtarget.relpath} ../bin/ .. outputdir .. /Sandbox)}-- 不同配置下的预定义不同filter configurations:Debugdefines YT_DEBUGsymbols Onfilter configurations:Releasedefines YT_RELEASEoptimize Onfilter configurations:Distdefines YT_DISToptimize Onproject Sandboxlocation Sandboxkind ConsoleApplanguage Ctargetdir (bin/ .. outputdir .. /%{prj.name})objdir (bin-int/ .. outputdir .. /%{prj.name})files{%{prj.name}/src/**.h,%{prj.name}/src/**.cpp}-- 同样包含spdlog头文件includedirs{YOTOEngine/vendor/spdlog-1.x/include,YOTOEngine/src}-- 引用hazellinks{YOTOEngine}filter system:windowscppdialect C17staticruntime Onsystemversion latestdefines{YT_PLATFORM_WINDOWS}filter configurations:Debugdefines YT_DEBUGsymbols Onfilter configurations:Releasedefines YT_RELEASEoptimize Onfilter configurations:Distdefines YT_DISToptimize On把所有使用库文件的地方都换成#includeytpch.h即可