void GameCompleteScreen::setup(sf::View view , int sheepHerded) { mBackground.setPosition(view.getCenter()); sf::Vector2f textPos(view.getCenter()); float fontSize = mTitleText.getCharacterSize(); textPos.y -= fontSize; mTitleText.setPosition(textPos); textPos.y += fontSize; mSheepStatText.setString("Sheep Herded " + std::to_string(sheepHerded)); sf::FloatRect bounds = mSheepStatText.getLocalBounds(); mSheepStatText.setOrigin(bounds.width / 2.f , 0.f); fontSize = mSheepStatText.getCharacterSize(); mSheepStatText.setPosition(textPos); textPos.y += fontSize; int inc = mMenuText.at(0).getCharacterSize(); textPos.y += inc; for(sf::Text& text : mMenuText) { text.setPosition(textPos); textPos.y += inc; } }
void Map::Drawbackground(sf::View& ViewRef, E_Ground Ground) { int MinX, MaxX, MinY, MaxY; if (WCR.inMapMaker) { MinX = (int)((WCR.MMPtr->MapMakrView.getCenter().x - (WCR.MMPtr->MapMakrView.getSize().x / 2)) / CellSize); MaxX = (int)((WCR.MMPtr->MapMakrView.getCenter().x + (WCR.MMPtr->MapMakrView.getSize().x / 2)) / CellSize); MinY = (int)((WCR.MMPtr->MapMakrView.getCenter().y - (WCR.MMPtr->MapMakrView.getSize().y / 2)) / CellSize); MaxY = (int)((WCR.MMPtr->MapMakrView.getCenter().y + (WCR.MMPtr->MapMakrView.getSize().y / 2)) / CellSize); } else { MinX = (int)((ViewRef.getCenter().x - ViewRef.getSize().x / 2) / CellSize); MaxX = (int)((ViewRef.getCenter().x + ViewRef.getSize().x / 2) / CellSize); MinY = (int)((ViewRef.getCenter().y - ViewRef.getSize().y / 2) / CellSize); MaxY = (int)((ViewRef.getCenter().y + ViewRef.getSize().y / 2) / CellSize); } WCR.LimitVariable(0, MapWidth - 1, MinX); WCR.LimitVariable(0, MapWidth - 1, MaxX); WCR.LimitVariable(0, MapHeight - 1, MinY); WCR.LimitVariable(0, MapHeight - 1, MaxY); for (int LID = 0; LID < BackgroundMatrix.size(); LID++) { if (WCR.MMPtr->OnlyShowThisLayer->IsActive() && WCR.inMapMaker && OrderedBackgroundLayers[LID] != WCR.MMPtr->curTileLayer) continue;//Skip, not the layer we want. int BL_ = BackgroundLayers[OrderedBackgroundLayers[LID]]; if (Ground == E_vForeground) { if (BL_ >= 0) continue;//Not up to yet } else if (Ground == E_vMiddleground) { if (BL_ < 0) break;//Gone past if (BL_ != 0) continue;//Go till found. } else if (Ground == E_vBackground) { if (BL_ < 1) break;//Gone past } BL_ = OrderedBackgroundLayers[LID]; for (int i = MinX; i <= MaxX; i++) for (int ii = MinY; ii <= MaxY; ii++) { if (BackgroundMatrix[BL_][i][ii].tileID != -1)//Not empty space. { WCR.MMPtr->TileSets[BackgroundMatrix[BL_][i][ii].tileSetID].Tiles[BackgroundMatrix[BL_][i][ii].tileID].setPosition(i * CellSize, ii * CellSize); WCR.RenderRef.draw(WCR.MMPtr->TileSets[BackgroundMatrix[BL_][i][ii].tileSetID].Tiles[BackgroundMatrix[BL_][i][ii].tileID]); } } } }
inline bool isInsideView(const sf::View& targetView, const sf::FloatRect& boundingBox) { sf::FloatRect view(targetView.getCenter().x - targetView.getSize().x / 2.f, targetView.getCenter().y - targetView.getSize().y / 2.f, targetView.getSize().x, targetView.getSize().y); view.left -= VIEW_MARGIN; view.top -= VIEW_MARGIN; view.width += 2 * VIEW_MARGIN; view.height += 2 * VIEW_MARGIN; return view.intersects(boundingBox); }
void drawInViewSprites(sf::RenderWindow &window, sf::View &view,gameMap &map, std::vector< std::vector < sf::Sprite > > &sprites ) { int leftX = floor((view.getCenter().x - view.getSize().x/2)/(map.getTile(0).getTileDims().x)); int rightX = ceil((view.getCenter().x + view.getSize().x/2)/(map.getTile(0).getTileDims().x)); int upY = floor((view.getCenter().y - view.getSize().y/2)/(map.getTile(0).getTileDims().y)); int downY = ceil((view.getCenter().y + view.getSize().y/2)/(map.getTile(0).getTileDims().y)); for(int y = upY; y<downY; y++) { for(int x = leftX; x<rightX; x++) { window.draw(sprites[y][x]); } } }
void BonusesPanelUpdate(BonusesPanel& panel, sf::View view) { float view_top = view.getCenter().y - view.getSize().y / 2.f; float view_left = view.getCenter().x - view.getSize().x / 2.f; panel.sprite.setPosition(view_left + PanelXPos, view_top + PanelYPos); sf::FloatRect panel_box = panel.sprite.getGlobalBounds(); float cell_x = panel_box.left + PanelLeftBorderWidth; float cell_y = panel_box.top + PanelTopBorderHeight; float delimeter = 0; for (size_t i = 0; i < panel.items->size(); i++) { if (i != 0) { delimeter = CellDelimeterHeight; } panel.items[0][i]->visual->sprite.setPosition(cell_x, cell_y + (CellHeight * i) + delimeter); } }
void Interface::update(sf::View v) { //Positions the text relative to the view: _text[CLOCK].setPosition( ((v.getCenter().x - (v.getSize().x / 2)) + 5), //X ((v.getCenter().y - (v.getSize().y / 2)) + 5)); //Y _text[CLOCK_BESTTIME].setPosition( (_text[CLOCK].getPosition().x), //X (_text[CLOCK].getPosition().y + (_text[CLOCK].getGlobalBounds().height + 2))); //Y _text[FPS].setPosition( ((v.getCenter().x + (v.getSize().x / 2)) - (_text[FPS].getGlobalBounds().width + 5)), //X ((v.getCenter().y + (v.getSize().y / 2)) - (_text[FPS].getGlobalBounds().height + 5))); //Y }
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 Minimap::DrawTheMinimap(sf::RenderWindow& window, Game& game, sf::View view) { if (ShouldBeUpdated) { UpdateTheMinimap(game.map); ShouldBeUpdated = false; } MinimapSprite.setPosition(sf::Vector2f(window.getSize().x - 200.f, 50.f)); window.draw(MinimapSprite); MinimapBackground.setPosition(sf::Vector2f(window.getSize().x - 200.f, 50.f)); window.draw(MinimapBackground); for (int j = 0; j < game.MOBs.size(); j++) { AllyMobSprite.setPosition(sf::Vector2f(game.MOBs[j]->position.x * MINIMAP_WIDTH / MAP_DIM / TEX_DIM + window.getSize().x - 200.f - 4, game.MOBs[j]->position.y * MINIMAP_WIDTH / MAP_DIM / TEX_DIM + 50.f)); window.draw(AllyMobSprite); } for (int j = 0; j < game.structures.size(); j++) { AllyPlantSprite.setPosition(sf::Vector2f(game.structures[j].position.x * MINIMAP_WIDTH / MAP_DIM + window.getSize().x - 200.f - 4, game.structures[j].position.y * MINIMAP_WIDTH / MAP_DIM + 50.f)); window.draw(AllyPlantSprite); } sf::Vector2f ViewSize = view.getSize(); sf::Vector2f ViewCornerPosition = view.getCenter() - ViewSize / 2.0f; MinimapFOVIndicator.setPosition(ViewCornerPosition.x * MINIMAP_WIDTH / MAP_DIM / TEX_DIM + window.getSize().x - 200.f, ViewCornerPosition.y * MINIMAP_WIDTH / MAP_DIM / TEX_DIM + 50.f); MinimapFOVIndicator.setSize(sf::Vector2f(ViewSize.x * MINIMAP_WIDTH / MAP_DIM / TEX_DIM, ViewSize.y * MINIMAP_WIDTH / MAP_DIM / TEX_DIM)); window.draw(MinimapFOVIndicator); }
/* ---------------------------------------------------------------------- * Author: Julian * Date: 25 March 2014 * Description: Renders the light manager * ---------------------------------------------------------------------- */ void LightManager::render(const sf::View& camera) { // Set the view of the camera sf::Vector2f cameraPos(camera.getCenter() - sf::Vector2f(context.window->getSize() / 2u)); context.window->setView(camera); // Clear the window to see light only //context.window->clear(sf::Color::White); // Clear the light texture lightTexture.clear(); // Render all the lights for(auto&& light : lightList) light.render(camera, lightTexture); // Render the dusk color over the light texture sf::RectangleShape shape(sf::Vector2f(context.window->getSize())); shape.setFillColor(duskColor); lightTexture.draw(shape); // Render the light texture on the rest of the window with BlendMultiply! lightTexture.display(); sf::Sprite sprite(lightTexture.getTexture()); sprite.setPosition(cameraPos); context.window->draw(sprite, sf::BlendMultiply); }
void rysuj_tekst( sf::RenderWindow & okno, sf::String _tresc, float x, float y ) { double kamera_xHud = kamera.getCenter().x - kamera.getSize().x/2; double kamera_yHud = kamera.getCenter().y - kamera.getSize().y/2; tekst.setString( _tresc ); tekst.setPosition( kamera_xHud + x, kamera_yHud + y ); okno.draw( tekst ); }
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()); }
void Renderer::setView(sf::View const& view) { m_renderer->setView(view); m_screenRect = m_renderer->getViewport(view); auto center = view.getCenter(); m_screenRect.left = center.x - m_screenRect.width / 2.f; m_screenRect.top = center.y - m_screenRect.height / 2.f; }
sf::Vector2f getViewPosition(sf::View& view) { sf::Vector2f center = view.getCenter(); sf::Vector2f half = view.getSize() / 2.0f; return center - half; }
void aktualizuj_hud() { aktualizuj_kamere(); double kamera_xHud = kamera.getCenter().x - kamera.getSize().x/2; double kamera_yHud = kamera.getCenter().y - kamera.getSize().y/2; ustaw_pozycjePaskaHp( kamera_xHud+10 , kamera_yHud + 5 ); ustaw_pozycjePaskaBonusu( kamera_xHud+30 , kamera_yHud + 440 ); aktualizuj_timer( 1 ); }
void Level::logic(sf::View& camera, unsigned int delta) { // Perform actions. ActionInterface::logic(*this); // Remove all dead entities. for(list<Entity*>::iterator it = mDeadEntities.begin(); it != mDeadEntities.end(); it++) mRemoveEntity(*it); mDeadEntities.clear(); // Perform all entity logic. for(list<Entity*>::iterator it = mEntities.begin(); it != mEntities.end(); it++) (*it)->logic(*this, delta); // If all items have been picked up, display a floating text and give a bonus. if(mPickups.empty() && !mPickupAward) { addFloatingText("Level Cleared!", mPlayer.getPosition(), sf::Color::Yellow, 30); distributeChangeScore(250); mPickupAward = true; } // Perform floating text logic if the interval is passed. if(mFloatingTextTimer.getTime() >= FLOATINGTEXT_TIMER_INTERVAL) { list<list<sf::Text>::iterator > removeList; for(list<sf::Text>::iterator it = mFloatingTexts.begin(); it != mFloatingTexts.end(); ++it) { it->move(0, -FLOATINGTEXT_MOVE_STEP); sf::Color c = it->getColor(); it->setColor(sf::Color(c.r, c.g, c.b, c.a - 3)); if(it->getColor().a == 0) removeList.push_back(it); } for(list<list<sf::Text>::iterator >::iterator it = removeList.begin(); it != removeList.end(); ++it) mFloatingTexts.erase(*it); mFloatingTextTimer.start(); } // Center the player on the screen. camera.setCenter(mPlayer.getX() + (mPlayer.getWidth() / 2), mPlayer.getY() + (mPlayer.getHeight() / 2)); // Make sure that the camera doesn't go out of bounds. sf::Vector2f center = camera.getCenter(), half(camera.getSize().x / 2, camera.getSize().y / 2); if(center.x - half.x < 0) center.x = half.x; if(center.x + half.x > mMap.getWidth()) center.x = mMap.getWidth() - half.x; if(center.y - half.y < 0) center.y = half.y; if(center.y + half.y > mMap.getHeight()) center.y = mMap.getHeight() - half.y; camera.setCenter(center); }
CameraNode::CameraNode(sf::View &camera) : SceneNode(Category::Camera), mCamera(camera), mOriginalCenter(camera.getCenter()), mShaking(false), mShakingDuration(sf::Time::Zero), mShakingET(sf::Time::Zero), mSplashing(false), mTransparency(128.f) { }
void credits( sf::RenderWindow & okno ) { sf::RectangleShape tlo_credits; tlo_credits.setFillColor( sf::Color::Black ); tlo_credits.setPosition( kamera.getCenter().x - kamera.getSize().x/2, kamera.getCenter().y - kamera.getSize().y/2 ); tlo_credits.setSize( sf::Vector2f( 640, 480 ) ); okno.draw( tlo_credits ); rysuj_tekst( okno, "Square War Alpha 0.9", 320, 20 ); rysuj_tekst( okno, "Autorzy:\nOlek Szatko\nMarcin Kula\nRafal Kot", 320, 240 ); }
sf::FloatRect viewRect(sf::View const& view) { sf::Vector2f const viewCenter(view.getCenter()); sf::Vector2f const viewSize(view.getSize()); sf::FloatRect const viewRect( viewCenter.x - viewSize.x / 2, viewCenter.y - viewSize.y / 2, viewSize.x, viewSize.y); return viewRect; }
void gridLine(sf::View view, float xSep, float ySep, sf::Color c, std::vector<sf::Vertex> &Line) { float Left = view.getCenter().x - view.getSize().x / 2; float Right = view.getCenter().x + view.getSize().x / 2; float Top = view.getCenter().y - view.getSize().y / 2; float Bottom = view.getCenter().y + view.getSize().y / 2; int noOfLinesX = Right / xSep - (Left - fmod(Left, xSep)) / xSep + 1; int noOfLinesY = Bottom / ySep - (Top - fmod(Top, ySep)) / ySep + 1; float x = Left - fmod(Left, xSep); for(int i = 0; i < noOfLinesX; i++) { Line.emplace_back(sf::Vector2f(x, Top), c); Line.emplace_back(sf::Vector2f(x, Bottom), c); x += xSep; } x = Top - fmod(Top, ySep); for(int i = 0; i < noOfLinesY; i++) { Line.emplace_back(sf::Vector2f(Left, x), c); Line.emplace_back(sf::Vector2f(Right, x), c); x += ySep; } // window.draw(&Line[0], Line.size(), sf::Lines); }
sf::FloatRect Helper::getViewBounds(const sf::View &view) { sf::FloatRect rt; rt.left = view.getCenter().x - view.getSize().x/2; rt.top = view.getCenter().y - view.getSize().y/2; rt.width = view.getSize().x; rt.height = view.getSize().y; // std::cout << "View bounds" << rt.left << " " << rt.top << " " << rt.width << " " << rt.height << std::endl; return rt; }
void GamePlay::screenMovement(Player &p, sf::View &v, sf::Sprite &s) //!< screen movement function to keep in track with player { //!screen move right if(p.getPos().getX() > v.getCenter().x + 40)v.move(2.75f,0.f); if(p.getPos().getX() > v.getCenter().x + 40 && p.getBuff() == 1)v.move(4.f,0.f); //!screen move left if(p.getPos().getX() < v.getCenter().x - 40)v.move(-2.75f,0.f); if(p.getPos().getX() < v.getCenter().x - 40 && p.getBuff() == 1)v.move(-4.f,0.f); //!screen move up if(p.getPos().getY() < v.getCenter().y + 110)v.move(0.f,-5.f); //!screen move down if(p.getPos().getY() > v.getCenter().y + 120) v.move(0.f,5.f); //!keeps background static s.setPosition(v.getCenter().x-400.f ,v.getCenter().y -300.f); //!keeps messages in place message[1].setPosition(v.getCenter().x-295.f ,v.getCenter().y -225.f); message[2].setPosition(v.getCenter().x+175.f ,v.getCenter().y -225.f); message[3].setPosition(v.getCenter().x+120.f ,v.getCenter().y -200.f); }
void Tilemap::update(const sf::View& view, int margin) { sf::IntRect newView((view.getCenter().x - (view.getSize().x / 2.f)) / tilesize, (view.getCenter().y - (view.getSize().y / 2.f)) / tilesize, (view.getSize().x) / tilesize, (view.getSize().y) / tilesize); // @TODO Handle rotations & all the shizzle because SFML. sf::IntRect maprect(0, 0, wid, hei); newView = clip(newView, maprect); const sf::Vector2i topleft(newView.left, newView.top), bottomright(newView.left + newView.width, newView.top + newView.height); if (wasTilemapChanged || !vboview.contains(topleft) || !vboview.contains(bottomright)) // if a VBO update is required { vboview = sf::IntRect(newView.left - margin, newView.top - margin, newView.width + (margin * 2), newView.height + (margin * 2)); vboview = clip(vboview, maprect); vbo = buildVertexArray(vboview); } }
void MeshScene::SetView(const sf::View& view) { //we only want to do this if the aspect ratio has changed //to avoid unnecessary calls to tan const float aspect = view.getSize().x / view.getSize().y; if (m_camera->GetAspectRatio() != aspect)//potentially rendered moot by float comparison... { m_camera->SetAspectRatio(aspect); const float angle = std::tan(m_camera->GetFOV() / 2.f * 0.0174532925f); m_cameraZ = (static_cast<float>(view.getSize().y) / 2.f) / angle; m_cameraZ *= -m_sceneScale; } //set position m_camera->SetPosition(-view.getCenter().x * m_sceneScale, view.getCenter().y * m_sceneScale, m_cameraZ); //update viewport sf::Vector2u winSize = m_renderWindow.getSize(); GLuint x = static_cast<GLuint>(view.getViewport().left * static_cast<float>(winSize.x)); GLuint y = static_cast<GLuint>((1.f - view.getViewport().top) * static_cast<float>(winSize.y)); GLuint w = static_cast<GLuint>(view.getViewport().width * static_cast<float>(winSize.x)); GLuint h = static_cast<GLuint>(view.getViewport().height * static_cast<float>(winSize.y)); //invert position y -= h; glViewport(x, y, w, h); //update directional light if (m_useDirectionalLight) { glm::vec3 pos = m_camera->GetPosition(); m_directionalLight->SetPosition(-pos.x + lightPosOffset.x, -pos.y + lightPosOffset.y, directionalLightNear); m_directionalLight->SetTarget(-pos.x + lightTargetOffset.x, -pos.y + lightTargetOffset.y, directionalLightFar); } }
void Wall::setType(Wall::Type type, const sf::View &view) { float width = abs(view.getSize().x), height = abs(view.getSize().y), x = view.getCenter().x, y = view.getCenter().y; if (type == Left) { setPosition(-0.5, y); setSize(1, height); } else if (type == Right) { setPosition(width + 0.5, y); setSize(1, height); } else if (type == Top){ setPosition(x, height + 0.25); setSize(width, 0.5); } else if (type == Bottom) { setPosition(x, -0.25); setSize(width, 0.5); } else { _type = None; return ; } _type = type; }
bool entity_ojectInView(tmx::MapObject& object, sf::View& view) { return createRect(object.GetPosition(), object.GetSize()).intersects((createRect(view.getCenter() - view.getSize() / 1.5f, view.getSize() * 1.5f))); }
sf::View updateView(sf::View view) { // Calculate view movement int x = view.getCenter().x; int y = view.getCenter().y; float percX = gameEvents.lastMouseWindow.x / window_width; float percY = gameEvents.lastMouseWindow.y / window_height; if(gameEvents.isAHeld) { x -= SCROLL_SPEED; } else if(percX >= 0.0f && percX <= DEFAULT_SCROLL_BORDER && global_scrollBordersEnabled) { x -= SCROLL_SPEED_MODIFIER * SCROLL_SPEED; } if(gameEvents.isDHeld) { x += SCROLL_SPEED; } else if(percX >= 1.0f - DEFAULT_SCROLL_BORDER && percX <= 1.0f && global_scrollBordersEnabled) { x += SCROLL_SPEED_MODIFIER * SCROLL_SPEED; } if(gameEvents.isWHeld) { y -= SCROLL_SPEED; } else if(percY >= 0.0f && percY <= DEFAULT_SCROLL_BORDER && global_scrollBordersEnabled) { y -= SCROLL_SPEED_MODIFIER * SCROLL_SPEED; } if(gameEvents.isSHeld) { y += SCROLL_SPEED; } else if(percY >= 1.0f - DEFAULT_SCROLL_BORDER && percY <= 1.0f && global_scrollBordersEnabled) { y += SCROLL_SPEED_MODIFIER * SCROLL_SPEED; } // Check for horizontal boundaries // if(x < window_width/2 - BOX_SIZE/2) // { // x = window_width/2 - BOX_SIZE/2; // } // // if(x > MAX_WORLD_WIDTH * BOX_SIZE - window_width/2 - BOX_SIZE/2) // { // x = MAX_WORLD_WIDTH * BOX_SIZE - window_width/2 - BOX_SIZE/2; // } // Check for vertical boundaries if(y > MAX_WORLD_HEIGHT * BOX_SIZE + GROUND_STARTING_HEIGHT - window_height/2 - BOX_SIZE/2) { y = MAX_WORLD_HEIGHT * BOX_SIZE + GROUND_STARTING_HEIGHT - window_height/2 - BOX_SIZE/2; } // 53 Blocks to the top! view.setCenter(x, y); return view; }
void Map::DrawMap(sf::View& ViewRef) { int MinX, MaxX, MinY, MaxY; if (WCR.inMapMaker) { MinX = (int)((WCR.MMPtr->MapMakrView.getCenter().x - (WCR.MMPtr->MapMakrView.getSize().x / 2)) / CellSize); MaxX = (int)((WCR.MMPtr->MapMakrView.getCenter().x + (WCR.MMPtr->MapMakrView.getSize().x / 2)) / CellSize); MinY = (int)((WCR.MMPtr->MapMakrView.getCenter().y - (WCR.MMPtr->MapMakrView.getSize().y / 2)) / CellSize); MaxY = (int)((WCR.MMPtr->MapMakrView.getCenter().y + (WCR.MMPtr->MapMakrView.getSize().y / 2)) / CellSize); } else { MinX = (int)((ViewRef.getCenter().x - ViewRef.getSize().x / 2) / CellSize); MaxX = (int)((ViewRef.getCenter().x + ViewRef.getSize().x / 2) / CellSize); MinY = (int)((ViewRef.getCenter().y - ViewRef.getSize().y / 2) / CellSize); MaxY = (int)((ViewRef.getCenter().y + ViewRef.getSize().y / 2) / CellSize); } WCR.LimitVariable(0, MapWidth - 1, MinX); WCR.LimitVariable(0, MapWidth - 1, MaxX); WCR.LimitVariable(0, MapHeight - 1, MinY); WCR.LimitVariable(0, MapHeight - 1, MaxY); //sf::Drawable* DrawPtr; for (int i = MinX; i <= MaxX; i++) for (int ii = MinY; ii <= MaxY; ii++) { if (MapMatrix[i][ii].tileID != -1)//Not empty space. { if (!(WCR.inMapMaker && WCR.MMPtr->HideObjectSprites->IsActive())) { WCR.MMPtr->TileSets[MapMatrix[i][ii].tileSetID].Tiles[MapMatrix[i][ii].tileID].setPosition(i * CellSize, ii * CellSize); WCR.RenderRef.draw(WCR.MMPtr->TileSets[MapMatrix[i][ii].tileSetID].Tiles[MapMatrix[i][ii].tileID]); } if (WCR.inMapMaker) if (WCR.MMPtr->viewObjectTypes) { sf::RectangleShape DrawRect; DrawRect.setSize(sf::Vector2f(32, 32)); switch (MapMatrix[i][ii].objectType) { case 0:DrawRect.setFillColor(sf::Color::White); break;//Invis case 1:DrawRect.setFillColor(sf::Color::Blue); break;//Solid case 2:DrawRect.setFillColor(sf::Color::Green); break;//Bouncy case 3:DrawRect.setFillColor(sf::Color::Red); break;//Death case 4:DrawRect.setFillColor(sf::Color::Yellow); break;//Next Level case 5:DrawRect.setFillColor(sf::Color::Cyan); break;//Ice } DrawRect.setFillColor(sf::Color(DrawRect.getFillColor().r, DrawRect.getFillColor().g, DrawRect.getFillColor().b, 100)); DrawRect.setPosition(i * CellSize, ii * CellSize); WCR.RenderRef.draw(DrawRect); if (MapMatrix[i][ii].pixelPerfect) { sf::CircleShape DrawCirc; DrawCirc.setRadius(8); DrawCirc.setPosition(i * CellSize + 8, ii * CellSize + 8); sf::Color MyCol_ = sf::Color::Black; MyCol_.a = 100; DrawCirc.setFillColor(MyCol_); WCR.RenderRef.draw(DrawCirc); } } } } }
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 LevelLoader::render(sf::RenderWindow &window, sf::View view) { sf::Texture errtexture; errtexture.loadFromFile("error.png"); sf::Sprite errsprite; errsprite.setTexture(errtexture); for(int i = 0; i < width; i++) { for(int j = 0; j < height; j++) { if(map_t[i][j].getX() < view.getCenter().x + view.getSize().x/2 && map_t[i][j].getX() + tilesize > view.getCenter().x - view.getSize().x/2 && map_t[i][j].getY() < view.getCenter().y + view.getSize().y/2 && map_t[i][j].getY() + tilesize > view.getCenter().y - view.getSize().y/2) { int tempInt = 0; if(map_t[i][j].ID == 1) { map_t[i][j].setSprite(sprites[tempInt]); map_t[i][j].setPosition(i * tilesize, j * tilesize); } tempInt++; if(map_t[i][j].ID == 2) { map_t[i][j].setSprite(sprites[tempInt]); map_t[i][j].setPosition(i * tilesize, j * tilesize); } tempInt++; if(map_t[i][j].ID == 3) { map_t[i][j].setSprite(sprites[tempInt]); map_t[i][j].setPosition(i * tilesize, j * tilesize); } tempInt++; if(map_t[i][j].ID == 4) { map_t[i][j].setSprite(sprites[tempInt]); map_t[i][j].setPosition(i * tilesize, j * tilesize); } tempInt++; if(map_t[i][j].ID == 5) { map_t[i][j].setSprite(sprites[tempInt]); map_t[i][j].setPosition(i * tilesize, j * tilesize); } tempInt++; if(map_t[i][j].ID == 6) { map_t[i][j].setSprite(sprites[tempInt]); map_t[i][j].setPosition(i * tilesize, j * tilesize); } tempInt++; if(map_t[i][j].ID == 7) { map_t[i][j].setSprite(sprites[tempInt]); map_t[i][j].setPosition(i * tilesize, j * tilesize); } tempInt++; if(map_t[i][j].ID == 8) { map_t[i][j].setSprite(sprites[tempInt]); map_t[i][j].setPosition(i * tilesize, j * tilesize); } tempInt++; if(map_t[i][j].ID == 9) { map_t[i][j].setSprite(sprites[tempInt]); map_t[i][j].setPosition(i * tilesize, j * tilesize); } if(map_t[i][j].ID == 0) { map_t[i][j].setSprite(errsprite); map_t[i][j].setPosition(i * tilesize, j * tilesize); } window.draw(map_t[i][j].getSprite()); } } } }
void LightDirectionEmission::render(const sf::View &view, sf::RenderTexture &lightTempTexture, sf::RenderTexture &antumbraTempTexture, const std::vector<QuadtreeOccupant*> &shapes, sf::Shader &unshadowShader, float shadowExtension) { lightTempTexture.setView(view); LightSystem::clear(lightTempTexture, sf::Color::White); // Mask off light shape (over-masking - mask too much, reveal penumbra/antumbra afterwards) for (int i = 0; i < shapes.size(); i++) { LightShape* pLightShape = static_cast<LightShape*>(shapes[i]); // Get boundaries std::vector<LightSystem::Penumbra> penumbras; std::vector<int> innerBoundaryIndices; std::vector<int> outerBoundaryIndices; std::vector<sf::Vector2f> innerBoundaryVectors; std::vector<sf::Vector2f> outerBoundaryVectors; LightSystem::getPenumbrasDirection(penumbras, innerBoundaryIndices, innerBoundaryVectors, outerBoundaryIndices, outerBoundaryVectors, pLightShape->_shape, _castDirection, _sourceRadius, _sourceDistance); if (innerBoundaryIndices.size() != 2 || outerBoundaryIndices.size() != 2) continue; LightSystem::clear(antumbraTempTexture, sf::Color::White); antumbraTempTexture.setView(view); sf::ConvexShape maskShape; float maxDist = 0.0f; for (int j = 0; j < pLightShape->_shape.getPointCount(); j++) maxDist = std::max(maxDist, vectorMagnitude(view.getCenter() - pLightShape->_shape.getTransform().transformPoint(pLightShape->_shape.getPoint(j)))); float totalShadowExtension = shadowExtension + maxDist; maskShape.setPointCount(4); maskShape.setPoint(0, pLightShape->_shape.getTransform().transformPoint(pLightShape->_shape.getPoint(innerBoundaryIndices[0]))); maskShape.setPoint(1, pLightShape->_shape.getTransform().transformPoint(pLightShape->_shape.getPoint(innerBoundaryIndices[1]))); maskShape.setPoint(2, pLightShape->_shape.getTransform().transformPoint(pLightShape->_shape.getPoint(innerBoundaryIndices[1])) + vectorNormalize(innerBoundaryVectors[1]) * totalShadowExtension); maskShape.setPoint(3, pLightShape->_shape.getTransform().transformPoint(pLightShape->_shape.getPoint(innerBoundaryIndices[0])) + vectorNormalize(innerBoundaryVectors[0]) * totalShadowExtension); maskShape.setFillColor(sf::Color::Black); antumbraTempTexture.draw(maskShape); sf::VertexArray vertexArray; vertexArray.setPrimitiveType(sf::PrimitiveType::Triangles); vertexArray.resize(3); { sf::RenderStates states; states.blendMode = sf::BlendAdd; states.shader = &unshadowShader; // Unmask with penumbras for (int j = 0; j < penumbras.size(); j++) { unshadowShader.setParameter("lightBrightness", penumbras[j]._lightBrightness); unshadowShader.setParameter("darkBrightness", penumbras[j]._darkBrightness); vertexArray[0].position = penumbras[j]._source; vertexArray[1].position = penumbras[j]._source + vectorNormalize(penumbras[j]._lightEdge) * totalShadowExtension; vertexArray[2].position = penumbras[j]._source + vectorNormalize(penumbras[j]._darkEdge) * totalShadowExtension; vertexArray[0].texCoords = sf::Vector2f(0.0f, 1.0f); vertexArray[1].texCoords = sf::Vector2f(1.0f, 0.0f); vertexArray[2].texCoords = sf::Vector2f(0.0f, 0.0f); antumbraTempTexture.draw(vertexArray, states); } } antumbraTempTexture.display(); // Multiply back to lightTempTexture sf::RenderStates antumbraRenderStates; antumbraRenderStates.blendMode = sf::BlendMultiply; sf::Sprite s; s.setTexture(antumbraTempTexture.getTexture()); lightTempTexture.setView(lightTempTexture.getDefaultView()); lightTempTexture.draw(s, antumbraRenderStates); lightTempTexture.setView(view); } for (int i = 0; i < shapes.size(); i++) { LightShape* pLightShape = static_cast<LightShape*>(shapes[i]); if (pLightShape->_renderLightOverShape) { pLightShape->_shape.setFillColor(sf::Color::White); lightTempTexture.draw(pLightShape->_shape); } } // Multiplicatively blend the light over the shadows sf::RenderStates lightRenderStates; lightRenderStates.blendMode = sf::BlendMultiply; lightTempTexture.setView(lightTempTexture.getDefaultView()); lightTempTexture.draw(_emissionSprite, lightRenderStates); lightTempTexture.display(); }