Running Gacela programs in the browser (rectification)

Sometimes I have to stick a potato in my mouth before saying anything. Or take a potato in each hand before blogging anything.

In previous post about loading files with javascript, I said that it was not possible with Ajax, it was necessary to use iframes, etc, etc … Filthy Lie! Of course you can, and is much easier.

The code for loadinga Lisp program, compile it and run it would be:

<html>
    <head>
        <script type="text/javascript" src="lisp2js.js"></script>
        <script id="head_js" type="text/javascript"></script>
        <script type="text/javascript">
            function load_file (file) {
                var req = new XMLHttpRequest();
                req.open('GET', file, false);
                req.send(null);
                if (req.status == 200) {
                    var lisp_code = req.responseText;
                    var js_code = string2js(lisp_code);
                    document.getElementById('head_js').text = js_code;
                }
            }
        </script>
    </head>

    <body onLoad="load_file('my_program.lisp'); init();">
    </body>
</html>

This would be the synchronous version, but it can also be done asynchronously. You can see more examples in Using XMLHttpRequest.

Advertisements
Posted in Uncategorized | 1 Comment

Running Gacela programs in the browser

Update: Everything said in this post can be done better using XMLHttpRequest object, as explained in Running Gacela programs in the browser (rectification). If you want to know how not to do it, read on 😀

Gacela project, which I have worked nearly two years and a half, consists of three subprojects:

  • Gacela, language definition and the compiler / interpreter, that runs locally on the computer.
  • Lisp2js, the Lisp to Javascript compiler for translating Gacela programs and running them on a web page.
  • Gacela on Wheels, a web environment for game development using Gacela.

Last months I’m focused in Lisp2js and in the ability to run programs written with Gacela in the browser. The idea is to include a Javascript code as file2js(‘mi_game.lisp’); init_game(); in a web page, translating Lisp code into Javascript code, embedding it into the page and running it. But I encountered a difficulty that is not expected and that surprised me; Javascript, for security reasons, has no functions for working with files.

Next, I’ll explain how we can load files using Javascript and how can Gacela add Javascript code to a web page and run it. Another day I’ll explain Lisp2js‘ inner workings.

To access a file with Javascript (I am talking of files at the same domain) we’ll use the iframe element. There are other ways, for example with Ajax, but that requires using some PHP on the server and the idea is to run locally without a web server.

Here is my code:

<html>
    <head>
        <script type="text/javascript" src="lisp2js.js"></script>
        <script id="head_js" type="text/javascript"></script>
        <script type="text/javascript">
            function load_file (my_file, func) {
                var el = document.getElementById('my_iframe');
                if (el == null) {
                    var el = document.createElement("iframe");
                    el.setAttribute('id', 'my_iframe');
                    el.onload = function() { run_my_code(func); }
                    el.style.display='none';
                    document.body.appendChild(el);
                }
                el.setAttribute('src', my_file);
            }

            function run_my_code (func) {
                var el = document.getElementById('my_iframe');
                var lisp_code = el.contentWindow.document.body.textContent || el.contentWindow.document.body.innerText;
                var js_code = string2js(lisp_code);
                document.getElementById('head_js').text = js_code;
                func();
            }
        </script>
    </head>

    <body onLoad="load_file('my_program.lisp', function() { init(); });">
    </body>
</html>

Two functions are used, the first to create an iframe and specify the file to open and the second to run our code. Two functions are needed because iframe works asynchronously, so we use onload property to say what to do at the end of the content loading.

The first function, load_file, creates the iframe, but if it already exists the function only indicates the file to load. Although in this example it does not make much sense, it’s usefull if we had to upload multiple files.

The second function, run_my_code, executes when the iframe just uploaded the file. By textContent or innerText properties (it depends on the browser) we collect the Lisp code, translate it to Javascript and embed it into the right section in the web page header. Last, we init the execution of the generated code.

Thus, although more transparent, Gacela is capable of loading Lisp code on a web page and run it. That is, the same code runs in the same way locally on a computer or remotely through the browser, which is what is intended.

Posted in Uncategorized | Leave a comment

The Free Game Lag

In Free Software Foundation‘s December Bulletin we can read The Free Game Lag, wrote by Sarvodaya, about relationship between computer games and free software.

For the most part I agree with Sarvodaya, but I think the issue is more important that he says. According to the author:

There is a natural tendency for free software to take on more essential aspects of computing first. While subjective, it is clear that gaming is not a top-priority and, as such, has not advanced as rapidly as say, web browsers or word processors.

