Shark.cpp
branchv_0
changeset 11 0aeedc35ebed
parent 10 8382173bfc35
child 12 076e3b2d97ac
--- a/Shark.cpp	Sat Dec 02 21:25:02 2023 +0100
+++ b/Shark.cpp	Sat Dec 02 23:11:56 2023 +0100
@@ -198,6 +198,7 @@
 	std::shared_ptr<Program> loadShaders();
 	bool reloadShader(const std::string& fileName);
 	void setTitle(const std::string& suffix = "");
+	static const std::string getDefaultFile(const std::string& relativePath);
 
 };
 
@@ -536,6 +537,19 @@
 	}
 }
 
+const std::string
+Shark::Shark::Impl::getDefaultFile(const std::string& relativePath) {
+	const char* envName = "SHADER_SHARK_DATA_DIR";
+	const char* envValue = ::getenv(envName);
+	if (envValue) {
+		return std::string(envValue) + "/" + relativePath;
+	} else {
+		throw std::invalid_argument(std::string("Configure $") + envName
+				+ " in order to use defaults"
+				" or specify textures and shaders as parameters");
+	}
+}
+
 Shark::Impl::Texture Shark::Impl::loadTexture(const std::string& fileName) {
 	Texture tex;
 	tex.fileName = fileName;
@@ -565,6 +579,10 @@
 }
 
 void Shark::Impl::loadTextures() {
+	// Load default texture if there is no configured:
+	if (cfg.textures.empty())
+		cfg.textures.push_back({getDefaultFile("textures/default.png")});
+
 	for (const Configuration::Texture& tex : cfg.textures) {
 		textures.push_back(loadTexture(tex.fileName));
 		watchedFiles.push_back(fileMonitor.watch(tex.fileName));
@@ -601,10 +619,16 @@
 		glGenBuffers(1, &vbo);
 		glBindBuffer(GL_ARRAY_BUFFER, vbo);
 
-		if (cfg.shaders.empty()) {
-			// TODO: configurable absolute path or embedded defaults
-			cfg.shaders.push_back({"shaders/first.vert", "vertex"});
-			cfg.shaders.push_back({"shaders/first.frag", "fragment"});
+		{
+			// Load default shaders if there are no configured:
+			int vc = 0;
+			int fc = 0;
+			auto& ss = cfg.shaders;
+			for (const auto& s : ss) if (s.type == "vertex") vc++;
+			for (const auto& s : ss) if (s.type == "fragment") fc++;
+			auto& d = getDefaultFile;
+			if (vc == 0) ss.push_back({d("shaders/default.vert"), "vertex"});
+			if (fc == 0) ss.push_back({d("shaders/default.frag"), "fragment"});
 		}
 
 		std::shared_ptr<Program> program = std::make_shared<Program>();