
OpenTTD Compiled to WebAssembly - garaetjjte
https://milek7.pl/openttd-wasm/
======
Game_Ender
The combination of C++ and OpenGL is really the ultimate in portability. A
language supported on almost every platform by one if not more implementations
(sometimes 3), coupled with probably the most widely supported graphics API.
Of course you lose accessibility, and many platform API conveniences, but you
get a more stable and performant platform to work with. In fact it's the
platform that most web browser themselves are heavily based on. For another
example just look at what Epic was able to do with Fortnite, one code base
that literally runs everywhere.

Magnum [0] is a favorite of mine for creating portable OpenGL C++
applications. It also has support for compiling to WebAssembly [1] and using
WebGL2.

0 - [https://magnum.graphics](https://magnum.graphics)

1 -
[https://magnum.graphics/showcase/picking/](https://magnum.graphics/showcase/picking/)

~~~
ianlevesque
Well, it was for a while:

[https://www.anandtech.com/show/12894/apple-deprecates-
opengl...](https://www.anandtech.com/show/12894/apple-deprecates-opengl-
across-all-oses)

~~~
Game_Ender
Yeah there is MoltenGL which will probably be open sourced eventually. The
real future is Vulkan which is moving to replace OpenGL and for the Apple
platforms there is MoltenVK [0] (Vulkan re-implemented on top of Apple's new
Metal) which is open source. Magnum has an initial Vulkan backend [1], and
many other engines have them or are building them as well.

0 -
[https://github.com/KhronosGroup/MoltenVK](https://github.com/KhronosGroup/MoltenVK)

1 -
[https://blog.magnum.graphics/announcements/2018.10/](https://blog.magnum.graphics/announcements/2018.10/)

~~~
ianlevesque
That’s cool I didn’t realize MoltenVK was maintained by Khronos.

~~~
AlphaSite
I think it was donated to them.

~~~
SXX
Valve acquired the project and made it open source.

------
kjeetgill
For anyone else wondering OpenTTD is Open Transport Tycoon Deluxe.

~~~
ncmncm
I wondered too.

"OpenTTD is a business simulation game in which players try to earn money via
transporting passengers and freight by road, rail, water and air. It is an
open-source[2] remake and expansion of the 1995 Chris Sawyer video game
Transport Tycoon Deluxe."

[https://en.m.wikipedia.org/wiki/OpenTTD](https://en.m.wikipedia.org/wiki/OpenTTD)

~~~
keyle
It was and is a fantastic game. I played it for many hours growing up.

EDIT: the original, Transport Tycoon.

------
codexon
I ran it for a few minutes and then it said:

    
    
      Uncaught RuntimeError: float unrepresentable in integer range
        at wasm-function[3502]:49
        at wasm-function[6940]:395
        at wasm-function[6939]:347
        at wasm-function[3103]:334
        at wasm-function[3284]:30
        at wasm-function[4902]:3105
        at wasm-function[4903]:654
        at wasm-function[8083]:442
        at wasm-function[8082]:3
        at wasm-function[9782]:13

~~~
beefsack
You can avoid that issue by using clamp mode when compiling:
[https://emscripten.org/docs/compiling/WebAssembly.html#trap-...](https://emscripten.org/docs/compiling/WebAssembly.html#trap-
mode)

~~~
garaetjjte
Thanks, changed that setting.

------
kwindla
Porting c/c++ codebases to WebAssembly is both super fun and frustrating.

Network stuff, as mentioned in other comments, is futzy because of the
limitations of running in a browser environment. A surprising amount of
network code "works," in the sense that it compiles and runs translated to web
sockets. (Great work from the WebAssembly and Emscripten community.) But, you
know, web sockets.

pthreads support landed in Chrome recently:
[https://developers.google.com/web/updates/2018/10/wasm-
threa...](https://developers.google.com/web/updates/2018/10/wasm-threads)

Pipes, select, and unix domain sockets aren't really supported (afaik).

------
Freak_NL
It's a shame the online content functionality isn't working because of a
missing zlib library. OpenTTD really has quite an active community of modders
and asset creators.

You want the trains from your home country? They are available, and probably
up to date to the current year. Just download them from within the game,
enable them, and start a new game.

------
neiman
It looks amazing, but for some reason it's not polished on my machine. There
are many small bugs that don't exist in the native version. For example, when
I scroll, the pointer stay frozen till I finish scrolling, and only then
"jumps" to the new location.

It makes a huge difference between enjoyment and suffering in games.

------
NietTim
This is super cool, TTD always has a special place in my heart. Any technical
explanation why level generation is _so much_ quicker? Not even a loading
screen

~~~
benj111
TTD or _Open_ TTD?

TT on my 486 took time to generate a map, (TTD is basically the same code I
believe). Open TTD when you've cranked up the map size takes time too.

Perhaps you were playing TTD on a relatively powerful pc?

------
Simple_Guy
One of the coolest things to do is go on one of the many multiplayer servers
and see some of the super train systems that have been built. 16 lanes high
speed rail connecting thousands of nodes.

------
goddtriffin
Multiplayer functionality doesn't seem to be working for me. Is this just me,
or an artifact of the compilation?

~~~
db48x
The browser doesn't provide the usual POSIX api for opening TCP connections.
With some work you can write a shim library that implements it in terms of
websockets, but it still can't connect to arbitrary hosts. Instead you have to
run a proxy server for it to connect to.

~~~
reubenmorais
Could you use WebRTC instead, for true P2P?

~~~
lxtx
You can. I've used WebRTC for multiplayer support in Quake 3 compiled to
WebAssembly.

~~~
fb03
Do you have a link for that? Sounds super cool

~~~
lxtx
The forked ioquake3 is available at
[https://github.com/seemk/ioq3](https://github.com/seemk/ioq3)

However it's not trivial to get it working at the moment, will hopefully soon
come back to this project and finish it :D

------
danielvf
It even runs on my IOS phone. Incredible!

~~~
Mirioron
It runs on my old android phone too, but I don't think the game is actually
playable. I couldn't move the camera around.

~~~
SXX
Actually it's quite playable as soon as you learn UI and used to multi touch
that game is using. E.g as far as I can recall you just need 2-finger touch to
move camera.

~~~
Mirioron
Didn't work for me.

------
db48x
I'm surprised at how good the sound is; that's where I've had the most
trouble.

~~~
garaetjjte
It's just uses Emscripten SDL2 for audio output. Here's build with MIDI music
enabled: [https://milek7.pl/openttd-wasm-music/](https://milek7.pl/openttd-
wasm-music/)

------
vortico
Hasn't OpenTTD been ported to JS in 2015?
[https://epicport.com/en/ttd](https://epicport.com/en/ttd) Regardless, a
WebAssembly port is perhaps more useful since improvements can be merged from
upstream more easily.

------
LeanderK
Moving the cursor was really sluggish on Safari for the first two to five
minutes and afterwards I didn't notice lag. Maybe it's some JIT-compiler warm-
up phase?

------
adhambadr
this is really cool. Can't wait for GTA2

~~~
recrof
is GTA2 opensource yet?

~~~
adhambadr
no but neither TTD, the OpenTTD is a brilliant remake that is based on TTD.

------
fs2
Really amazing!

------
pjmlp
Yep, go Flash.

[https://en.m.wikipedia.org/wiki/CrossBridge](https://en.m.wikipedia.org/wiki/CrossBridge)

Everything old is new again.

~~~
propelol
It's not a fair comparison. Flash was a closed source non-standard technology.

~~~
skrebbel
That's not why most people hated it.

~~~
dcbadacd
But it's part of the reason. It was also notorious for it's security flaws.

~~~
josefx
One reason I liked flash: You could disable malicious ads with flashblock
without breaking 90% of the internet. With the cancerous growth of JavaScript
APIs embrancing and extending on all that made flash evil that is no longer
possible.

~~~
dcbadacd
That train has sailed already anyways with JS, so I see no difference in that.
Though I do expect websites to become more obfuscated - rendered by wasm so
that adblocks and privacy protectors can't reach as well.

~~~
pjmlp
Flash now lives as Animate CC, however I can easily see future Websites being
something like an Unity export.

