[DEV] Bennu as embebed scripting (Sample)

Started by SplinterGU, August 10, 2008, 05:36:01 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

SplinterGU

It is well known that a lot of commercial games (regardless of being written in C or some other powerful language) use scripting languages to do certain things like gamelogic and other scriptable stuff, so it's not in the compiled code (binary of the program). This way modification is easy and fast, as only the script has to be reloaded instead of recompiling the entire binary. Examples of this are Lucas Arts's games and Lua or even Quake and their quakec, to name a few.

Well, we cannot fall behind, so here is an example of how Bennu can be used like a script in projects (C in this case).


/*
*  Copyright © 2008 SplinterGU
*
*  This file is part of Bennu - Game Development
*
*  Bennu is free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  Bennu is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with this program; if not, write to the Free Software
*  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
*
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "bgdi.h"
#include "bgdrtm.h"
#include "offsets.h"

/* ---------------------------------------------------------------------- */

int main (int argc, char **argv)
{
    INSTANCE * r ;
    PROCDEF * proc;
    int ret;

    /* Initialization (modules needed before dcb_load) */
    string_init() ;
    init_c_type() ;

    /* Load dcb file */
    if (!dcb_load("sample.dcb")) {
        printf ("Error loading dcb\n") ;
        return -1;
    }

    /* Initialization (modules needed after dcb_load) */
    sysproc_init () ;

    /* ---------- Sum two Numbers ---------------------- */

    proc = procdef_get_by_name ("ADD"); // Get definition function (name must be in uppercase)
    if (!proc)
    {
        printf ("Procc 'ADD' not found\n") ;
        return -1 ;
    }
    r = instance_new (proc, 0) ; // Create Function

    PRIDWORD(r, 0) = 12 ;    // Argument 1
    PRIDWORD(r, 4) = 32 ;    // Argument 2

    ret = instance_go(r) ; // Call Function

    printf ("%d\n", ret);

    /* --------- Create file with random data ---------- */

    proc = procdef_get_by_name ("CREATE_FILE"); // Get definition function (name must be in uppercase)
    if (!proc)
    {
        printf ("Procc 'CREATE_FILE' not found\n") ;
        return -1 ;
    }
    r = instance_new (proc, 0) ; // Create Function

    PRIDWORD(r, 0) = string_new("file.dat") ; // Argument 1
    string_use(PRIDWORD(r, 0));

    ret = instance_go(r) ; // Call Function

    printf ("%d\n", ret);

    /* ------------------------------------------------- */

    do_exit(0);

    return 0;
}

/* ---------------------------------------------------------------------- */



import "mod_rand";
import "mod_file";
import "mod_string";
import "mod_say";


process add(int a, int b)
begin
    say("sum>"+a+"+"+b+"="+(a+b));
    return (a+b);
end


process create_file(string f)
private
    n;
    fp;
    v1,v2;
begin

    say("create_file entry");

    fp = fopen(f, O_WRITE);
    for (n = 0; n < 10; n++)
        v1 = rand(1,100);
        v2 = rand(1,100);
        fputs(fp,"add("+v1+","+v2+")="+add(v1,v2));
    end
    fclose(fp);
    say("create_file exit");
end

begin
end


This is a proof of concept and demonstrates it works presently. For the final version, I'll create shortcuts (without removing the present way of doing it) to simplify this some more.

For those people who don't understand this fully: you can load a DCB and call functions in that DCB; this DCB contains generated bytecode of functions programmed in the language Bennu. So this means you can also call DCB functions from within a DLL made for Bennu (like callbacks, seen from the Bennu perspective).

PS: Thanks Sandman for fix translation.

Link for sample download http://rapidshare.com/files/136430226/embebed.zip.html
Download Lastest BennuGD Release: http://www.bennugd.org/node/2

Sandman

Good stuff. I'll be using this functionality a lot in the future, thanks!
-- Sandman

SplinterGU

Download Lastest BennuGD Release: http://www.bennugd.org/node/2

square

Great.

Although I shall not be using Bennu in such a manner as for scripting, it's nice to know people can.
Square-theorist.

handsource-dyko

Very nice. Can this be used to embed a bennu application inside a windows/gtk/qt/whatever gui application?

That would be cool, because it would make it possible to make bennu-gui applications.
I can imagine image converters / drawing applications that can natively use bennu's map and fpg formats, simply by embedding
a dcb with the appropriate routines inside some c program.

Also, it may be usefull for creating levelmap editors with a proper windows gui.


TkGhoul


SplinterGU

Download Lastest BennuGD Release: http://www.bennugd.org/node/2