Discuss Scratch
- Discussion Forums
- » Advanced Topics
- » Scratch to C++ translation and how to make it better
- liavka
-
47 posts
Scratch to C++ translation and how to make it better
Hi! Right now I'm working on some kind of runtime for scratch on C++.
The goal is to make it as fast as possible losing no readability of the resulting scripts.
E.g make it fast without translation-time optimizations.
Example project: (https://github.com/MiloLug/c-scratch).
The scripts.h file is actual scripts file for sprites.
The sprites.h file is for sprites list specification (like position, visible/not visible etc.).
This is exact representation of our (with my friend, Luna) light simulation, how it should be (ideally) generated.
So the code will look like this:
It already has full support of strings and numbers (even in one variable), almost full support for control blocks and motion.
It also allows many sprites and has pen support, except for non-RGB color-setting functions like saturation, brightness etc.
The problem is that we need to generate it after all :) :)
So my question is HOW…
Right now we stick to Snap, but it seems too hard and not well documented way. Also it would be good to not depend on side projects. There is a case already, when it was updated incompatible with our parser.
So I basically need at least a hint…
The goal is to make it as fast as possible losing no readability of the resulting scripts.
E.g make it fast without translation-time optimizations.
Example project: (https://github.com/MiloLug/c-scratch).
The scripts.h file is actual scripts file for sprites.
The sprites.h file is for sprites list specification (like position, visible/not visible etc.).
This is exact representation of our (with my friend, Luna) light simulation, how it should be (ideally) generated.
So the code will look like this:
Coroutine spriteStartScript2(Sprite * sprite) {
forever {
waitUntil(v_tick == 100);
v_timer = timer;
v_tick = 0;
// don't have graphical output rn, so it's just like this
wprintf(L“timer = %ls\n”, v_timer.toString());
cs_yield;
}
stopThisScript();
}
It already has full support of strings and numbers (even in one variable), almost full support for control blocks and motion.
It also allows many sprites and has pen support, except for non-RGB color-setting functions like saturation, brightness etc.
The problem is that we need to generate it after all :) :)
So my question is HOW…
Right now we stick to Snap, but it seems too hard and not well documented way. Also it would be good to not depend on side projects. There is a case already, when it was updated incompatible with our parser.
So I basically need at least a hint…
- liavka
-
47 posts
Scratch to C++ translation and how to make it better
P.S. The example project works fine. But it's hard to set it up on Windows. So you can try to run it on Linux. You need to install libraries from the requirements, then run
cmake .And
makeAnd then just run the app executable
- WojtekGame
-
1000+ posts
Scratch to C++ translation and how to make it better
I can contribute because i undestand C++
Also if we need to use graphics, we use graphics.h or OpenGL.
Also if we need to use graphics, we use graphics.h or OpenGL.

