跟着cherno手搓游戏引擎【5】layer(层)、Glad

编写基类层:

Layer.h:提供Attach链接、Detach解绑、Update刷新、Event事件、GetName方法

#pragma once
#include"YOTO/Core.h"
#include"YOTO/Event/Event.h"
namespace YOTO {class YOTO_API Layer{public:Layer(const std::string& name = "Layer");virtual ~Layer();virtual void OnAttach(){}virtual void OnDetach() {}virtual void OnUpdate() {}virtual void OnEvent(Event& event) {}inline const std::string& GetName() const { return m_DebugName; }protected:std::string m_DebugName;};}

Layer.cpp:随便写写

#include "ytpch.h"
#include "Layer.h"
namespace YOTO {Layer::Layer(const std::string &debugName):m_DebugName(debugName){}Layer::~Layer(){} 
}

编写层栈:

LayerStack.h:建立用来存储层的栈(用vector)

#pragma once
#include"YOTO/Core.h"
#include"Layer.h"
#include<vector>
namespace YOTO {class  YOTO_API LayerStack{public:LayerStack();~LayerStack();void PushLayer(Layer* layer);void PushOverlay(Layer* layer);void PopLayer(Layer* layer);void PopOverLay(Layer* layer);std::vector<Layer*>::iterator begine() { return m_Layers.begin(); }std::vector<Layer*>::iterator end() { return m_Layers.end(); }private:std::vector<Layer*>m_Layers;std::vector<Layer*>::iterator m_LayerInsert;};}

 LayerStack.cpp:看注释,需要解释的有点多

#include "ytpch.h"
#include "LayerStack.h"
namespace YOTO {LayerStack::LayerStack() {m_LayerInsert = m_Layers.begin();}LayerStack::~LayerStack() {for (Layer* layer : m_Layers)delete layer;}//普通push在队列最左(查找时候性能更优)void LayerStack::PushLayer(Layer* layer) {// emplace在vector容器指定位置之前插入一个新的元素。返回插入元素的位置// 插入 1 2 3,vector是 3 2 1m_LayerInsert = m_Layers.emplace(m_LayerInsert, layer);}//在最右插入void LayerStack::PushOverlay(Layer* overlay) {//m_LayerInsert = m_Layers.begin();//如果报错,则把这个注释取消m_Layers.emplace_back(overlay);}//查找void LayerStack::PopLayer(Layer* layer) {auto it = std::find(m_Layers.begin(), m_Layers.end(), layer);if (it != m_Layers.end()) {m_Layers.erase(it);m_LayerInsert--;	// 指向Begin}}void LayerStack::PopOverlay(Layer* overlay) {auto it = std::find(m_Layers.begin(), m_Layers.end(), overlay);if (it != m_Layers.end())m_Layers.erase(it);}
}

 YOTO.h:加一个Layer.h

#pragma once
#include "YOTO/Application.h"
#include"YOTO/Layer.h"
#include "YOTO/Log.h"
//入口点
#include"YOTO/EntryPoint.h"

Application.h:Stack实例,Push方法添加层。

#pragma once
#include"Core.h"
#include"Event/Event.h"
#include"Event/ApplicationEvent.h"
#include "YOTO/Window.h"
#include"YOTO/LayerStack.h"
namespace YOTO {class YOTO_API Application{public:Application();virtual ~Application();void Run();void OnEvent(Event &e);void PushLayer(Layer* layer);void PushOverlay(Layer* layer);private:bool  OnWindowClosed(WindowCloseEvent& e);std::unique_ptr<Window>  m_Window;bool m_Running = true;LayerStack m_LayerStack;};//在客户端定义Application* CreateApplication();
}

Application.cpp:添加Push即可。

