Skip to content

Commit

Permalink
Merge pull request pjasicek#151 from RicoRodriges/emscripten
Browse files Browse the repository at this point in the history
Emscripten compatibility and some improvements
  • Loading branch information
pjasicek authored Jan 26, 2020
2 parents adfeeae + 883fa12 commit b5e7647
Show file tree
Hide file tree
Showing 32 changed files with 498 additions and 189 deletions.
9 changes: 3 additions & 6 deletions Build_Release/ASSETS/ACTOR_PROTOTYPES/LEVEL1/LEVEL1_RAT.XML
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,8 @@
<AttackAction>
<Animation>throweastwest</Animation>
<AttackAnimFrameIdx>2</AttackAnimFrameIdx>
<AttackType>1</AttackType>
<AttackFxImageSet>/LEVEL1/IMAGES/RATBOMB/*</AttackFxImageSet>
<AttackSpawnPositionOffset x="-15" y="0" />
<AttackAreaSize width="0" height="0" />
<Damage>20</Damage>
<AttackSpawnPositionOffset x="-10" y="5" />
<ProjectilePrototype>ActorPrototype_Level1_RatBomb</ProjectilePrototype>
<AgroSensorFixture>
<FixtureType>EnemyAIRangedSensor</FixtureType>
<CollisionShape>Rectangle</CollisionShape>
Expand All @@ -69,7 +66,7 @@
<CollisionMask>2</CollisionMask>
<Friction>0.000000</Friction>
<Density>0.000000</Density>
<Restitution>-1.#QNAN0</Restitution>
<Restitution>0.000000</Restitution>
</AgroSensorFixture>
</AttackAction>
</Attacks>
Expand Down
22 changes: 22 additions & 0 deletions Build_Release/ASSETS/ACTOR_PROTOTYPES/LEVEL1/LEVEL1_RAT_BOMB.XML
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Actor Type="Level1_RatBomb" Parent="ActorPrototype_BaseProjectile" ActorPrototypeName="ActorPrototype_Level1_RatBomb">
<ActorRenderComponent>
<Mirrored>true</Mirrored>
<ImagePath>/LEVEL1/IMAGES/RATBOMB/*</ImagePath>
<Palette>/LEVEL1/PALETTES/MAIN.PAL</Palette>
</ActorRenderComponent>
<AnimationComponent>
<AnimationPath>/LEVEL1/ANIS/RATBOMB/FALLEASTWEST.ANI</AnimationPath>
</AnimationComponent>
<ProjectileAIComponent>
<Damage>20</Damage>
<ProjectileType>DamageType_Explosion</ProjectileType>
<!-- CAN BE MODIFIED IN CODE -->
<ProjectileSpeed x="9.00000" y="0.000000" />
</ProjectileAIComponent>
<PhysicsComponent>
<CollisionFlag>262144</CollisionFlag>
<CollisionMask>1048834</CollisionMask>
<CollisionSize width="25" height="25" />
<PositionOffset x="-10" y="0" />
</PhysicsComponent>
</Actor>
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,13 @@
<StatePriority>5</StatePriority>
<Attacks>
<AttackAction>
<Animation>strike2</Animation>
<AttackAnimFrameIdx>3</AttackAnimFrameIdx>
<!-- The same as RangedAttackAIStateComponent -->
<!-- Level 1 soldiers do not have duck projectile attacks -->
<Animation>strike</Animation>
<AttackAnimFrameIdx>2</AttackAnimFrameIdx>
<AttackType>1</AttackType>
<AttackFxImageSet>/LEVEL1/IMAGES/MUSKETBALL/*</AttackFxImageSet>
<AttackSpawnPositionOffset x="-42" y="7" />
<AttackSpawnPositionOffset x="-42" y="-38" />
<AttackAreaSize width="0" height="0" />
<Damage>10</Damage>
<AgroSensorFixture>
Expand Down
4 changes: 2 additions & 2 deletions Build_Release/ASSETS/LEVEL_METADATA/LEVEL4.XML
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
</LogicsToActorPrototypes>
<ClawSpawnPositions>
<ClawSpawnPosition spawnNumber="0" x="1439" y="4760" />
<ClawSpawnPosition spawnNumber="1" x="9300" y="4248" />
<ClawSpawnPosition spawnNumber="2" x="7081" y="5254" />
<ClawSpawnPosition spawnNumber="1" x="7081" y="5254" />
<ClawSpawnPosition spawnNumber="2" x="9300" y="4248" />
</ClawSpawnPositions>
<TopLadderEnds>
<TopLadderEnd tileId="181" offsetX="0" offsetY="7" />
Expand Down
202 changes: 185 additions & 17 deletions Build_Release/openclaw.html
Original file line number Diff line number Diff line change
@@ -1,14 +1,160 @@
<!DOCTYPE HTML>
<html>
<head></head>
<html lang="en">
<head>
<meta charset="UTF-8">

<title>OpenClaw</title>
<meta name="description" content="Reimplementation of Captain Claw (1997) platformer">

<meta property="og:title" content="OpenClaw project">
<meta property="og:description" content="OpenClaw open source project">
<!--<meta property="og:image" content="preview.png">-->

<!-- Change default viewport width here according to canvas width -->
<meta name="viewport" content="width=660">

<style>
#console {
width: 100%;
height: 12em;
display: block;
background-color: black;
color: white;
font-family: 'Lucida Console', Monaco, monospace;
outline: none;
border: none;
font-size: 0.8em;
}

div {
margin: 0.7em 0;
}

button {
padding: 0.5em 2em;
}
</style>
</head>
<body>
<canvas class="emscripten" id="canvas" oncontextmenu="event.preventDefault()" tabindex=-1></canvas>
<div>
<span id="loading"></span>
<!-- Config's width and height will be used if canvas does not have this attributes -->
<canvas id="canvas" width="640" height="480" oncontextmenu="event.preventDefault()" tabindex=-1></canvas>
</div>
<div>
<button onclick="Module.canvas.requestFullscreen()">Enable Fullscreen Mode</button>
<button onclick="Console.toggleConsole()">Debug Panel</button>
</div>
<div class="debug_panel" style="display:none">
<div class="cheats">
<div>
Cheat codes:
<button onclick="CheatCodeManager.infiniteJumps()">Infinite Jumps</button>
<button onclick="CheatCodeManager.invincible()">God Mode</button>
<button onclick="CheatCodeManager.physicsDebug()">Physics Debug</button>
<button onclick="CheatCodeManager.infiniteAmmo()">Infinite Ammo</button>
</div>
<div>
Powerups:
<button onclick="CheatCodeManager.write('catnip'.toUpperCase(), Module.canvas)">Catnip</button>
<button onclick="CheatCodeManager.write('firesword'.toUpperCase(), Module.canvas)">Fire Sword</button>
<button onclick="CheatCodeManager.write('frostsword'.toUpperCase(), Module.canvas)">Frost Sword</button>
<button onclick="CheatCodeManager.write('lightningsword'.toUpperCase(), Module.canvas)">Lightning Sword</button>
<button onclick="CheatCodeManager.write('invisible'.toUpperCase(), Module.canvas)">Invisible</button>
<button onclick="CheatCodeManager.write('invulnerable'.toUpperCase(), Module.canvas)">Invulnerable</button>
</div>
</div>
<button onclick="Console.clear()">Clear Console</button>
<textarea id="console" rows="12"></textarea>
</div>


<script type='text/javascript'>
var CheatCodeManager = {
jumps: false,
god: false,
physics: false,
ammo: false,
pushEvent: function (key, event, target) {
return target.dispatchEvent(
new KeyboardEvent(event, {
'keyCode': key,
'charCode': key,
'view': window,
'bubbles': true,
'cancelable': true
}));
},
click: function (key, target) {
if (CheatCodeManager.pushEvent(key, 'keydown', target)) {
CheatCodeManager.pushEvent(key, 'keypress', target);
}
CheatCodeManager.pushEvent(key, 'keyup', target);
},
openOrCloseConsole: function (target) {
CheatCodeManager.click(192, target); // "`"
CheatCodeManager.click(8, target); // Backspace
CheatCodeManager.click(8, target); // Backspace
},
write: function (cheat, target) {
CheatCodeManager.openOrCloseConsole(target);
for (var i = 0; i < cheat.length; i++) {
CheatCodeManager.click(cheat.charCodeAt(i), target);
}
CheatCodeManager.click(13, target); // "Enter"
CheatCodeManager.openOrCloseConsole(target);
},
infiniteJumps: function () {
CheatCodeManager.jumps = !CheatCodeManager.jumps;
CheatCodeManager.write("INFINITEJUMP " + (CheatCodeManager.jumps ? "ON" : "OFF"), Module.canvas);
},
invincible: function () {
CheatCodeManager.god = !CheatCodeManager.god;
CheatCodeManager.write("INVINCIBLE " + (CheatCodeManager.god ? "ON" : "OFF"), Module.canvas);
},
physicsDebug: function () {
CheatCodeManager.physics = !CheatCodeManager.physics;
CheatCodeManager.write("PHYSICSDEBUG " + (CheatCodeManager.physics ? "ON" : "OFF"), Module.canvas);
},
infiniteAmmo: function () {
CheatCodeManager.ammo = !CheatCodeManager.ammo;
CheatCodeManager.write("INFINITEAMMO " + (CheatCodeManager.ammo ? "ON" : "OFF"), Module.canvas);
},
};
</script>
<script type='text/javascript'>
var Console = {
elem: document.getElementById('console'),
enabled: false,
toggleConsole: function () {
Console.enabled = !Console.enabled;
Console.elem.parentNode.style.display = Console.enabled ? "block" : "none";
},
printToConsole: function (text) {
if (arguments.length > 1) text = Array.prototype.slice.call(arguments).join(' ');
console.log(text);
Console.elem.value += text + "\n";
},
clear: function () {
Console.elem.value = '';
},
};
</script>
<script type='text/javascript'>
var loadingElement = document.getElementById("loading");

// Emscripten structure
var Module = {
preRun: [],
postRun: [],
preRun: [function () {
// Here you can change canvas size dynamically if it's needed
// var ratio = window.devicePixelRatio || 1;
// Module.canvas.width = Math.max(screen.width, screen.height) * ratio;
// Module.canvas.height = Math.min(screen.width, screen.height) * ratio;

loadingElement.remove();
loadingElement = null;
Module.canvas.style.display = "block";
}],
canvas: (function () {
var canvas = document.getElementById('canvas');

Expand All @@ -22,22 +168,44 @@

return canvas;
})(),

dataFileDownloads: {
"openclaw.data": {
total: 0,
loaded: 0,
}
},
setStatus: function (text) {
if (loadingElement) {
Module.canvas.style.display = "none";
var progress = (Module.dataFileDownloads["openclaw.data"].loaded / Module.dataFileDownloads["openclaw.data"].total * 100) | 0;
if (progress < 100) {
loadingElement.innerHTML = "Game resources are downloading... " + (progress | 0) + "%";
} else {
loadingElement.innerHTML = "Game resources have been downloaded. Game is loading...";
}
}
},

// Could be removed. This code for debug build only
print: Console.printToConsole,
printErr: Console.printToConsole,
};
</script>
<script>
!function (e, t) {
console.info("Loading client...");
var o = t.createElement("script");
o.async = !0;
o.type = "text/javascript";
o.src = "openclaw.js";
o.onerror = function (t) {

function loadGame() {
var script = document.createElement("script");
script.async = !0;
script.type = "text/javascript";
script.src = "openclaw.js";
script.onerror = function (t) {
console.error("Script Error");
console.error(t);
};
var r = t.getElementsByTagName("script")[0];
r.parentNode.insertBefore(o, r)
}(window, document);
var r = document.getElementsByTagName("script")[0];
r.parentNode.insertBefore(script, r);
}

loadGame();
</script>
</body>
</html>
8 changes: 7 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ cmake_minimum_required(VERSION 3.2)
option(Android "Android" OFF)

option(Emscripten "Build as WASM" OFF)
option(Extern_Config "Do not embed config file" ON)
set(EMSCRIPTEN_PATH "CHANGE ME PLEASE!!!/emsdk")

project(OpenClaw)
Expand Down Expand Up @@ -64,7 +65,12 @@ list(APPEND TARGET_LIBS
Box2D
)
if (Emscripten)
set_target_properties(openclaw PROPERTIES LINK_FLAGS "-s WASM=1 -s BINARYEN_METHOD='native-wasm' -s EXPORTED_FUNCTIONS='[_main]' -s USE_SDL=2 -s USE_SDL_IMAGE=2 -s SDL2_IMAGE_FORMATS='[\"pcx\",\"png\",\"tga\"]' -s USE_SDL_TTF=2 -s USE_SDL_GFX=2 -s ASYNCIFY=1 -s TOTAL_MEMORY=268435456 --preload-file ../Build_Release/[email protected] --preload-file ../Build_Release/[email protected] --preload-file ../Build_Release/[email protected] --preload-file ../Build_Release/[email protected] --preload-file ../Build_Release/[email protected] --preload-file ../Build_Release/[email protected]")
if (Extern_Config)
set(CONFIG_PRELOAD_FILE "")
else (Extern_Config)
set(CONFIG_PRELOAD_FILE "--preload-file ../Build_Release/[email protected]")
endif (Extern_Config)
set_target_properties(openclaw PROPERTIES LINK_FLAGS "-s WASM=1 -s BINARYEN_METHOD='native-wasm' -s EXPORTED_FUNCTIONS='[_main]' -s USE_SDL=2 -s USE_SDL_IMAGE=2 -s SDL2_IMAGE_FORMATS='[\"pcx\",\"png\",\"tga\"]' -s USE_SDL_TTF=2 -s USE_SDL_GFX=2 -s ASYNCIFY=1 -s TOTAL_MEMORY=268435456 ${CONFIG_PRELOAD_FILE} --preload-file ../Build_Release/[email protected] --preload-file ../Build_Release/[email protected] --preload-file ../Build_Release/[email protected] --preload-file ../Build_Release/[email protected]")
else (Emscripten)
list(APPEND TARGET_LIBS
SDL2
Expand Down
4 changes: 2 additions & 2 deletions OpenClaw/ActorController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,9 +312,9 @@ bool ActorController::OnTap(int id, const Touch_TapEvent &evt) {
event.type = SDL_KEYDOWN;
event.key.keysym.sym = key;
event.key.keysym.scancode = SDL_GetScancodeFromKey(key);
g_pApp->OnEvent(event);
SDL_PushEvent(&event);
event.type = SDL_KEYUP;
g_pApp->OnEvent(event);
SDL_PushEvent(&event);
return true;
}
case WEAPON_TAP_RECOGNIZER:
Expand Down
2 changes: 1 addition & 1 deletion OpenClaw/ClawGameLogic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ void ClawGameLogic::CheckpointReachedDelegate(IEventDataPtr pEventData)
m_pGameSaveMgr->AddCheckpointSave(m_pCurrentLevel->GetLevelNumber(), checkpointSave);

// If not in testing mode
if (!g_pApp->GetGlobalOptions()->loadAllLevelSaves)
if (m_pGameSaveMgr->IsSaveSupported() && !g_pApp->GetGlobalOptions()->loadAllLevelSaves)
{
TiXmlDocument saveGamesDoc;
saveGamesDoc.LinkEndChild(m_pGameSaveMgr->ToXml());
Expand Down
1 change: 1 addition & 0 deletions OpenClaw/Engine/Actor/ActorTemplates.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1530,6 +1530,7 @@ namespace ActorTemplates

TiXmlElement* CreateXmlData_PowderKegActor(std::string imageSet, Point position, int32 damage, int32 zCoord)
{
// TODO: create xml actor prototype and implement lift-throw logic
TiXmlElement* pActor = new TiXmlElement("Actor");
pActor->SetAttribute("Type", imageSet.c_str());

Expand Down
Loading

0 comments on commit b5e7647

Please sign in to comment.