Skip to content

Commit af453d6

Browse files
committed
Add vectors and strings to frame loop example
1 parent 208ac2f commit af453d6

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

engine/scripts/src/level2.cpp

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,35 @@
11
#include "interface.hpp"
22
#include <api.h>
33
using namespace api;
4+
static_assert(sizeof(std::string) == 32,
5+
"std::string must be 32 bytes in size to be compatible with the guest std::string SSO");
46

57
PUBLIC(void start())
68
{
79
print("Hello from Level 2!\n");
810
int value = gameplay_allowed_function(123);
911
print("Back in Level2! Got result value = ", value, "\n");
1012

11-
// Create a simple event loop
13+
// Create a simple event loop with frame data
14+
struct {
15+
std::vector<std::string> strings;
16+
std::vector<int> integers;
17+
int frame;
18+
} frame_data;
1219
while (true) {
1320
// Wait for a frame to complete
14-
struct {
15-
int frame = 0;
16-
} frame_data;
1721
Game::wait(frame_data);
1822

1923
// Do some work here, like updating game state, rendering, etc.
20-
print("Frame ", frame_data.frame, " in Level2!\n");
24+
print("Frame ", frame_data.frame, " in Level2 with strings: ");
25+
for (const std::string& str : frame_data.strings) {
26+
print("", str, ", ");
27+
}
28+
print(" and integers: ");
29+
for (int i : frame_data.integers) {
30+
print("", i, ", ");
31+
}
32+
print("\n");
2133
}
2234
}
2335

engine/src/main.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ int main()
7373
to frame data that is updated each frame. */
7474
struct FrameData
7575
{
76+
riscv::GuestStdVector<Script::MARCH, riscv::GuestStdString<Script::MARCH>> strings;
77+
riscv::GuestStdVector<Script::MARCH, int> integers;
7678
int frame = 0;
7779
};
7880
/* When the guest pauses, the pointer to the frame data is the first argument (A0). */
@@ -87,6 +89,16 @@ int main()
8789
/* Set the data for the current frame.
8890
This is accessible to the guest program when it resumes. */
8991
frame_data->frame = i;
92+
/* Wrappers around guest-stored vectors (and other types) always
93+
take the machine as the first argument. */
94+
frame_data->strings.clear(level2.machine());
95+
frame_data->strings.push_back(level2.machine(), "Hello");
96+
frame_data->strings.push_back(level2.machine(), "World");
97+
98+
frame_data->integers.clear(level2.machine());
99+
frame_data->integers.push_back(level2.machine(), i);
100+
frame_data->integers.push_back(level2.machine(), i * 2);
101+
90102
/* Resume the level2 program, which unpauses the guest and continues
91103
execution until it loops around to the Game::wait() call again. */
92104
if (!level2.resume(5'000)) {

ext/libriscv

0 commit comments

Comments
 (0)