void GameLoop() { while (GameRunning) { sf::Event Event; while (GameWin.pollEvent(Event)) { if (Event.type == sf::Event::Closed) { GameWin.close(); GameRunning = false; } if (Event.type == sf::Event::KeyPressed) { if (Event.key.code == sf::Keyboard::Escape) { GameWin.close(); GameRunning = false; } if (Event.key.code == sf::Keyboard::P && Paused == true) { Paused = false; } else if (Event.key.code == sf::Keyboard::P && Paused == false) { Paused = true; } } if (Event.type == sf::Event::KeyReleased) { } } if(!Paused) World->Step(timeStep, velIter, posIter); b2Vec2 pos = Body->GetPosition(); float angle = Body->GetAngle(); box.setPosition(pos.x, pos.y); box.setRotation(angle); b2Vec2 pos2 = Body2->GetPosition(); float angle2 = Body2->GetAngle(); box2.setPosition(pos2.x, pos2.y); box2.setRotation(angle2); DrawGame(); } }
void cWindow::paintLighting() { float power; int tex; int lightsDisplayed = 0; int priority = 0; for (int a = 0; a < 2; a++) { priority = 0; while (priority < LIMIT_PRIORITY_LIGHT && lightsDisplayed < game.unitCounter) { for (int i = 0; i < game.unitCounter; i++) { if (game.unit[i].light.priority == priority && game.unit[i].light.power > 0 && game.unit[i].light.texture != -1) { tex = game.unit[i].light.texture; brushRect.setTexture(&visual.gameTex[tex].handle); brushRect.setPosition(game.unit[i].pos); brushRect.setFillColor(sf::Color(255, 255, 255, max(0.00f, min(255.00f, 300.00f - game.ambientLight)))); if (!settings.enableDynamicLight) { brushRect.setTexture(&visual.gameTex[visual.addTexture("light_white.png")].handle); brushRect.setFillColor(sf::Color(255, 255, 255, 50.00f)); } brushRect.setTextureRect(sf::IntRect(0, 0, visual.gameTex[tex].handle.getSize().x, visual.gameTex[tex].handle.getSize().y)); power = game.unit[i].light.power; if (game.unit[i].light.flickerMod != 0.00f) { power += power * (game.unit[i].light.flickerMod * abs(game.unit[i].light.flickerCurTime / game.unit[i].light.flickerTime - 1.00f)); power += math.randf(-5.00f, 5.00f); } brushRect.setOrigin(sf::Vector2f(power, power)); brushRect.setSize(sf::Vector2f(power * 2.00f, power * 2.00f)); // Directional brushRect.setRotation(0.00f); if (game.unit[i].light.directional) { brushRect.setRotation(-game.unit[i].facingAngle); } // Painting to two textures if (settings.enableDynamicLight && a == 0) { window.texHandleLight.draw(brushRect, window.matrixHandle); } else if (settings.enableDynamicLight && a == 1) { window.texHandleLightMult.draw(brushRect, window.matrixHandle); } else { window.texHandle.draw(brushRect, window.matrixHandle); } } } priority += 1; } if (!math.intToBool(settings.enableBetterLight)) { break; } } brushRect.setRotation(0.00f); window.texHandleLight.display(); if (settings.enableBetterLight) { window.texHandleLightMult.display(); } }
void Sprited::draw(sf::RenderWindow *window) { // Scroll sprite sf::Vector2i shift = this->currentAnimation->frames[this->currentFrame]; this->sprite.setTextureRect(sf::IntRect(shift.x, shift.y, this->width, this->height)); this->sprite.setRotation((this->getWRotation() - this->camera->getWRotation()) / (2 * M_PI) * 360); // + this->wRotation / (2 * M_PI) * 360); if (this->camera) { // Transform sprite wPosition based on camera wPosition & wRotation this->sprite.setPosition(this->applyCameraTransformation(this->getWPosition())); } else { // No camera set, meaning this object's wPosition is not affected by camera. this->sprite.setPosition(this->getWPosition().x, this->getWPosition().y); } window->draw(this->sprite); Movable::draw(window); #ifdef DEBUG static sf::RectangleShape origin; origin.setOrigin(2, 2); origin.setSize(sf::Vector2f(5, 5)); origin.setPosition(this->sprite.getPosition()); origin.setFillColor(sf::Color(255, 0, 0, 128)); window->draw(origin); static sf::RectangleShape rect; rect.setFillColor(sf::Color::Transparent); rect.setOutlineThickness(1); rect.setOutlineColor(sf::Color::Blue); rect.setOrigin(this->sprite.getOrigin()); rect.setPosition(this->sprite.getPosition()); rect.setRotation(this->sprite.getRotation()); rect.setSize(sf::Vector2f(this->sprite.getTextureRect().width, this->sprite.getTextureRect().height)); window->draw(rect); sf::FloatRect wHitbox = this->getWHitbox(); static sf::RectangleShape hitboxRect; hitboxRect.setFillColor(sf::Color::Transparent); hitboxRect.setOutlineThickness(1); hitboxRect.setOutlineColor(sf::Color::Red); hitboxRect.setOrigin(sf::Vector2f(wHitbox.width / 2, wHitbox.height / 2)); hitboxRect.setSize(sf::Vector2f(wHitbox.width, wHitbox.height)); hitboxRect.setPosition(this->applyCameraTransformation(this->getWPosition())); hitboxRect.setRotation(-this->camera->getWRotation() / M_PI * 180); window->draw(hitboxRect); #endif }
sf::RectangleShape Spriteeditor::shapeRealBorder(Sprite * sprite, sf::RectangleShape shape) { shape.setPosition(sprite->getPosition().x, sprite->getPosition().y); sf::Rect<float> border = sprite->getLocalBounds(); shape.setSize(sf::Vector2f(border.width, border.height)); shape.setScale(sprite->getScale()); shape.setRotation(sprite->getRotation()); return shape; }
void Init() { GameWin.create(sf::VideoMode(800, 600, 32), "Physics Test - [SFML & Box2D]"); if (!font.loadFromFile("arial.ttf")) { exit(0); } PauseText.setFont(font); pauseText = to_string(Paused); PauseText.setCharacterSize(10); PauseText.setString("PAUSED:" + pauseText); PauseText.setPosition(sf::Vector2f(700, 10)); PauseText.setFillColor(sf::Color::Yellow); box.setPosition(sf::Vector2f(15, 5)); box.setSize(sf::Vector2f(10, 10)); box.setFillColor(sf::Color(255, 0, 0)); box2.setPosition(sf::Vector2f(50, 5)); box2.setSize(sf::Vector2f(15, 15)); box2.setFillColor(sf::Color(0, 255, 0)); ground.setPosition(sf::Vector2f(0, 540)); ground.setSize(sf::Vector2f(800, 560)); ground.setFillColor(sf::Color(0, 0,255)); wall.setPosition(sf::Vector2f(1, 1)); wall.setSize(sf::Vector2f(10, 580)); wall.setFillColor(sf::Color(0, 0, 255)); wall2.setPosition(sf::Vector2f(790, 1)); wall2.setSize(sf::Vector2f(10, 580)); wall2.setFillColor(sf::Color(0, 0, 255)); World = new b2World(gravity); World->SetGravity(gravity); groundBodyDef.type = b2_staticBody; groundBodyDef.position.Set(0, 540); groundBody = World->CreateBody(&groundBodyDef); wallBodyDef.type = b2_staticBody; wallBodyDef.position.Set(10, 580); wallBody = World->CreateBody(&wallBodyDef); wallBodyDef2.type = b2_staticBody; wallBodyDef2.position.Set(790, 1); wallBody2 = World->CreateBody(&wallBodyDef2); ballBodyDef.type = b2_dynamicBody; ballVector.Set(10, 10); ballBodyDef.angularVelocity = 0.0f; ballBodyDef.linearVelocity = ballVector; ballBodyDef2.type = b2_dynamicBody; ballVector2.Set(15, 15); ballBodyDef2.angularVelocity = 0.0f; ballBodyDef2.linearVelocity = ballVector2; ballBodyDef.position.Set(15, 0); ballBodyDef.awake = true; Body = World->CreateBody(&ballBodyDef); ballBodyDef2.position.Set(30, 0); ballBodyDef2.awake = true; Body2 = World->CreateBody(&ballBodyDef2); boxShapeDef.shape = &groundBox; boxShapeDef.density = 2.0f; boxShapeDef.restitution = 0.5f; groundBox.SetAsBox(800, 0); groundBody->CreateFixture(&groundBox, 0); wallBoxDef.shape = &wallBox; wallBoxDef.density = 2.0f; wallBoxDef.restitution = 0.5f; wallBox.SetAsBox(1, 600); wallBody->CreateFixture(&wallBox, 0); wallBoxDef2.shape = &wallBox2; wallBoxDef2.density = 2.0f; wallBoxDef2.restitution = 0.5f; wallBox2.SetAsBox(1, 600); wallBody2->CreateFixture(&wallBox2, 0); dynamicBox.SetAsBox(10.0f, 10.0f); dynamicBox2.SetAsBox(10.0f, 10.0f); fixtureDef.shape = &dynamicBox; fixtureDef.density = 2.0f; fixtureDef.friction = 1.5f; fixtureDef.restitution = 0.9f; Body->CreateFixture(&fixtureDef); fixtureDef2.shape = &dynamicBox2; fixtureDef2.density = 5.0f; fixtureDef2.friction = 5.0f; fixtureDef2.restitution = 1.0f; Body2->CreateFixture(&fixtureDef2); timeStep = 1.0f / 600.0f; velIter = 1; posIter = 1; World->Step(timeStep, velIter, posIter); b2Vec2 pos = Body->GetPosition(); float angle = Body->GetAngle(); box.setPosition(pos.x, pos.y); box.setRotation(angle); b2Vec2 pos2 = Body2->GetPosition(); float angle2 = Body2->GetAngle(); box2.setPosition(pos2.x, pos2.y); box2.setRotation(angle2); }