Yes, it’s true that it’s very important to focus efforts in programs like web browsers, but it’s also important to develop a lot of free games for attracting new generations of programmers and users. Many of us have enter to the computer world thanks to games, developing some little game or modifying an existing one. It’d be very interesting to use communicative power of games to transmit the free software ideas, freedom and collaboration.

But, how can a games company, with the actual model, make money in the free software world? According to Sarvodaya, It can’t, but it can be better. The Internet and music downloads have made the music bussiness model obsolete, and traditional games bussiness model is arriving to the end too. It’s clear that charging per copy isn’t too profitable, except for some big companies, and little Javascript games in a web with free access get more players. Accessibility wins.

But, how is possible to make money with free games? Ok, I don’t know. Advertising, network games with subscription, … Some years ago nobody knows how to make money with search engines, until Google found the gold mine with advertising. Whoever finds the new bussiness model for computer games will become the Google of digital entertainment world.

P.D.: A game called “Transport Simulator”, and illegal copy of the free game Rigs of Rods, is being sold by the “company” Butterfly Software and distributed without its GPLv3 license and without source code. More information here.

Posted in Uncategorized | Leave a comment

Dependencies

For compiling Gacela’s interpreter you will need the next packages:

  • gcl
  • libsdl1.2-dev
  • libsdl-image1.2-dev
  • libsdl-mixer1.2-dev
  • libftgl-dev
  • libsm-dev
  • libxmu-dev
  • libxaw7-dev
  • libgmp3-dev
  • libreadline5-dev

And, if you want to compile the editor, you will need libgtk2.0-dev too.

Posted in Uncategorized | Leave a comment

Resources Management

Resources Management is one of the strongest features of Gacela. Because Gacela is based on Lisp, we have garbage collection, but Gacela goes one step ahead, managing the memory for images and sounds. In the process of making a game, we need to allocate memory for resources as images, textures, fonts, sounds, songs, etc, and we must free all this memory at the end of the game, or at the end of the level, screen, … Gacela does all this for you.

Gacela has an internal list of resources. Each resource is saved with its properties (memory address, type, …), constructor (function to allocate the resource), destructor (function to free the memory used by the resource) and a timestamp for knowing time since the last access to the resource. When a resource is accessed Gacela updates the timestamp.

There are two types of allocating resources, depending on the timestamp. We can allocate temporary resources, using the value of the timestamp for controlling the use of the resource. When a resource has spent 50 seconds without been accessed, Gacela frees the memory using the destructor, but if the game needs the resource another time, Gacela uses the constructor for allocating it automatically.

A resource can be allocated as a static resource too. A true value t is set as a timestamp and the memory will belong to the resource while the game is running. Of course, the game can free the memory of the resource in an explicit way, using “free-resource”.

We will see Gacela’s Resources Management more in detail in future posts.

¡¡Happy Hacking!!

Posted in Uncategorized | Leave a comment

Setting Game Properties

In my opinion Gacela is a good way of making games. You can program a game and see your changes in real time. For example, you can change the title and the size of the window and see it at the moment, known as setting game properties. It’s possible to change the game properties in any place of the game.

When we program with Gacela, the way of playing with game properties is using “set-game-properties” and “get-game-properties” functions. With “get-game-properties” we get a list with the title of the game, width and height of the window, bits per pixel, frames per second and mode (2d or 3d). And using “set-game-properties”, we can change any of these parameters of the game.

For example, if we want to change game dimensions, we can execute (set-game-properties :width 800 :height 600) and automatically dimensions of the game window are changed.

Or better, we can switch between 2d-mode and 3d-mode, simple using (set-game-properties :mode ‘3d). With Gacela, there is no diferences between a 2-dimensional and 3-dimensional game, only when we indicate the game mode.

Gacela is easy, and in next entries you’ll see that it’s so easy.

See you.

Posted in Uncategorized | Leave a comment

Welcome

Welcome to the Gacela official blog, in wich I’ll keep people up to date with new features, tutorials, and a lot of interesting things about Gacela. But, what is Gacela?

Gacela is an extension of the Lisp programming language to develop games quickly and reliably. When I say “quickly”, I mean making a serious, professional game prototype in an hour or less. I want ordinary people to be able to program their own games to play on their computers, their websites, or their consoles. And I think this is possible, or well, this will be possible, with Gacela.

If you’re interested in learning more about Gacela, stay tuned to this blog, or visit the Gacela website. See you!

Posted in Uncategorized | Leave a comment