Skip to content

Commit

Permalink
engineAddPlugin
Browse files Browse the repository at this point in the history
simplify plugin registration to one function
  • Loading branch information
KilledByAPixel committed Oct 4, 2024
1 parent d6f7104 commit f3a6465
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 21 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "littlejsengine",
"version": "1.9.8",
"version": "1.9.9",
"description": "LittleJS - Tiny and Fast HTML5 Game Engine",
"main": "dist/littlejs.esm.js",
"types": "dist/littlejs.d.ts",
Expand Down
9 changes: 5 additions & 4 deletions plugins/box2d.js
Original file line number Diff line number Diff line change
Expand Up @@ -799,15 +799,16 @@ function box2dEngineInit(gameInit, gameUpdate, gameUpdatePost, gameRender, gameR
box2dWorld.SetDebugDraw(box2dDebugDraw);

// hook up box2d plugin to update and render
addPluginUpdate(function()
engineAddPlugin(box2dUpdate, box2dRender);
function box2dUpdate()
{
box2dWorld.Step(timeDelta, box2dStepIterations, box2dStepIterations);
});
addPluginRender(function()
}
function box2dRender()
{
if (box2dDebug || debugPhysics && debugOverlay)
box2dWorld.DrawDebugData();
});
}

// start littlejs
engineInit(gameInit, gameUpdate, gameUpdatePost, gameRender, gameRenderPost, imageSources);
Expand Down
5 changes: 3 additions & 2 deletions plugins/postProcess.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ function initPostProcess(shaderCode, includeOverlay=false)
overlayCanvas.style.visibility = 'hidden';

// Render the post processing shader, called automatically by the engine
addPluginRender(function()
engineAddPlugin(undefined, postProcessRender);
function postProcessRender()
{
if (headlessMode) return;

Expand Down Expand Up @@ -97,5 +98,5 @@ function initPostProcess(shaderCode, includeOverlay=false)
glContext.uniform1f(uniformLocation('iTime'), time);
glContext.uniform3f(uniformLocation('iResolution'), mainCanvas.width, mainCanvas.height, 1);
glContext.drawArrays(gl_TRIANGLE_STRIP, 0, 4);
});
}
}
23 changes: 12 additions & 11 deletions src/engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const engineName = 'LittleJS';
* @type {String}
* @default
* @memberof Engine */
const engineVersion = '1.9.8';
const engineVersion = '1.9.9';

/** Frames per second to update
* @type {Number}
Expand Down Expand Up @@ -89,14 +89,14 @@ let frameTimeLastMS = 0, frameTimeBufferMS = 0, averageFPS = 0;
const pluginUpdateList = [], pluginRenderList = [];

/** Add a new update function for a plugin
* @param {Function} updateFunction
* @param {Function} [updateFunction]
* @param {Function} [renderFunction]
* @memberof Engine */
function addPluginUpdate(updateFunction) { pluginUpdateList.push(updateFunction); }

/** Add a new render function for a plugin
* @param {Function} renderFunction
* @memberof Engine */
function addPluginRender(renderFunction) { pluginRenderList.push(renderFunction); }
function engineAddPlugin(updateFunction, renderFunction)
{
updateFunction && pluginUpdateList.push(updateFunction);
renderFunction && pluginRenderList.push(renderFunction);
}

///////////////////////////////////////////////////////////////////////////////
// Main engine functions
Expand Down Expand Up @@ -269,10 +269,11 @@ function engineInit(gameInit, gameUpdate, gameUpdatePost, gameRender, gameRender
const styleBody =
'margin:0;overflow:hidden;' + // fill the window
'background:#000;' + // set background color
'touch-action:none;' + // prevent mobile pinch to resize
'user-select:none;' + // prevent mobile hold to select
'user-select:none;' + // prevent hold to select
'-webkit-user-select:none;' + // compatibility for ios
'-webkit-touch-callout:none'; // compatibility for ios
(!touchInputEnable ? '' : // no touch css setttings
'touch-action:none;' + // prevent mobile pinch to resize
'-webkit-touch-callout:none');// compatibility for ios
document.body.style.cssText = styleBody;
document.body.appendChild(mainCanvas = document.createElement('canvas'));
mainContext = mainCanvas.getContext('2d');
Expand Down
2 changes: 2 additions & 0 deletions src/engineExport.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export {
engineObjectsUpdate,
engineObjectsDestroy,
engineObjectsCallback,
engineAddPlugin,

// Globals
debug,
Expand Down Expand Up @@ -101,6 +102,7 @@ export {
setObjectMaxSpeed,
setGravity,
setParticleEmitRateScale,
setTouchInputEnable,
setGamepadsEnable,
setGamepadDirectionEmulateStick,
setInputWASDEmulateDirection,
Expand Down
5 changes: 3 additions & 2 deletions src/engineMedals.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ function medalsInit(saveName)
medalsForEach(medal=> medal.unlocked = (localStorage[medal.storageKey()] | 0));

// engine automatically renders medals
addPluginRender(function()
engineAddPlugin(undefined, medalsRender);
function medalsRender()
{
if (!medalsDisplayQueue.length)
return;
Expand All @@ -55,7 +56,7 @@ function medalsInit(saveName)
time > slideOffTime ? (time - slideOffTime) / medalDisplaySlideTime : 0;
medal.render(hidePercent);
}
});
}
}

/** Calls a function for each medal
Expand Down

0 comments on commit f3a6465

Please sign in to comment.