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

Emscripten support #7510

Open
wants to merge 14 commits into
base: master
from

Conversation

@Milek7
Copy link

commented Apr 13, 2019

This is currently WIP, but I would appreciate feedback on it.
Libtimidity is restored with mixer output, Emscripten support is added and various fixes for Emscripten quirks are applied.
Dockerfile for build environment is here: https://github.com/Milek7/openttd-emscripten-dockerfile/tree/sdl1

It currently works on SDL1 on 32bpp blitter (8bpp blitter doesn't work, I suspect broken SDL palette support in emscripten shim). It doesn't look correct though, because html provides BGRA buffer and all blitters assume RGBA. Modifying union Colour works, but this is hacky solution.

It works too on SDL2 branch, with significantly better performance, both on 8bpp (with correct colors) and 32bpp (same BGRA issue, but it is faster than 8bpp). Maybe this PR should wait on SDL2, as it works much better on it.

Network is handled by emscripten as WebSockets, both UDP and TCP. It requires proxy, like this one: https://gist.github.com/Milek7/b921ec9f8d875fe2d5a8b06bfd533834
sockaddr sizes and NetworkAddress comparing needs to be investigated, current workaround is to delete domain name after resolving, and comparing generated hostnames by strcmp. Using original code caused duplicated entries in gamelist as UDP queries received weren't matched to correct entry.

@Milek7 Milek7 force-pushed the Milek7:ems1 branch from 3a00b1c to 4976b1d Apr 14, 2019
@glx22

This comment has been minimized.

Copy link
Contributor

commented Apr 14, 2019

It's hidden in the commit-checker log but

*** b/src/video/sdl_v.h:56: Use tabs for indentation: '    uint32 cur_ticks;'
*** b/src/video/sdl_v.h:57: Use tabs for indentation: '    uint32 last_cur_ticks;'
*** b/src/video/sdl_v.h:58: Use tabs for indentation: '    uint32 next_tick;'
*** b/src/video/sdl_v.h:59: Use tabs for indentation: '    std::thread draw_thread;'
*** b/src/video/sdl_v.h:60: Use tabs for indentation: '    std::unique_lock<std::recursive_mutex> draw_lock;'
src/network/core/address.cpp Outdated Show resolved Hide resolved
@Milek7 Milek7 force-pushed the Milek7:ems1 branch from 4976b1d to 5ca5c3a Apr 14, 2019
src/network/core/address.cpp Show resolved Hide resolved
if (err != 0) {
#else
if (err != 0 && errno != EINPROGRESS) {
#endif

This comment has been minimized.

Copy link
@LordAro

LordAro Apr 14, 2019

Member

missing /* __EMSCRIPTEN__ */ suffix

also in other places

src/network/core/address.cpp Show resolved Hide resolved
@@ -24,8 +24,8 @@
const char *NetworkAddress::GetHostname()
{
if (StrEmpty(this->hostname) && this->address.ss_family != AF_UNSPEC) {
assert(this->address_length != 0);
getnameinfo((struct sockaddr *)&this->address, this->address_length, this->hostname, sizeof(this->hostname), nullptr, 0, NI_NUMERICHOST);
int size = this->address.ss_family == AF_INET6 ? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in);

This comment has been minimized.

Copy link
@LordAro

LordAro Apr 14, 2019

Member

why this change? seems like it should be explained sufficiently and split into its own commit
(and this->address_length removed?)

This comment has been minimized.

Copy link
@Milek7

Milek7 Apr 14, 2019

Author

Emscripten getnameinfo returns error when passed size other than sizeof(sockaddr_in)
emscripten-core/emscripten#7636
It isn't guarded by emscripten specific ifdef as it shouldn't hurt other platforms.

int r = this->GetAddressLength() - address.GetAddressLength();
if (r == 0) r = this->address.ss_family - address.address.ss_family;
if (r == 0) r = memcmp(&this->address, &address.address, this->address_length);
if (r == 0) r = this->GetPort() - address.GetPort();
return r;
#else
return strcmp(GetHostname(), address.GetHostname());

This comment has been minimized.

Copy link
@LordAro

LordAro Apr 14, 2019

Member

explanation please.

also it would be easier to read if the ifndef was inverted, imo

src/video/sdl_v.cpp Outdated Show resolved Hide resolved
src/video/sdl_v.cpp Show resolved Hide resolved
@@ -14,8 +14,16 @@

#include "video_driver.hpp"

#ifdef __EMSCRIPTEN__
void em_loop(void *arg);

This comment has been minimized.

Copy link
@LordAro

LordAro Apr 14, 2019

Member

is there a particular reason not to make this a (conditional) class method? would save the duplicate friend definition

This comment has been minimized.

Copy link
@Milek7

Milek7 Apr 14, 2019

Author

It is called by function pointer by emscripten. Is it safe to assume that calling convention would match?

@@ -42,6 +52,12 @@ class VideoDriver_SDL : public VideoDriver {
int PollEvent();
bool CreateMainSurface(uint w, uint h);
void SetupKeyboard();

uint32 cur_ticks;

This comment has been minimized.

Copy link
@LordAro

LordAro Apr 14, 2019

Member

I don't like that these are necessary at all...

src/3rdparty/squirrel/squirrel/sqvm.cpp Show resolved Hide resolved
@Milek7 Milek7 force-pushed the Milek7:ems1 branch from 5ca5c3a to aa29a4a May 6, 2019
@Milek7 Milek7 force-pushed the Milek7:ems1 branch from aa29a4a to 1f8c5c9 Jul 4, 2019
@Milek7 Milek7 force-pushed the Milek7:ems1 branch from 1f8c5c9 to ad3be14 Jul 31, 2019
@Milek7 Milek7 force-pushed the Milek7:ems1 branch from ad3be14 to 76cc236 Sep 16, 2019
@Milek7

This comment has been minimized.

Copy link
Author

commented Sep 17, 2019

waiting for #7086 to merge both sdl1 and sdl2 emscripten changes at same time

@Milek7 Milek7 closed this Sep 17, 2019
@Milek7 Milek7 reopened this Sep 19, 2019
@Milek7 Milek7 force-pushed the Milek7:ems1 branch 2 times, most recently from b6a5634 to 2280b2e Sep 19, 2019
@Milek7 Milek7 force-pushed the Milek7:ems1 branch from 2280b2e to 66ec590 Oct 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.