#include"ytpch.h"
#include "Application.h"#include"Log.h"
#include<GLFW/glfw3.h>
namespace YOTO {
#define BIND_EVENT_FN(x) std::bind(&x, this, std::placeholders::_1)Application::Application() {//智能指针m_Window = std::unique_ptr<Window>(Window::Creat());//设置回调函数m_Window->SetEventCallback(BIND_EVENT_FN(Application::OnEvent));}Application::~Application() {}/// <summary>/// 所有的Window事件都会在这触发,作为参数e/// </summary>/// <param name="e"></param>void Application::OnEvent(Event& e) {//根据事件类型绑定对应事件EventDispatcher dispatcher(e);dispatcher.Dispatch<WindowCloseEvent>(BIND_EVENT_FN(Application::OnWindowClosed));//输出事件信息YT_CORE_INFO("{0}",e);for (auto it = m_LayerStack.end(); it != m_LayerStack.begin();) {(*--it)->OnEvent(e);if (e.m_Handled)break;}}bool Application::OnWindowClosed(WindowCloseEvent& e) {m_Running = false;return true;}void Application::Run() {WindowResizeEvent e(1280, 720);if (e.IsInCategory(EventCategoryApplication)) {YT_CORE_TRACE(e);}if (e.IsInCategory(EventCategoryInput)) {YT_CORE_ERROR(e);}while (m_Running){glClearColor(1,0,1,1);glClear(GL_COLOR_BUFFER_BIT);for (Layer* layer : m_LayerStack) {layer->OnUpdate();}m_Window->OnUpdate();}}void Application::PushLayer(Layer* layer) {m_LayerStack.PushLayer(layer);}void Application::PushOverlay(Layer* layer) {m_LayerStack.PushOverlay(layer);}
}

SandboxApp.cpp:创建测试Layer,在构造方法中Push加入

#include<YOTO.h>
#include<stdio.h>class ExampleLayer:public YOTO::Layer
{
public:ExampleLayer():Layer("Example") {}void OnUpdate()override {YT_CLIENT_INFO("测试update");}void	OnEvent(YOTO::Event& e)override {YT_CLIENT_TRACE("测试event{0}",e);}private:};class Sandbox:public YOTO::Application
{
public:Sandbox() {PushLayer(new ExampleLayer());}~Sandbox() {}private:};YOTO::Application* YOTO::CreateApplication() {printf("helloworld");return new Sandbox();
}

测试:

这个问题,是因为没改SRC下的premake5.lua

 添加buildoptions:"\MDd"和buildoptions:"\MD"在YOTOEngine和Sandbox下。

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}"IncludeDir={}
IncludeDir["GLFW"]="YOTOEngine/vendor/GLFW/include"include "YOTOEngine/vendor/GLFW"project "YOTOEngine"		--Hazel项目location "YOTOEngine"--在sln所属文件夹下的Hazel文件夹kind "SharedLib"--dll动态库language "C++"targetdir ("bin/" .. outputdir .. "/%{prj.name}") -- 输出目录objdir ("bin-int/" .. outputdir .. "/%{prj.name}")-- 中间目录pchheader "ytpch.h"pchsource "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","%{IncludeDir.GLFW}"}links{"GLFW","opengl32.lib"}-- 如果是window系统filter "system:windows"cppdialect "C++17"-- On:代码生成的运行库选项是MTD,静态链接MSVCRT.lib库;-- Off:代码生成的运行库选项是MDD,动态链接MSVCRT.dll库;打包后的exe放到另一台电脑上若无这个dll会报错staticruntime "On"	systemversion "latest"	-- windowSDK版本-- 预处理器定义defines{"YT_PLATFORM_WINDOWS","YT_BUILD_DLL","YT_ENABLE_ASSERTS",}-- 编译好后移动Hazel.dll文件到Sandbox文件夹下postbuildcommands{("{COPY} %{cfg.buildtarget.relpath} ../bin/" .. outputdir .. "/Sandbox")}-- 不同配置下的预定义不同filter "configurations:Debug"defines "YT_DEBUG"buildoptions"/MDd"symbols "On"filter "configurations:Release"defines "YT_RELEASE"buildoptions"/MD"optimize "On"filter "configurations:Dist"defines "YT_DIST"buildoptions"/MD"optimize "On"project "Sandbox"location "Sandbox"kind "ConsoleApp"language "C++"targetdir ("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","GLFW","opengl32.lib"}filter "system:windows"cppdialect "C++17"staticruntime "On"systemversion "latest"defines{"YT_PLATFORM_WINDOWS"}filter "configurations:Debug"defines "YT_DEBUG"buildoptions"/MDd"symbols "On"filter "configurations:Release"defines "YT_RELEASE"buildoptions"/MD"optimize "On"filter "configurations:Dist"defines "YT_DIST"buildoptions"/MD"optimize "On"

