This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2009-07-26 23:06:54

menway
Scratcher
Registered: 2009-07-26
Posts: 5

Porting Scratch Projects to Free Pascal (Lazarus)

Hello everyone,
I found this site last week and have tested several projects (most are games :-) ) before writing this topic. I have to say I'm very excited to see such an easy-to-use software that could help people build applications in such a creative way.
I'm still new to Scratch but I see so many people complained that no "export" functions were supported by Scratch so far. I saw some workarounds to combine the image and project in a single executable, but some people preferred a binary file that could run without the Scratch image, which could also hide their codes :-). An idea came to my mind that it'll be helpful if there were a tool that could help porting Scratch projects to Lazarus projects (a open source replacement for well-known Delphi). Then with the help of Lazarus, we can build NATIVE applications for win32, linux, mac or even wince. Of course, the tool will be written in Free Pascal. I listed the milestones below to achieve this.

1. Read .SB file and extract resources, scripts to individual files
2. Build up equivalent APIs for Scratch (written in Free Pascal)
3. Translate Scratch scripts to Free Pascal grammar
4. Merge resources to Lazarus project
5. The Lazarus project is generated and could be compiled to several different targets

I'm in the first step now and I'm searching for documents that describe the file format of .SB file, I need extract resources, scripts out of the .SB file before I can proceed. Anyone has clues? Thanks.

For more information about Free Pascal and Lazarus, please visit: http://www.lazarus.freepascal.org/

Regards,
Menway

Offline

 

#2 2009-07-27 03:32:50

menway
Scratcher
Registered: 2009-07-26
Posts: 5

Re: Porting Scratch Projects to Free Pascal (Lazarus)

Oh I've got the format info in class ObjReader of ScratchApplet package. Now need some time to translate to Free Pascal. I'll post the result when there's any update. :-)

Offline

 

#3 2009-07-27 04:24:11

billyedward
Scratcher
Registered: 2008-01-03
Posts: 500+

Re: Porting Scratch Projects to Free Pascal (Lazarus)

That's a very ambitious solution... However, I'm sure that there is a better language to use. I have even contemplated scratch to javascript!


"I'd love to change the world, but they haven't released the source code yet."
Check out the latest version of Streak --> http://billy.scienceontheweb.net/Streak

Offline

 

#4 2009-07-27 05:39:21

menway
Scratcher
Registered: 2009-07-26
Posts: 5

Re: Porting Scratch Projects to Free Pascal (Lazarus)

Yes I think if we can successfully translate scratch to one language in this way, it'll be much easier to translate it to another one: just apply a different grammar and implement same APIs.

After translate it to Free Pascal, I'll continue to translate to c++, etc. My final target is to deploy scratch projects to embedded devices such as windowsmobile, iphone, Android and psp. :-) Then scratch will be a united platform for 2d game design.

And javascript as you mentioned is also a good idea, so that we can play games on web pages. :-)

Offline

 

#5 2009-07-27 20:12:51

billyedward
Scratcher
Registered: 2008-01-03
Posts: 500+

Re: Porting Scratch Projects to Free Pascal (Lazarus)

