private Impl class also for the Shark v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sat, 02 Dec 2023 21:25:02 +0100
branchv_0
changeset 10 8382173bfc35
parent 9 53ba7e52c67c
child 11 0aeedc35ebed
private Impl class also for the Shark
FileMonitor.h
Program.h
Shader.h
Shark.cpp
Shark.h
--- a/FileMonitor.h	Sat Dec 02 19:33:31 2023 +0100
+++ b/FileMonitor.h	Sat Dec 02 21:25:02 2023 +0100
@@ -24,13 +24,13 @@
 
 class WatchedFile {
 public:
-	class Impl;
 	WatchedFile();
 	virtual ~WatchedFile();
 	std::string getFileName() const;
 	uint32_t getMask() const;
 	FileMonitor getFileMonitor() const;
 private:
+	class Impl;
 	std::shared_ptr<Impl> impl;
 	friend FileMonitor;
 };
@@ -44,7 +44,6 @@
 
 class FileMonitor {
 public:
-	class Impl;
 	FileMonitor();
 	virtual ~FileMonitor();
 	/**
@@ -64,6 +63,7 @@
 	 */
 	bool readEvent(FileEvent& event);
 private:
+	class Impl;
 	std::shared_ptr<Impl> impl;
 	FileMonitor(std::shared_ptr<Impl> impl);
 	friend WatchedFile;
--- a/Program.h	Sat Dec 02 19:33:31 2023 +0100
+++ b/Program.h	Sat Dec 02 21:25:02 2023 +0100
@@ -22,7 +22,6 @@
 
 class Program {
 public:
-	class Impl;
 	Program();
 	virtual ~Program();
 	void attachShader(const Shader& shader);
@@ -34,6 +33,7 @@
 	GLint getFragDataLocation(const std::string& name);
 	void bindFragDataLocation(const std::string& name, const GLint color);
 private:
+	class Impl;
 	Impl* impl;
 	Program(const Program&) = delete;
 	Program& operator=(const Program&) = delete;
--- a/Shader.h	Sat Dec 02 19:33:31 2023 +0100
+++ b/Shader.h	Sat Dec 02 21:25:02 2023 +0100
@@ -26,7 +26,6 @@
 
 class Shader {
 public:
-	class Impl;
 
 	enum class Type : GLenum {
 		FRAGMENT = GL_FRAGMENT_SHADER,
@@ -39,6 +38,7 @@
 	const std::string getFileName() const;
 	void update(const Buffer& source);
 private:
+	class Impl;
 	Impl* impl;
 	Shader(const Shader&) = delete;
 	Shader& operator=(const Shader&) = delete;
--- a/Shark.cpp	Sat Dec 02 19:33:31 2023 +0100
+++ b/Shark.cpp	Sat Dec 02 21:25:02 2023 +0100
@@ -15,28 +15,211 @@
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include <iostream>
+#include <iomanip>
+#include <string>
 #include <memory>
+#include <functional>
+#include <sstream>
+#include <vector>
+
+#include "x11.h"
+#include "opengl.h"
+#include "EPoll.h"
+#include "Logger.h"
+#include "MappedFile.h"
+#include "ImageLoader.h"
+#include "Shader.h"
+#include "Program.h"
+#include "FileMonitor.h"
 
 #include "Shark.h"
 
+class Shark::Impl {
+public:
+
+	struct {
+		GLint aVertexXYZ = -2;
+		GLint aTextureXY = -2;
+
+		GLint fColor = -2;
+
+		GLint uModel = -2;
+		GLint uView = -2;
+		GLint uProjection = -2;
+		GLint uTexture = -2;
+	} ProgAttr;
+
+	struct {
+		float yaw = -90.f;
+		float pitch = 0.f;
+		float roll = 0.f;
+		float fov = 45.0f;
+		glm::vec3 cameraPos = glm::vec3(0.0f, 0.0f, 3.0f);
+		glm::vec3 cameraFront = glm::vec3(0.0f, 0.0f, -1.0f);
+		glm::vec3 cameraUp = glm::vec3(0.0f, 1.0f, 0.0f);
+
+		void adjustFov(float diff) {
+			fov += diff;
+			if (fov < 1.0f) fov = 1.0f;
+			else if (fov > 120.0f) fov = 120.0f;
+			std::cerr << "field of view: " << fov << " °" << std::endl;
+		}
+
+		void moveForward(const float cameraSpeed) {
+			cameraPos += cameraSpeed * cameraFront;
+		}
+
+		void moveBackward(const float cameraSpeed) {
+			cameraPos -= cameraSpeed * cameraFront;
+		}
+
+		void moveLeft(const float cameraSpeed) {
+			cameraPos -= glm::normalize(
+					glm::cross(cameraFront, cameraUp)) * cameraSpeed;
+		}
+
+		void moveRight(const float cameraSpeed) {
+			cameraPos += glm::normalize(
+					glm::cross(cameraFront, cameraUp)) * cameraSpeed;
+		}
+
+		void moveUp(const float cameraSpeed) {
+			cameraPos += cameraSpeed * glm::normalize(cameraUp);
+		}
+
+		void moveDown(const float cameraSpeed) {
+			cameraPos -= cameraSpeed * glm::normalize(cameraUp);
+		}
+
+		void updateCameraFrontAndUp() {
+			std::cerr << "--- updateCameraFrontAndUp() --------" << std::endl;
+			dump("pitch, yaw, roll", glm::vec3(pitch, yaw, roll));
+			dump("cameraPos", cameraPos);
+			dump("cameraFront", cameraFront);
+			const auto pitchR = glm::radians(pitch); // around X axis
+			const auto yawR = glm::radians(yaw); //     around Y axis
+			const auto rollR = glm::radians(roll); //   around Z axis
+
+			cameraFront.x = cos(pitchR) * cos(yawR);
+			cameraFront.y = sin(pitchR);
+			cameraFront.z = cos(pitchR) * sin(yawR);
+			cameraFront = glm::normalize(cameraFront);
+			dump("cameraFront", cameraFront);
+			dump("cameraUp", cameraUp);
+
+			// TODO: review ROLL rotation and default angle
+			glm::mat4 rollMatrix = glm::rotate(
+					glm::mat4(1.0f), rollR, cameraFront);
+			cameraUp = glm::mat3(rollMatrix) * glm::vec3(0., 1., 0.);
+			dump("cameraUp", cameraUp);
+			std::cerr << "-------------------------------------" << std::endl;
+		}
+
+		void limitPitch() {
+			if (pitch > +89.0f) pitch = +89.0f;
+			if (pitch < -89.0f) pitch = -89.0f;
+		}
+
+		void turnLeft(const float angleSpeed) {
+			yaw -= angleSpeed;
+			updateCameraFrontAndUp();
+		}
+
+		void turnRight(const float angleSpeed) {
+			yaw += angleSpeed;
+			updateCameraFrontAndUp();
+		}
+
+		void turnUp(const float angleSpeed) {
+			pitch += angleSpeed;
+			limitPitch();
+			updateCameraFrontAndUp();
+		}
+
+		void turnDown(const float angleSpeed) {
+			pitch -= angleSpeed;
+			limitPitch();
+			updateCameraFrontAndUp();
+		}
+
+		void rollLeft(const float angleSpeed) {
+			roll += angleSpeed;
+			updateCameraFrontAndUp();
+		}
+
+		void rollRight(const float angleSpeed) {
+			roll -= angleSpeed;
+			updateCameraFrontAndUp();
+		}
+
+	} 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;
+	GLXContext glc;
+
+	FileMonitor fileMonitor;
+	std::vector<WatchedFile> watchedFiles;
+	ImageLoader imageLoader;
+	std::vector<std::shared_ptr<Shader>> shaders;
+	std::shared_ptr<Program> shaderProgram;
+	std::vector<Texture> textures;
+
+	Configuration cfg;
+	std::ostream& logOutput = std::cerr;
+
+	Impl(Configuration cfg) : cfg(cfg) {
+	}
+
+	void run();
+	void clear();
+	void runShaders();
+	Window getRootWindow(Window defaultValue);
+	void log(LogLevel level, std::string message);
+	int setNonBlocking(int fd);
+	void loadVertices();
+	Texture loadTexture(const std::string& fileName);
+	bool reloadTexture(const std::string& fileName);
+	void loadTextures();
+	std::shared_ptr<Program> loadShaders();
+	bool reloadShader(const std::string& fileName);
+	void setTitle(const std::string& suffix = "");
+
+};
+
 Shark::Shark(const Configuration& configuration) :
-cfg(configuration) {
+impl(new Impl(configuration)) {
 }
 
 Shark::~Shark() {
 	// TODO: more SBRM
-	shaders.clear();
-	shaderProgram = nullptr;
-	XFree(vi);
+	impl->shaders.clear();
+	impl->shaderProgram = nullptr;
+	XFree(impl->vi);
 	// for (auto page : pdfTextures) glDeleteTextures(1, &page.texture);
-	glXMakeCurrent(dpy, None, NULL);
-	glXDestroyContext(dpy, glc);
-	XDestroyWindow(dpy, win);
-	XCloseDisplay(dpy);
+	glXMakeCurrent(impl->dpy, None, NULL);
+	glXDestroyContext(impl->dpy, impl->glc);
+	XDestroyWindow(impl->dpy, impl->win);
+	XCloseDisplay(impl->dpy);
+	delete impl;
 	// std::cerr << "~Shark()" << std::endl;
 }
 
-void Shark::setTitle(const std::string& suffix) {
+void Shark::Impl::setTitle(const std::string& suffix) {
 	std::stringstream title;
 	title << "ShaderShark";
 	if (suffix.size()) title << ": " << suffix.c_str();
@@ -45,6 +228,10 @@
 }
 
 void Shark::run() {
+	impl->run();
+}
+
+void Shark::Impl::run() {
 	dpy = XOpenDisplay(NULL);
 
 	if (dpy == NULL) throw std::logic_error("Unable to connect to X server");
@@ -248,7 +435,7 @@
 	}
 }
 
-void Shark::clear() {
+void Shark::Impl::clear() {
 	glClearColor(
 			(cfg.backgroundColor >> 16 & 0xFF) / 256.,
 			(cfg.backgroundColor >> 8 & 0xFF) / 256.,
@@ -257,7 +444,7 @@
 	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 }
 
-void Shark::runShaders() {
+void Shark::Impl::runShaders() {
 	shaderProgram->use();
 	checkError(&std::cerr);
 
@@ -293,17 +480,17 @@
 	std::cerr << "GLSL: glDrawArrays()" << std::endl;
 }
 
-void Shark::log(LogLevel level, std::string message) {
+void Shark::Impl::log(LogLevel level, std::string message) {
 	::log(logOutput, level, message);
 }
 
-int Shark::setNonBlocking(int fd) {
+int Shark::Impl::setNonBlocking(int fd) {
 	int flags = fcntl(fd, F_GETFL, 0);
 	fcntl(fd, F_SETFL, flags | O_NONBLOCK);
 	return fd;
 }
 
-void Shark::loadVertices() {
+void Shark::Impl::loadVertices() {
 	for (int i = 0; i < textures.size(); i++) {
 		const Texture& tex = textures[i];
 		GLfloat ratio = tex.getRatio();
@@ -349,7 +536,7 @@
 	}
 }
 
-Shark::Texture Shark::loadTexture(const std::string& fileName) {
+Shark::Impl::Texture Shark::Impl::loadTexture(const std::string& fileName) {
 	Texture tex;
 	tex.fileName = fileName;
 	MappedFile file(tex.fileName);
@@ -377,7 +564,7 @@
 	return tex;
 }
 
-void Shark::loadTextures() {
+void Shark::Impl::loadTextures() {
 	for (const Configuration::Texture& tex : cfg.textures) {
 		textures.push_back(loadTexture(tex.fileName));
 		watchedFiles.push_back(fileMonitor.watch(tex.fileName));
@@ -388,7 +575,7 @@
 	}
 }
 
-bool Shark::reloadTexture(const std::string& fileName) {
+bool Shark::Impl::reloadTexture(const std::string& fileName) {
 	for (const Configuration::Texture& tex : cfg.textures) {
 		if (tex.fileName == fileName) {
 			logOutput << "TODO: reload texture: " << fileName.c_str() << "\n";
@@ -398,7 +585,7 @@
 	return false;
 }
 
-std::shared_ptr<Program> Shark::loadShaders() {
+std::shared_ptr<Program> Shark::Impl::loadShaders() {
 	try {
 		// Vertex Array Object (VAO)
 		GLuint vao;
@@ -471,7 +658,7 @@
 	throw std::logic_error("GLSL: loadShaders() failed");
 }
 
-bool Shark::reloadShader(const std::string& fileName) {
+bool Shark::Impl::reloadShader(const std::string& fileName) {
 	for (auto shader : shaders) {
 		if (shader->getFileName() == fileName) {
 			shader->update(MappedFile(fileName));
--- a/Shark.h	Sat Dec 02 19:33:31 2023 +0100
+++ b/Shark.h	Sat Dec 02 21:25:02 2023 +0100
@@ -17,191 +17,16 @@
 
 #pragma once
 
-#include <iostream>
-#include <iomanip>
-#include <string>
-#include <memory>
-#include <functional>
-#include <sstream>
-#include <vector>
-#include <chrono>
-#include <unistd.h>
-#include <sys/stat.h>
-
 #include "Configuration.h"
-#include "x11.h"
-#include "opengl.h"
-#include "EPoll.h"
-#include "Logger.h"
-#include "MappedFile.h"
-#include "ImageLoader.h"
-#include "Shader.h"
-#include "Program.h"
-#include "FileMonitor.h"
 
 class Shark {
-private:
-
-	struct {
-		GLint aVertexXYZ = -2;
-		GLint aTextureXY = -2;
-
-		GLint fColor = -2;
-
-		GLint uModel = -2;
-		GLint uView = -2;
-		GLint uProjection = -2;
-		GLint uTexture = -2;
-	} ProgAttr;
-
-	struct {
-		float yaw = -90.f;
-		float pitch = 0.f;
-		float roll = 0.f;
-		float fov = 45.0f;
-		glm::vec3 cameraPos = glm::vec3(0.0f, 0.0f, 3.0f);
-		glm::vec3 cameraFront = glm::vec3(0.0f, 0.0f, -1.0f);
-		glm::vec3 cameraUp = glm::vec3(0.0f, 1.0f, 0.0f);
-
-		void adjustFov(float diff) {
-			fov += diff;
-			if (fov < 1.0f) fov = 1.0f;
-			else if (fov > 120.0f) fov = 120.0f;
-			std::cerr << "field of view: " << fov << " °" << std::endl;
-		}
-
-		void moveForward(const float cameraSpeed) {
-			cameraPos += cameraSpeed * cameraFront;
-		}
-
-		void moveBackward(const float cameraSpeed) {
-			cameraPos -= cameraSpeed * cameraFront;
-		}
-
-		void moveLeft(const float cameraSpeed) {
-			cameraPos -= glm::normalize(
-					glm::cross(cameraFront, cameraUp)) * cameraSpeed;
-		}
-
-		void moveRight(const float cameraSpeed) {
-			cameraPos += glm::normalize(
-					glm::cross(cameraFront, cameraUp)) * cameraSpeed;
-		}
-
-		void moveUp(const float cameraSpeed) {
-			cameraPos += cameraSpeed * glm::normalize(cameraUp);
-		}
-
-		void moveDown(const float cameraSpeed) {
-			cameraPos -= cameraSpeed * glm::normalize(cameraUp);
-		}
-
-		void updateCameraFrontAndUp() {
-			std::cerr << "--- updateCameraFrontAndUp() --------" << std::endl;
-			dump("pitch, yaw, roll", glm::vec3(pitch, yaw, roll));
-			dump("cameraPos", cameraPos);
-			dump("cameraFront", cameraFront);
-			const auto pitchR = glm::radians(pitch); // around X axis
-			const auto yawR = glm::radians(yaw); //     around Y axis
-			const auto rollR = glm::radians(roll); //   around Z axis
-
-			cameraFront.x = cos(pitchR) * cos(yawR);
-			cameraFront.y = sin(pitchR);
-			cameraFront.z = cos(pitchR) * sin(yawR);
-			cameraFront = glm::normalize(cameraFront);
-			dump("cameraFront", cameraFront);
-			dump("cameraUp", cameraUp);
-
-			// TODO: review ROLL rotation and default angle
-			glm::mat4 rollMatrix = glm::rotate(
-					glm::mat4(1.0f), rollR, cameraFront);
-			cameraUp = glm::mat3(rollMatrix) * glm::vec3(0., 1., 0.);
-			dump("cameraUp", cameraUp);
-			std::cerr << "-------------------------------------" << std::endl;
-		}
-
-		void limitPitch() {
-			if (pitch > +89.0f) pitch = +89.0f;
-			if (pitch < -89.0f) pitch = -89.0f;
-		}
-
-		void turnLeft(const float angleSpeed) {
-			yaw -= angleSpeed;
-			updateCameraFrontAndUp();
-		}
-
-		void turnRight(const float angleSpeed) {
-			yaw += angleSpeed;
-			updateCameraFrontAndUp();
-		}
-
-		void turnUp(const float angleSpeed) {
-			pitch += angleSpeed;
-			limitPitch();
-			updateCameraFrontAndUp();
-		}
-
-		void turnDown(const float angleSpeed) {
-			pitch -= angleSpeed;
-			limitPitch();
-			updateCameraFrontAndUp();
-		}
-
-		void rollLeft(const float angleSpeed) {
-			roll += angleSpeed;
-			updateCameraFrontAndUp();
-		}
-
-		void rollRight(const float angleSpeed) {
-			roll -= angleSpeed;
-			updateCameraFrontAndUp();
-		}
-
-	} 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;
-	GLXContext glc;
-
-	FileMonitor fileMonitor;
-	std::vector<WatchedFile> watchedFiles;
-	ImageLoader imageLoader;
-	std::vector<std::shared_ptr<Shader>> shaders;
-	std::shared_ptr<Program> shaderProgram;
-	std::vector<Texture> textures;
-
-	Configuration cfg;
-	std::ostream& logOutput = std::cerr;
-
-	void clear();
-	void runShaders();
-	Window getRootWindow(Window defaultValue);
-	void log(LogLevel level, std::string message);
-	int setNonBlocking(int fd);
-	void loadVertices();
-	Texture loadTexture(const std::string& fileName);
-	bool reloadTexture(const std::string& fileName);
-	void loadTextures();
-	std::shared_ptr<Program> loadShaders();
-	bool reloadShader(const std::string& fileName);
-	void setTitle(const std::string& suffix = "");
 public:
 	Shark(const Configuration& cfg);
 	virtual ~Shark();
+	void run();
+private:
+	class Impl;
+	Impl* impl;
 	Shark(const Shark&) = delete;
 	Shark& operator=(const Shark&) = delete;
-	void run();
 };