改变窗口大小: 

cool! 

Glad下载:

glad.dav1d.de,配置如下,点击Generate生成

 点击zip下载 

 在如下位置创建Glad文件夹,并将下载的include和src拖入

Glad配置:

在Glad文件夹下创建premake5.lua:把glad,KHR,src包含进来

project "Glad"kind "StaticLib"language "C"staticruntime "off"warnings "off"targetdir ("bin/" .. outputdir .. "/%{prj.name}")objdir ("bin-int/" .. outputdir .. "/%{prj.name}")files{"include/glad/glad.h","include/KHR/khrplatform.h","src/glad.c"}includedirs{"include"-- 为了glad.c直接#include <glad/glad.h>,而不用#include <include/glad/glad.h}filter "system:windows"systemversion "latest"filter { "system:windows", "configurations:Debug-AS" }	runtime "Debug"symbols "on"sanitize { "Address" }flags { "NoRuntimeChecks", "NoIncrementalLink" }filter "configurations:Debug"defines "YT_DEBUG"buildoptions "/MTd"symbols "On"filter "configurations:Release"defines "YT_RELEASE"buildoptions "/MT"symbols "On"filter "configurations:Dist"defines "YT_DIST"buildoptions "/MT"symbols "On"

 同时修改SRC下的premake5.lua:和GLFW类似

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}"
-- 包含相对解决方案的目录
IncludeDir={}
IncludeDir["GLFW"]="YOTOEngine/vendor/GLFW/include"
IncludeDir["Glad"]="YOTOEngine/vendor/Glad/include"
include "YOTOEngine/vendor/GLFW"
include "YOTOEngine/vendor/Glad"project "YOTOEngine"		--YOTOEngine项目location "YOTOEngine"--在sln所属文件夹下的YOTOEngine文件夹kind "SharedLib"--dll动态库language "C++"targetdir ("bin/" .. outputdir .. "/%{prj.name}") -- 输出目录objdir ("bin-int/" .. outputdir .. "/%{prj.name}")-- 中间目录pchheader "ytpch.h"pchsource "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","%{IncludeDir.GLFW}","%{IncludeDir.Glad}"}links{"GLFW",-- GLFW.lib库链接到YOTOEngine项目中"Glad",-- Glad.lib库链接到YOTOEngine项目中"opengl32.lib"}-- 如果是window系统filter "system:windows"cppdialect "C++17"-- On:代码生成的运行库选项是MTD,静态链接MSVCRT.lib库;-- Off:代码生成的运行库选项是MDD,动态链接MSVCRT.dll库;打包后的exe放到另一台电脑上若无这个dll会报错staticruntime "On"	systemversion "latest"	-- windowSDK版本-- 预处理器定义defines{"YT_PLATFORM_WINDOWS","YT_BUILD_DLL","YT_ENABLE_ASSERTS","GLFW_INCLUDE_NONE"-- 让GLFW不包含OpenGL}-- 编译好后移动Hazel.dll文件到Sandbox文件夹下postbuildcommands{("{COPY} %{cfg.buildtarget.relpath} ../bin/" .. outputdir .. "/Sandbox")}-- 不同配置下的预定义不同filter "configurations:Debug"defines "YT_DEBUG"buildoptions"/MDd"symbols "On"filter "configurations:Release"defines "YT_RELEASE"buildoptions"/MD"optimize "On"filter "configurations:Dist"defines "YT_DIST"buildoptions"/MD"optimize "On"project "Sandbox"location "Sandbox"kind "ConsoleApp"language "C++"targetdir ("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"}-- 引用YOTOEnginelinks{"YOTOEngine","GLFW","opengl32.lib"}filter "system:windows"cppdialect "C++17"staticruntime "On"systemversion "latest"defines{"YT_PLATFORM_WINDOWS"}filter "configurations:Debug"defines "YT_DEBUG"buildoptions"/MDd"symbols "On"filter "configurations:Release"defines "YT_RELEASE"buildoptions"/MD"optimize "On"filter "configurations:Dist"defines "YT_DIST"buildoptions"/MD"optimize "On"

生成:

测试:

写一段测试代码在Application.cpp

 能run,没问题

cool! 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/620446.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

KB5034439更新安装失败(0x80070643)的简易解决方法

KB5034439&#xff0c;官方的说明为&#xff1a;适用于 Azure Stack HCI 版本 22H2 和 Windows Server 2022 的 Windows 恢复环境更新&#xff08;2024年1月9日发布&#xff09;。 这个更新与Win10上的KB5034441作用类似&#xff0c;因此也遭遇了相同的安装问题。 服务器在安…

微机原理常考填空总结

hello大家好我是吃个西瓜嘤&#xff0c;这篇节只总结微机原理常考填空题都是干货展示常出现的易错点以及微机原理注意事项。 以下仅代表个人发言 #微机原理 正文开始&#xff1a; 1&#xff0c;区分JZ&#xff0c;JNZ技巧 也就是D70用JZ&#xff1b;D71用JNZ。 JZ;条件ZF1时…

【河海大学论文LaTeX+VSCode全指南】

河海大学论文LaTeXVSCode全指南 前言一、 LaTeX \LaTeX{} LATE​X的安装二、VScode的安装三、VScode的配置四、验证五、优化 前言 LaTeX \LaTeX{} LATE​X在论文写作方面具有传统Word无法比拟的优点&#xff0c;VScode作为一个轻量化的全功能文本编辑器&#xff0c;由于其极强的…

一、QT的前世今

一、Qt是什么 1、Qt 是一个1991年由奇趣科技开发的跨平台C图形用户界面应用程序开发框架。它既可以开发GUI程序&#xff0c;也可用于开发非GUI程序&#xff0c;比如控制台工具和服务。 2、Qt是面向对象的框架&#xff0c;具有面向对象语言的特性&#xff1a;封装、继承、多态。…

短视频抖音文案策划创作运营手册资料大全

【干货资料持续更新&#xff0c;以防走丢】 短视频抖音文案策划创作运营手册资料大全 部分资料预览 资料部分是网络整理&#xff0c;仅供学习参考。 抖音运营资料合集&#xff08;完整资料包含以下内容&#xff09; 目录 制作短视频的四部曲 主题 主题是短视频脚本的基调…

sqlalchemy 中的缓存机制解释

SQLAlchemy 的缓存机制主要涉及两个层面&#xff1a;会话&#xff08;Session&#xff09;缓存和查询缓存。这两种缓存机制对于提升应用性能和数据一致性都非常重要。下面详细解释这两种缓存机制&#xff1a; 1. 会话&#xff08;Session&#xff09;缓存 会话缓存是 SQLAlch…

019、错误处理:不可恢复错误与panic!

鉴于上一篇文章过长&#xff0c;不方便大家阅读和理解&#xff0c;因此关于Rust中的错误处理&#xff0c; 我将分以下3篇来讲。 另外&#xff0c;随着我们学习的不断深入&#xff0c;难度也会越来越大&#xff0c;但不用担心。接下来只需要让自己的脚步慢一些&#xff0c;认真搞…

Python入门知识点分享——(十五)自定义函数

函数是一段事先组织好可重复使用的代码块&#xff0c;用于执行特定的任务。函数可以接受输入参数&#xff0c;并返回一个结果&#xff0c;从而提高应用的模块性和代码的重复利用率。先前我们已经介绍了Python中的内置函数&#xff0c;现在我们要学习创建自定义函数&#xff0c;…

深度学习烦人的基础知识(2)---Nvidia-smi功率低,util高---nvidia_smi参数详解

文章目录 问题现象解释解决方案 磨刀不误砍柴工--nvidia-smi参数解读 问题 如下图所示&#xff0c;GPU功率很低&#xff0c;Util占用率高。这个训练时不正常的&#xff01; 现象解释 Pwr是指GPU运行时耗电情况&#xff0c;如图中GPU满载是300W&#xff0c;目前是86W与GPU2的…

Springboot Jackson 序列化与反序列化配置

可解决在使用默认反序列化Jackson时&#xff0c;LocalDateTime类型的请求参数反序列化失败的问题 import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonProcessingException; import com…

