From 121ae735171e44472fcf013cbbcf45e5c5392059 Mon Sep 17 00:00:00 2001 From: Joscha Date: Mon, 29 May 2017 17:18:29 +0000 Subject: [PATCH] Add Control class --- .gitignore | 1 + Makefile | 2 +- src/Control.cpp | 124 ++++++++++++++++++++++++++++++++++++++++++++++++ src/Control.hpp | 56 ++++++++++++++++++++++ src/main.cpp | 22 +++++++-- 5 files changed, 199 insertions(+), 6 deletions(-) create mode 100644 src/Control.cpp create mode 100644 src/Control.hpp diff --git a/.gitignore b/.gitignore index fc7c252..0376889 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ build/* gross *.jpg *.png +*.ttf diff --git a/Makefile b/Makefile index 09ee422..9fe502f 100644 --- a/Makefile +++ b/Makefile @@ -9,7 +9,7 @@ SOURCEFILES = cpp SRCDIR = src TMPDIR = build TARGET = gross -FILES = Genes Chromosome Fitness Generation main +FILES = Genes Chromosome Fitness Generation Control main #SOURCES = $(patsubst %,$(SRCDIR)/%.cpp,$(FILES)) OBJECTS = $(patsubst %,$(TMPDIR)/%.o,$(FILES)) diff --git a/src/Control.cpp b/src/Control.cpp new file mode 100644 index 0000000..e15ab67 --- /dev/null +++ b/src/Control.cpp @@ -0,0 +1,124 @@ +#include "Control.hpp" + +#include + +Fitness* Control::fitness; +Generation* Control::generation; + +float Control::barMargin = 4; + + +Control::Control(float winW, float winH, std::string name) +{ + this->window.create(sf::VideoMode(winW, winH), name); + + this->targetSprite.setTexture(Control::fitness->target.getTexture()); + this->chromosomeSprite.setTexture(Control::fitness->chromosome.getTexture()); + + this->leftText.setCharacterSize(20); + this->leftText.setFillColor(sf::Color::Black); + + this->rightText.setCharacterSize(20); + this->rightText.setFillColor(sf::Color::Black); + + this->bar.setFillColor(sf::Color(127, 127, 127)); + + this->updateBar(); +} + + +Control::~Control() +{ +} + + +void Control::interactive() +{ + while (this->window.isOpen()) { + sf::Event event; + while (this->window.pollEvent(event)) { + } + + this->window.clear(); + this->window.draw(this->bar); + this->window.draw(this->rightText); + this->window.draw(this->leftText); + this->window.display(); + } +} + + +void Control::setFont(const sf::Font& font) +{ + this->leftText.setFont(font); + this->rightText.setFont(font); + + this->updateBar(); +} + + +void Control::setLeftText(const std::string& text) +{ + this->leftText.setString(text); + + this->updateBar(); +} + + +void Control::setRightText(const std::string& text) +{ + this->rightText.setString(text); + + this->updateBar(); +} + + +void Control::updateBar() +{ + // update bar size and position + this->bar.setSize( + sf::Vector2f( + this->window.getSize().x, + this->leftText.getCharacterSize() + 2*Control::barMargin + ) + ); + + this->bar.setPosition( + sf::Vector2f( + 0, + this->window.getSize().y - this->bar.getSize().y + ) + ); + + // update left text size and position + sf::FloatRect bounds = this->leftText.getLocalBounds(); + this->leftText.setOrigin( + sf::Vector2f( + bounds.left, + this->rightText.getCharacterSize()/4 // slightly hacky solution to text offset issue + ) + ); + + this->leftText.setPosition( + sf::Vector2f( + Control::barMargin, + this->window.getSize().y - this->leftText.getCharacterSize() - Control::barMargin + ) + ); + + // update right text size and position + bounds = this->rightText.getLocalBounds(); + this->rightText.setOrigin( + sf::Vector2f( + bounds.left + bounds.width, + this->rightText.getCharacterSize()/4 // slightly hacky solution to text offset issue + ) + ); + + this->rightText.setPosition( + sf::Vector2f( + this->window.getSize().x - Control::barMargin, + this->window.getSize().y - this->rightText.getCharacterSize() - Control::barMargin + ) + ); +} diff --git a/src/Control.hpp b/src/Control.hpp new file mode 100644 index 0000000..ce8c241 --- /dev/null +++ b/src/Control.hpp @@ -0,0 +1,56 @@ +#pragma once + +#include +#include +#include "Chromosome.hpp" +#include "Fitness.hpp" +#include "Generation.hpp" + + +/* + * TODO: + * display modes etc. + * - overlap (transparency) + * - side-by-side + * + * What I want: + * - generation screen + * - side-by-side/overlap view + * - generation-specific bar + * + * - stepping-through screen + * - show whole generation? + * - evaluate fitness one-by-one + * - side-by-side/overlap view + * - chromosome-specific bar + */ + + +class Control { +public: + static Fitness* fitness; + static Generation* generation; + + static float barMargin; + + Control(float winW, float winH, std::string name); // creates a window + ~Control(); + + void interactive(); + + void setFont(const sf::Font& font); + void setLeftText(const std::string& text); + void setRightText(const std::string& text); + +private: + sf::RenderWindow window; + + sf::Sprite targetSprite; + sf::Sprite chromosomeSprite; + + sf::RectangleShape bar; + sf::Text leftText; + sf::Text rightText; + + void updateBar(); +}; diff --git a/src/main.cpp b/src/main.cpp index bd9a72b..32b8b96 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,14 +2,14 @@ #include #include #include +#include "Genes.hpp" #include "Chromosome.hpp" #include "Fitness.hpp" #include "Generation.hpp" +#include "Control.hpp" int main() { - const float winW = 480; - const float winH = 480; std::minstd_rand randomEngine; randomEngine.seed(time(nullptr)); @@ -22,19 +22,30 @@ int main() Generation::size = 100; Generation::living = 10; - Generation::crossover = 0; + Generation::crossover = 0.0; Fitness fitn; -// fitn.loadTarget("tom-face.png"); fitn.loadTarget("firefox.png"); +// fitn.loadTarget("tom-face.png"); // fitn.loadTarget("goldman_sachs.jpg"); fitn.loadShader("compare.glsl"); Gene::size = sf::Vector2f(fitn.target.getSize()); Generation::fitness = &fitn; - Generation gen; + Control::fitness = Generation::fitness; + Control::generation = &gen; + Control con(800, 600, "gross"); // window size + + sf::Font font; + font.loadFromFile("FreeMonoBold.ttf"); + con.setFont(font); + + con.interactive(); +} + +/* // Chromosome a; // for (int i=0; i<1000; ++i) a.mutate(); // Chromosome b(a); @@ -81,3 +92,4 @@ int main() // std::cout << genr.individuals[0].chromosome.length() << ")" << std::endl; } } +*/