Advertisement
frogi16

SFML lights

Jul 18th, 2018
673
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.51 KB | None | 0 0
  1. main.cpp
  2.  
  3. #include <SFML/Graphics/RenderWindow.hpp>
  4. #include <SFML/Window.hpp>
  5.  
  6. int main()
  7. {
  8.     sf::RenderWindow window(sf::VideoMode(1280, 720), "Light Engine");
  9.  
  10.     std::vector<Light> lights;
  11.     lights.push_back(Light(sf::Vector2f(700, 300), 0, 360, 500, sf::Color::Red));
  12.     lights.push_back(Light(sf::Vector2f(200, 300), 20, 70, 600, sf::Color::Green));
  13.  
  14.     sf::Texture text;
  15.     text.loadFromFile("test.png");
  16.     sf::Sprite sprite(text);
  17.     sprite.setPosition(0, 0);
  18.  
  19.     while (window.isOpen())
  20.     {
  21.         sf::Mouse mouse;
  22.         sf::Event event;
  23.         while (window.pollEvent(event))
  24.         {
  25.             if (event.type == sf::Event::Closed)
  26.                 window.close();
  27.         }
  28.  
  29.         lights[0].setPosition(sf::Vector2f(mouse.getPosition().x, mouse.getPosition().y));
  30.  
  31.         window.clear();
  32.         window.draw(sprite);
  33.        
  34.         {                                                               //DRAWING
  35.             sf::RenderTexture finalMask;
  36.             finalMask.create(1280, 720);
  37.             finalMask.clear(sf::Color::Black);
  38.  
  39.             for (auto& light : lights)
  40.             {
  41.                 sf::RenderTexture mask;
  42.                 mask.create(1280, 720);
  43.                 mask.clear(sf::Color::Transparent);
  44.                 light.draw(mask);
  45.                 mask.display();
  46.                 const sf::Texture& texture = mask.getTexture();
  47.                 sf::Sprite maskSprite(texture);
  48.                 sf::BlendMode masksCombining = sf::BlendAdd;
  49.                 masksCombining.colorEquation = sf::BlendMode::Add;
  50.                 //masksCombining.alphaEquation = sf::BlendMode::Add;                //UNCOMMENT TO GET FIRST STAGE
  51.                 //masksCombining.alphaEquation = sf::BlendMode::ReverseSubtract;    //UNCOMMENT TO GET SECOND STAGE
  52.                 finalMask.draw(maskSprite, masksCombining);
  53.             }
  54.  
  55.             finalMask.display();
  56.             const sf::Texture& texture = finalMask.getTexture();
  57.             sf::Sprite maskSprite(texture);
  58.             window.draw(maskSprite);
  59.         }
  60.  
  61.         window.display();
  62.     }
  63.  
  64.     return 0;
  65. }
  66.  
  67. Light.h:
  68. #pragma once
  69.  
  70. #include <SFML/System/Vector2.hpp>
  71. #include <SFML/Graphics/Color.hpp>
  72.  
  73. #include "GradientCircle.h"
  74.  
  75. class Light
  76. {
  77. public:
  78.     Light();
  79.     Light(sf::Vector2f gPosition, float gRotation, float gSpread, float gIntensity, sf::Color gColor);
  80.     void draw(sf::RenderTarget &target);
  81.     void setPosition(sf::Vector2f pos) { position = pos; sprite.setPosition(pos); }
  82.     float getIntensity() { return intensity; }
  83.     sf::Vector2f getPosition() { return position; }
  84.  
  85. private:
  86.     sf::Vector2f position;
  87.     float rotation;     //where the light is targeted. clockwise, 0 is right
  88.     float spread;       //angle of light beam in angles
  89.     float intensity;    //in normal circumstances (clarity of environment equals 1) intensity is identical as range. Otherwise range will be smaller.
  90.     sf::Color color;    //color of source
  91.     GradientCircle sprite;
  92. };
  93.  
  94. Light.cpp:
  95. #include "Light.h"
  96.  
  97. Light::Light()
  98. {
  99. }
  100.  
  101.  
  102. Light::Light(sf::Vector2f gPosition, float gRotation, float gSpread, float gIntensity, sf::Color gColor) :
  103.     position(gPosition),
  104.     rotation(gRotation),
  105.     spread(gSpread),
  106.     intensity(gIntensity),
  107.     color(gColor),
  108.     sprite(intensity, color, rotation, spread)
  109. {
  110.     sprite.setPosition(position);
  111. }
  112.  
  113. void Light::draw(sf::RenderTarget & target)
  114. {
  115.     target.draw(sprite);
  116. }
  117.  
  118. GradientCircle.h:
  119. #pragma once
  120.  
  121. #include <SFML/Graphics.hpp>
  122.  
  123. class GradientCircle : public sf::Drawable, public sf::Transformable
  124. {
  125. public:
  126.     GradientCircle();
  127.     GradientCircle(float radius, sf::Color color, float rotation, float spread = 360, int vertices = 30);
  128.     virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
  129. private:
  130.     sf::VertexArray vertexArray;
  131. };
  132.  
  133. GradientCircle.cpp:
  134. #include "GradientCircle.h"
  135.  
  136. #define _USE_MATH_DEFINES
  137. #include <math.h>
  138.  
  139. GradientCircle::GradientCircle()
  140. {
  141. }
  142.  
  143. GradientCircle::GradientCircle(float radius, sf::Color color, float rotation, float spread, int vertices)
  144. {
  145.     vertexArray.append(sf::Vertex(sf::Vector2f(0, 0), color));
  146.  
  147.     color.a = 0;
  148.     double step = spread / vertices;
  149.     double startingRotation = rotation - (spread) / 2;
  150.  
  151.     for (size_t i = 0; i <= vertices; i++)      //weak inequality beacuse of TriangleFan way of working. It's needed to properly draw last triangle
  152.     {
  153.         float x = radius * cos( (startingRotation + step*i) * M_PI / 180);
  154.         float y = radius * sin( (startingRotation + step*i) * M_PI / 180);
  155.         vertexArray.append(sf::Vertex(sf::Vector2f(x, y), color));
  156.     }
  157.  
  158.     vertexArray.setPrimitiveType(sf::PrimitiveType::TriangleFan);
  159. }
  160.  
  161. void GradientCircle::draw(sf::RenderTarget & target, sf::RenderStates states) const
  162. {
  163.     // apply the entity's transform -- combine it with the one that was passed by the caller
  164.     states.transform *= getTransform(); // getTransform() is defined by sf::Transformable
  165.  
  166.     // draw the vertex array
  167.     target.draw(vertexArray, states);
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement