跟着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 1
		m_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"
	}
	-- 引用hazel
	links{
		"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"
	}
	-- 引用YOTOEngine
	links{
		"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.mfbz.cn/a/318633.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

【软件测试】学习笔记-不同视角的软件性能与性能指标

本篇文章探讨新的测试主题&#xff1a;性能测试&#xff0c;因为性能测试的专业性很强&#xff0c;所以我会以从0到1的入门者视角&#xff0c;系统性地阐述性能测试的方法以及应用领域&#xff0c;用实例去诠释各种性能指标。 本篇文章站在全局的视角&#xff0c;帮你梳理软件性…

【期末不挂科-C++考前速过系列P6】大二C++实验作业-模板(4道代码题)【解析,注释】

前言 大家好吖&#xff0c;欢迎来到 YY 滴C考前速过系列 &#xff0c;热烈欢迎&#xff01; 本章主要内容面向接触过C的老铁 主要内容含&#xff1a; 欢迎订阅 YY滴C专栏&#xff01;更多干货持续更新&#xff01;以下是传送门&#xff01; YY的《C》专栏YY的《C11》专栏YY的《…

统计学-R语言-4.2

文章目录 前言单变量数据的描述分析分类型数据频数表条形图饼图 数值型数据数值型数据数据的集中趋势--均值数据的集中趋势--众数 离散程度离散程度--极差离散程度--四分位数极差离散程度--方差离散程度--加权方差离散程度--标准差离散程度--变异系数 数据的形状数据的形状--偏…

RAG(检索增强生成 )

&#x1f4d1;前言 本文主要是【RAG】——RAG(检索增强生成 )的文章&#xff0c;如果有什么需要改进的地方还请大佬指出⛺️ &#x1f3ac;作者简介&#xff1a;大家好&#xff0c;我是听风与他&#x1f947; ☁️博客首页&#xff1a;CSDN主页听风与他 &#x1f304;每日一句…

最新消息:OpenAI GPT Store 正式上线,GPTs 应用商店来了

OpenAI推出的两款新产品和服务&#xff1a;GPT Store和ChatGPT Team&#xff0c;提供了许多全新的解决方案和功能&#xff0c;旨在帮助用户更轻松地使用和构建GPT工具&#xff0c;同时也增加了公司的收入来源。GPT Store是一个全新的在线平台&#xff0c;允许用户创建和分享自定…

小程序基础学习(多插槽)

先创建插槽 定义多插槽的每一个插槽的属性 在js文件中启用多插槽 在页面使用多插槽 组件代码 <!--components/my-slots/my-slots.wxml--><view class"container"><view class"left"> <slot name"left" ></slot>&…

【DC快速上手教程--1 Setup the DC】

DC快速上手教程--1 Setup the DC 0 Intro1 DC Demo 本篇系列教程介绍总结DC Flow&#xff0c;为了不涉密&#xff0c;在这里以DC labs为Demo做一个入门的介绍&#xff1b;目标&#xff1a;用起来EDA 工具是最基础也是最简单的&#xff1b;重点是如何去分析报告&#xff0c;依据…

Jenkins集成Sonar Qube

下载插件 重启Jenkins 容器 sonarqube 使用令牌 Jenkins 配置 重新构建

Windows平台RTMP推送|轻量级RTSP服务录像模块如何支持中文路径?

技术背景 我们在做Windows平台RTMP推送、轻量级RTSP服务录像模块的时候&#xff0c;部分开发者抱怨路径无法设置中文&#xff0c;只能设置为英文。 以C#的接口为例&#xff0c;早期的设计如下&#xff1a; /** 设置本地录像目录, 必须是英文目录&#xff0c;否则会失败*/[DllI…

YOLOv5改进 | 注意力篇 | CGAttention实现级联群体注意力机制 (全网首发改进)

一、本文介绍 本文给大家带来的改进机制是实现级联群体注意力机制CascadedGroupAttention,其主要思想为增强输入到注意力头的特征的多样性。与以前的自注意力不同,它为每个头提供不同的输入分割,并跨头级联输出特征。这种方法不仅减少了多头注意力中的计算冗余,而且通过增…

Spring | Spring框架最基本核心的jar包、Spring的入门程序、依赖注入

目录&#xff1a; 1.Spring框架最基本、最核心的jar包2.Spring的入门程序3.依赖注入3.1 依赖注入的概念3.2 依赖注入的实现方式 1.Spring框架最基本、最核心的jar包 Spring是一个轻量级框架&#xff0c;Spring最基本、最核心的的jar包括 : beans、context、core、expression。 …

Dobbo---分布式系统通信方式

通信方式 分布式系统通信方式1. RMIRMI 通信实现案例2. RPC常用RPC框架 分布式系统通信方式 1. RMI RMI ( Remote Method Invocation 远程方法调用) 图1.1 客户端-服务端通信方式 客户端将要调用的方法及参数&#xff0c;打包为辅助对象&#xff0c;通过网络socket&#xff…

AI-图片转换中国风动漫人物

&#x1f3e1; 个人主页&#xff1a;IT贫道-CSDN博客 &#x1f6a9; 私聊博主&#xff1a;私聊博主加WX好友&#xff0c;获取更多资料哦~ &#x1f514; 博主个人B栈地址&#xff1a;豹哥教你学编程的个人空间-豹哥教你学编程个人主页-哔哩哔哩视频 目录 1. AI卡通秀原理 2. …

Android WiFi Service启动-Android13

Android WiFi Service启动 - Android13 1、SystemServer中入口2、WifiService启动2.1 关键类概要2.2 启动时序图 Android WiFi基础概览 AOSP > 文档 > 心主题 > WiFi概览 1、SystemServer中入口 编译生成对应的jar包&#xff1a;"/apex/com.android.wifi/javalib…

Sonar Qube基本使用

中文化 Sonar Qube的使用方式很多&#xff0c;Maven可以整合&#xff0c;也可以采用sonar-scanner的方式&#xff0c;再查看Sonar Qube的检测效果 Sonar-scanner实现代码检测 下载Sonar-scanner&#xff1a;https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/…

Qt/C++编写视频监控系统83-自定义悬浮条信息

一、前言 一般视频控件上会给出个悬浮条&#xff0c;这个悬浮条用于显示分辨率或者一些用户期望看到的信息&#xff0c;一般常用的信息除了分辨率以外&#xff0c;还有帧率、封装格式、视频解码器名称、音频解码器名称、实时码率等&#xff0c;由于实际的场景不一样&#xff0…

(南京观海微电子)——色调介绍

色温可以把她理解为宏观上的一种大的环境。你拿起一张图片&#xff0c;整体感觉上在光线分布上颜色是饱满温和还是单调冷艳&#xff0c;从字面意思理解可以简单理解为给人的一种整体印象。包括温暖 白色 清凉或者说冷。 色调就是说在整体环境下色彩的浓淡分配方面的定义。 色调…

通信入门系列——微积分中极限、连续、导数、微分、积分

本节目录 一、极限 1、数列极限 2、函数极限 二、连续 三、导数 四、微分 五、积分本节内容 一、极限 1、数列极限 数列极限&#xff1a;设{xn}为一个实数列&#xff0c;A为一个定数。若对任意给定的ε>0&#xff0c;总存在正整数N,使得当n>N时&#xff0c;有|xn-A|<…

蓝桥杯AcWing学习笔记 8-1数论的学习(上)

蓝桥杯 我的AcWing 题目及图片来自蓝桥杯C AB组辅导课 数论&#xff08;上&#xff09; 蓝桥杯省赛中考的数论不是很多&#xff0c;这里讲几个蓝桥杯常考的知识点。 欧几里得算法——辗转相除法 欧几里得算法代码&#xff1a; import java.util.Scanner ;public class Main…

小程序基础学习(js混编)

在组件中使用外部js代码实现数据改变 先创建js文件 编写一些组件代码 编写外部js代码 在组件的js中引入外部js 在 app.json中添加路径规则 组件代码 <!--components/my-behavior/my-behavior.wxml--> <view><view>当前计数为{{count}}</view> <v…
最新文章