JavaScript 异步编程解决方案-中篇

天下事有难易乎&#xff1f; 为之&#xff0c;则难者亦易矣&#xff1b;不为&#xff0c; 则易者亦难矣。人之为学有难易乎&#xff1f; 学之&#xff0c;则难者亦易矣&#xff1b;不学&#xff0c;则易者亦难矣。 async 函数 和promise then的规则一样 async function fun() …

SpringMVC(六)RESTful

1.RESTful简介 REST:Representational State Transfer,表现层资源状态转移 (1)资源 资源是一种看待服务器的方式,即,将服务器看作是由很多离散的资源组成。每个资源是服务器上一个可命名的抽象概念。因为资源是一个抽象的概念,所以它不仅仅能代表服务器文件系统中的一个文件…

Apache Answer,最好的开源问答系统

Apache Answer是一款适合任何团队的问答平台软件。无论是社区论坛、帮助中心还是知识管理平台&#xff0c;你可以永远信赖 Answer。 目前该项目在github超过10K星&#xff0c;系统采用go语言开发&#xff0c;安装配置简单&#xff0c;界面清洁易用&#xff0c;且开源免费。项目…

Spring Boot - Application Events 的发布顺序_ApplicationFailedEvent

文章目录 Pre概述Code源码分析 Pre Spring Boot - Application Events 的发布顺序_ApplicationEnvironmentPreparedEvent 概述 Spring Boot 的广播机制是基于观察者模式实现的&#xff0c;它允许在 Spring 应用程序中发布和监听事件。这种机制的主要目的是为了实现解耦&#…

go中常见的错误-以及泛型

https://github.com/teivah/100-go-mistakes#table-of-contents nil Map map记得要make初始化&#xff0c; slice可以不用初始化&#xff01; func main() { //assignment to nil map var course map[string]string //如果不初始化&#xff0c;就会为nilcourse["name&quo…

开源云原生安全的现状

近年来&#xff0c;人们非常重视软件供应链的安全。尤其令人担忧的是开源软件发行版中固有的风险越来越多。这引发了围绕云原生开源安全的大量开发&#xff0c;其形式包括软件物料清单 (SBOM)、旨在验证 OSS 包来源的项目等。 许多组织循环使用大型开源包&#xff0c;但只使用…

openGauss学习笔记-196 openGauss 数据库运维-常见故障定位案例-强制结束指定的问题会话

文章目录 openGauss学习笔记-196 openGauss 数据库运维-常见故障定位案例-强制结束指定的问题会话196.1 强制结束指定的问题会话196.1.1 问题现象196.1.2 处理办法 openGauss学习笔记-196 openGauss 数据库运维-常见故障定位案例-强制结束指定的问题会话 196.1 强制结束指定的…

HTML--表单

睡不着就看书之------------------------ 表单 作用&#xff1a;嗯~~动态页面需要借助表单实现 表单标签&#xff1a; 主要分五种&#xff1a; form&#xff0c;input&#xff0c;textarea&#xff0c;select&#xff0c;option 从外观来看&#xff0c;表单就包含以下几种&…

SFP/SFP+/QSFP/QSFP+光模块和GTP/GTX/GTH/GTZ/GTY/GTM高速收发器

SFP/SFP/QSFP/QSFP光模块和GTP/GTX/GTH/GTZ/GTY/GTM高速收发器 SFP/SFP/QSFP/QSFP光模块概述SFPSFPQSFPQSFP关键参数说明 GTP/GTX/GTH/GTZ/GTY/GTM高速收发器区别XILINX 7系列FPGA中高速收发器使用 SFP/SFP/QSFP/QSFP光模块 概述 SFP&#xff08; small form-factor pluggabl…

第 3 场 小白入门赛(1~6) + 第 3 场 强者挑战赛 (1 ~ 5)

第 3 场 小白入门赛 1、厉不厉害你坤哥&#xff08;暴力&#xff09; 2、思维 3、暴力&#xff0c;前缀和&#xff0c;贪心 4、二分 5、DP 6、容斥&#xff0c;双指针 第 3 场 强者挑战赛 2、BFS 5、树上倍增求第k祖先 1. 召唤神坤 题意&#xff1a; 可以发现,如果我…