Skip to content

Commit c435555

Browse files
committed
Engine: Add broadcast methods
1 parent 5bdc44b commit c435555

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/engine/engine.cpp

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,11 @@ void Engine::compile()
7676
if (block->topLevel()) {
7777
auto section = blockSection(block->opcode());
7878
if (section) {
79+
auto vm = std::make_shared<VirtualMachine>(target.get(), this);
80+
m_scripts[block] = vm;
81+
7982
compiler.compile(block);
8083

81-
auto vm = std::make_shared<VirtualMachine>(target.get(), this);
8284
vm->setFunctions(m_functions);
8385
vm->setConstValues(compiler.constValues());
8486
vm->setVariables(compiler.variablePtrs());
@@ -88,7 +90,6 @@ void Engine::compile()
8890
auto b = block->inputAt(block->findInput("custom_block"))->valueBlock();
8991
procedureBytecodeMap[b->mutationPrototype()->procCode()] = vm->bytecode();
9092
}
91-
m_scripts[block] = vm;
9293
} else
9394
std::cout << "warning: unsupported top level block: " << block->opcode() << std::endl;
9495
}
@@ -179,6 +180,24 @@ void Engine::startScript(std::shared_ptr<Block> topLevelBlock, std::shared_ptr<T
179180
}
180181
}
181182

183+
/*! Starts the script of the broadcast with the given index. */
184+
void libscratchcpp::Engine::broadcast(unsigned int index)
185+
{
186+
const std::vector<VirtualMachine *> scripts = m_broadcastMap[index];
187+
for (auto vm : scripts) {
188+
auto it = std::find(m_runningScripts.begin(), m_runningScripts.end(), vm);
189+
if (it != m_runningScripts.end()) {
190+
// Reset the script if it's already running
191+
auto i = it - m_runningScripts.begin();
192+
m_scriptPositions[i] = m_runningScripts[i]->bytecode();
193+
} else {
194+
m_runningScripts.push_back(vm);
195+
m_scriptPositions.push_back(vm->bytecode());
196+
scriptCount++;
197+
}
198+
}
199+
}
200+
182201
/*! Stops the given script. */
183202
void Engine::stopScript(VirtualMachine *vm)
184203
{

src/engine/engine.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class LIBSCRATCHCPP_EXPORT Engine
3535
void start();
3636
void stop();
3737
void startScript(std::shared_ptr<Block> topLevelBlock, std::shared_ptr<Target> target);
38+
void broadcast(unsigned int index);
3839
void stopScript(VirtualMachine *vm);
3940
void stopTarget(Target *target, VirtualMachine *exceptScript);
4041
void run();
@@ -54,6 +55,8 @@ class LIBSCRATCHCPP_EXPORT Engine
5455
int findBroadcast(const std::string &broadcastName) const;
5556
int findBroadcastById(const std::string &broadcastId) const;
5657

58+
void addBroadcastScript(std::shared_ptr<Block> whenReceivedBlock, std::shared_ptr<Broadcast> broadcast);
59+
5760
std::vector<std::shared_ptr<Target>> targets() const;
5861
void setTargets(const std::vector<std::shared_ptr<Target>> &newTargets);
5962
Target *targetAt(int index) const;
@@ -71,6 +74,7 @@ class LIBSCRATCHCPP_EXPORT Engine
7174
std::vector<std::shared_ptr<IBlockSection>> m_sections;
7275
std::vector<std::shared_ptr<Target>> m_targets;
7376
std::vector<std::shared_ptr<Broadcast>> m_broadcasts;
77+
std::unordered_map<unsigned int, std::vector<VirtualMachine *>> m_broadcastMap;
7478
std::vector<std::string> m_extensions;
7579
std::vector<VirtualMachine *> m_runningScripts;
7680
std::vector<unsigned int *> m_scriptPositions;

0 commit comments

Comments
 (0)