Skip to content

Commit ce0ecbb

Browse files
committed
Use Broadcast::isBackdropBroadcast() in Engine
1 parent 4f72f34 commit ce0ecbb

File tree

3 files changed

+28
-33
lines changed

3 files changed

+28
-33
lines changed

src/blocks/eventblocks.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,7 @@ void EventBlocks::compileWhenBroadcastReceived(Compiler *compiler)
7777

7878
void EventBlocks::compileWhenBackdropSwitchesTo(Compiler *compiler)
7979
{
80-
if (Stage *stage = compiler->engine()->stage()) {
81-
std::string backdropName = compiler->field(BACKDROP)->value().toString();
82-
int index = stage->findCostume(backdropName);
83-
84-
if (index != -1)
85-
compiler->engine()->addBroadcastScript(compiler->block(), stage->costumeAt(index)->broadcast());
86-
}
80+
// TODO: Register this hat in engine
8781
}
8882

8983
void EventBlocks::compileWhenKeyPressed(Compiler *compiler)

src/engine/internal/engine.cpp

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -576,32 +576,35 @@ bool Engine::broadcastRunning(unsigned int index, VirtualMachine *sourceScript)
576576

577577
bool Engine::broadcastByPtrRunning(Broadcast *broadcast, VirtualMachine *sourceScript)
578578
{
579-
assert(m_broadcastMap.find(broadcast) != m_broadcastMap.cend());
580-
const auto &scripts = m_broadcastMap[broadcast];
581-
582-
for (auto thread : m_threads) {
583-
if (!thread->atEnd()) {
584-
auto it = std::find_if(scripts.begin(), scripts.end(), [thread](Script *script) { return thread->script() == script; });
585-
586-
if (it != scripts.end())
587-
return true;
579+
if (broadcast->isBackdropBroadcast()) {
580+
// This broadcast belongs to a backdrop
581+
assert(m_broadcastMap.find(broadcast) == m_broadcastMap.cend());
582+
583+
for (auto thread : m_threads) {
584+
if (!thread->atEnd()) {
585+
// TODO: Store the top block in Script
586+
Script *script = thread->script();
587+
auto it = std::find_if(m_scripts.begin(), m_scripts.end(), [script](const std::pair<std::shared_ptr<Block>, std::shared_ptr<Script>> pair) { return pair.second.get() == script; });
588+
assert(it != m_scripts.end());
589+
auto topBlock = it->first;
590+
591+
// TODO: Add a map for "when backdrop switches to" hats
592+
if ((topBlock->opcode() == "event_whenbackdropswitchesto") && (topBlock->findFieldById(EventBlocks::BACKDROP)->value().toString() == broadcast->name()))
593+
return true;
594+
}
588595
}
589-
}
596+
} else {
597+
// This is a regular broadcast
598+
assert(m_broadcastMap.find(broadcast) != m_broadcastMap.cend());
599+
const auto &scripts = m_broadcastMap[broadcast];
590600

591-
// TODO: Check whether the broadcast belongs to a backdrop
592-
// maybe using something like broadcast->isBackdropBroadcast()
593-
// after adding it to Broadcast
594-
for (auto thread : m_threads) {
595-
if (!thread->atEnd()) {
596-
// TODO: Store the top block in Script
597-
Script *script = thread->script();
598-
auto it = std::find_if(m_scripts.begin(), m_scripts.end(), [script](const std::pair<std::shared_ptr<Block>, std::shared_ptr<Script>> pair) { return pair.second.get() == script; });
599-
assert(it != m_scripts.end());
600-
auto topBlock = it->first;
601+
for (auto thread : m_threads) {
602+
if (!thread->atEnd()) {
603+
auto it = std::find_if(scripts.begin(), scripts.end(), [thread](Script *script) { return thread->script() == script; });
601604

602-
// TODO: Add a map for "when backdrop switches to" hats
603-
if ((topBlock->opcode() == "event_whenbackdropswitchesto") && (topBlock->findFieldById(EventBlocks::BACKDROP)->value().toString() == broadcast->name()))
604-
return true;
605+
if (it != scripts.end())
606+
return true;
607+
}
605608
}
606609
}
607610
return false;
@@ -736,6 +739,7 @@ int Engine::findBroadcastById(const std::string &broadcastId) const
736739

737740
void Engine::addBroadcastScript(std::shared_ptr<Block> whenReceivedBlock, Broadcast *broadcast)
738741
{
742+
assert(!broadcast->isBackdropBroadcast());
739743
Script *script = m_scripts[whenReceivedBlock].get();
740744
auto it = m_broadcastMap.find(broadcast);
741745

test/blocks/event_blocks_test.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -290,9 +290,6 @@ TEST_F(EventBlocksTest, WhenBackdropSwitchesTo)
290290
auto backdrop = std::make_shared<Costume>("backdrop2", "a", "svg");
291291
stage.addCostume(backdrop);
292292

293-
EXPECT_CALL(m_engineMock, stage()).WillOnce(Return(&stage));
294-
EXPECT_CALL(m_engineMock, addBroadcastScript(block1, backdrop->broadcast()));
295-
296293
compiler.init();
297294
compiler.setBlock(block1);
298295
EventBlocks::compileWhenBackdropSwitchesTo(&compiler);

0 commit comments

Comments
 (0)