Skip to content

Commit 95b53f7

Browse files
committed
Print rendertime on screen
1 parent 8b4105a commit 95b53f7

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-2
lines changed

CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ add_executable(raytracer
1919
src/Sphere.cpp
2020
)
2121
target_link_libraries(raytracer PRIVATE SFML::Graphics)
22+
target_compile_definitions(raytracer PRIVATE FONT_PATH="${CMAKE_SOURCE_DIR}/data")
2223

2324
add_custom_target(format
2425
COMMAND clang-format -i `git ls-files *.hpp *.cpp`

data/font.ttf

178 KB
Binary file not shown.

src/Raytracer.cpp

+9-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
#include <array>
1111
#include <chrono>
12-
#include <iostream>
1312
#include <memory>
1413

1514
namespace {
@@ -44,6 +43,10 @@ int main()
4443
auto window = sf::RenderWindow(sf::VideoMode({ image_width, image_height }), "Raytracer");
4544
window.setFramerateLimit(30);
4645

46+
auto font = sf::Font();
47+
if (!font.loadFromFile(FONT_PATH / std::filesystem::path("font.ttf")))
48+
throw std::runtime_error("Failed to load font");
49+
4750
// Heap allocate to accomodate systems with small (<1MB) stack sizes
4851
const auto pixels_allocation = std::make_unique<std::array<std::array<sf::Color, image_width>, image_height>>();
4952
auto& pixels = *pixels_allocation;
@@ -66,7 +69,6 @@ int main()
6669

6770
const auto rendering_time
6871
= std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - start_of_rendering);
69-
std::cout << "Rendered in " << rendering_time.count() << " ms" << std::endl;
7072

7173
auto image = sf::Image();
7274
image.create({ image_width, image_height }, reinterpret_cast<uint8_t*>(pixels.data()));
@@ -75,6 +77,10 @@ int main()
7577
throw std::runtime_error("Failed to load texture");
7678
const auto sprite = sf::Sprite(texture);
7779

80+
auto text = sf::Text(std::to_string(rendering_time.count()) + " ms", font, 28);
81+
text.setPosition({ 5, 0 });
82+
text.setOutlineThickness(2.f);
83+
7884
while (window.isOpen()) {
7985
for (auto event = sf::Event(); window.pollEvent(event);) {
8086
switch (event.type) {
@@ -88,6 +94,7 @@ int main()
8894

8995
window.clear();
9096
window.draw(sprite);
97+
window.draw(text);
9198
window.display();
9299
}
93100
}

0 commit comments

Comments
 (0)