Compare commits

...

4 commits

9 changed files with 132 additions and 41 deletions

View file

@ -40,7 +40,6 @@ endif()
add_executable(compte_mots compte_mots.cpp) add_executable(compte_mots compte_mots.cpp)
add_library(polynomial SHARED Polynomial.cpp Polynomial.tpp) add_library(polynomial SHARED Polynomial.cpp Polynomial.tpp)
#target_link_libraries(polynomial -static)
if(GTest_FOUND) if(GTest_FOUND)
add_executable(polynomialTest gTestPolynomial.cpp) add_executable(polynomialTest gTestPolynomial.cpp)

View file

@ -22,6 +22,11 @@ endif()
find_path(PugiXML_INCLUDE_DIR pugixml.hpp) 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 add_library(xmlParser SHARED
Circle.cpp Circle.cpp
Circle.h Circle.h
@ -35,7 +40,11 @@ add_library(xmlParser SHARED
target_link_libraries(xmlParser target_link_libraries(xmlParser
sfml-window sfml-window
sfml-graphics sfml-graphics
pugixml) sfml-system
pugixml
ImGui-SFML::ImGui-SFML)
target_compile_options(xmlParser PRIVATE -Wall -Wpedantic -Wextra)
add_executable(gTestXMLParser gTestXMLParser.cpp) add_executable(gTestXMLParser gTestXMLParser.cpp)
target_compile_options(gTestXMLParser PRIVATE -pthread) target_compile_options(gTestXMLParser PRIVATE -pthread)
@ -45,5 +54,7 @@ target_link_libraries(gTestXMLParser
gtest gtest
sfml-graphics sfml-graphics
sfml-window sfml-window
pugixml) sfml-system
pugixml
ImGui-SFML::ImGui-SFML)
target_link_options(gTestXMLParser PRIVATE -pthread) target_link_options(gTestXMLParser PRIVATE -pthread)

View file

@ -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) 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<sf::Shape>(new sf::CircleShape(r));
shape->setPosition(x-r,y-r); shape->setPosition(x-r,y-r);
shape->setFillColor(color); shape->setFillColor(color);
} }
@ -20,16 +20,11 @@ namespace xmlParser
{ {
label = "Test Circle"; label = "Test Circle";
} }
shape = new sf::CircleShape(r); shape = std::unique_ptr<sf::Shape>(new sf::CircleShape(r));
shape->setPosition(x-r,y-r); shape->setPosition(x-r,y-r);
shape->setFillColor(color); shape->setFillColor(color);
} }
void Circle::draw(sf::RenderWindow& window)
{
window.draw(*(dynamic_cast<sf::CircleShape*>(shape)));
}
float Circle::getR() const float Circle::getR() const
{ {
return r; return r;
@ -38,13 +33,18 @@ namespace xmlParser
void Circle::setR(float newR) void Circle::setR(float newR)
{ {
r = newR; r = newR;
dynamic_cast<sf::CircleShape*>(shape)->setRadius(r); dynamic_cast<sf::CircleShape*>(shape.get())->setRadius(r);
shape->setPosition(x-r,y-r); shape->setPosition(x-r,y-r);
} }
const std::string Circle::toString() const const std::string Circle::toString(int indent) const
{ {
std::stringstream string; std::stringstream string;
for(int i = 0;i<indent;i++)
{
string << "\t";
}
string << label << " at x:" << getX() << " y:" << getY() << " | r:" << getR(); string << label << " at x:" << getX() << " y:" << getY() << " | r:" << getR();
string << " color:" << getStringColor(); string << " color:" << getStringColor();
return(string.str()); return(string.str());

View file

@ -22,8 +22,6 @@ namespace xmlParser
explicit Circle(const pugi::xml_node&); explicit Circle(const pugi::xml_node&);
void draw(sf::RenderWindow&) override;
void setX(float newX) override; void setX(float newX) override;
void setY(float newY) override; void setY(float newY) override;
@ -31,7 +29,7 @@ namespace xmlParser
float getR() const; float getR() const;
void setR(float newR); void setR(float newR);
const std::string toString() const override; const std::string toString(int indent) const override;
private: private:
float r; float r;
}; };

View file

@ -9,16 +9,21 @@
namespace xmlParser namespace xmlParser
{ {
DrawingElement::DrawingElement(std::string label, float x, float y, const sf::Color& color) : x(x), y(-y), label(std::move(label)), DrawingElement::DrawingElement(std::string label, float x, float y, const sf::Color& color) : label(std::move(label)), x(x), y(-y),
color(color), shape(nullptr) color(color), shape(nullptr)
{} {}
DrawingElement::DrawingElement(const pugi::xml_node& node) : x(node.attribute("x").as_int(0)), DrawingElement::DrawingElement(const pugi::xml_node& node) : label(node.attribute("label").as_string("Test DrawingElement")),
y(-node.attribute("y").as_int(1)), label(node.attribute("label").as_string("Test DrawingElement")), shape(nullptr) x(node.attribute("x").as_int(0)), y(-node.attribute("y").as_int(1)), shape(nullptr)
{ {
setColor(node.attribute("color").as_string("Yellow")); setColor(node.attribute("color").as_string("Yellow"));
} }
void DrawingElement::draw(sf::RenderWindow& window)
{
window.draw(*shape);
}
void DrawingElement::draw(sf::RenderWindow& window, float referenceX, float referenceY) void DrawingElement::draw(sf::RenderWindow& window, float referenceX, float referenceY)
{ {
setX(x+referenceX); setX(x+referenceX);
@ -37,7 +42,7 @@ namespace xmlParser
{ {
x = newX; x = newX;
if(shape != nullptr) if(shape)
{ {
shape->setPosition(x,-y); shape->setPosition(x,-y);
} }
@ -52,7 +57,7 @@ namespace xmlParser
{ {
y = newY; y = newY;
if(shape != nullptr) if(shape)
{ {
shape->setPosition(x,-y); shape->setPosition(x,-y);
} }
@ -83,17 +88,9 @@ namespace xmlParser
color = strToColor(stringColor); 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) std::ostream& operator<<(std::ostream& ostream, const DrawingElement& element)
{ {
ostream << element.toString(); ostream << element.toString(0);
return(ostream); return(ostream);
} }
} }

View file

@ -20,12 +20,13 @@ namespace xmlParser
{ {
public: 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, explicit DrawingElement(std::string label = "Test DrawingElement", float x = 0, float y = 1,
const sf::Color& = sf::Color::Yellow); const sf::Color& = sf::Color::Yellow);
explicit DrawingElement(const pugi::xml_node&); 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); void draw(sf::RenderWindow&, float referenceX, float referenceY);
std::string label; std::string label;
@ -41,7 +42,7 @@ namespace xmlParser
void setColor(const sf::Color& newColor); void setColor(const sf::Color& newColor);
void setColor(const std::string& stringColor); 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&); friend std::ostream& operator<<(std::ostream&,const DrawingElement&);
@ -51,7 +52,7 @@ namespace xmlParser
sf::Color color; sf::Color color;
sf::Shape* shape; std::unique_ptr<sf::Shape> shape;
}; };
} }

View file

@ -6,7 +6,7 @@
namespace xmlParser 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) Group::Group(const pugi::xml_node& node) : DrawingElement(node)
@ -32,7 +32,7 @@ namespace xmlParser
!strncmp(childNode.name(),"Group",5) ? dynamic_cast<DrawingElement*>(new Group(childNode)) : nullptr; !strncmp(childNode.name(),"Group",5) ? dynamic_cast<DrawingElement*>(new Group(childNode)) : nullptr;
if(newElement != nullptr) if(newElement != nullptr)
{ {
drawingElements.insert(std::make_pair(newElement->label,newElement)); drawingElements.emplace(std::make_pair(newElement->label,std::move(std::unique_ptr<DrawingElement>(newElement))));
} }
} }
} }
@ -42,13 +42,13 @@ namespace xmlParser
std::transform(elements.begin(),elements.end(),std::inserter(drawingElements,drawingElements.begin()), std::transform(elements.begin(),elements.end(),std::inserter(drawingElements,drawingElements.begin()),
[](std::pair<std::string,DrawingElement*> pair) [](std::pair<std::string,DrawingElement*> pair)
{ {
return(pair); return(std::make_pair(pair.first,std::move(std::unique_ptr<DrawingElement>(pair.second))));
}); });
} }
bool Group::addDrawingElement(DrawingElement* drawingElement) 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>(drawingElement)))).second);
} }
bool Group::removeDrawingElement(const std::string& label) bool Group::removeDrawingElement(const std::string& label)
@ -58,7 +58,7 @@ namespace xmlParser
DrawingElement* Group::getDrawingElement(const std::string& label) DrawingElement* Group::getDrawingElement(const std::string& label)
{ {
return(drawingElements.at(label)); return(drawingElements.at(label).get());
} }
void Group::draw(sf::RenderWindow& window) 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; std::stringstream string;
for(int i = 0;i<indent;i++)
{
string << "\t";
}
string << label << " at x:" << getX() << " y:" << getY(); string << label << " at x:" << getX() << " y:" << getY();
string << " color:" << getStringColor() << std::endl; string << " color:" << getStringColor() << std::endl;
for(const auto& element : drawingElements) for(const auto& element : drawingElements)
{ {
string << "\t" << *(element.second) << std::endl; string << element.second->toString(indent+1) << std::endl;
} }
return(string.str()); return(string.str());
} }

View file

@ -34,9 +34,9 @@ namespace xmlParser
void draw(sf::RenderWindow&) override; void draw(sf::RenderWindow&) override;
const std::string toString() const override; const std::string toString(int indent) const override;
private: private:
std::map<std::string,DrawingElement*> drawingElements; std::map<std::string,std::unique_ptr<DrawingElement>> drawingElements;
}; };
} }

View file

@ -5,12 +5,44 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <iostream> #include <iostream>
#include "imgui.h"
#include "imgui-SFML.h"
#include "Circle.h" #include "Circle.h"
#include "xmlParser.h" #include "xmlParser.h"
#include "SFML/Graphics.hpp" #include "SFML/Graphics.hpp"
#include "Group.h" #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) TEST(xmlParserInits,initEmptyCircle)
{ {
xmlParser::Circle cercle = xmlParser::Circle(); 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<sf::Uint8>(color[0] * 255.f);
bgColor.g = static_cast<sf::Uint8>(color[1] * 255.f);
bgColor.b = static_cast<sf::Uint8>(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) int main(int argc, char** argv)
{ {
::testing::InitGoogleTest(&argc,argv); ::testing::InitGoogleTest(&argc,argv);