Skip to content

Commit

Permalink
Merge pull request #1368 from Albeleon/patch-1
Browse files Browse the repository at this point in the history
Implement Random Blocks transition
  • Loading branch information
carstene1ns authored May 28, 2018
2 parents a1ce052 + 061884c commit 5f7b64d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
29 changes: 28 additions & 1 deletion src/graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <algorithm>
#include <sstream>
#include <vector>
#include <array>

#include "graphics.h"
#include "bitmap.h"
Expand All @@ -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();
Expand Down Expand Up @@ -60,6 +66,8 @@ namespace Graphics {

std::unique_ptr<MessageOverlay> message_overlay;
std::unique_ptr<FpsOverlay> fps_overlay;

std::array<uint32_t, size_array_random_blocks> random_blocks;
}

unsigned SecondToFrame(float const second) {
Expand All @@ -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() {
Expand Down Expand Up @@ -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;
Expand All @@ -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:
Expand All @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 8 additions & 0 deletions src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <string>
#include <sstream>
#include <vector>
#include <random>
#include "system.h"

namespace Utils {
Expand Down Expand Up @@ -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.
*
Expand Down

0 comments on commit 5f7b64d

Please sign in to comment.