me on my way for Minecraft… (1 DAY TO GET IT LET'S GOOO)
JS: code-gui | scratch-player | Roost.js | Pandoa | TOP: Roost.js || C++: Gamine | Gamiinuu | TOP: Gamine
Else: Emoji Painter | Forum Shopping Mall | TOP: Emoji Painter || Profile:Scratch | GitHub
Orgs: Chroast | Twerkish | Komond OS | JS Inventions | TOP: JS Inventions || Ocular Naming: 0 | 1 | 2 | 3
Games: Mom Hid My Game! | Creeper Adventuring
else might be outdated or removed by the ST.
- liavka
-
47 posts
Scratch to C++ translation and how to make it better
Oh that's cool. The code is a mess right now tho. Since it's been a week or two and I was just trying to get to the point when I can run our project.
And it has graphics already. We decided to use SDL for graphics, since It has also audio, keyboard and other features support
And it has graphics already. We decided to use SDL for graphics, since It has also audio, keyboard and other features support
- liavka
-
47 posts
Scratch to C++ translation and how to make it better
So right now I'm going to do some refactoring before it gets too far
- WojtekGame
-
1000+ posts
Scratch to C++ translation and how to make it better
It's better interpret the project, and make it use libcurl to get the project's SB3 contents, i also suggest making it into seperate files, the easiest one without command-line arguments is .cpp,
anyways i use mingw because i cannot use ubuntu because of size.
anyways i use mingw because i cannot use ubuntu because of size.
Last edited by WojtekGame (May 1, 2023 19:57:45)

me on my way for Minecraft… (1 DAY TO GET IT LET'S GOOO)
JS: code-gui | scratch-player | Roost.js | Pandoa | TOP: Roost.js || C++: Gamine | Gamiinuu | TOP: Gamine
Else: Emoji Painter | Forum Shopping Mall | TOP: Emoji Painter || Profile:Scratch | GitHub
Orgs: Chroast | Twerkish | Komond OS | JS Inventions | TOP: JS Inventions || Ocular Naming: 0 | 1 | 2 | 3
Games: Mom Hid My Game! | Creeper Adventuring
else might be outdated or removed by the ST.
- lolecksdeehaha
-
1000+ posts
Scratch to C++ translation and how to make it better
The project.json file has a list of all the blocks and scripts; you can search for hat blocks and blocks stemming from them, including the variables and the sprites included, and eventually just generate the code from all of that.
Beep boop. I am a robot, and this action was performed automatically. If you have any concerns, please contact the owner of this robot.
ing season coming “soon”

I'm the founder of Forumoji, the source of all those neat forum emojis you occasionally see (the ones not in the default menu).







I'm the founder of Forumoji, the source of all those neat forum emojis you occasionally see (the ones not in the default menu).

- WojtekGame
-
1000+ posts
Scratch to C++ translation and how to make it better
yeah you can use The project.json file has a list of all the blocks and scripts; you can search for hat blocks and blocks stemming from them, including the variables and the sprites included, and eventually just generate the code from all of that.this library to parse json and do something with it.

me on my way for Minecraft… (1 DAY TO GET IT LET'S GOOO)
JS: code-gui | scratch-player | Roost.js | Pandoa | TOP: Roost.js || C++: Gamine | Gamiinuu | TOP: Gamine
Else: Emoji Painter | Forum Shopping Mall | TOP: Emoji Painter || Profile:Scratch | GitHub
Orgs: Chroast | Twerkish | Komond OS | JS Inventions | TOP: JS Inventions || Ocular Naming: 0 | 1 | 2 | 3
Games: Mom Hid My Game! | Creeper Adventuring
else might be outdated or removed by the ST.
- liavka
-
47 posts
Scratch to C++ translation and how to make it better
Well it would be better if the goal wasn't the speed of execution and further code editing possibility. Cause in case of interpreting the project, you can't rely on the c++ compiler optimizations. You can either interpret it with a recursion or by making some byte-code layer, and both these options are not as good as just translating to c++. The first one is too slow and the second is just horrible in terms of development effort
- liavka
-
47 posts
Scratch to C++ translation and how to make it better
yeah you can use The project.json file has a list of all the blocks and scripts; you can search for hat blocks and blocks stemming from them, including the variables and the sprites included, and eventually just generate the code from all of that.this library to parse json and do something with it.
Oh.. I will look into this. Thank you

- liavka
-
47 posts
Scratch to C++ translation and how to make it better
OH BUT we could use the runtime as a lib for such interpreter. Just as an option. I mean, stick to the translation way, but also make an interpreter project that uses the runtime part to interpret SB3.
Last edited by liavka (May 1, 2023 20:31:26)
- WojtekGame
-
1000+ posts
Scratch to C++ translation and how to make it better
i don't think compiling is a good option, unless you want to add it… Well it would be better if the goal wasn't the speed of execution and further code editing possibility. Cause in case of interpreting the project, you can't rely on the c++ compiler optimizations. You can either interpret it with a recursion or by making some byte-code layer, and both these options are not as good as just translating to c++. The first one is too slow and the second is just horrible in terms of development effort

me on my way for Minecraft… (1 DAY TO GET IT LET'S GOOO)
JS: code-gui | scratch-player | Roost.js | Pandoa | TOP: Roost.js || C++: Gamine | Gamiinuu | TOP: Gamine
Else: Emoji Painter | Forum Shopping Mall | TOP: Emoji Painter || Profile:Scratch | GitHub
Orgs: Chroast | Twerkish | Komond OS | JS Inventions | TOP: JS Inventions || Ocular Naming: 0 | 1 | 2 | 3
Games: Mom Hid My Game! | Creeper Adventuring
else might be outdated or removed by the ST.
- Spentine
-
1000+ posts
Scratch to C++ translation and how to make it better
That'll be a very difficult task. Scratch has lots of weird quirks and so does JavaScript. For example, many of the following are present:
There are more, but they probably don't apply to Scratch. It's very difficult to create weird auto-conversions for everything, so good luck!
console.log(Number("0x1") + 1) // 2 console.log(Number("0b11") + 1) // 4 console.log(Number("0o11") + 1) // 10
- bigspeedfpv
-
500+ posts
Scratch to C++ translation and how to make it better
these aren't quirks they're just additions in different bases That'll be a very difficult task. Scratch has lots of weird quirks and so does JavaScript. For example, many of the following are present:There are more, but they probably don't apply to Scratch. It's very difficult to create weird auto-conversions for everything, so good luck!console.log(Number("0x1") + 1) // 2 console.log(Number("0b11") + 1) // 4 console.log(Number("0o11") + 1) // 10
- liavka
-
47 posts
Scratch to C++ translation and how to make it better
That'll be a very difficult task. Scratch has lots of weird quirks and so does JavaScript. For example, many of the following are present:There are more, but they probably don't apply to Scratch. It's very difficult to create weird auto-conversions for everything, so good luck!console.log(Number("0x1") + 1) // 2 console.log(Number("0b11") + 1) // 4 console.log(Number("0o11") + 1) // 10
Well… As I've said the strings/numbers are implemented rn, so I can
str1 = “some22Test”
str2 = letterOf(str1, 5)
num = str2 + 10
// num = 12 now
I even have some tests file I use while developing features, and there is a good example:
Value var1 = 1;
Value var2 = var1 + 15;
Value someVar = var2.toString();
Value test = someVar == (var1 + 15);
ValueArray testArr({1, 2, L“gg 10”});
wprintf(L“out1: someVar = %ls; test = %ls\n”, someVar.toString(), test.toString());
test = join(join(join(join(var1, L“ + ”), 15), L“ = ”), someVar);
wprintf(
L“out2: %i\n”,
join(join(join(join(var1, L“ + ”), 15), L“ = ”), someVar) == L“333 + 15 = 348”
);
wprintf(L“out3: %ls\n”, test.toString());
test = 20 + join(1, 50);
wprintf(L“out4: %ls\n”, test.toString());
test = letterOf(test, 2) + 20;
wprintf(L“out5: %ls\n”, test.toString());
test = testArr;
testArr.push(testArr);
test = join(L“testArr = ”, testArr);
wprintf(L“out7: %ls\n”, test.toString());
Last edited by liavka (May 2, 2023 07:51:40)
- liavka
-
47 posts
Scratch to C++ translation and how to make it better
Well.. I actually want to do iti don't think compiling is a good option, unless you want to add it… Well it would be better if the goal wasn't the speed of execution and further code editing possibility. Cause in case of interpreting the project, you can't rely on the c++ compiler optimizations. You can either interpret it with a recursion or by making some byte-code layer, and both these options are not as good as just translating to c++. The first one is too slow and the second is just horrible in terms of development effort
- WojtekGame
-
1000+ posts
Scratch to C++ translation and how to make it better
is this just converting sb3 to .cpp?That'll be a very difficult task. Scratch has lots of weird quirks and so does JavaScript. For example, many of the following are present:There are more, but they probably don't apply to Scratch. It's very difficult to create weird auto-conversions for everything, so good luck!console.log(Number("0x1") + 1) // 2 console.log(Number("0b11") + 1) // 4 console.log(Number("0o11") + 1) // 10
Well… As I've said the strings/numbers are implemented rn, so I canstr1 = “some22Test”
str2 = letterOf(str1, 5)
num = str2 + 10
// num = 12 now
I even have some tests file I use while developing features, and there is a good example:Value var1 = 1;
Value var2 = var1 + 15;
Value someVar = var2.toString();
Value test = someVar == (var1 + 15);
ValueArray testArr({1, 2, L“gg 10”});
wprintf(L“out1: someVar = %ls; test = %ls\n”, someVar.toString(), test.toString());
test = join(join(join(join(var1, L“ + ”), 15), L“ = ”), someVar);
wprintf(
L“out2: %i\n”,
join(join(join(join(var1, L“ + ”), 15), L“ = ”), someVar) == L“333 + 15 = 348”
);
wprintf(L“out3: %ls\n”, test.toString());
test = 20 + join(1, 50);
wprintf(L“out4: %ls\n”, test.toString());
test = letterOf(test, 2) + 20;
wprintf(L“out5: %ls\n”, test.toString());
test = testArr;
testArr.push(testArr);
test = join(L“testArr = ”, testArr);
wprintf(L“out7: %ls\n”, test.toString());
this can be a option but having a bare bones exe is also good, but it will just show the sdl gui.

me on my way for Minecraft… (1 DAY TO GET IT LET'S GOOO)
JS: code-gui | scratch-player | Roost.js | Pandoa | TOP: Roost.js || C++: Gamine | Gamiinuu | TOP: Gamine
Else: Emoji Painter | Forum Shopping Mall | TOP: Emoji Painter || Profile:Scratch | GitHub
Orgs: Chroast | Twerkish | Komond OS | JS Inventions | TOP: JS Inventions || Ocular Naming: 0 | 1 | 2 | 3
Games: Mom Hid My Game! | Creeper Adventuring
else might be outdated or removed by the ST.
- liavka
-
47 posts
Scratch to C++ translation and how to make it better
is this just converting sb3 to .cpp?That'll be a very difficult task. Scratch has lots of weird quirks and so does JavaScript. For example, many of the following are present:There are more, but they probably don't apply to Scratch. It's very difficult to create weird auto-conversions for everything, so good luck!console.log(Number("0x1") + 1) // 2 console.log(Number("0b11") + 1) // 4 console.log(Number("0o11") + 1) // 10
Well… As I've said the strings/numbers are implemented rn, so I canstr1 = “some22Test”
str2 = letterOf(str1, 5)
num = str2 + 10
// num = 12 now
I even have some tests file I use while developing features, and there is a good example:Value var1 = 1;
Value var2 = var1 + 15;
Value someVar = var2.toString();
Value test = someVar == (var1 + 15);
ValueArray testArr({1, 2, L“gg 10”});
wprintf(L“out1: someVar = %ls; test = %ls\n”, someVar.toString(), test.toString());
test = join(join(join(join(var1, L“ + ”), 15), L“ = ”), someVar);
wprintf(
L“out2: %i\n”,
join(join(join(join(var1, L“ + ”), 15), L“ = ”), someVar) == L“333 + 15 = 348”
);
wprintf(L“out3: %ls\n”, test.toString());
test = 20 + join(1, 50);
wprintf(L“out4: %ls\n”, test.toString());
test = letterOf(test, 2) + 20;
wprintf(L“out5: %ls\n”, test.toString());
test = testArr;
testArr.push(testArr);
test = join(L“testArr = ”, testArr);
wprintf(L“out7: %ls\n”, test.toString());
this can be a option but having a bare bones exe is also good, but it will just show the sdl gui.
Right now it doesn't exactly convert sb3, but rather a snap project through some snap magic. But in fact, yes, we just convert projects to c++.
But you can easily use the same runtime to create an interpreter. It's not some hardcoded environment at all. You can dynamically add new coroutines, sprites etc
- liavka
-
47 posts
Scratch to C++ translation and how to make it better
Sooo we almost have mouse support and I also found some good SDL2 GUI libraries. It seems, it won't be too hard to add gui input/output rn
- ajskateboarder
-
1000+ posts
Scratch to C++ translation and how to make it better
Nice project! Maybe you could add some previews when GUI support arrives?
I always seemed to get mad at people in 2022 posts and overuse things like :facepalm: If you are looking at one of those posts, just note I don't write like this anymore
software developer
artist
swimming and biking enjoyer

- Discussion Forums
- » Advanced Topics
-
» Scratch to C++ translation and how to make it better