Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bunch of tweaks and fixes! #25

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions alleg.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 26 additions & 7 deletions allegro.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ function _touchstart(e)
var point = e.changedTouches.item(c);
var t = {
sx:point.clientX-rect.left,
sx:point.clientY-rect.top,
sy:point.clientY-rect.top,
mx:0,
my:0,
px:point.clientX-rect.left,
Expand Down Expand Up @@ -434,16 +434,21 @@ function install_int_ex(procedure,speed)
/// registered loop procedure
var _loopproc;

// time at which the loopproc was called the last time
var _lasttime;

/// Performs some loop tasks, such as cleaning up pressed[] and released[]
function _uberloop()
{
if (_mouse_installed)
{
mouse_mx = mouse_x - _last_mouse_x;
mouse_my = mouse_y - _last_mouse_y;
mouse_mz = mouse_z - _last_mouse_z;
mouse_mz = mouse_z - _last_mouse_z;
}
_loopproc();
var delta = performance.now() - _lasttime;
_lasttime += delta;
_loopproc(delta);
if (_keyboard_installed)
{
for (var c=0;c<0x80;c++)
Expand Down Expand Up @@ -478,14 +483,28 @@ function _uberloop()
}
}

function _frame()
{
_uberloop();
window.requestAnimationFrame(_frame);
}

/// Game loop interrupt
/// Loop is the same as interrupt, except, it cannot be stopped once it's started. It's meant for game loop. remove_int() and remove_all_ints() have no effect on this. Since JS can't have blocking (continuously executing) code and realise on events and timers, you cannot have your game loop inside a while or for argument. Instead, you should use this to create your game loop to be called at given interval. There should only be one loop() function! Note that mouse mickeys (mouse_mx, etc.), and pressed indicators (pressed[] and mouse_pressed) will only work inside loop()
/// @param procedure function to be looped, preferably inline, but let's not talk coding styles here
/// @param speed speed in the same format as install_int_ex()
/// @param procedure function to be looped, preferably inline, but let's not talk coding styles here. Takes optional "delta" parameter with time (ms) that passed since the last invocation.
/// @param speed speed in the same format as install_int_ex(), or 0 to synchronize with refresh rate
function loop(procedure,speed)
{
_lasttime = performance.now();
_loopproc = procedure;
var timer_id = window.setInterval(_uberloop,speed);
if (speed)
{
var timer_id = window.setInterval(_uberloop,speed);
}
else
{
window.requestAnimationFrame(_frame);
}
log("Game loop initialised!");
//_installed_timers.push({timer:procedure,id:timer_id});
}
Expand Down Expand Up @@ -513,7 +532,7 @@ function _progress_check()
}
if (_downloadables[c].ready) num_loaded++;
}
if (_bar_proc) _bar_proc(num_assets/num_loaded);
if (_bar_proc) _bar_proc(num_loaded/num_assets);
if (num_loaded<num_assets)
{
window.setTimeout(_progress_check,100);
Expand Down
Loading