diff --git a/JGE/include/DebugRoutines.h b/JGE/include/DebugRoutines.h index 43569b162..9b1045b6a 100644 --- a/JGE/include/DebugRoutines.h +++ b/JGE/include/DebugRoutines.h @@ -25,6 +25,7 @@ std::string ToHex(T* pointer) #ifdef LINUX #define OutputDebugString(val) (std::cerr << val); +#define OutputDebugStringA(val) (std::cerr << val); #endif #ifdef _DEBUG diff --git a/JGE/include/Downloader.h b/JGE/include/Downloader.h index f77c55303..0d0d81678 100644 --- a/JGE/include/Downloader.h +++ b/JGE/include/Downloader.h @@ -72,7 +72,13 @@ protected: QNetworkReply* mNetworkReply; static QNetworkAccessManager networkAccessManager; #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: DownloadRequest(string localPath="", @@ -96,6 +102,7 @@ public: totalSize = mTotalSize; currentSize = mCurrentSize; }; + void waitUntilCompleted(); friend ostream& operator<<(ostream& out, const DownloadRequest& d); friend istream& operator>>(istream&, DownloadRequest&); diff --git a/JGE/src/Downloader.cpp b/JGE/src/Downloader.cpp index 0cd38d229..997643b51 100644 --- a/JGE/src/Downloader.cpp +++ b/JGE/src/Downloader.cpp @@ -7,6 +7,9 @@ #ifdef QT_CONFIG QNetworkAccessManager DownloadRequest::networkAccessManager; #endif +#ifdef __EMSCRIPTEN__ +#include "emscripten.h" +#endif DownloadRequest::DownloadRequest(string localPath, string remoteResourceURL, @@ -52,12 +55,80 @@ void DownloadRequest::startGet() SLOT(downloadProgress(int64_t, int64_t))); connect(mNetworkReply, SIGNAL(finished()), SLOT(fileDownloaded())); #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() { #ifdef QT_CONFIG - do { + do { QByteArray eTagByteArray = mNetworkReply->rawHeader("ETag"); if(!eTagByteArray.isEmpty()) { string oldETag = mETag; @@ -68,10 +139,7 @@ void DownloadRequest::fileDownloaded() // let's check some error if(mNetworkReply->error() != QNetworkReply::NoError) { - DebugTrace(mNetworkReply->errorString().toStdString()); - mDownloadStatus = DownloadRequest::DOWNLOAD_ERROR; - mFile.close(); - JFileSystem::GetInstance()->Remove(getTempLocalPath()); + processError(NetworkReply->error(), mNetworkReply->errorString().toStdString()); break; } @@ -90,25 +158,15 @@ void DownloadRequest::fileDownloaded() return; } - if(mFile.is_open()) - { - 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; + QByteArray byteArray = mNetworkReply->readAll(); + processBufferDownloaded(byteArray.size(), byteArray.constData()); } while(0); Downloader::GetInstance()->Update(); - mNetworkReply->deleteLater(); + mNetworkReply->deleteLater(); emit statusChanged((int)mDownloadStatus); #endif - } 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 QByteArray byteArray = mNetworkReply->readAll(); mFile.write(byteArray.constData(), byteArray.size()); +#endif mCurrentSize = bytesReceived; mTotalSize = bytesTotal; int percent = 0; if(bytesTotal) percent = (bytesReceived/bytesTotal)*100; +#ifdef QT_CONFIG emit percentChanged(percent); #endif } diff --git a/JGE/src/JFileSystem.cpp b/JGE/src/JFileSystem.cpp index cb25968c0..e85cab0b3 100644 --- a/JGE/src/JFileSystem.cpp +++ b/JGE/src/JFileSystem.cpp @@ -406,7 +406,6 @@ bool JFileSystem::OpenFile(const string &filename) mCurrentFileInZip = &(it2->second); mFileSize = it2->second.m_Size; return true; - } diff --git a/platforms/emscripten/configure.cmake b/platforms/emscripten/configure.cmake index bdbedc639..09b4ce8ed 100644 --- a/platforms/emscripten/configure.cmake +++ b/platforms/emscripten/configure.cmake @@ -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") add_definitions(-DLINUX) +add_definitions(-D_DEBUG) add_definitions(-DUSERDIR=".wagic") add_definitions(-DRESDIR="Res") diff --git a/projects/mtg/src/SDLmain.cpp b/projects/mtg/src/SDLmain.cpp index a2c5bcc16..0ca9d7e60 100644 --- a/projects/mtg/src/SDLmain.cpp +++ b/projects/mtg/src/SDLmain.cpp @@ -16,6 +16,7 @@ #include "../include/JRenderer.h" #include "../include/JGameLauncher.h" #include "DebugRoutines.h" +#include "Downloader.h" #include #include #include @@ -127,6 +128,7 @@ public: static void OneIter() { + DebugTrace("OneIter"); SDL_Event Event; if (g_engine) { @@ -148,14 +150,14 @@ public: } #ifdef __EMSCRIPTEN__ - emscripten_set_main_loop(OneIter, 60, 1); + emscripten_set_main_loop(SdlApp::OneIter, 60, 1); #else while(Running) { OneIter(); } -#endif OnCleanup(); +#endif return 0; } @@ -290,8 +292,8 @@ public: void OnCleanup() { - SDL_GL_DeleteContext(gl_context); - SDL_Quit(); + SDL_GL_DeleteContext(gl_context); + SDL_Quit(); } }; SdlApp* SdlApp::sInstance = 0; @@ -651,8 +653,13 @@ bool SdlApp::OnInit() { 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; } @@ -660,7 +667,7 @@ bool SdlApp::OnInit() SDL_GetCurrentDisplayMode(0, ¤tDisplayMode); 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_h = currentDisplayMode.h; #else @@ -668,7 +675,9 @@ bool SdlApp::OnInit() window_h = ACTUAL_SCREEN_HEIGHT; #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_BLUE_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_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_MULTISAMPLESAMPLES, &samples); if (buffers == 0 || samples == 0) @@ -693,10 +701,20 @@ bool SdlApp::OnInit() SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 0); } - SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 1); - SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_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; #else Uint32 flags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE; @@ -714,7 +732,10 @@ bool SdlApp::OnInit() 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) glClearDepthf(1.0f); // Depth Buffer Setup @@ -780,6 +801,15 @@ int main(int argc, char* argv[]) 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(); u32 flags = g_launcher->GetInitFlags(); @@ -793,7 +823,8 @@ int main(int argc, char* argv[]) int result = g_SdlApp->OnExecute(); - if (g_launcher) +#ifndef __EMSCRIPTEN__ + if (g_launcher) delete g_launcher; if(g_SdlApp) @@ -801,6 +832,7 @@ int main(int argc, char* argv[]) // Shutdown DestroyGame(); +#endif //__EMSCRIPTEN__ return result; }