Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- main.cpp
- #include <SFML/Graphics/RenderWindow.hpp>
- #include <SFML/Window.hpp>
- int main()
- {
- sf::RenderWindow window(sf::VideoMode(1280, 720), "Light Engine");
- std::vector<Light> lights;
- lights.push_back(Light(sf::Vector2f(700, 300), 0, 360, 500, sf::Color::Red));
- lights.push_back(Light(sf::Vector2f(200, 300), 20, 70, 600, sf::Color::Green));
- sf::Texture text;
- text.loadFromFile("test.png");
- sf::Sprite sprite(text);
- sprite.setPosition(0, 0);
- while (window.isOpen())
- {
- sf::Mouse mouse;
- sf::Event event;
- while (window.pollEvent(event))
- {
- if (event.type == sf::Event::Closed)
- window.close();
- }
- lights[0].setPosition(sf::Vector2f(mouse.getPosition().x, mouse.getPosition().y));
- window.clear();
- window.draw(sprite);
- { //DRAWING
- sf::RenderTexture finalMask;
- finalMask.create(1280, 720);
- finalMask.clear(sf::Color::Black);
- for (auto& light : lights)
- {
- sf::RenderTexture mask;
- mask.create(1280, 720);
- mask.clear(sf::Color::Transparent);
- light.draw(mask);
- mask.display();
- const sf::Texture& texture = mask.getTexture();
- sf::Sprite maskSprite(texture);
- sf::BlendMode masksCombining = sf::BlendAdd;
- masksCombining.colorEquation = sf::BlendMode::Add;
- //masksCombining.alphaEquation = sf::BlendMode::Add; //UNCOMMENT TO GET FIRST STAGE
- //masksCombining.alphaEquation = sf::BlendMode::ReverseSubtract; //UNCOMMENT TO GET SECOND STAGE
- finalMask.draw(maskSprite, masksCombining);
- }
- finalMask.display();
- const sf::Texture& texture = finalMask.getTexture();
- sf::Sprite maskSprite(texture);
- window.draw(maskSprite);
- }
- window.display();
- }
- return 0;
- }
- Light.h:
- #pragma once
- #include <SFML/System/Vector2.hpp>
- #include <SFML/Graphics/Color.hpp>
- #include "GradientCircle.h"
- class Light
- {
- public:
- Light();
- Light(sf::Vector2f gPosition, float gRotation, float gSpread, float gIntensity, sf::Color gColor);
- void draw(sf::RenderTarget &target);
- void setPosition(sf::Vector2f pos) { position = pos; sprite.setPosition(pos); }
- float getIntensity() { return intensity; }
- sf::Vector2f getPosition() { return position; }
- private:
- sf::Vector2f position;
- float rotation; //where the light is targeted. clockwise, 0 is right
- float spread; //angle of light beam in angles
- float intensity; //in normal circumstances (clarity of environment equals 1) intensity is identical as range. Otherwise range will be smaller.
- sf::Color color; //color of source
- GradientCircle sprite;
- };
- Light.cpp:
- #include "Light.h"
- Light::Light()
- {
- }
- Light::Light(sf::Vector2f gPosition, float gRotation, float gSpread, float gIntensity, sf::Color gColor) :
- position(gPosition),
- rotation(gRotation),
- spread(gSpread),
- intensity(gIntensity),
- color(gColor),
- sprite(intensity, color, rotation, spread)
- {
- sprite.setPosition(position);
- }
- void Light::draw(sf::RenderTarget & target)
- {
- target.draw(sprite);
- }
- GradientCircle.h:
- #pragma once
- #include <SFML/Graphics.hpp>
- class GradientCircle : public sf::Drawable, public sf::Transformable
- {
- public:
- GradientCircle();
- GradientCircle(float radius, sf::Color color, float rotation, float spread = 360, int vertices = 30);
- virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
- private:
- sf::VertexArray vertexArray;
- };
- GradientCircle.cpp:
- #include "GradientCircle.h"
- #define _USE_MATH_DEFINES
- #include <math.h>
- GradientCircle::GradientCircle()
- {
- }
- GradientCircle::GradientCircle(float radius, sf::Color color, float rotation, float spread, int vertices)
- {
- vertexArray.append(sf::Vertex(sf::Vector2f(0, 0), color));
- color.a = 0;
- double step = spread / vertices;
- double startingRotation = rotation - (spread) / 2;
- for (size_t i = 0; i <= vertices; i++) //weak inequality beacuse of TriangleFan way of working. It's needed to properly draw last triangle
- {
- float x = radius * cos( (startingRotation + step*i) * M_PI / 180);
- float y = radius * sin( (startingRotation + step*i) * M_PI / 180);
- vertexArray.append(sf::Vertex(sf::Vector2f(x, y), color));
- }
- vertexArray.setPrimitiveType(sf::PrimitiveType::TriangleFan);
- }
- void GradientCircle::draw(sf::RenderTarget & target, sf::RenderStates states) const
- {
- // apply the entity's transform -- combine it with the one that was passed by the caller
- states.transform *= getTransform(); // getTransform() is defined by sf::Transformable
- // draw the vertex array
- target.draw(vertexArray, states);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement