• 【Cherno的OpenGL视频】Creating tests in OpenGL


    Last time we talked about creating this whole test framework and test classes and all that, we had sth where we could actually create test classes which extended like kind of a base test class, and we could demonstrate each individual OpenGL or feature or whatever we want to really do with OpenGL in kind of a sandbox test environment that was really clear for us to demonstrate and have samples of the various pages that we learned about in OpenGL.
    Today we’re gonna be extending that, how we could create some test menu where we could click on a test, and it would construct a new test, and then when we went back to the main menu, it would delete that test and unload all the resources and present us with that test menu again?

    1、Why didn’t make these Test methods pure virtual functions?

    The reason we left them as just virtual function and implemented them with a blank body like that code below is that we could selectively choose which ones we actually want to implement ourselves, if there were equals 0 if they were all pure virtual we’d have to have all these 3 methods in TestMenu class, but with making them as virtual function we could just go ahead and delete some of the functions that we don’t need, like this:

    	class Test
    	{
    	public:
    		Test() {}
    		virtual ~Test() {}
    
    		virtual void OnUpdate(float deltatime) {}
    		// virtual void OnUpdate(float deltatime) = 0; // pure virtual.
    		virtual void OnRender() {}
    		virtual void OnImGuiRender() {}
    	};
    
    	class TestMenu : public Test
    	{
    	public:
    
    		TestMenu();
    		~TestMenu() {}
    		void OnImGuiRender() override;
    	};
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    and everything will still work fine, we don’t have to implement them, it’s just an option that’s given to us.

    2、代码实现

    在原有的Test.h文件基础上,增加一个新类:TestMenu
    The TestMenu class is going to do 2 things: it’s going to contain a collection of all of our tests and we want a way to be able to basically present them with a specific name, so we need a pointer to a test class, a test object instance, but we also want to store a name that we can provide the test like a label, it would appears for our button in the test menu. Another thing is: a pointer to our current test, the reason we need this is our menu is going to change the current test is active.
    Test.h代码:

    #pragma once
    #include 
    #include 
    #include 
    #include 
    namespace test
    {
    	class Test
    	{
    	public:
    		Test() {}
    		virtual ~Test() {}
    
    		virtual void OnUpdate(float deltatime) {}
    		virtual void OnRender() {}
    		virtual void OnImGuiRender() {}
    	};
    
    	class TestMenu : public Test
    	{
    	public:
    
    		TestMenu(Test*& currentTestPointer);
    
    		void OnImGuiRender() override;
    
    		template<typename T>
    		void RegisterTest(const std::string& name)
    		{
    			std::cout << "Registering test " << name << std::endl;
    			m_Tests.push_back(std::make_pair(name, []() { return new T(); }));
    		}
    
    	private:
    		// Mark it as reference so that the TestMenu is able to write to this pointer and thus change the current test.
    		Test*& m_CurrentTest;
    
    		// Instead of providing a Test pointer for the second element of our pair, we're gonna provide it with a lambda, 
    		// a function that constructs that Test class of ours.(this function returns a Test pointer and doesn't take 
    		// any parameters)
    		std::vector< std::pair<std::string, std::function<Test*()>> > m_Tests;
    	};
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43

    如图:src/tests下增加Test.cpp文件。
    在这里插入图片描述
    回到Application.cpp,所做的修改如图:
    在这里插入图片描述

    3、运行效果

    测试1
    hit F5
    在这里插入图片描述
    点击按钮“Clear Color”
    在这里插入图片描述
    可以修改背景颜色
    在这里插入图片描述
    点击按钮“<-”
    在这里插入图片描述
    点击“关闭”
    在这里插入图片描述
    程序关闭正常
    测试2
    再次hit F5
    在这里插入图片描述
    点击按钮“Clear Color”
    在这里插入图片描述
    点击关闭
    在这里插入图片描述
    程序关闭正常

  • 相关阅读:
    常用的linux命令简要说明以及命令全名理解
    2023.10.19 关于设计模式 —— 单例模式
    攻防世界-T1 Training-WWW-Robots
    IP地址冲突解决办法
    [附源码]计算机毕业设计养生药膳推荐系统Springboot程序
    探索ChatGPT在学术写作中的应用与心得
    简单但现代的服务器仪表板Dashdot
    Java 新手入门:基础知识点一览
    让 K8s 更简单!8款你不得不知的 AI 工具-Part 1
    SQL面试题之按照指定顺序进行多行转一列
  • 原文地址:https://blog.csdn.net/AlexiaDong/article/details/126679690