From 061884ced61cc5a29ead39f5aa8f843764b98782 Mon Sep 17 00:00:00 2001 From: Albeleon <37343812+Albeleon@users.noreply.github.com> Date: Sun, 27 May 2018 15:48:54 +0200 Subject: [PATCH] RandomBlocks Transition Changes to add functionality with RandomBlocks transition. The changes are: - Utils.h: Function &GetRNG() added, which returns the common random number generator which will be needed for our function in graphics.cpp - Utils.cpp: Function &Utils::GetRNG() added. - Graphics.cpp: -> Library added, to use the functions "begin" and "end". -> An anonymous namespace with two constant expressions: "random_size_blocks" (defines the width and height of each random block) and "random_array_size_blocks" (defines the number of blocks needed to cover the whole screen. This number (4800) was calculated after doing the operation (320 / random_size_blocks) * (240 / random_size_blocks) ). -> A variable "random_blocks" added in the namespace "Graphics", and initiated in "Graphics::Init". It's an array with the size of random_array_size_blocks (4800) and each value has a sequential number (0, 1, 2, ..., 4799) which we will use as an index for each block. -> In "Graphics::Transition" (each time the transition is invoked) we do a "Shuffle" to the array so the numbers are in a random order. We use here begin(), end() and Utils::GetRNG(). -> In "Graphics::UpdateTransition", we create the function that inserts blocks. The "percentage" affects the number of blocks being invoked. --- src/graphics.cpp | 29 ++++++++++++++++++++++++++++- src/utils.cpp | 4 ++++ src/utils.h | 8 ++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/graphics.cpp b/src/graphics.cpp index e41619445c..cead9aa3ae 100644 --- a/src/graphics.cpp +++ b/src/graphics.cpp @@ -19,6 +19,7 @@ #include #include #include +#include #include "graphics.h" #include "bitmap.h" @@ -33,6 +34,11 @@ #include "message_overlay.h" #include "scene.h" +namespace { + constexpr uint32_t size_random_blocks = 4; + constexpr uint32_t size_array_random_blocks = 4800; +} + namespace Graphics { void UpdateTitle(); void DrawFrame(); @@ -60,6 +66,8 @@ namespace Graphics { std::unique_ptr message_overlay; std::unique_ptr fps_overlay; + + std::array random_blocks; } unsigned SecondToFrame(float const second) { @@ -80,6 +88,10 @@ void Graphics::Init() { fps_overlay.reset(new FpsOverlay()); next_fps_time = 0; + + for (uint32_t i = 0; i < size_array_random_blocks; i++) { + random_blocks[i] = i; + } } void Graphics::Quit() { @@ -250,6 +262,10 @@ void Graphics::Transition(TransitionType type, int duration, bool erase) { else screen1 = screen2; } + + if (type == TransitionRandomBlocks) { + std::shuffle(random_blocks.begin(), random_blocks.end(), Utils::GetRNG()); + } } screen_erased = erase; @@ -274,7 +290,6 @@ void Graphics::UpdateTransition() { // Fallback to FadeIn/Out for not implemented transition types: // (Remove from here when implemented below) switch (transition_type) { - case TransitionRandomBlocks: case TransitionRandomBlocksUp: case TransitionRandomBlocksDown: case TransitionZoomIn: @@ -296,6 +311,18 @@ void Graphics::UpdateTransition() { dst->Blit(0, 0, *screen2, screen2->GetRect(), 255 * percentage / 100); break; case TransitionRandomBlocks: + dst->Blit(0, 0, *screen1, screen1->GetRect(), 255); + for (uint32_t i = 0; i < size_array_random_blocks * percentage / 100; i++) { + dst->Blit(random_blocks[i] % (w / size_random_blocks) * size_random_blocks, + random_blocks[i] / (w / size_random_blocks) * size_random_blocks, + *screen2, + Rect( + random_blocks[i] % (w / size_random_blocks) * size_random_blocks, + random_blocks[i] / (w / size_random_blocks) * size_random_blocks, + size_random_blocks, + size_random_blocks), + 255); + } break; case TransitionRandomBlocksUp: break; diff --git a/src/utils.cpp b/src/utils.cpp index d156e9094d..445607338a 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -383,6 +383,10 @@ int32_t Utils::GetRandomNumber(int32_t from, int32_t to) { return int32_t(ures); } +std::mt19937 &Utils::GetRNG() { + return rng; +} + bool Utils::ChanceOf(int32_t n, int32_t m) { assert(n >= 0 && m > 0); return GetRandomNumber(1, m) <= n; diff --git a/src/utils.h b/src/utils.h index 48bb78c99e..4e7bc2bf3c 100644 --- a/src/utils.h +++ b/src/utils.h @@ -22,6 +22,7 @@ #include #include #include +#include #include "system.h" namespace Utils { @@ -172,6 +173,13 @@ namespace Utils { */ int32_t GetRandomNumber(int32_t from, int32_t to); + /** + * Gets the seeded Random Number Generator (RNG). + * + * @return the random number generator + */ + std::mt19937 &GetRNG(); + /** * Has an n/m chance of returning true. If n>m, always returns true. *