Add Control class
This commit is contained in:
parent
f8a5ac20c8
commit
121ae73517
5 changed files with 199 additions and 6 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -4,3 +4,4 @@ build/*
|
||||||
gross
|
gross
|
||||||
*.jpg
|
*.jpg
|
||||||
*.png
|
*.png
|
||||||
|
*.ttf
|
||||||
|
|
|
||||||
2
Makefile
2
Makefile
|
|
@ -9,7 +9,7 @@ SOURCEFILES = cpp
|
||||||
SRCDIR = src
|
SRCDIR = src
|
||||||
TMPDIR = build
|
TMPDIR = build
|
||||||
TARGET = gross
|
TARGET = gross
|
||||||
FILES = Genes Chromosome Fitness Generation main
|
FILES = Genes Chromosome Fitness Generation Control main
|
||||||
|
|
||||||
#SOURCES = $(patsubst %,$(SRCDIR)/%.cpp,$(FILES))
|
#SOURCES = $(patsubst %,$(SRCDIR)/%.cpp,$(FILES))
|
||||||
OBJECTS = $(patsubst %,$(TMPDIR)/%.o,$(FILES))
|
OBJECTS = $(patsubst %,$(TMPDIR)/%.o,$(FILES))
|
||||||
|
|
|
||||||
124
src/Control.cpp
Normal file
124
src/Control.cpp
Normal file
|
|
@ -0,0 +1,124 @@
|
||||||
|
#include "Control.hpp"
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
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
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
56
src/Control.hpp
Normal file
56
src/Control.hpp
Normal file
|
|
@ -0,0 +1,56 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <SFML/Graphics.hpp>
|
||||||
|
#include <SFML/System.hpp>
|
||||||
|
#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();
|
||||||
|
};
|
||||||
22
src/main.cpp
22
src/main.cpp
|
|
@ -2,14 +2,14 @@
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <random>
|
#include <random>
|
||||||
#include <SFML/Graphics.hpp>
|
#include <SFML/Graphics.hpp>
|
||||||
|
#include "Genes.hpp"
|
||||||
#include "Chromosome.hpp"
|
#include "Chromosome.hpp"
|
||||||
#include "Fitness.hpp"
|
#include "Fitness.hpp"
|
||||||
#include "Generation.hpp"
|
#include "Generation.hpp"
|
||||||
|
#include "Control.hpp"
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
const float winW = 480;
|
|
||||||
const float winH = 480;
|
|
||||||
std::minstd_rand randomEngine;
|
std::minstd_rand randomEngine;
|
||||||
randomEngine.seed(time(nullptr));
|
randomEngine.seed(time(nullptr));
|
||||||
|
|
||||||
|
|
@ -22,19 +22,30 @@ int main()
|
||||||
|
|
||||||
Generation::size = 100;
|
Generation::size = 100;
|
||||||
Generation::living = 10;
|
Generation::living = 10;
|
||||||
Generation::crossover = 0;
|
Generation::crossover = 0.0;
|
||||||
|
|
||||||
Fitness fitn;
|
Fitness fitn;
|
||||||
// fitn.loadTarget("tom-face.png");
|
|
||||||
fitn.loadTarget("firefox.png");
|
fitn.loadTarget("firefox.png");
|
||||||
|
// fitn.loadTarget("tom-face.png");
|
||||||
// fitn.loadTarget("goldman_sachs.jpg");
|
// fitn.loadTarget("goldman_sachs.jpg");
|
||||||
fitn.loadShader("compare.glsl");
|
fitn.loadShader("compare.glsl");
|
||||||
|
|
||||||
Gene::size = sf::Vector2f(fitn.target.getSize());
|
Gene::size = sf::Vector2f(fitn.target.getSize());
|
||||||
Generation::fitness = &fitn;
|
Generation::fitness = &fitn;
|
||||||
|
|
||||||
Generation gen;
|
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;
|
// Chromosome a;
|
||||||
// for (int i=0; i<1000; ++i) a.mutate();
|
// for (int i=0; i<1000; ++i) a.mutate();
|
||||||
// Chromosome b(a);
|
// Chromosome b(a);
|
||||||
|
|
@ -81,3 +92,4 @@ int main()
|
||||||
// std::cout << genr.individuals[0].chromosome.length() << ")" << std::endl;
|
// std::cout << genr.individuals[0].chromosome.length() << ")" << std::endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue