diff --git a/snippets/CMakeLists.txt b/snippets/CMakeLists.txt index a03ef56..e15085c 100644 --- a/snippets/CMakeLists.txt +++ b/snippets/CMakeLists.txt @@ -40,7 +40,6 @@ endif() add_executable(compte_mots compte_mots.cpp) add_library(polynomial SHARED Polynomial.cpp Polynomial.tpp) -#target_link_libraries(polynomial -static) if(GTest_FOUND) add_executable(polynomialTest gTestPolynomial.cpp) diff --git a/xmlParser/CMakeLists.txt b/xmlParser/CMakeLists.txt index e1acc4e..d2d788e 100644 --- a/xmlParser/CMakeLists.txt +++ b/xmlParser/CMakeLists.txt @@ -22,6 +22,11 @@ endif() find_path(PugiXML_INCLUDE_DIR pugixml.hpp) +find_package(ImGui-SFML REQUIRED) +if(NOT ImGui-SFML_FOUND) + message(FATAL "ImGui-SFML non trouvé") +endif() + add_library(xmlParser SHARED Circle.cpp Circle.h @@ -35,7 +40,11 @@ add_library(xmlParser SHARED target_link_libraries(xmlParser sfml-window sfml-graphics - pugixml) + sfml-system + pugixml + ImGui-SFML::ImGui-SFML) + +target_compile_options(xmlParser PRIVATE -Wall -Wpedantic -Wextra) add_executable(gTestXMLParser gTestXMLParser.cpp) target_compile_options(gTestXMLParser PRIVATE -pthread) @@ -45,5 +54,7 @@ target_link_libraries(gTestXMLParser gtest sfml-graphics sfml-window - pugixml) + sfml-system + pugixml + ImGui-SFML::ImGui-SFML) target_link_options(gTestXMLParser PRIVATE -pthread) \ No newline at end of file diff --git a/xmlParser/Circle.cpp b/xmlParser/Circle.cpp index d320994..343d535 100644 --- a/xmlParser/Circle.cpp +++ b/xmlParser/Circle.cpp @@ -9,7 +9,7 @@ namespace xmlParser Circle::Circle(std::string label, float x, float y, float r, const sf::Color& color) : DrawingElement(std::move(label),x,y,color), r(2) { - shape = new sf::CircleShape(r); + shape = std::unique_ptr(new sf::CircleShape(r)); shape->setPosition(x-r,y-r); shape->setFillColor(color); } @@ -20,16 +20,11 @@ namespace xmlParser { label = "Test Circle"; } - shape = new sf::CircleShape(r); + shape = std::unique_ptr(new sf::CircleShape(r)); shape->setPosition(x-r,y-r); shape->setFillColor(color); } - void Circle::draw(sf::RenderWindow& window) - { - window.draw(*(dynamic_cast(shape))); - } - float Circle::getR() const { return r; @@ -38,13 +33,18 @@ namespace xmlParser void Circle::setR(float newR) { r = newR; - dynamic_cast(shape)->setRadius(r); + dynamic_cast(shape.get())->setRadius(r); shape->setPosition(x-r,y-r); } - const std::string Circle::toString() const + const std::string Circle::toString(int indent) const { std::stringstream string; + for(int i = 0;isetPosition(x,-y); } @@ -52,7 +57,7 @@ namespace xmlParser { y = newY; - if(shape != nullptr) + if(shape) { shape->setPosition(x,-y); } @@ -83,17 +88,9 @@ namespace xmlParser color = strToColor(stringColor); } - const std::string DrawingElement::toString() const - { - std::stringstream string; - string << label << " at x:" << getX() << " y:" << getY(); - string << " | color:" << getStringColor(); - return(string.str()); - } - std::ostream& operator<<(std::ostream& ostream, const DrawingElement& element) { - ostream << element.toString(); + ostream << element.toString(0); return(ostream); } } \ No newline at end of file diff --git a/xmlParser/DrawingElement.h b/xmlParser/DrawingElement.h index acb5356..a52956f 100644 --- a/xmlParser/DrawingElement.h +++ b/xmlParser/DrawingElement.h @@ -20,12 +20,13 @@ namespace xmlParser { public: + // TODO : S'arranger pour n'avoir que des constructeurs sensés : pas de constructeur vide/résultat non évident explicit DrawingElement(std::string label = "Test DrawingElement", float x = 0, float y = 1, const sf::Color& = sf::Color::Yellow); explicit DrawingElement(const pugi::xml_node&); - virtual void draw(sf::RenderWindow&) = 0; + virtual void draw(sf::RenderWindow&); void draw(sf::RenderWindow&, float referenceX, float referenceY); std::string label; @@ -41,7 +42,7 @@ namespace xmlParser void setColor(const sf::Color& newColor); void setColor(const std::string& stringColor); - virtual const std::string toString() const; + virtual const std::string toString(int indent) const = 0; friend std::ostream& operator<<(std::ostream&,const DrawingElement&); @@ -51,7 +52,7 @@ namespace xmlParser sf::Color color; - sf::Shape* shape; + std::unique_ptr shape; }; } diff --git a/xmlParser/Group.cpp b/xmlParser/Group.cpp index 9ac432f..a9735b6 100644 --- a/xmlParser/Group.cpp +++ b/xmlParser/Group.cpp @@ -6,7 +6,7 @@ namespace xmlParser { - Group::Group(std::string label, float x, float y, const sf::Color& color) : DrawingElement(std::move(label),x,y,color), drawingElements({}) + Group::Group(std::string label, float x, float y, const sf::Color& color) : DrawingElement(std::move(label),x,y,color), drawingElements() {} Group::Group(const pugi::xml_node& node) : DrawingElement(node) @@ -32,7 +32,7 @@ namespace xmlParser !strncmp(childNode.name(),"Group",5) ? dynamic_cast(new Group(childNode)) : nullptr; if(newElement != nullptr) { - drawingElements.insert(std::make_pair(newElement->label,newElement)); + drawingElements.emplace(std::make_pair(newElement->label,std::move(std::unique_ptr(newElement)))); } } } @@ -42,13 +42,13 @@ namespace xmlParser std::transform(elements.begin(),elements.end(),std::inserter(drawingElements,drawingElements.begin()), [](std::pair pair) { - return(pair); + return(std::make_pair(pair.first,std::move(std::unique_ptr(pair.second)))); }); } bool Group::addDrawingElement(DrawingElement* drawingElement) { - return(drawingElements.insert(std::make_pair(drawingElement->label,drawingElement)).second); + return(drawingElements.insert(std::make_pair(drawingElement->label,std::move(std::unique_ptr(drawingElement)))).second); } bool Group::removeDrawingElement(const std::string& label) @@ -58,7 +58,7 @@ namespace xmlParser DrawingElement* Group::getDrawingElement(const std::string& label) { - return(drawingElements.at(label)); + return(drawingElements.at(label).get()); } void Group::draw(sf::RenderWindow& window) @@ -69,15 +69,20 @@ namespace xmlParser } } - const std::string Group::toString() const + const std::string Group::toString(int indent) const { std::stringstream string; + for(int i = 0;itoString(indent+1) << std::endl; } return(string.str()); } diff --git a/xmlParser/Group.h b/xmlParser/Group.h index 99c7a42..1d7b2c7 100644 --- a/xmlParser/Group.h +++ b/xmlParser/Group.h @@ -34,9 +34,9 @@ namespace xmlParser void draw(sf::RenderWindow&) override; - const std::string toString() const override; + const std::string toString(int indent) const override; private: - std::map drawingElements; + std::map> drawingElements; }; } diff --git a/xmlParser/gTestXMLParser.cpp b/xmlParser/gTestXMLParser.cpp index 23534d3..62e4947 100644 --- a/xmlParser/gTestXMLParser.cpp +++ b/xmlParser/gTestXMLParser.cpp @@ -5,12 +5,44 @@ #include #include +#include "imgui.h" +#include "imgui-SFML.h" + #include "Circle.h" #include "xmlParser.h" #include "SFML/Graphics.hpp" #include "Group.h" +class ImGui_F : public ::testing::Test +{ +protected: + + sf::RenderWindow* window; + + sf::Color bgColor; + + sf::Clock deltaClock; + + float color[3] = { 0.f, 0.f, 0.f }; + + // let's use char array as buffer, see next part + // for instructions on using std::string with ImGui + char windowTitle[255]; + + void SetUp() override + { + window = new sf::RenderWindow(sf::VideoMode(640, 480), ""); + window->setVerticalSyncEnabled(true); + ImGui::SFML::Init(*window); + + strcpy(windowTitle,"ImGui + SFML = <3"); + + window->setTitle(windowTitle); + window->resetGLStates(); // call it if you only draw ImGui. Otherwise not needed. + } +}; + TEST(xmlParserInits,initEmptyCircle) { xmlParser::Circle cercle = xmlParser::Circle(); @@ -191,6 +223,54 @@ TEST(drawFace,drawFace) } } +TEST_F(ImGui_F,ImGui_Example) +{ + while (window->isOpen()) { + sf::Event event; + while (window->pollEvent(event)) { + ImGui::SFML::ProcessEvent(event); + + if (event.type == sf::Event::Closed) { + window->close(); + } + } + + ImGui::SFML::Update(*window, deltaClock.restart()); + + ImGui::Begin("Sample window"); // begin window + + // Background color edit + if (ImGui::ColorEdit3("Background color", color)) { + // this code gets called if color value changes, so + // the background color is upgraded automatically! + bgColor.r = static_cast(color[0] * 255.f); + bgColor.g = static_cast(color[1] * 255.f); + bgColor.b = static_cast(color[2] * 255.f); + } + + // Window title text edit + ImGui::InputText("Window title", windowTitle, 255); + + if (ImGui::Button("Update window title")) { + // this code gets if user clicks on the button + // yes, you could have written if(ImGui::InputText(...)) + // but I do this to show how buttons work :) + window->setTitle(windowTitle); + } + ImGui::End(); // end window + + window->clear(bgColor); // fill background with color + ImGui::SFML::Render(*window); + window->display(); + } + + ImGui::SFML::Shutdown(); +} + +TEST_F(ImGui_F,ImGui_Perso) +{ + // TODO : Copy examples to create new shapes 'n stuff +} int main(int argc, char** argv) { ::testing::InitGoogleTest(&argc,argv);