allegro.js is a simple structural programming library for creating browser based games using HTML5 canvas written in javascript. It aims to be a simple tool that is both easy to pick up for beginners and industry veterans alike as well as a go-to tool for game jams and cool things that run in a browser. That said, here are some cool selling points of allegro.js, to which it will hopefully live up to. It uses structural programming that was long lost among game developers, but still proves much more handy in situations that require swift and foolproof solutions and is much more understandable and readable for beginners with no prior knowledge of game programming.
Here's a simple Hello World code for allegro.js. Hopefully, it doesn't need much explanation.
function main()
{
allegro_init();
set_gfx_mode("canvas_id", 640, 480);
clear_to_color(canvas,makecol(255,255,255));
textout_centre(canvas,font,"Hello World!",SCREEN_W/2,SCREEN_H/2,24,makecol(0,0,0));
return 0;
}
END_OF_MAIN();
And here is the html code you need to provide to launch this.
<!doctype html>
<html>
<head>
<script src="allegro.js"></script>
<script src="exhello.js"></script>
</head>
<body>
<canvas id="canvas_id" width="640" height="480"></canvas>
</body>
</html>
Where allegro.js is the library file, and exhello.js contains the aforementioned code. This is all you need to get started! allegro.js aim is to provide a dive-in experience, without worrying about complicated concepts.
I hope for allegro.js to be a tool perfectly suitable for absolute beginners, who never had experience using game engines, such as I was when I started in Allegro, but it is also perfectly suitable for experienced Javascript developers who are looking for a lightweight solution, game jammers seeking for a RAD game making tool, developers looking for a non-OOP tool to create games and obviously, hard core old school classic Allegro nerds who somehow prevailed and want to come back to using it.
First up, read all of this README! Then, there are several placed you can go to get documentation
load_bitmap()
, draw_sprite()
, stretch_sprite()
, rotate_sprite()
, pivot_sprite()
, rotate_scaled_sprite()
, pivot_scaled_sprite()
, blit()
, stretch_blit()
load_sample()
and play_sample()
getpixel(), putpixel(), rect(), rectfill(), line(), vline(), hline(), triangle(), trianglefill(), polygon(), polygonfill()
load_font(), textout(), textout_centre(), textout_right()
intall_int(), intall_int_ex(), loop()
mouse_x, mouse_y, mouse_z, mouse_b
key[KEY_*]
RAD(), DEG(), distance(), dot(), distance2(), scale(), clamp(), scaleclamp(), lerp(), abs(), sgn(), angle(), andglediff()
enable_debug(), log() and wipe_log()
ready()
and display a progress bag using loading_bar()
allegro.js is a minimal javascript port of a really popular game programming library called Allegro 4 from the first decade of our century. Allegro 4 is a simple game programming library that abstracts a set of functions in C programming language to the developer allowing for simple and rapid creation of video games, without the need to program entity systems, classes and so on. Allegro 5 is the current Allegro version, but it differs greatly as to how the API works, thus alienating many hardcore Allegro users. This library aims to provide a simple wrapper for a subset of canvas operations making it look like good old Allegro.
Since the underlying system is completely different for allegro.js (Allegro used C and a bit of assembly inside too), there are a bunch of things that doesn't make sense for allegro.js, but there are new things that make perfect sense here, while they didn't back in the days. As I will try to keep it as close to original as possible, not to break the feel, there are bound to be discrepancies, and I'll list them right here!
set_gfx_mode
takes canvas id, and pixel dimensions as arguments.rotate_sprite
draws the rotated sprite centred, as opposed to top-left alignmentEND_OF_FUNCTION()
and LOCK_
macros are no longer necessaryloop()
function, as js doesn't support blocking codeready()
function wrapper lets you execute code ensuring everything has been loaded already.ready()
can display a custom loading bar as well as a default oneremove_all_ints()
has been added for simplicitypreventDefault()
, meaning that i.e. pressing backspace in game won't take you to previous website!create_video_bitmap()
are all gone, everything is accelerated in js anywaysmakecol
always produces 32bit 0xAARRGGBB value and takes 4 components with optional alphamakecolf
has been added for 0..1 colorsarcfill
, trainglefill
and polygonfill
addedmasked_blit
functions are no longer needed, sicne everythign relies on alpha channeltextprintf
functions are gone, since textout
can do the same without printf
stufftextout
has a size argument now, due to all fonts being ttfplay_sample
no longer supports panningThat's what it's made for! Here's exgame.js with comments stripped down. In under 50 lines of code, you can have an actual game!
var man,apple,bg;
var munch;
var apple_x=200,apple_y=200;
var player_x=100,player_y=100;
var score = 0;
function draw()
{
draw_sprite(canvas,bg,0,0);
draw_sprite(canvas,man,player_x,player_y);
draw_sprite(canvas,apple,apple_x,apple_y);
textout(canvas,font,"Score: " + score,10,20,24,makecol(255,255,255));
}
function update()
{
if (key[KEY_UP]) player_y-=4;
if (key[KEY_DOWN]) player_y+=4;
if (key[KEY_LEFT]) player_x-=4;
if (key[KEY_RIGHT]) player_x+=4;
if (distance(player_x,player_y,apple_x,apple_y)<20)
{
play_sample(munch);
apple_x = rand()%(SCREEN_W-32);
apple_y = rand()%(SCREEN_H-32);
score++;
log("Apple eaten!");
}
}
function main()
{
enable_debug('debug');
allegro_init_all("canvas_id", 640, 480);
man = load_bmp("data/man.png");
apple = load_bmp("data/apple.png");
bg = load_bmp("data/grass.jpg");
munch = load_sample("data/munch.mp3");
ready(function(){
loop(function(){
update();
draw();
},BPS_TO_TIMER(60));
});
return 0;
}
END_OF_MAIN();
allegro.js was made by me, Sos Sosowski. I always liked Allegro 4, and it felt wrong that it's gone already, so I wanted to give it a new life in form of this library. I hope that not only Allegro freaks like me, but also people new to javascript, or even game making in general will pick this up and create amazing stuff.
If you have any questions, suggestions, bug reports or would like to show me a game made with allegro.js (I'd love to know!) Drop me a line!
And don't forget to join the #allegrojs IRC channel at Freenode!
Everyone is mostly welcome to help extend, fix and maintain allegro.js! But if you want to help creating allegro.js, please keep the following several things in mind. These will help me manage pull requests and allegro.js to stay clean of bloat and usable!
--compilation_level SIMPLE_OPTIMIZATIONS --generate_exports --js allegro.js --js_output_file alleg.js
Similarly, if you don't have access to Closure or Java, leave a note!function word_word(a,b,c,d);
schemevar wordword;
schemevar _wordword;
scheme{}
) and defined as follows for proper documenting: function BITMAP_OBJECT(w,h,canvas,context,ready,type) {}
allegro.js licence is the same old Allegro 4 gift-ware licence!
Allegro is gift-ware. It was created by a number of people
working in cooperation, and is given to you freely as a gift.
You may use, modify, redistribute, and generally hack it about
in any way you like, and you do not have to give us anything
in return.
However, if you like this product you are encouraged to thank us
by making a return gift to the Allegro community. This could be
by writing an add-on package, providing a useful bug report,
making an improvement to the library, or perhaps just releasing
the sources of your program so that other people can learn
from them. If you redistribute parts of this code or make a game
using it, it would be nice if you mentioned Allegro somewhere in
the credits, but you are not required to do this.
We trust you not to abuse our generosity.
By Sos Sosowski, 05 August 2015.
(originally written by Shawn Hargreaves, 18 October 1998)
DISCLAIMER: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY
OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT
OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.