void EffectsHandler::Update(float elapsed, sf::View &view, sf::RenderWindow &rw) { if (effectsList.empty()) { return; } for (std::list<Effect>::iterator it = effectsList.begin(); it != effectsList.end(); it++) { switch (*it) { case ZoomIn: if (view.getSize().x > rw.getSize().x/2) { view.setSize(view.getSize().x-(ZOOM_SPEED_X*elapsed), view.getSize().y-(ZOOM_SPEED_Y*elapsed)); view.setCenter(view.getCenter().x, view.getCenter().y-(ZOOM_POS_ABOVE_MIDDLE*elapsed)); } else effectsList.erase(it++); break; case ZoomOut: if (view.getSize().x <= rw.getDefaultView().getSize().x && view.getSize().y <= rw.getDefaultView().getSize().y) //seems silly to take &window just for getdefaultview. { view.setSize(view.getSize().x+(ZOOM_SPEED_X*elapsed), view.getSize().y+(ZOOM_SPEED_Y*elapsed)); view.setCenter(view.getCenter().x, view.getCenter().y+(ZOOM_POS_ABOVE_MIDDLE*elapsed)); } else { effectsList.erase(it++); view = rw.getDefaultView(); } break; case Pause: totalelapsed += elapsed; if (totalelapsed > 1.0f) { effectsList.erase(it++); totalelapsed = 0; } break; default: break; } } }
void set_up(sf::RenderWindow& window, sf::View& view, sf::Text& winner, std::list<Cycle*>& player) { sf::Vector2u size = window.getSize(); if (size.x * 3 < size.y * 4) { view.setSize(v2f(800, (800.f * size.y) / size.x)); window.setView(view); } else if (size.x * 3 > size.y * 4) { view.setSize(v2f((600.f * size.x) / size.y, 600.f)); window.setView(view); } for (auto p : player) p->set_text_pos(view.getCenter()); winner.setPosition(view.getCenter()); }
int main(int argc, char** argv) { fcnt = 0; speed = 0.5; gen = 1; evolution_rate = 10; counter = 300; bestavg = 0; decline_counter = 0; srand(time(0)); camera.setSize(800,600); camera.setCenter(AREA_W/2,AREA_H/2); sf::ContextSettings settings; settings.antialiasingLevel = 8; window = new sf::RenderWindow(sf::VideoMode(SCREEN_W,SCREEN_H),"Evolution 0.3",sf::Style::Default,settings); window->setFramerateLimit(60); window->setView(camera); bg.setOutlineColor(sf::Color::Cyan); bg.setOutlineThickness(4.f); bg.setFillColor(sf::Color(51,153,153)); sf::CircleShape* tmp; tmp = new sf::CircleShape(15.f,5); tmp->setOutlineColor(sf::Color::Red); tmp->setFillColor(sf::Color(75,75,75)); tmp->setOutlineThickness(2.f); tmp->setOrigin(tmp->getLocalBounds().width/2,tmp->getLocalBounds().height/2); fToken = tmp; sf::Font font; font.loadFromFile("revalia.ttf"); text = new sf::Text(); text->setFont(font); text->setCharacterSize(15.f); text->setColor(sf::Color::White); vpop = new VectorPopulation(); vpop->setMutationScale(current_mutation_rate); for(int i = 0; i<CREATURE_CAP; i++) { creatures.push_back(std::shared_ptr<Creature>(new Creature(4,2,1,6))); creatures.back()->setPos(sf::Vector2f(10 + rand() % (AREA_W-20), 10 + rand() % (AREA_H-20))); creatures.back()->areaSize(AREA_W,AREA_H); creatures.back()->setSpeed(3); } for(int i = 0; i<FOOD_CAP; i++) { food.push_back(sf::Vector2f(20 + rand() % (AREA_W-40), 10 + rand() % (AREA_H-20))); } while(update()) { render(); } return 0; }
bool handleEvents(sf::Window& window, sf::View& view) { sf::Event event; bool continueRunning = true; while (window.pollEvent(event)) { if (event.type == sf::Event::Closed) { continueRunning = false; } if (event.type == sf::Event::Resized) { view.setSize(event.size.width, event.size.height); } } return continueRunning; }
bool handleResize(void){ bool limitWindowSize = true; int minWindowSize = 71, smallSide; if ( (limitWindowSize) && (event.size.height < minWindowSize) ) { event.size.height = minWindowSize; window.setSize( sf::Vector2u(event.size.width, event.size.height) ); } view.setSize(event.size.width, event.size.height); if (event.size.width <= event.size.height) { view.zoom(1.0 * InitWinX / event.size.width); } else { view.zoom(1.0 * InitWinY / event.size.height); } window.setView(view); }
void setViewRect(sf::View& view, sf::FloatRect const& newRect) { view.setCenter(center(newRect)); view.setSize(newRect.width, newRect.height); }
int main() { deltaTime = 0; sfg::SFGUI sfgui; // map sprite sf::Texture map_texture; sf::Sprite map_sprite; map_texture.loadFromFile("resources/map.png"); map_sprite.setTexture(map_texture); // window window.create(sf::VideoMode(WINDOW_WIDTH, WINDOW_HEIGHT, 32), "Runestone"); view.setSize(sf::Vector2f(WINDOW_WIDTH, WINDOW_HEIGHT)); view.setCenter(sf::Vector2f(0, 0)); window.setView(view); // timer sf::Clock clock; // thor actions thor::Action quit(sf::Event::Closed); thor::Action left(sf::Keyboard::A, thor::Action::Hold); thor::Action right(sf::Keyboard::D, thor::Action::Hold); thor::Action up(sf::Keyboard::W, thor::Action::Hold); thor::Action down(sf::Keyboard::S, thor::Action::Hold); thor::Action mouse_down(sf::Mouse::Left, thor::Action::PressOnce); thor::Action mouse_up(sf::Mouse::Left, thor::Action::ReleaseOnce); // thor action map thor::ActionMap<std::string> action_map; action_map["quit"] = quit; action_map["left"] = left; action_map["right"] = right; action_map["up"] = up; action_map["down"] = down; action_map["mouse_down"] = mouse_down; action_map["mouse_up"] = mouse_up; thor::ActionMap<std::string>::CallbackSystem action_callbacks; action_callbacks.connect("quit", &OnQuit); action_callbacks.connect("left", &OnLeft); action_callbacks.connect("right", &OnRight); action_callbacks.connect("up", &OnUp); action_callbacks.connect("down", &OnDown); action_callbacks.connect("mouse_down", &OnMouseDown); action_callbacks.connect("mouse_up", &OnMouseUp); // thor particles sf::Texture particleTexture; particleTexture.loadFromFile("Media/particle.png"); emitter.setEmissionRate(100.0f); emitter.setParticleLifetime(sf::seconds(1.0f)); particle_system.setTexture(particleTexture); // sfgui auto sfgui_window = sfg::Window::Create(); sfgui_window->SetTitle("Runestones: "); auto rune_one = sfg::Button::Create("Rune One"); rune_one->GetSignal(sfg::Button::OnMouseLeftPress).Connect(std::bind(&OnRuneOne)); rune_one->GetSignal(sfg::Button::OnMouseLeftPress).Connect(std::bind(&OnButtonClicked)); auto rune_two = sfg::Button::Create("Rune Two"); rune_two->GetSignal(sfg::Button::OnMouseLeftPress).Connect(std::bind(&OnRuneTwo)); rune_two->GetSignal(sfg::Button::OnMouseLeftPress).Connect(std::bind(&OnButtonClicked)); auto rune_three = sfg::Button::Create("Rune Three"); rune_three->GetSignal(sfg::Button::OnMouseLeftPress).Connect(std::bind(&OnRuneThree)); rune_three->GetSignal(sfg::Button::OnMouseLeftPress).Connect(std::bind(&OnButtonClicked)); auto rune_four = sfg::Button::Create("Rune Four"); rune_four->GetSignal(sfg::Button::OnMouseLeftPress).Connect(std::bind(&OnRuneFour)); rune_four->GetSignal(sfg::Button::OnMouseLeftPress).Connect(std::bind(&OnButtonClicked)); auto hbox = sfg::Box::Create(sfg::Box::Orientation::HORIZONTAL, 4.0f); hbox->Pack(rune_one); hbox->Pack(rune_two); hbox->Pack(rune_three); hbox->Pack(rune_four); sfgui_window->Add(hbox); sfgui_window->SetStyle(!sfg::Window::Style::RESIZE); sfgui_window->SetPosition(sf::Vector2f((WINDOW_WIDTH / 2.0f) - (sfgui_window->GetAllocation().width / 2.0f), WINDOW_HEIGHT - sfgui_window->GetAllocation().height)); // tileset sf::Texture tileTexture; tileTexture.loadFromFile("resources/tiles.png"); rf::TileMap tileMap(4, 8); tileMap.setTexture(tileTexture); int layout[4][8] = { {-1, -1, -1, 1, 1, -1, -1, -1}, {1,1,1,1,1,1,1,1}, {-1,-1,1,1,1,1,-1,-1}, {1,1,1,1,1,1,1,1} }; for (size_t i = 0; i < 4; i++) { for (size_t j = 0; j < 8; j++) { tileMap.setTile(layout[i][j], i, j); } } while (window.isOpen()) { deltaTime = clock.restart().asSeconds(); action_map.clearEvents(); sf::Event event; while (window.pollEvent(event)) { sfgui_window->HandleEvent(event); action_map.pushEvent(event); } particle_system.update(sf::seconds(deltaTime)); sfgui_window->Update(deltaTime); action_map.invokeCallbacks(action_callbacks, &window); window.setView(view); window.clear(sf::Color::Black); window.draw(map_sprite); window.draw(tileMap); window.draw(particle_system); sfgui.Display(window); window.display(); } return 0; }
bool update() { sf::Event event; while(window->pollEvent(event)) { switch (event.type) { case sf::Event::Closed: return false; case sf::Event::KeyPressed: switch(event.key.code) { case sf::Keyboard::Up: camera.move(0,-1); break; case sf::Keyboard::Down: camera.move(0,1); std::cout << camera.getCenter().x; break; case sf::Keyboard::Left: camera.move(-1,0); break; case sf::Keyboard::Right: camera.move(1,0); break; case sf::Keyboard::F1: for(int i = 0; i<10; i++) { updateCreatures(); } break; case sf::Keyboard::F2: for(int i = 0; i<100; i++) { updateCreatures(); } break; case sf::Keyboard::F3: fastForward(10); break; case sf::Keyboard::F4: fastForward(50); break; } break; case sf::Event::MouseMoved: if(sf::Mouse::isButtonPressed(sf::Mouse::Button::Left)) { camera.move(mousepos-window->mapPixelToCoords(sf::Mouse::getPosition(),camera)); } mousepos = window->mapPixelToCoords(sf::Mouse::getPosition(),camera); break; case sf::Event::MouseWheelMoved: if(event.mouseWheel.delta>0) { camera.zoom(1.15); } else if(event.mouseWheel.delta<0) { camera.zoom(0.85); } break; case sf::Event::MouseButtonPressed: if(mouseclock.getElapsedTime().asMilliseconds()<333) { auto center = window->mapPixelToCoords(sf::Vector2i(event.mouseButton.x,event.mouseButton.y),camera); camera.setCenter(center); camera.setSize(sf::Vector2f(SCREEN_W,SCREEN_H)); } prev_click = mouseclock.restart(); break; } } double ftest = 0; avg = 0; for(auto& creature: creatures) { creature->foodPool(food); creature->update(); for(auto fd = food.begin(); fd<food.end(); fd++) { if(distance(creature->getPos(),*fd)<15) { fd = food.erase(fd); creature->increaseFitness(1.f); } } avg += creature->getFitness(); if(creature->getFitness()>ftest) { ftest = creature->getFitness(); } } pavg = avg; avg = avg/creatures.size(); if(avg>bestavg) { best_so_far = vpop->getPopulationRef(); } else if(avg < bestavg*0.85) { vpop->revertPopulation(best_so_far); } while(food.size()<FOOD_CAP) { food.push_back(sf::Vector2f(20 + rand() % (AREA_W-40), 10 + rand() % (AREA_H-20))); } if(counter>1) { counter--; } else { updateCreatures(); counter = 60*evolution_rate; std::cout << "Generation: " << gen++; std::cout << " fittest: " << ftest; std::cout << " average: " << avg << std::endl; } return true; }
void viewSetRect(sf::View &view, sf::IntRect &rect) { sf::Vector2i size (rect.width, rect.height); sf::Vector2i center (rect.left + rect.width / 2, rect.top + rect.height / 2); view.setCenter((sf::Vector2f) center); view.setSize((sf::Vector2f) size); }