Skip to content

Commit

Permalink
#1216: added network support for WASM, fixed html canvas scaling
Browse files Browse the repository at this point in the history
  • Loading branch information
nesbox committed Nov 23, 2020
1 parent e74b272 commit 13889bd
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 12 deletions.
10 changes: 3 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,6 @@ if (BAREMETALPI)

endif()

if(EMSCRIPTEN)
set(DISABLE_NETWORKING TRUE)
endif()

if (DISABLE_NETWORKING)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DDISABLE_NETWORKING")
endif()
Expand Down Expand Up @@ -524,7 +520,7 @@ endif()

if (NOT DISABLE_NETWORKING)

if (NOT N3DS)
if (NOT N3DS AND NOT EMSCRIPTEN)

set(BUILD_SHARED_LIBS OFF CACHE BOOL "")

Expand Down Expand Up @@ -699,7 +695,7 @@ endif()
if(NOT DISABLE_NETWORKING)
if(N3DS)
target_link_libraries(tic80studio curl mbedtls mbedx509 mbedcrypto)
else()
elseif(NOT EMSCRIPTEN)
target_link_libraries(tic80studio libcurl)
endif()
endif()
Expand Down Expand Up @@ -828,7 +824,7 @@ if(BUILD_SDL)
endif()

if(EMSCRIPTEN)
set_target_properties(tic80 PROPERTIES LINK_FLAGS "-s WASM=1 -s USE_SDL=2 -s TOTAL_MEMORY=67108864 -lidbfs.js")
set_target_properties(tic80 PROPERTIES LINK_FLAGS "-s WASM=1 -s USE_SDL=2 -s TOTAL_MEMORY=67108864 -s FETCH=1 -lidbfs.js")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -s USE_SDL=2")

if(CMAKE_BUILD_TYPE STREQUAL "Debug")
Expand Down
137 changes: 135 additions & 2 deletions src/ext/net.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,87 @@
#include <string.h>

#ifndef DISABLE_NETWORKING
#if defined(__EMSCRIPTEN__)

#include <emscripten/fetch.h>

typedef struct
{
HttpGetCallback callback;
void* calldata;
} FetchData;

struct Net
{
emscripten_fetch_attr_t attr;
};

static void downloadSucceeded(emscripten_fetch_t *fetch)
{
FetchData* data = (FetchData*)fetch->userData;

HttpGetData getData =
{
.type = HttpGetDone,
.done =
{
.size = fetch->numBytes,
.data = (u8*)fetch->data,
},
.calldata = data->calldata,
.url = fetch->url,
};

data->callback(&getData);

free(data);

emscripten_fetch_close(fetch);
}

static void downloadFailed(emscripten_fetch_t *fetch)
{
FetchData* data = (FetchData*)fetch->userData;

HttpGetData getData =
{
.type = HttpGetError,
.error =
{
.code = fetch->status,
},
.calldata = data->calldata,
.url = fetch->url,
};

data->callback(&getData);

free(data);

emscripten_fetch_close(fetch);
}

static void downloadProgress(emscripten_fetch_t *fetch)
{
FetchData* data = (FetchData*)fetch->userData;

HttpGetData getData =
{
.type = HttpGetProgress,
.progress =
{
.size = fetch->dataOffset + fetch->numBytes,
.total = fetch->totalBytes,
},
.calldata = data->calldata,
.url = fetch->url,
};

data->callback(&getData);
}

#else

#include <curl/curl.h>

typedef struct
Expand Down Expand Up @@ -102,14 +183,35 @@ static size_t writeCallback(char *ptr, size_t size, size_t nmemb, void *userdata
return total;
}

#endif
#endif

void netGet(Net* net, const char* path, HttpGetCallback callback, void* calldata)
{
#ifdef DISABLE_NETWORKING
// !TODO: call callback here
return;

HttpGetData getData =
{
.type = HttpGetError,
.calldata = calldata,
.url = path,
};
callback(&getData);

#else
#if defined(__EMSCRIPTEN__)

FetchData* data = calloc(1, sizeof(FetchData));
*data = (FetchData)
{
.callback = callback,
.calldata = calldata,
};

net->attr.userData = data;
emscripten_fetch(&net->attr, path);

#else

struct Curl_easy* curl = curl_easy_init();

Expand All @@ -136,14 +238,23 @@ void netGet(Net* net, const char* path, HttpGetCallback callback, void* calldata
curl_multi_add_handle(net->multi, curl);
}

#endif
#endif
}

void* netGetSync(Net* net, const char* path, s32* size)
{
#ifdef DISABLE_NETWORKING

return NULL;

#else
#if defined(__EMSCRIPTEN__)

return NULL;

#else

CurlData data = {NULL, 0};

if(net->sync)
Expand All @@ -167,13 +278,16 @@ void* netGetSync(Net* net, const char* path, s32* size)

return data.buffer;

#endif
#endif
}

void netTick(Net *net)
{
#ifndef DISABLE_NETWORKING

#if !defined(__EMSCRIPTEN__)

{
s32 running = 0;
curl_multi_perform(net->multi, &running);
Expand Down Expand Up @@ -232,6 +346,7 @@ void netTick(Net *net)
curl_easy_cleanup(msg->easy_handle);
}
}
#endif
#endif
}

Expand All @@ -240,7 +355,20 @@ Net* createNet()
#ifdef DISABLE_NETWORKING
return NULL;
#else

Net* net = (Net*)malloc(sizeof(Net));

#if defined(__EMSCRIPTEN__)

emscripten_fetch_attr_init(&net->attr);
strcpy(net->attr.requestMethod, "GET");
net->attr.attributes = EMSCRIPTEN_FETCH_LOAD_TO_MEMORY;
net->attr.onsuccess = downloadSucceeded;
net->attr.onerror = downloadFailed;
net->attr.onprogress = downloadProgress;

#else

if (net != NULL)
{
*net = (Net)
Expand All @@ -252,6 +380,8 @@ Net* createNet()
curl_easy_setopt(net->sync, CURLOPT_WRITEFUNCTION, writeCallbackSync);
}

#endif

return net;
#endif
}
Expand All @@ -260,12 +390,15 @@ void closeNet(Net* net)
{
#ifndef DISABLE_NETWORKING

#if !defined(__EMSCRIPTEN__)
if(net->sync)
curl_easy_cleanup(net->sync);

if(net->multi)
curl_multi_cleanup(net->multi);

#endif

free(net);
#endif
}
9 changes: 6 additions & 3 deletions src/system/sdl/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1605,10 +1605,13 @@ static s32 start(s32 argc, const char **argv, const char* folder)
platform.window = SDL_CreateWindow( TIC_TITLE, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
Width, Height, SDL_WINDOW_SHOWN
| SDL_WINDOW_RESIZABLE
#if defined(CRT_SHADER_SUPPORT)
#if defined(CRT_SHADER_SUPPORT)
| SDL_WINDOW_OPENGL
#endif
| SDL_WINDOW_ALLOW_HIGHDPI);
#endif
#if !defined(__EMSCRIPTEN__)
| SDL_WINDOW_ALLOW_HIGHDPI
#endif
);

setWindowIcon();
createMouseCursors();
Expand Down

0 comments on commit 13889bd

Please sign in to comment.