Skip to content

Commit 3d4a7aa

Browse files
committed
Add fadeOutOnly option to jngl::Fade
1 parent ac6900e commit 3d4a7aa

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

src/jngl/Fade.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2025 Jan Niklas Hasse <jhasse@bixense.com>
1+
// Copyright 2025-2026 Jan Niklas Hasse <jhasse@bixense.com>
22
// For conditions of distribution and use, see copyright notice in LICENSE.txt
33
#include "Fade.hpp"
44

@@ -11,12 +11,13 @@
1111

1212
namespace jngl {
1313

14-
Fade::Fade(std::shared_ptr<Work> work, int speed)
15-
: work(std::move(work)), oldWork(getWork()), fadeCount(0), speed(speed) {
14+
Fade::Fade(std::shared_ptr<Work> work, std::optional<Options> options)
15+
: work(std::move(work)), oldWork(getWork()), fadeCount(0), speed(options ? options->speed : 20),
16+
fadeOutOnly(options ? options->fadeOutOnly : false) {
1617
}
1718

18-
Fade::Fade(std::function<std::shared_ptr<Scene>()> factory, int speed)
19-
: Fade(std::make_shared<WorkFactory>(std::move(factory)), speed) {
19+
Fade::Fade(std::function<std::shared_ptr<Scene>()> factory, std::optional<Options> options)
20+
: Fade(std::make_shared<WorkFactory>(std::move(factory)), options) {
2021
}
2122

2223
Fade::~Fade() {
@@ -52,6 +53,9 @@ void Fade::step() {
5253
break;
5354
}
5455
}
56+
if (fadeOutOnly) {
57+
setScene(work);
58+
}
5559
}
5660
}
5761

src/jngl/Fade.hpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,33 @@
1-
// Copyright 2025 Jan Niklas Hasse <jhasse@bixense.com>
1+
// Copyright 2025-2026 Jan Niklas Hasse <jhasse@bixense.com>
22
// For conditions of distribution and use, see copyright notice in LICENSE.txt
33
/// Contains jngl::Fade class
44
/// @file
55
#pragma once
66

77
#include "Scene.hpp"
88

9+
#include <optional>
10+
911
namespace jngl {
1012

1113
/// Fades between two scenes, first fading to jngl::getBackgroundColor() and then fading to the new
1214
/// scene
1315
class Fade : public Scene {
1416
public:
15-
explicit Fade(std::shared_ptr<Scene>, int speed = 20);
17+
struct Options {
18+
int speed = 20;
19+
20+
/// If true, only fades out and then immediately switches to the new scene
21+
bool fadeOutOnly = false;
22+
};
23+
24+
explicit Fade(std::shared_ptr<Scene>, std::optional<Options> options = {});
1625

1726
/// Uses factory() to get the new Scene when the screen has faded to black (or the background
1827
/// color). This is useful when the ctor of your new Scene might take a long time (e.g. loading
1928
/// assets) and you don't want the lag to be noticable.
20-
explicit Fade(std::function<std::shared_ptr<Scene>()> factory, int speed = 20);
29+
explicit Fade(std::function<std::shared_ptr<Scene>()> factory,
30+
std::optional<Options> options = {});
2131

2232
~Fade() override;
2333
void step() override;
@@ -31,6 +41,7 @@ class Fade : public Scene {
3141
double fadeCount;
3242
int speed;
3343
bool quit = false;
44+
bool fadeOutOnly;
3445
};
3546

3647
} // namespace jngl

0 commit comments

Comments
 (0)