monitor texture and shader file writes using inotify: reload textures v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sun, 03 Dec 2023 00:17:48 +0100
branchv_0
changeset 12 076e3b2d97ac
parent 11 0aeedc35ebed
child 13 82bb4fe2fd4a
monitor texture and shader file writes using inotify: reload textures
Shark.cpp
--- a/Shark.cpp	Sat Dec 02 23:11:56 2023 +0100
+++ b/Shark.cpp	Sun Dec 03 00:17:48 2023 +0100
@@ -193,6 +193,7 @@
 	int setNonBlocking(int fd);
 	void loadVertices();
 	Texture loadTexture(const std::string& fileName);
+	void updateTexture(Texture& tex, const Buffer& file);
 	bool reloadTexture(const std::string& fileName);
 	void loadTextures();
 	std::shared_ptr<Program> loadShaders();
@@ -554,7 +555,11 @@
 	Texture tex;
 	tex.fileName = fileName;
 	MappedFile file(tex.fileName);
+	updateTexture(tex, file);
+	return tex;
+}
 
+void Shark::Impl::updateTexture(Texture& tex, const Buffer& file) {
 	std::shared_ptr<ImageLoader::ImageBuffer> img(imageLoader.loadImage(file));
 
 	tex.width = img->width;
@@ -572,10 +577,9 @@
 	glTexParameteri(GLT2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
 	glTexParameteri(GLT2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
 	glGenerateMipmap(GLT2D);
-	std::cerr << "loadTexture(\"" << fileName.c_str() << "\", "
+	std::cerr << "loadTexture(\"" << tex.fileName.c_str() << "\", "
 			<< tex.width << ", " << tex.height << ") = " << tex.id << std::endl;
 	checkError(&std::cerr);
-	return tex;
 }
 
 void Shark::Impl::loadTextures() {
@@ -594,9 +598,10 @@
 }
 
 bool Shark::Impl::reloadTexture(const std::string& fileName) {
-	for (const Configuration::Texture& tex : cfg.textures) {
+	for (Texture& tex : textures) {
 		if (tex.fileName == fileName) {
-			logOutput << "TODO: reload texture: " << fileName.c_str() << "\n";
+			updateTexture(tex, MappedFile(fileName));
+			loadVertices();
 			return true;
 		}
 	}