Autor Tema: [DEV] Bennu as embebed scripting (Sample)  (Leído 9302 veces)

0 Usuarios y 1 Visitante están viendo este tema.

Conectado SplinterGU

  • Hero Member
  • *****
  • Mensajes: 12517
  • Karma: 369
  • Sexo: Masculino
    • Ver Perfil
[DEV] Bennu as embebed scripting (Sample)
« en: Agosto 10, 2008, 09:36:01 am »
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).

[code language="c"]
/*
 *  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;
}

/* ---------------------------------------------------------------------- */
[/code]

[code language="bennu"]
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
[/code]

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
« última modificación: Agosto 14, 2008, 04:53:36 am por SplinterGU »
Download Lastest BennuGD Release: http://www.bennugd.org/node/2

Desconectado Sandman

  • Sr. Member
  • ****
  • Mensajes: 298
  • Karma: 48
  • Sexo: Masculino
    • Ver Perfil
Re: [DEV] Bennu like embebed scripting (Sample)
« Respuesta #1 en: Agosto 10, 2008, 11:06:08 am »
Good stuff. I'll be using this functionality a lot in the future, thanks!
-- Sandman

Conectado SplinterGU

  • Hero Member
  • *****
  • Mensajes: 12517
  • Karma: 369
  • Sexo: Masculino
    • Ver Perfil
Re: [DEV] Bennu like embebed scripting (Sample)
« Respuesta #2 en: Agosto 10, 2008, 06:52:22 pm »
Sample download link added.
Download Lastest BennuGD Release: http://www.bennugd.org/node/2

Desconectado square

  • Newbie
  • *
  • Mensajes: 41
  • Karma: 3
  • Sexo: Masculino
  • Square-theorist.
    • Ver Perfil
Re: [DEV] Bennu like embebed scripting (Sample)
« Respuesta #3 en: Agosto 11, 2008, 05:28:35 am »
Great.

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

Desconectado handsource-dyko

  • Hero Member
  • *****
  • Mensajes: 568
  • Karma: 32
  • Sexo: Masculino
  • I'm the eye in the sky.
    • Ver Perfil
    • dyko designs
Re: [DEV] Bennu as embebed scripting (Sample)
« Respuesta #4 en: Octubre 18, 2010, 06:38:15 am »
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.

Conectado FreeYourMind

  • Hero Member
  • *****
  • Mensajes: 5557
  • Karma: 125
  • Sexo: Masculino
    • Ver Perfil
    • GECA soft
Re: [DEV] Bennu as embebed scripting (Sample)
« Respuesta #5 en: Octubre 18, 2010, 01:49:22 pm »
File not found...