From c06244cd327a20a485ab4abe25ea8427e297e209 Mon Sep 17 00:00:00 2001 From: Diogo Matos Date: Sun, 3 Mar 2024 17:44:28 +0000 Subject: [PATCH] chore: improvements --- engine/include/Camera.hpp | 4 ++++ engine/include/Configuration.hpp | 11 +++++++---- engine/include/Window.hpp | 4 ++++ engine/include/parse.hpp | 4 +++- engine/include/utils.hpp | 6 ++++++ engine/src/Camera.cpp | 14 ++++++++++++++ engine/src/Configuration.cpp | 21 ++++++++++++++++++++- engine/src/Window.cpp | 10 ++++++++++ engine/src/main.cpp | 6 ++++++ engine/src/parse.cpp | 12 ++++++------ engine/test.xml | 15 +++++++++++++++ 11 files changed, 95 insertions(+), 12 deletions(-) create mode 100644 engine/test.xml diff --git a/engine/include/Camera.hpp b/engine/include/Camera.hpp index 04a1d95..23b931c 100644 --- a/engine/include/Camera.hpp +++ b/engine/include/Camera.hpp @@ -4,6 +4,8 @@ #include "Camera.hpp" #include "utils.hpp" +#include + class Camera { public: Point position; @@ -16,6 +18,8 @@ class Camera { Camera(); Camera(Point position, Point lookAt, Point up, int fov, float near, float far); + + std::string toString(); }; #endif // CAMERA_HPP \ No newline at end of file diff --git a/engine/include/Configuration.hpp b/engine/include/Configuration.hpp index 647f503..1c46b1e 100644 --- a/engine/include/Configuration.hpp +++ b/engine/include/Configuration.hpp @@ -1,19 +1,22 @@ #ifndef CONFIGURATION_HPP #define CONFIGURATION_HPP -#include - #include "Camera.hpp" #include "Configuration.hpp" #include "Window.hpp" +#include +#include + class Configuration { public: Window window; Camera camera; - std::vector models; + std::vector models; + + Configuration(Window window, Camera camera, std::vector models); - Configuration(Window window, Camera camera, std::vector models); + std::string toString(); }; #endif // CONFIGURATION_HPP \ No newline at end of file diff --git a/engine/include/Window.hpp b/engine/include/Window.hpp index 2bfb7c3..2c78b97 100644 --- a/engine/include/Window.hpp +++ b/engine/include/Window.hpp @@ -3,6 +3,8 @@ #include "Window.hpp" +#include + class Window { public: float width; @@ -10,6 +12,8 @@ class Window { Window(); Window(float width, float height); + + std::string toString(); }; #endif // WINDOW_HPP \ No newline at end of file diff --git a/engine/include/parse.hpp b/engine/include/parse.hpp index 3025e2a..584f854 100644 --- a/engine/include/parse.hpp +++ b/engine/include/parse.hpp @@ -5,6 +5,8 @@ #include "Configuration.hpp" #include "parse.hpp" -Configuration parseConfig(char* filename); +#include + +Configuration parseConfig(std::string filename); #endif // PARSE_HPP \ No newline at end of file diff --git a/engine/include/utils.hpp b/engine/include/utils.hpp index d07c52d..c156c7b 100644 --- a/engine/include/utils.hpp +++ b/engine/include/utils.hpp @@ -18,6 +18,12 @@ typedef struct Point { // Constructor Point(float x_val = 0.0f, float y_val = 0.0f, float z_val = 0.0f) : x(x_val), y(y_val), z(z_val) {} + + std::string toString() { + std::stringstream ss; + ss << "Point(" << x << ", " << y << ", " << z << ")"; + return ss.str(); + } } Point; // Function to convert Point to string diff --git a/engine/src/Camera.cpp b/engine/src/Camera.cpp index 6df4a16..16aad56 100644 --- a/engine/src/Camera.cpp +++ b/engine/src/Camera.cpp @@ -1,5 +1,8 @@ #include "Camera.hpp" +#include +#include + Camera::Camera() { this->position = Point(); this->lookAt = Point(); @@ -18,3 +21,14 @@ Camera::Camera(Point position, Point lookAt, Point up, int fov, float near, this->near = near; this->far = far; } + +std::string Camera::toString() { + std::stringstream ss; + ss << "\tPosition: " << position.toString() << std::endl + << "\tLookAt: " << lookAt.toString() << std::endl + << "\tUp: " << up.toString() << std::endl + << "\tFOV: " << std::to_string(fov).c_str() << std::endl + << "\tNear: " << std::to_string(near).c_str() << std::endl + << "\tFar: " << std::to_string(far).c_str() << std::endl; + return ss.str(); +} diff --git a/engine/src/Configuration.cpp b/engine/src/Configuration.cpp index 0c149d3..a2adefc 100644 --- a/engine/src/Configuration.cpp +++ b/engine/src/Configuration.cpp @@ -1,8 +1,27 @@ #include "Configuration.hpp" +#include +#include + Configuration::Configuration(Window window, Camera camera, - std::vector models) { + std::vector models) { this->window = window; this->camera = camera; this->models = models; } + +std::string vectorToString(std::vector v) { + std::stringstream ss; + for (auto i : v) { + ss << i << " "; + } + return ss.str(); +} + +std::string Configuration::toString() { + std::stringstream ss; + ss << "Window:\n" << window.toString() << std::endl + << "Camera:\n" << camera.toString() << std::endl + << "Models: " << vectorToString(models) << std::endl; + return ss.str(); +} diff --git a/engine/src/Window.cpp b/engine/src/Window.cpp index a02b3ba..3b4fd8d 100644 --- a/engine/src/Window.cpp +++ b/engine/src/Window.cpp @@ -1,5 +1,8 @@ #include "Window.hpp" +#include +#include + Window::Window() { this->width = 0; this->height = 0; @@ -9,3 +12,10 @@ Window::Window(float width, float height) { this->width = width; this->height = height; } + +std::string Window::toString() { + std::stringstream ss; + ss << "\tWidth: " << width << std::endl + << "\tHeight: " << height << std::endl; + return ss.str(); +} diff --git a/engine/src/main.cpp b/engine/src/main.cpp index 4a0ee2e..6c4387e 100644 --- a/engine/src/main.cpp +++ b/engine/src/main.cpp @@ -8,6 +8,7 @@ #include #include "draw.hpp" +#include "parse.hpp" float cameraAngle = 90.0f; float cameraAngleY = 0.0f; @@ -94,6 +95,11 @@ void processSpecialKeys(int key, int xx, int yy) { } int main(int argc, char** argv) { + std::string filename; + filename.assign("/home/diogo/LEI/3ano/2sem/CG/CG-Project/engine/test.xml"); + Configuration c = parseConfig(filename); + printf(c.toString().c_str()); + // put GLUT�s init here glutInit(&argc, argv); glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA); diff --git a/engine/src/parse.cpp b/engine/src/parse.cpp index 2874642..f48a4a9 100644 --- a/engine/src/parse.cpp +++ b/engine/src/parse.cpp @@ -1,12 +1,12 @@ #include "parse.hpp" -#include #include +#include +#include -Configuration parseConfig(char* filename) { +Configuration parseConfig(std::string filename) { // open file in read mode - std::fstream file; - file.open(filename, std::ios::in); + std::ifstream file(filename); // check if the file was opened successfully if (!file.is_open()) { @@ -58,12 +58,12 @@ Configuration parseConfig(char* filename) { Camera camera_info = Camera(position, lookAt, up, fov, near, far); // models - std::vector models_info; + std::vector models_info; rapidxml::xml_node<>* models = root->first_node("group")->first_node("models"); for (rapidxml::xml_node<>* model = models->first_node("model"); model; model = model->next_sibling("model")) { - models_info.push_back(model->first_attribute("file")->value()); + models_info.push_back(std::string(model->first_attribute("file")->value())); } return Configuration(window_info, camera_info, models_info); diff --git a/engine/test.xml b/engine/test.xml new file mode 100644 index 0000000..9c2cc3a --- /dev/null +++ b/engine/test.xml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + \ No newline at end of file