Add states and a setup state to Control

This commit is contained in:
Joscha 2017-06-22 17:43:22 +00:00
parent 121ae73517
commit 5369805616
7 changed files with 400 additions and 13 deletions

View file

@ -2,18 +2,40 @@
#include <iostream>
/*
* Setup:
* - current item
* - circles
* - triangles
* - spheres
* - max number limited?
*
* Generations:
* - paused
* - save image
* - display mode
* - saving?
*
* StepThrough:
* - which step am I at? -> enums
*/
Fitness* Control::fitness;
Generation* Control::generation;
float Control::barMargin = 4;
Control::Control(float winW, float winH, std::string name)
Control::Control(float winW, float winH, std::string name) :
screenSetup(this)
{
this->window.create(sf::VideoMode(winW, winH), name);
this->targetSprite.setTexture(Control::fitness->target.getTexture());
this->chromosomeSprite.setTexture(Control::fitness->chromosome.getTexture());
// this->targetSprite.setTexture(Control::fitness->target.getTexture());
// this->chromosomeSprite.setTexture(Control::fitness->chromosome.getTexture());
// this->currentScreen = &this->screenSetup;
// this->nextScreen = this->currentScreen;
this->leftText.setCharacterSize(20);
this->leftText.setFillColor(sf::Color::Black);
@ -34,17 +56,52 @@ Control::~Control()
void Control::interactive()
{
this->currentScreen = nullptr;
this->switchScreen(this->screenSetup);
std::cout << "Starting interactive loop" << std::endl;
sf::Clock clock;
while (this->window.isOpen()) {
sf::Event event;
while (this->window.pollEvent(event)) {
if (this->currentScreen != this->nextScreen) {
if (this->currentScreen != nullptr) this->currentScreen->exit();
if (this->nextScreen == nullptr) {
std::cout << "Switched to null screen; window closed" << std::endl;
window.close();
continue;
}
this->currentScreen = this->nextScreen;
this->currentScreen->enter();
}
this->window.clear();
this->window.draw(this->bar);
this->window.draw(this->rightText);
this->window.draw(this->leftText);
// handle window events
sf::Event event;
while (this->window.pollEvent(event)) {
this->currentScreen->event(event);
}
// update screen
this->currentScreen->update(clock.restart());
// draw screen
this->window.clear(sf::Color::White);
this->window.draw(*this->currentScreen);
this->window.display();
}
std::cout << "Interactive loop stopped; window closed" << std::endl;
}
void Control::switchScreen(Screen& newScreen)
{
std::cout << "Switched screens" << std::endl;
this->nextScreen = &newScreen;
}
void Control::close()
{
this->nextScreen = nullptr;
}
@ -53,6 +110,8 @@ void Control::setFont(const sf::Font& font)
this->leftText.setFont(font);
this->rightText.setFont(font);
this->screenSetup.setFont(font);
this->updateBar();
}
@ -73,6 +132,15 @@ void Control::setRightText(const std::string& text)
}
void Control::setText(const std::string& left, const std::string& right)
{
this->leftText.setString(left);
this->rightText.setString(right);
this->updateBar();
}
void Control::updateBar()
{
// update bar size and position
@ -122,3 +190,41 @@ void Control::updateBar()
)
);
}
void Control::drawBar(sf::RenderTarget& target, sf::RenderStates states) const
{
target.draw(this->bar, states);
target.draw(this->rightText, states);
target.draw(this->leftText, states);
}
/*
void Control::modeGenerations()
{
// handle window events
sf::Event event;
while (this->window.pollEvent(event)) {
// window closed
if (event.type == sf::Event::Closed) {
window.close();
return;
}
// key pressed
if (event.type == sf::Event::KeyPressed) {
if (event.key.code == sf::Keyboard::Q) {
window.close();
return;
}
}
}
this->window.clear();
this->window.draw(this->bar);
this->window.draw(this->rightText);
this->window.draw(this->leftText);
this->window.display();
}
*/