From 0e7d394af479f8da70d1efa3a5261bedf0cb19df Mon Sep 17 00:00:00 2001
From: Vincent Tavernier <vincent.tavernier@inria.fr>
Date: Thu, 8 Feb 2018 13:07:22 +0100
Subject: [PATCH] Add default parameter to GetBufferByName

---
 debian/changelog                    |  6 ++++++
 examples/50-shadertoy/main.cpp      | 28 +++++++++++-----------------
 include/shadertoy/RenderContext.hpp |  5 +++--
 src/RenderContext.cpp               | 20 ++++++++++++++++----
 4 files changed, 36 insertions(+), 23 deletions(-)

diff --git a/debian/changelog b/debian/changelog
index fc9d8b1..f6d46e1 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -1,3 +1,9 @@
+libshadertoy (0.1.7) unstable; urgency=medium
+
+  * Add default parameter for buffer query from context
+
+ -- Vincent Tavernier <vincent.tavernier@inria.fr>  Thu, 08 Feb 2018 13:15:00 +0200
+
 libshadertoy (0.1.6) unstable; urgency=medium
 
   * Add fragment shader execution time query support (iTimeDelta)
diff --git a/examples/50-shadertoy/main.cpp b/examples/50-shadertoy/main.cpp
index dd5a519..98a94b7 100644
--- a/examples/50-shadertoy/main.cpp
+++ b/examples/50-shadertoy/main.cpp
@@ -127,7 +127,7 @@ int parseOptions(string &shaderId, string &shaderApiKey, bool &dump, int argc, c
 }
 
 int loadRemote(const string &shaderId, const string &shaderApiKey,
-				shadertoy::ContextConfig &contextConfig, string &imageBufferName)
+				shadertoy::ContextConfig &contextConfig)
 {
 	// Init CURL
 	curl_global_init(CURL_GLOBAL_DEFAULT);
@@ -281,9 +281,6 @@ int loadRemote(const string &shaderId, const string &shaderApiKey,
 		pair<string, shadertoy::BufferConfig> imagebuf(*contextConfig.bufferConfigs.begin());
 		contextConfig.bufferConfigs.erase(contextConfig.bufferConfigs.begin());
 		contextConfig.bufferConfigs.insert(imagebuf);
-
-		// Fetch image buffer name
-		imageBufferName = imagebuf.first;
 	}
 	catch (exception &ex)
 	{
@@ -298,9 +295,7 @@ int loadRemote(const string &shaderId, const string &shaderApiKey,
 	return code;
 }
 
-int render(GLFWwindow* window,
-	shadertoy::ContextConfig &contextConfig,
-	const string &imageBufferName)
+int render(GLFWwindow* window, shadertoy::ContextConfig &contextConfig)
 {
 	int code = 0;
 
@@ -388,15 +383,15 @@ int render(GLFWwindow* window,
 			context.RenderScreenQuad();
 
 			// Print execution time
-			auto buffer = context.GetBufferByName(imageBufferName);
+			auto buffer = context.GetBufferByName();
 
 			if (buffer)
 			{
 				auto renderTime = buffer->GetElapsedTime();
-				std:cerr << imageBufferName << " time: " << renderTime
-						 << "ns fps: " << (1e9 / renderTime)
-						 << " mpx/s: " << (contextConfig.width * contextConfig.height / (renderTime / 1e3))
-						 << std::endl;
+				std::cerr << "frame time: " << renderTime
+						  << "ns fps: " << (1e9 / renderTime)
+						  << " mpx/s: " << (contextConfig.width * contextConfig.height / (renderTime / 1e3))
+						  << std::endl;
 			}
 
 			// Buffer swapping
@@ -410,7 +405,7 @@ int render(GLFWwindow* window,
 	return code;
 }
 
-int performRender(shadertoy::ContextConfig &contextConfig, const string &imageBufferName)
+int performRender(shadertoy::ContextConfig &contextConfig)
 {
 	int code = 0;
 
@@ -437,7 +432,7 @@ int performRender(shadertoy::ContextConfig &contextConfig, const string &imageBu
 	glfwMakeContextCurrent(window);
 	glfwSwapInterval(1);
 
-	code = render(window, contextConfig, imageBufferName);
+	code = render(window, contextConfig);
 
 	glfwDestroyWindow(window);
 	glfwTerminate();
@@ -465,13 +460,12 @@ int main(int argc, char *argv[])
 	contextConfig.dumpShaders = dumpShaders;
 
 	// Fetch shader code
-	string imageBufferName;
-	code = loadRemote(shaderId, shaderApiKey, contextConfig, imageBufferName);
+	code = loadRemote(shaderId, shaderApiKey, contextConfig);
 	if (code > 0)
 		return code;
 
 	// Render
-	code = performRender(contextConfig, imageBufferName);
+	code = performRender(contextConfig);
 
 	return code;
 }
diff --git a/include/shadertoy/RenderContext.hpp b/include/shadertoy/RenderContext.hpp
index b7e79e5..92e889c 100644
--- a/include/shadertoy/RenderContext.hpp
+++ b/include/shadertoy/RenderContext.hpp
@@ -256,11 +256,12 @@ public:
 	/**
 	 * Obtains the buffer object for the given name.
 	 *
-	 * @param  name Name of the buffer object to obtain
+	 * @param  name [optional] Name of the buffer object to obtain. If empty,
+	 *              returns the image buffer.
 	 * @return      Pointer to the buffer object, or a null pointer if no such
 	 *              buffer exists.
 	 */
-	std::shared_ptr<ToyBuffer> GetBufferByName(const std::string &name);
+	std::shared_ptr<ToyBuffer> GetBufferByName(const std::string &name = std::string());
 };
 
 }
diff --git a/src/RenderContext.cpp b/src/RenderContext.cpp
index a637f73..9099fff 100644
--- a/src/RenderContext.cpp
+++ b/src/RenderContext.cpp
@@ -398,11 +398,23 @@ OpenGL::Shader &RenderContext::GetScreenQuadVertexShader()
 
 shared_ptr<ToyBuffer> RenderContext::GetBufferByName(const string &name)
 {
-	auto it = buffers.find(name);
-	if (it == buffers.end())
+	if (name.empty())
 	{
-		return shared_ptr<ToyBuffer>();
+		if (buffers.empty())
+		{
+			return shared_ptr<ToyBuffer>();
+		}
+
+		return buffers.rbegin()->second;
 	}
+	else
+	{
+		auto it = buffers.find(name);
+		if (it == buffers.end())
+		{
+			return shared_ptr<ToyBuffer>();
+		}
 
-	return it->second;
+		return it->second;
+	}
 }
-- 
GitLab