【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;
	};

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 <functional>
#include <vector>
#include <string>
#include <iostream>
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;
	};
}

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

3、运行效果

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值