RayTracer 1.0
Ray tracing is a technique used to generate realistic digital images by simulating the inverse path of light. Our goal is to create a program able to generate an image from a file describing the scene.
 
Loading...
Searching...
No Matches
Display Class Reference

#include <Display.hpp>

Classes

class  Pixel
 
struct  PixelEntry
 

Public Member Functions

 Display (int width, int height)
 
 ~Display ()
 
void init ()
 
void run ()
 
void pushPixel (int x, int y, const Pixel &px)
 
void notifyDone ()
 

Private Attributes

int _width
 
int _height
 
std::queue< PixelEntry_pixelQueue
 
std::mutex _queueMtx
 
std::condition_variable _queueCv
 
bool _done = false
 
sf::RenderWindow _window
 
sf::Texture _texture
 
sf::Sprite _sprite
 

Detailed Description

Definition at line 16 of file Display.hpp.

Constructor & Destructor Documentation

◆ Display()

Display::Display ( int width,
int height )

Definition at line 9 of file Display.cpp.

9 : _width(width), _height(height)
10{}
int _height
Definition Display.hpp:36
int _width
Definition Display.hpp:35

References _height, and _width.

◆ ~Display()

Display::~Display ( )
inline

Definition at line 19 of file Display.hpp.

19{};

Member Function Documentation

◆ init()

void Display::init ( )

Definition at line 12 of file Display.cpp.

13{
14 _window.create(sf::VideoMode((unsigned)_width, (unsigned)_height), "Raytracer");
15 _texture.create(_width, _height);
16 _sprite.setTexture(_texture, true);
17}
sf::Texture _texture
Definition Display.hpp:48
sf::Sprite _sprite
Definition Display.hpp:49
sf::RenderWindow _window
Definition Display.hpp:47

References _height, _sprite, _texture, _width, and _window.

◆ notifyDone()

void Display::notifyDone ( )

Definition at line 26 of file Display.cpp.

27{
28 std::lock_guard<std::mutex> lock(_queueMtx);
29 _done = true;
30 _queueCv.notify_one();
31}
bool _done
Definition Display.hpp:45
std::condition_variable _queueCv
Definition Display.hpp:44
std::mutex _queueMtx
Definition Display.hpp:43

References _done, _queueCv, and _queueMtx.

◆ pushPixel()

void Display::pushPixel ( int x,
int y,
const Pixel & px )

Definition at line 19 of file Display.cpp.

20{
21 std::lock_guard<std::mutex> lock(_queueMtx);
22 _pixelQueue.push(PixelEntry{x, y, px});
23 _queueCv.notify_one();
24}
std::queue< PixelEntry > _pixelQueue
Definition Display.hpp:42
Definition Display.hpp:38

References _pixelQueue, _queueCv, and _queueMtx.

◆ run()

void Display::run ( )

Definition at line 33 of file Display.cpp.

34{
35 sf::Image image;
36 image.create(_width, _height, sf::Color::Black);
37
38 while (_window.isOpen()) {
39 sf::Event ev;
40 while (_window.pollEvent(ev)) {
41 if (ev.type == sf::Event::Closed)
42 _window.close();
43 else if (ev.type == sf::Event::KeyPressed) {
44 if (ev.key.code == sf::Keyboard::Enter)
45 _window.close();
46 }
47 }
48
49 {
50 std::unique_lock<std::mutex> lock(_queueMtx);
51 _queueCv.wait(lock, [&]{ return !_pixelQueue.empty() || _done; });
52
53 while (!_pixelQueue.empty()) {
54 auto entry = _pixelQueue.front();
55 _pixelQueue.pop();
56 image.setPixel(entry.x, entry.y, sf::Color(entry.color.r, entry.color.g, entry.color.b));
57 }
58 }
59
60 _texture.update(image);
61 _window.clear();
62 _window.draw(_sprite);
63 _window.display();
64 }
65}

References _done, _height, _pixelQueue, _queueCv, _queueMtx, _sprite, _texture, _width, and _window.

Member Data Documentation

◆ _done

bool Display::_done = false
private

Definition at line 45 of file Display.hpp.

◆ _height

int Display::_height
private

Definition at line 36 of file Display.hpp.

◆ _pixelQueue

std::queue<PixelEntry> Display::_pixelQueue
private

Definition at line 42 of file Display.hpp.

◆ _queueCv

std::condition_variable Display::_queueCv
private

Definition at line 44 of file Display.hpp.

◆ _queueMtx

std::mutex Display::_queueMtx
private

Definition at line 43 of file Display.hpp.

◆ _sprite

sf::Sprite Display::_sprite
private

Definition at line 49 of file Display.hpp.

◆ _texture

sf::Texture Display::_texture
private

Definition at line 48 of file Display.hpp.

◆ _width

int Display::_width
private

Definition at line 35 of file Display.hpp.

◆ _window

sf::RenderWindow Display::_window
private

Definition at line 47 of file Display.hpp.


The documentation for this class was generated from the following files: