Improved Emscripten port... still not there yet.
This commit is contained in:
@@ -25,6 +25,7 @@ std::string ToHex(T* pointer)
|
|||||||
|
|
||||||
#ifdef LINUX
|
#ifdef LINUX
|
||||||
#define OutputDebugString(val) (std::cerr << val);
|
#define OutputDebugString(val) (std::cerr << val);
|
||||||
|
#define OutputDebugStringA(val) (std::cerr << val);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef _DEBUG
|
#ifdef _DEBUG
|
||||||
|
|||||||
@@ -72,7 +72,13 @@ protected:
|
|||||||
QNetworkReply* mNetworkReply;
|
QNetworkReply* mNetworkReply;
|
||||||
static QNetworkAccessManager networkAccessManager;
|
static QNetworkAccessManager networkAccessManager;
|
||||||
#endif
|
#endif
|
||||||
|
#ifdef __EMSCRIPTEN__
|
||||||
|
static void onLoadCb(unsigned int handle, DownloadRequest* req, const char *buffer, unsigned int size);
|
||||||
|
static void onErrorCb(unsigned int handle, DownloadRequest* req, int errorCode, const char* errorText);
|
||||||
|
static void onProgressCb(unsigned int handle, DownloadRequest* req, int bytesReceived, int bytesTotal);
|
||||||
|
#endif
|
||||||
|
void processError(int errorCode, const char* errorText);
|
||||||
|
void processBufferDownloaded(unsigned int size, const char*buffer);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
DownloadRequest(string localPath="",
|
DownloadRequest(string localPath="",
|
||||||
@@ -96,6 +102,7 @@ public:
|
|||||||
totalSize = mTotalSize;
|
totalSize = mTotalSize;
|
||||||
currentSize = mCurrentSize;
|
currentSize = mCurrentSize;
|
||||||
};
|
};
|
||||||
|
void waitUntilCompleted();
|
||||||
|
|
||||||
friend ostream& operator<<(ostream& out, const DownloadRequest& d);
|
friend ostream& operator<<(ostream& out, const DownloadRequest& d);
|
||||||
friend istream& operator>>(istream&, DownloadRequest&);
|
friend istream& operator>>(istream&, DownloadRequest&);
|
||||||
|
|||||||
@@ -7,6 +7,9 @@
|
|||||||
#ifdef QT_CONFIG
|
#ifdef QT_CONFIG
|
||||||
QNetworkAccessManager DownloadRequest::networkAccessManager;
|
QNetworkAccessManager DownloadRequest::networkAccessManager;
|
||||||
#endif
|
#endif
|
||||||
|
#ifdef __EMSCRIPTEN__
|
||||||
|
#include "emscripten.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
DownloadRequest::DownloadRequest(string localPath,
|
DownloadRequest::DownloadRequest(string localPath,
|
||||||
string remoteResourceURL,
|
string remoteResourceURL,
|
||||||
@@ -52,12 +55,80 @@ void DownloadRequest::startGet()
|
|||||||
SLOT(downloadProgress(int64_t, int64_t)));
|
SLOT(downloadProgress(int64_t, int64_t)));
|
||||||
connect(mNetworkReply, SIGNAL(finished()), SLOT(fileDownloaded()));
|
connect(mNetworkReply, SIGNAL(finished()), SLOT(fileDownloaded()));
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifdef __EMSCRIPTEN__
|
||||||
|
emscripten_async_wget2_data(mRemoteResourceURL.c_str(), "GET", 0, this, 1,
|
||||||
|
(em_async_wget2_data_onload_func)DownloadRequest::onLoadCb,
|
||||||
|
(em_async_wget2_data_onerror_func)DownloadRequest::onErrorCb,
|
||||||
|
(em_async_wget2_data_onprogress_func)DownloadRequest::onProgressCb);
|
||||||
|
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef __EMSCRIPTEN__
|
||||||
|
void DownloadRequest::onLoadCb(unsigned int handle, DownloadRequest* req, const char *buffer, unsigned int size)
|
||||||
|
{
|
||||||
|
|
||||||
|
DebugTrace("DownloadRequest::onLoadCb: " << size);
|
||||||
|
req->processBufferDownloaded(size, buffer);
|
||||||
|
Downloader::GetInstance()->Update();
|
||||||
|
}
|
||||||
|
|
||||||
|
void DownloadRequest::onErrorCb(unsigned int handle, DownloadRequest* req, int errorCode, const char* errorText)
|
||||||
|
{
|
||||||
|
DebugTrace("DownloadRequest::onErrorCb");
|
||||||
|
req->processError(errorCode, errorText);
|
||||||
|
Downloader::GetInstance()->Update();
|
||||||
|
}
|
||||||
|
|
||||||
|
void DownloadRequest::onProgressCb(unsigned int handle, DownloadRequest* req, int bytesReceived, int bytesTotal)
|
||||||
|
{
|
||||||
|
DebugTrace("DownloadRequest::onProgressCb");
|
||||||
|
req->DownloadRequest::downloadProgress(bytesReceived, bytesTotal);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
void DownloadRequest::waitUntilCompleted()
|
||||||
|
{
|
||||||
|
while(mDownloadStatus != DownloadRequest::DOWNLOAD_ERROR && mDownloadStatus != DownloadRequest::DOWNLOADED )
|
||||||
|
{
|
||||||
|
#ifdef __EMSCRIPTERN
|
||||||
|
emscripten_sleep_with_yield(100)
|
||||||
|
#else
|
||||||
|
sleep(100);
|
||||||
|
#endif
|
||||||
|
DebugTrace("DownloadRequest::waitUntilCompleted");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void DownloadRequest::processError(int errorCode, const char* errorText)
|
||||||
|
{
|
||||||
|
DebugTrace(errorText);
|
||||||
|
mDownloadStatus = DownloadRequest::DOWNLOAD_ERROR;
|
||||||
|
mFile.close();
|
||||||
|
JFileSystem::GetInstance()->Remove(getTempLocalPath());
|
||||||
|
}
|
||||||
|
|
||||||
|
void DownloadRequest::processBufferDownloaded(unsigned int size, const char*buffer)
|
||||||
|
{
|
||||||
|
if(mFile.is_open())
|
||||||
|
{
|
||||||
|
mTotalSize = size;
|
||||||
|
mFile.write(buffer, size);
|
||||||
|
mFile.close();
|
||||||
|
if(!JFileSystem::GetInstance()->Rename(getTempLocalPath(), mLocalPath)) {
|
||||||
|
mDownloadStatus = DownloadRequest::DOWNLOAD_ERROR;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
mDownloadStatus = DownloadRequest::DOWNLOADED;
|
||||||
}
|
}
|
||||||
|
|
||||||
void DownloadRequest::fileDownloaded()
|
void DownloadRequest::fileDownloaded()
|
||||||
{
|
{
|
||||||
#ifdef QT_CONFIG
|
#ifdef QT_CONFIG
|
||||||
do {
|
do {
|
||||||
QByteArray eTagByteArray = mNetworkReply->rawHeader("ETag");
|
QByteArray eTagByteArray = mNetworkReply->rawHeader("ETag");
|
||||||
if(!eTagByteArray.isEmpty()) {
|
if(!eTagByteArray.isEmpty()) {
|
||||||
string oldETag = mETag;
|
string oldETag = mETag;
|
||||||
@@ -68,10 +139,7 @@ void DownloadRequest::fileDownloaded()
|
|||||||
|
|
||||||
// let's check some error
|
// let's check some error
|
||||||
if(mNetworkReply->error() != QNetworkReply::NoError) {
|
if(mNetworkReply->error() != QNetworkReply::NoError) {
|
||||||
DebugTrace(mNetworkReply->errorString().toStdString());
|
processError(NetworkReply->error(), mNetworkReply->errorString().toStdString());
|
||||||
mDownloadStatus = DownloadRequest::DOWNLOAD_ERROR;
|
|
||||||
mFile.close();
|
|
||||||
JFileSystem::GetInstance()->Remove(getTempLocalPath());
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -90,25 +158,15 @@ void DownloadRequest::fileDownloaded()
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(mFile.is_open())
|
QByteArray byteArray = mNetworkReply->readAll();
|
||||||
{
|
processBufferDownloaded(byteArray.size(), byteArray.constData());
|
||||||
QByteArray byteArray = mNetworkReply->readAll();
|
|
||||||
mFile.write(byteArray.constData(), byteArray.size());
|
|
||||||
mFile.close();
|
|
||||||
if(!JFileSystem::GetInstance()->Rename(getTempLocalPath(), mLocalPath)) {
|
|
||||||
mDownloadStatus = DownloadRequest::DOWNLOAD_ERROR;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
mDownloadStatus = DownloadRequest::DOWNLOADED;
|
|
||||||
} while(0);
|
} while(0);
|
||||||
|
|
||||||
Downloader::GetInstance()->Update();
|
Downloader::GetInstance()->Update();
|
||||||
mNetworkReply->deleteLater();
|
|
||||||
|
|
||||||
|
mNetworkReply->deleteLater();
|
||||||
emit statusChanged((int)mDownloadStatus);
|
emit statusChanged((int)mDownloadStatus);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void DownloadRequest::downloadProgress(int64_t bytesReceived, int64_t bytesTotal)
|
void DownloadRequest::downloadProgress(int64_t bytesReceived, int64_t bytesTotal)
|
||||||
@@ -116,11 +174,13 @@ void DownloadRequest::downloadProgress(int64_t bytesReceived, int64_t bytesTotal
|
|||||||
#ifdef QT_CONFIG
|
#ifdef QT_CONFIG
|
||||||
QByteArray byteArray = mNetworkReply->readAll();
|
QByteArray byteArray = mNetworkReply->readAll();
|
||||||
mFile.write(byteArray.constData(), byteArray.size());
|
mFile.write(byteArray.constData(), byteArray.size());
|
||||||
|
#endif
|
||||||
mCurrentSize = bytesReceived;
|
mCurrentSize = bytesReceived;
|
||||||
mTotalSize = bytesTotal;
|
mTotalSize = bytesTotal;
|
||||||
int percent = 0;
|
int percent = 0;
|
||||||
if(bytesTotal)
|
if(bytesTotal)
|
||||||
percent = (bytesReceived/bytesTotal)*100;
|
percent = (bytesReceived/bytesTotal)*100;
|
||||||
|
#ifdef QT_CONFIG
|
||||||
emit percentChanged(percent);
|
emit percentChanged(percent);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -406,7 +406,6 @@ bool JFileSystem::OpenFile(const string &filename)
|
|||||||
mCurrentFileInZip = &(it2->second);
|
mCurrentFileInZip = &(it2->second);
|
||||||
mFileSize = it2->second.m_Size;
|
mFileSize = it2->second.m_Size;
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
set(CMAKE_CXX_FLAGS "-s USE_SDL=2 -std=c++11")
|
set(CMAKE_CXX_FLAGS "-s USE_SDL=2 -s FULL_ES2=1 -std=c++11 -g4 -s TOTAL_MEMORY=500000000 -s ALLOW_MEMORY_GROWTH=1")
|
||||||
set(CMAKE_EXECUTABLE_SUFFIX ".html")
|
set(CMAKE_EXECUTABLE_SUFFIX ".html")
|
||||||
add_definitions(-DLINUX)
|
add_definitions(-DLINUX)
|
||||||
|
add_definitions(-D_DEBUG)
|
||||||
add_definitions(-DUSERDIR=".wagic")
|
add_definitions(-DUSERDIR=".wagic")
|
||||||
add_definitions(-DRESDIR="Res")
|
add_definitions(-DRESDIR="Res")
|
||||||
|
|||||||
@@ -16,6 +16,7 @@
|
|||||||
#include "../include/JRenderer.h"
|
#include "../include/JRenderer.h"
|
||||||
#include "../include/JGameLauncher.h"
|
#include "../include/JGameLauncher.h"
|
||||||
#include "DebugRoutines.h"
|
#include "DebugRoutines.h"
|
||||||
|
#include "Downloader.h"
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
@@ -127,6 +128,7 @@ public:
|
|||||||
|
|
||||||
static void OneIter()
|
static void OneIter()
|
||||||
{
|
{
|
||||||
|
DebugTrace("OneIter");
|
||||||
SDL_Event Event;
|
SDL_Event Event;
|
||||||
if (g_engine)
|
if (g_engine)
|
||||||
{
|
{
|
||||||
@@ -148,14 +150,14 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
#ifdef __EMSCRIPTEN__
|
#ifdef __EMSCRIPTEN__
|
||||||
emscripten_set_main_loop(OneIter, 60, 1);
|
emscripten_set_main_loop(SdlApp::OneIter, 60, 1);
|
||||||
#else
|
#else
|
||||||
while(Running)
|
while(Running)
|
||||||
{
|
{
|
||||||
OneIter();
|
OneIter();
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
OnCleanup();
|
OnCleanup();
|
||||||
|
#endif
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -290,8 +292,8 @@ public:
|
|||||||
|
|
||||||
void OnCleanup()
|
void OnCleanup()
|
||||||
{
|
{
|
||||||
SDL_GL_DeleteContext(gl_context);
|
SDL_GL_DeleteContext(gl_context);
|
||||||
SDL_Quit();
|
SDL_Quit();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
SdlApp* SdlApp::sInstance = 0;
|
SdlApp* SdlApp::sInstance = 0;
|
||||||
@@ -651,8 +653,13 @@ bool SdlApp::OnInit()
|
|||||||
{
|
{
|
||||||
int window_w, window_h;
|
int window_w, window_h;
|
||||||
|
|
||||||
if(SDL_Init(SDL_INIT_EVERYTHING) < 0)
|
DebugTrace("I R in da OnInit()");
|
||||||
{
|
#ifndef __EMSCRIPTEN__
|
||||||
|
if(SDL_Init(SDL_INIT_EVERYTHING) < 0)
|
||||||
|
#else
|
||||||
|
if(SDL_Init(SDL_INIT_VIDEO) < 0)
|
||||||
|
#endif //__EMSCRIPTEN__
|
||||||
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -660,7 +667,7 @@ bool SdlApp::OnInit()
|
|||||||
SDL_GetCurrentDisplayMode(0, ¤tDisplayMode);
|
SDL_GetCurrentDisplayMode(0, ¤tDisplayMode);
|
||||||
DebugTrace("Video Display : h " << currentDisplayMode.h << ", w " << currentDisplayMode.w);
|
DebugTrace("Video Display : h " << currentDisplayMode.h << ", w " << currentDisplayMode.w);
|
||||||
|
|
||||||
#if (defined ANDROID) || (defined IOS)
|
#if (defined ANDROID) || (defined IOS)|| (defined __EMSCRIPTEN__)
|
||||||
window_w = currentDisplayMode.w;
|
window_w = currentDisplayMode.w;
|
||||||
window_h = currentDisplayMode.h;
|
window_h = currentDisplayMode.h;
|
||||||
#else
|
#else
|
||||||
@@ -668,7 +675,9 @@ bool SdlApp::OnInit()
|
|||||||
window_h = ACTUAL_SCREEN_HEIGHT;
|
window_h = ACTUAL_SCREEN_HEIGHT;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
|
#ifndef __EMSCRIPTEN__
|
||||||
|
int buffers, samples;
|
||||||
|
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
|
||||||
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
|
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
|
||||||
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
|
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
|
||||||
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
|
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
|
||||||
@@ -682,9 +691,8 @@ bool SdlApp::OnInit()
|
|||||||
SDL_GL_SetAttribute(SDL_GL_ACCUM_ALPHA_SIZE, 8);
|
SDL_GL_SetAttribute(SDL_GL_ACCUM_ALPHA_SIZE, 8);
|
||||||
|
|
||||||
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
|
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
|
||||||
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 2);
|
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 2);
|
||||||
|
|
||||||
int buffers, samples;
|
|
||||||
SDL_GL_GetAttribute(SDL_GL_MULTISAMPLEBUFFERS, &buffers);
|
SDL_GL_GetAttribute(SDL_GL_MULTISAMPLEBUFFERS, &buffers);
|
||||||
SDL_GL_GetAttribute(SDL_GL_MULTISAMPLESAMPLES, &samples);
|
SDL_GL_GetAttribute(SDL_GL_MULTISAMPLESAMPLES, &samples);
|
||||||
if (buffers == 0 || samples == 0)
|
if (buffers == 0 || samples == 0)
|
||||||
@@ -693,10 +701,20 @@ bool SdlApp::OnInit()
|
|||||||
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 0);
|
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 1);
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 1);
|
||||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
|
||||||
|
#else
|
||||||
|
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
|
||||||
|
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
|
||||||
|
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
|
||||||
|
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
|
||||||
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
|
||||||
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
|
||||||
|
SDL_DisplayMode current;
|
||||||
|
SDL_GetCurrentDisplayMode(0, ¤t);
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifdef ANDROID
|
#if (defined ANDROID)
|
||||||
Uint32 flags = SDL_WINDOW_OPENGL | SDL_WINDOW_FULLSCREEN | SDL_WINDOW_BORDERLESS;
|
Uint32 flags = SDL_WINDOW_OPENGL | SDL_WINDOW_FULLSCREEN | SDL_WINDOW_BORDERLESS;
|
||||||
#else
|
#else
|
||||||
Uint32 flags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE;
|
Uint32 flags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE;
|
||||||
@@ -714,7 +732,10 @@ bool SdlApp::OnInit()
|
|||||||
|
|
||||||
gl_context = SDL_GL_CreateContext(window);
|
gl_context = SDL_GL_CreateContext(window);
|
||||||
|
|
||||||
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Black Background (yes that's the way fuckers)
|
|
||||||
|
|
||||||
|
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Black Background (yes that's the way fuckers)
|
||||||
|
|
||||||
#if (defined GL_ES_VERSION_2_0) || (defined GL_VERSION_2_0)
|
#if (defined GL_ES_VERSION_2_0) || (defined GL_VERSION_2_0)
|
||||||
#if (defined GL_ES_VERSION_2_0)
|
#if (defined GL_ES_VERSION_2_0)
|
||||||
glClearDepthf(1.0f); // Depth Buffer Setup
|
glClearDepthf(1.0f); // Depth Buffer Setup
|
||||||
@@ -780,6 +801,15 @@ int main(int argc, char* argv[])
|
|||||||
|
|
||||||
DebugTrace("I R in da native");
|
DebugTrace("I R in da native");
|
||||||
|
|
||||||
|
DownloadRequest* downloadRequest = NULL;
|
||||||
|
Downloader*downloader = Downloader::GetInstance();
|
||||||
|
downloadRequest = downloader->Get(
|
||||||
|
"core.zip",
|
||||||
|
"file:///C:/Users/bieber/Documents/GitHub/wagic-cmake/build-emscripten/bin/Wagic-core.zip"
|
||||||
|
// "http://127.0.0.1:8000/Wagic-core.zip"
|
||||||
|
);
|
||||||
|
downloadRequest->waitUntilCompleted();
|
||||||
|
|
||||||
g_launcher = new JGameLauncher();
|
g_launcher = new JGameLauncher();
|
||||||
|
|
||||||
u32 flags = g_launcher->GetInitFlags();
|
u32 flags = g_launcher->GetInitFlags();
|
||||||
@@ -793,7 +823,8 @@ int main(int argc, char* argv[])
|
|||||||
|
|
||||||
int result = g_SdlApp->OnExecute();
|
int result = g_SdlApp->OnExecute();
|
||||||
|
|
||||||
if (g_launcher)
|
#ifndef __EMSCRIPTEN__
|
||||||
|
if (g_launcher)
|
||||||
delete g_launcher;
|
delete g_launcher;
|
||||||
|
|
||||||
if(g_SdlApp)
|
if(g_SdlApp)
|
||||||
@@ -801,6 +832,7 @@ int main(int argc, char* argv[])
|
|||||||
|
|
||||||
// Shutdown
|
// Shutdown
|
||||||
DestroyGame();
|
DestroyGame();
|
||||||
|
#endif //__EMSCRIPTEN__
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user