Yes. Here's the basics of how it would run:
> All variables are read and parsed, the 'var XXX' code is added to the beginning of a head section script. XXX being the variable name in scratch.
> The user is prompted for the root loction of all required media.
> There is a folder created in the root directory for every sprite, and one for the background.
> There are three folders in each of these: costumes, sounds, and scripts.
> All costumes are exported to png format into the correct costumes directory. (png is high quality, and supports the alpha channel)
> All sounds are exported to the sounds directory.
> all scripts are converted to javascript files. details below.
> A javascript object is created for each sprite.
> each script is loaded from the scripts folder, and a method is created for each.
> a 'return "done"' statement is added to the end, so allow the 'broadcast and wait' block to work properly.
> each object loads the first costume as an image. It is positioned absolute, at the coordinates 0,0 (left:240;top:180).
> Here are how some blocks will work:
- set x to [0] : document.getElementById('sprite').style.top='0'.
- switch to costume [costume1] : document.getElementById('sprite').src='ROOT/sprite/costumes/costume1'.
- if <boolean> code1 else code2 : if (boolean) {code1} else {code2};
- repeat(#) code : for (i=0;i<=#;i++) {code}
- ask[pick a number...] : var ASK1 = prompt('pick a number',' ');
- answer : ASK1
- ( + ) : +
- < or > : ||
> The rest of the blocks would be implemented similarly.


"I'd love to change the world, but they haven't released the source code yet."
Check out the latest version of Streak --> http://billy.scienceontheweb.net/Streak

Offline

 

#6 2009-08-21 03:20:03

menway
Scratcher
Registered: 2009-07-26
Posts: 5

Re: Porting Scratch Projects to Free Pascal (Lazarus)

[20090821] Update:
Finally I drafted a tool to extract all resources from Scratch project .SB file (including backgrounds, lists, scripts, sounds, sprites and variables). Current version 1.00 alpha, tested good. Any comment is welcomed~~

Download from here:
http://www.uushare.com/user/menway/file/1927774

Usage sample:
C:\>java -jar Extract.jar

  Scratch Project File Extract Tool, version: 1.00(alpha)
            Written by Menway [menway@gmail.com]
============================================================
Enter source SB filename: Test\FishChomp.sb
Enter destination folder: Test\FishChomp
Extracting Project Info...Done!
Extracting Stage (1)...
  Extracting Variables...
  Done!
  Extracting Scripts...
  Done!
  Extracting Sounds and Backgrounds...
    Extracting Background (underwater)...Done!
  Done!
    Extracting Sprites...
      Extracting Sprite (goldfish 2)...
        Extracting Scripts...
          Extracting Script (1)...
            Extracting Script (1)...
              Extracting Script (1)...
              Done!
            Done!
          Done!
        Done!
        Extracting Sounds and Backgrounds...
          Extracting Costume (costume 2)...Done!
        Done!
      Done!
      Extracting Sprite (goldfish 3)...
        Extracting Scripts...
          Extracting Script (1)...
            Extracting Script (1)...
              Extracting Script (1)...
              Done!
            Done!
          Done!
        Done!
        Extracting Sounds and Backgrounds...
          Extracting Costume (costume 2)...Done!
        Done!
      Done!
      Extracting Sprite (goldfish 1)...
        Extracting Scripts...
          Extracting Script (1)...
            Extracting Script (1)...
              Extracting Script (1)...
              Done!
            Done!
          Done!
        Done!
        Extracting Sounds and Backgrounds...
          Extracting Costume (costume 2)...Done!
        Done!
      Done!
      Extracting Sprite (hungry fish)...
        Extracting Scripts...
          Extracting Script (1)...
            Extracting Script (1)...
            Done!
          Done!
          Extracting Script (2)...
            Extracting Script (1)...
            Done!
          Done!
        Done!
        Extracting Sounds and Backgrounds...
          Extracting Costume (open-mouth)...Done!
          Extracting Costume (closed-mouth)...Done!
          Extracting Sound (chomp)...Done!
        Done!
      Done!
      Extracting Sprite (instructions)...
        Extracting Scripts...
        Done!
        Extracting Sounds and Backgrounds...
          Extracting Costume (costume)...Done!
        Done!
      Done!
    Done!
Done!

============================================================
All Done! Test\FishChomp.sb was extracted to folder: Test\FishChomp.

Last edited by menway (2009-08-21 03:44:54)

Offline

 

#7 2009-08-21 03:44:04

menway
Scratcher
Registered: 2009-07-26
Posts: 5

Re: Porting Scratch Projects to Free Pascal (Lazarus)

Some notes for extracted resources:

(*) Resource root directory and its sub-directories for each type of resource are auto-generated by this tool. So the "destination folder" doesn't need exist before running this tool.

(*) Command line interface is also available, which means, "source SB filename" and "destination folder" could be provided in parameters. e.g., java -jar Extract.jar Test\FishChomp.sb Test\FishChomp

(*) All sounds are saved in WAV format.

(*) All images are saved in PNG format (as billyedward suggested ^_^)

(*) All color values are generated as "RGB(red, green, blue)"

(*) All string values are covered by double quotation marks.

(*) All boolean values are presented as "true" or "false"

(*) A thumbnail image is generated for the project and placed in root dir.

(*) An attribs.txt file is generated for the project, which includes information about author, comment, scratch-version, history, language, etc. Multi-line values are using "<br>" as the line breaker.

(*) A sub-dir "stages" is created, which includes all stages in the project. (Even so far as I know, there's only 1 stage supported in Scratch. correct?)

(*) In all resource directories, items are stored in separated sub-directories, with a numeric folder name, starting from 1.

(*) For a stage, following resources are extracted: backgrounds, lists, scripts, sounds, sprites and variables in separate sub-dirs.

(*) An attribs.txt file is generated for a stage, which includes information about position and size (left, top, right, bottom), background color, name, default_background (the name of one of its backgrounds), sound attr (volume and tempo).

(**) For a background, a named PNG image file and a attribs.txt are generated. File attribs.txt includes information about background name and image file name.

(**) An attribs.txt is generated for a list, which includes information about name, position and size (left, top, right, bottom), visible (boolean), background color, shadow color and items list. Items list is a set of string values, e.g. ["first item", "second item", "third item"]

(**) For a script, attribs.txt and codes.txt are generated. In attribs.txt, position info is generated (left and top), which is only used in Scratch SDE. Script codes are saved in codes.txt.

(***) Also, a script may have sub-script(s), and these sub-scripts may have their sub-scripts... e.g. doForever(subScript[1]), where subScript[1] means the script placed in codes.txt under sub-dir "scripts/1/" of current directory.

(***) All sub-scripts are generated as elements of array subScript[], e.g. "subScript[index]"

(**) For a sound, an attribs.txt and a named wav file are generated. File attribs.txt includes information about name, sound file name and rate.

(**) For a sprite, an attribs.txt is generated as well as following sub-dirs: costumes, scripts and sounds. File attribs.txt includes information about position and size (left, top, right, bottom), name, rotate_degree, rotate_method ("normal" or "leftRight" or "none") and draggable (boolean).

(***) For a costume, an attribs.txt and a named image file are generated.File attribs.txt includes information about costume name and image file name.

(**) An attribs.txt is generated for a variable, which includes information about name, value, visible (boolean), position and size (left, top, right, bottom), name_visible (boolean).

Last edited by menway (2009-08-21 04:38:17)

Offline

 

#8 2009-08-21 05:11:54

Paddle2See
Scratch Team
Registered: 2007-10-27
Posts: 1000+

Re: Porting Scratch Projects to Free Pascal (Lazarus)

Sounds like you have made a lot of progress!  This is very exciting work  smile


http://i39.tinypic.com/2nav6o7.gif

Offline

 

Board footer