Shark.cpp
branchv_0
changeset 19 262828ae9682
parent 15 1eb7cfefbeea
child 20 0899e966993e
--- a/Shark.cpp	Sun Dec 03 19:22:42 2023 +0100
+++ b/Shark.cpp	Mon Dec 04 21:03:53 2023 +0100
@@ -29,6 +29,7 @@
 #include "Logger.h"
 #include "MappedFile.h"
 #include "ImageLoader.h"
+#include "Texture.h"
 #include "Shader.h"
 #include "Program.h"
 #include "FileMonitor.h"
@@ -155,18 +156,6 @@
 
 	} initialCtx, ctx;
 
-	class Texture {
-	public:
-		GLuint id;
-		std::string fileName;
-		int width;
-		int height;
-
-		GLfloat getRatio() const {
-			return (GLfloat) width / (GLfloat) height;
-		}
-	};
-
 	Display* dpy;
 	Window win;
 	XVisualInfo* vi;
@@ -177,7 +166,7 @@
 	ImageLoader imageLoader;
 	std::vector<std::shared_ptr<Shader>> shaders;
 	std::shared_ptr<Program> shaderProgram;
-	std::vector<Texture> textures;
+	std::vector<std::shared_ptr<Texture>> textures;
 
 	Configuration cfg;
 	std::ostream& logOutput = std::cerr;
@@ -192,8 +181,6 @@
 	void log(LogLevel level, std::string message);
 	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();
@@ -208,11 +195,10 @@
 }
 
 Shark::~Shark() {
-	// TODO: more SBRM
+	impl->textures.clear();
 	impl->shaders.clear();
 	impl->shaderProgram = nullptr;
 	XFree(impl->vi);
-	// for (auto page : pdfTextures) glDeleteTextures(1, &page.texture);
 	glXMakeCurrent(impl->dpy, None, NULL);
 	glXDestroyContext(impl->dpy, impl->glc);
 	XDestroyWindow(impl->dpy, impl->win);
@@ -498,8 +484,8 @@
 
 void Shark::Impl::loadVertices() {
 	for (int i = 0; i < textures.size(); i++) {
-		const Texture& tex = textures[i];
-		GLfloat ratio = tex.getRatio();
+		std::shared_ptr<Texture> tex = textures[i];
+		GLfloat ratio = tex->getRatio();
 		const std::vector<GLfloat> vertices = {
 			// Vertex XYZ                      Texture XY
 			-0.80f * ratio, +0.80f, +0.0, /**/ 0.0, 0.0,
@@ -543,7 +529,7 @@
 }
 
 const std::string
-Shark::Shark::Impl::getDefaultFile(const std::string& relativePath) {
+Shark::Impl::getDefaultFile(const std::string& relativePath) {
 	const char* envName = "SHADER_SHARK_DATA_DIR";
 	const char* envValue = ::getenv(envName);
 	if (envValue) {
@@ -555,44 +541,16 @@
 	}
 }
 
-Shark::Impl::Texture Shark::Impl::loadTexture(const std::string& fileName) {
-	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;
-	tex.height = img->height;
-
-	glGenTextures(1, &tex.id);
-	glBindTexture(GL_TEXTURE_2D, tex.id);
-	auto GLT2D = GL_TEXTURE_2D;
-	glTexImage2D(GLT2D, 0, GL_RGBA,
-			tex.width, tex.height,
-			0, GL_RGBA, GL_UNSIGNED_BYTE,
-			img->getData());
-	glTexParameteri(GLT2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
-	glTexParameteri(GLT2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
-	glTexParameteri(GLT2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
-	glTexParameteri(GLT2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
-	glGenerateMipmap(GLT2D);
-	std::cerr << "loadTexture(\"" << tex.fileName.c_str() << "\", "
-			<< tex.width << ", " << tex.height << ") = " << tex.id << std::endl;
-	checkError(&std::cerr);
-}
-
 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));
+		std::shared_ptr<ImageLoader::ImageBuffer>
+				img(imageLoader.loadImage(MappedFile(tex.fileName)));
+		textures.push_back(std::make_shared<Texture>(
+				img->width, img->height, *img, tex.fileName));
 		watchedFiles.push_back(fileMonitor.watch(tex.fileName));
 		// TODO: review texture loading and binding
 		// works even without this - default texture
@@ -602,9 +560,11 @@
 }
 
 bool Shark::Impl::reloadTexture(const std::string& fileName) {
-	for (Texture& tex : textures) {
-		if (tex.fileName == fileName) {
-			updateTexture(tex, MappedFile(fileName));
+	for (std::shared_ptr<Texture> tex : textures) {
+		if (tex->getFileName() == fileName) {
+			std::shared_ptr<ImageLoader::ImageBuffer>
+					img(imageLoader.loadImage(MappedFile(fileName)));
+			tex->update(img->width, img->height, *img);
 			loadVertices();
 			return true;
 		}