WIP: Emscripten support

This commit is contained in:
Ghabry 2018-04-29 19:18:36 +02:00
parent c65d9ad287
commit 11d3755116
12 changed files with 170 additions and 99 deletions

View File

@ -1,4 +1,5 @@
cmake_minimum_required(VERSION 2.8.11)
set(CMAKE_CXX_STANDARD 14)
Project(mkxp)
## Setup options ##
@ -14,7 +15,7 @@ set(EXTERNAL_LIB_PATH "" CACHE PATH "External precompiled lib prefix")
include(cmake/PrepUtils.cmake)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_EXECUTABLE_SUFFIX ".html")
IF("${CMAKE_SYSTEM}" MATCHES "Linux")
SET(LINUX ON)
ENDIF()
@ -53,6 +54,16 @@ elseif(APPLE)
set(LIB_PATH "lib")
endif()
set(EMTERPRETER_OPT "-O0 -s ASSERTIONS=0 -s TOTAL_MEMORY=67108864 -s ABORTING_MALLOC=0 -s EMTERPRETIFY=1 -s EMTERPRETIFY_ASYNC=1 -s NO_EXIT_RUNTIME=1 --embed-file \"game\"")
set(EMTERPRETER_OPT "${EMTERPRETER_OPT} -s EMTERPRETIFY_WHITELIST=\"['__ZN8Graphics10transitionEiPKci','__ZL18graphicsTransitioniPmm','_call_cfunc','_rb_call0','_rb_call','_rb_eval','_rb_eval_string','_eval_node','_eval','_rb_f_eval','_rb_funcall2','_rb_protect','_rb_yield_0','_loop_i','_rb_rescue2','_rb_f_loop','__ZL10evalHelperP7evalArg','__ZL10evalStringmmPi','__ZL14runRMXPScriptsR13BacktraceData','__Z13rgssThreadFunPv','_main','__ZL11inputUpdateiPmm','__ZN11EventThread7processER14RGSSThreadData','__ZL17mriBindingExecutev','__ZL14mriBindingInitv','_module_setup']\"")
message(STATUS ${EMTERPRETER_OPT})
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EMTERPRETER_OPT}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${EMTERPRETER_OPT}")
message(STATUS ${CMAKE_C_FLAGS})
set(CMAKE_SKIP_BUILD_RPATH TRUE)
set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
set(CMAKE_INSTALL_RPATH ${BIN_RPATH})
@ -92,9 +103,10 @@ pkg_check_modules(SDL2_TTF REQUIRED SDL2_ttf)
pkg_check_modules(SDL2_IMAGE REQUIRED SDL2_image)
pkg_check_modules(SDL_SOUND REQUIRED SDL_sound)
find_package(Boost 1.49 COMPONENTS program_options REQUIRED)
#find_package(Boost 1.49 COMPONENTS program_options REQUIRED)
find_package(OpenAL REQUIRED)
find_package(ZLIB REQUIRED)
pkg_check_modules(ZLIB REQUIRED zlib)
#find_package(ZLIB REQUIRED)
## Setup main source ##
@ -395,7 +407,7 @@ link_directories(
${PIXMAN_LIBRARY_DIRS}
${PHYSFS_LIBRARY_DIRS}
${SDL2_LIBRARY_DIRS} # Blindly assume other SDL bits are in same directory
${Boost_LIBRARY_DIR}
# ${Boost_LIBRARY_DIR}
${MRI_LIBDIR}
)
@ -417,11 +429,13 @@ target_include_directories(${PROJECT_NAME} PRIVATE
${PIXMAN_INCLUDE_DIRS}
${PHYSFS_INCLUDE_DIRS}
${SDL2_INCLUDE_DIRS} # Blindly assume other SDL bits are in same directory
${Boost_INCLUDE_DIR}
# ${Boost_INCLUDE_DIR}
${MRI_INCLUDE_DIRS}
${VORBISFILE_INCLUDE_DIRS}
${FLUID_INCLUDE_DIRS}
${OPENAL_INCLUDE_DIR}
${SDL_SOUND_INCLUDE_DIRS}
${FREETYPE_INCLUDE_DIRS}
)
target_link_libraries(${PROJECT_NAME}
@ -432,14 +446,21 @@ target_link_libraries(${PROJECT_NAME}
${SDL_SOUND_LIBRARIES}
${PHYSFS_LIBRARIES}
${PIXMAN_LIBRARIES}
${Boost_LIBRARIES}
# ${Boost_LIBRARIES}
${MRI_LIBRARIES}
${VORBISFILE_LIBRARIES}
${FLUID_LIBRARIES}
${OPENAL_LIBRARY}
${ZLIB_LIBRARY}
${PLATFORM_LIBRARIES}
"libruby-static.a"
"libfreetype.a"
"libz.a"
"libpng.a"
"libogg.a"
"libvorbis.a"
"libvorbisfile.a"
"libjpeg.a"
)
PostBuildMacBundle(${PROJECT_NAME} "" "${PLATFORM_COPY_LIBS}")

View File

@ -164,7 +164,9 @@ static void mriBindingInit()
static void
showMsg(const std::string &msg)
{
shState->eThread().showMessageBox(msg.c_str());
printf("Show msg %s\n", msg.c_str());
return;
// shState->eThread().showMessageBox(msg.c_str());
}
static void printP(int argc, VALUE *argv,
@ -350,6 +352,9 @@ static VALUE evalHelper(evalArg *arg)
return rb_funcall2(Qnil, rb_intern("eval"), ARRAY_SIZE(argv), argv);
}
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#endif
static VALUE evalString(VALUE string, VALUE filename, int *state)
{
evalArg arg = { string, filename };
@ -482,6 +487,10 @@ static void runRMXPScripts(BacktraceData &btData)
while (true)
{
#ifdef __EMSCRIPTEN
emscripten_sleep(10);
#endif
for (long i = 0; i < scriptCount; ++i)
{
VALUE script = rb_ary_entry(scriptArray, i);
@ -508,6 +517,9 @@ static void runRMXPScripts(BacktraceData &btData)
break;
}
printf("tick\n");
shState->eThread().process(shState->rtData());
VALUE exc = rb_gv_get("$!");
if (rb_obj_class(exc) != getRbData()->exc[Reset])
break;

View File

@ -24,12 +24,14 @@
#include "exception.h"
#include "binding-util.h"
#include "util.h"
#include "eventthread.h"
RB_METHOD(inputUpdate)
{
RB_UNUSED_PARAM;
shState->input().update();
shState->eThread().process(shState->rtData());
return Qnil;
}

View File

@ -21,10 +21,9 @@
#ifndef BOOSTHASH_H
#define BOOSTHASH_H
#include <boost/unordered/unordered_map.hpp>
#include <boost/unordered/unordered_set.hpp>
#include <string>
#include <map>
#include <set>
#include <utility>
/* Wrappers around the boost unordered template classes,
@ -34,7 +33,7 @@ template<typename K, typename V>
class BoostHash
{
private:
typedef boost::unordered_map<K, V> BoostType;
typedef std::map<K, V> BoostType;
typedef std::pair<K, V> PairType;
BoostType p;
@ -98,7 +97,7 @@ template<typename K>
class BoostSet
{
private:
typedef boost::unordered_set<K> BoostType;
typedef std::set<K> BoostType;
BoostType p;
public:

View File

@ -21,9 +21,9 @@
#include "config.h"
#include <boost/program_options/options_description.hpp>
#include <boost/program_options/parsers.hpp>
#include <boost/program_options/variables_map.hpp>
//#include <boost/program_options/options_description.hpp>
//#include <boost/program_options/parsers.hpp>
//#include <boost/program_options/variables_map.hpp>
#include <SDL_filesystem.h>
@ -141,8 +141,8 @@ std::set<T> setFromVec(const std::vector<T> &vec)
return std::set<T>(vec.begin(), vec.end());
}
typedef std::vector<std::string> StringVec;
namespace po = boost::program_options;
//typedef std::vector<std::string> StringVec;
//namespace po = boost::program_options;
#define CONF_FILE "mkxp.conf"
@ -151,6 +151,8 @@ Config::Config()
void Config::read(int argc, char *argv[])
{
gameFolder = "game";
#if 0
#define PO_DESC_ALL \
PO_DESC(rgssVersion, int, 0) \
PO_DESC(debugMode, bool, false) \
@ -267,6 +269,8 @@ void Config::read(int argc, char *argv[])
#undef PO_DESC
#undef PO_DESC_ALL
#endif
preloadScripts.insert("win32_wrap.rb");
rgssVersion = clamp(rgssVersion, 0, 3);
@ -299,6 +303,7 @@ static void setupScreenSize(Config &conf)
void Config::readGameINI()
{
if (!customScript.empty())
{
game.title = baseName(customScript);
@ -310,10 +315,12 @@ void Config::readGameINI()
return;
}
#if 0
#endif
std::string iniFilename = execName + ".ini";
SDLRWStream iniFile(iniFilename.c_str(), "r");
#if 0
if (iniFile)
{
INIConfiguration ic;
@ -344,6 +351,9 @@ void Config::readGameINI()
Debug() << "FAILED to open" << iniFilename;
}
#endif
game.scripts = "Data/Scripts.rxdata";
strReplace(game.scripts, '\\', '/');
#ifdef INI_ENCODING
/* Can add more later */
const char *languages[] =

View File

@ -28,57 +28,62 @@
struct Config
{
int rgssVersion;
int rgssVersion = 0;
bool debugMode;
bool printFPS;
bool debugMode = false;
bool printFPS = false;
bool winResizable;
bool fullscreen;
bool fixedAspectRatio;
bool smoothScaling;
bool vsync;
bool winResizable = false;
bool fullscreen = false;
bool fixedAspectRatio = true;
bool smoothScaling = true;
bool vsync = false;
<<<<<<< HEAD
int defScreenW;
int defScreenH;
std::string windowTitle;
=======
int defScreenW = 0;
int defScreenH = 0;
>>>>>>> WIP: Emscripten support
int fixedFramerate;
bool frameSkip;
bool syncToRefreshrate;
int fixedFramerate = 0;
bool frameSkip =true;
bool syncToRefreshrate = false;
bool solidFonts;
bool solidFonts = false;
bool subImageFix;
bool enableBlitting;
int maxTextureSize;
bool subImageFix = false;
bool enableBlitting = true;
int maxTextureSize = 0;
std::string gameFolder;
bool anyAltToggleFS;
bool enableReset;
bool allowSymlinks;
bool pathCache;
std::string gameFolder = ".";
bool anyAltToggleFS = false;
bool enableReset = true;
bool allowSymlinks = false;
bool pathCache=true;
std::string dataPathOrg;
std::string dataPathApp;
std::string iconPath;
std::string execName;
std::string execName = "Game";
std::string titleLanguage;
struct
{
std::string soundFont;
bool chorus;
bool reverb;
bool chorus = false;
bool reverb = false;
} midi;
struct
{
int sourceCount;
int sourceCount = 6;
} SE;
bool useScriptNames;
bool useScriptNames = false;
std::string customScript;
std::set<std::string> preloadScripts;

View File

@ -100,7 +100,9 @@ bool EventThread::allocUserEvents()
return true;
}
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#endif
EventThread::EventThread()
: fullscreen(false),
showCursor(false)
@ -109,63 +111,42 @@ EventThread::EventThread()
void EventThread::process(RGSSThreadData &rtData)
{
SDL_Event event;
UnidirMessage<Vec2i> windowSizeMsg;
static bool once = true;
if (once) {
SDL_Event event;
SDL_Window *win = rtData.window;
UnidirMessage<Vec2i> &windowSizeMsg = rtData.windowSizeMsg;
initALCFunctions(rtData.alcDev);
// XXX this function breaks input focus on OSX
#ifndef __MACOSX__
SDL_SetEventFilter(eventFilter, &rtData);
#endif
fullscreen = rtData.config.fullscreen;
int toggleFSMod = rtData.config.anyAltToggleFS ? KMOD_ALT : KMOD_LALT;
toggleFSMod = rtData.config.anyAltToggleFS ? KMOD_ALT : KMOD_LALT;
fps.lastFrame = SDL_GetPerformanceCounter();
fps.displayCounter = 0;
fps.acc = 0;
fps.accDiv = 0;
if (rtData.config.printFPS)
fps.sendUpdates.set();
bool displayingFPS = false;
bool cursorInWindow = false;
/* Will be updated eventually */
SDL_Rect gameScreen = { 0, 0, 0, 0 };
/* SDL doesn't send an initial FOCUS_GAINED event */
bool windowFocused = true;
bool terminate = false;
SDL_Joystick *js = 0;
if (SDL_NumJoysticks() > 0)
js = SDL_JoystickOpen(0);
char buffer[128];
char pendingTitle[128];
bool havePendingTitle = false;
bool resetting = false;
int winW, winH;
int i;
SDL_GetWindowSize(win, &winW, &winH);
SettingsMenu *sMenu = 0;
while (true)
{
if (!SDL_WaitEvent(&event))
{
Debug() << "EventThread: Event error";
break;
once = false;
}
while (SDL_PollEvent(&event))
{
#ifdef __EMSCRIPTEN__
emscripten_sleep(10);
#endif
// if (!SDL_WaitEvent(&event))
// {
// Debug() << "EventThread: Event error";
// break;
// }
if (sMenu && sMenu->onEvent(event))
{
@ -460,12 +441,12 @@ void EventThread::process(RGSSThreadData &rtData)
}
/* Just in case */
rtData.syncPoint.resumeThreads();
//rtData.syncPoint.resumeThreads();
if (SDL_JoystickGetAttached(js))
SDL_JoystickClose(js);
//if (SDL_JoystickGetAttached(js))
// SDL_JoystickClose(js);
delete sMenu;
//delete sMenu;
}
int EventThread::eventFilter(void *data, SDL_Event *event)

View File

@ -35,7 +35,7 @@
#include <string>
#include <stdint.h>
class SettingsMenu;
struct RGSSThreadData;
typedef struct ALCdevice_struct ALCdevice;
struct SDL_Window;
@ -123,6 +123,35 @@ private:
double acc;
uint32_t accDiv;
} fps;
SDL_Window *win;
int toggleFSMod;
bool displayingFPS = false;
bool cursorInWindow = false;
/* Will be updated eventually */
SDL_Rect gameScreen = { 0, 0, 0, 0 };
/* SDL doesn't send an initial FOCUS_GAINED event */
bool windowFocused = true;
bool terminate = false;
SDL_Joystick *js = 0;
char buffer[128];
char pendingTitle[128];
bool havePendingTitle = false;
bool resetting = false;
int winW, winH;
int i;
SettingsMenu *sMenu = 0;
};
/* Used to asynchronously inform the RGSS thread

View File

@ -18,7 +18,7 @@
** You should have received a copy of the GNU General Public License
** along with mkxp. If not, see <http://www.gnu.org/licenses/>.
*/
#include <cassert>
#include "filesystem.h"
#include "rgssad.h"

View File

@ -13,7 +13,7 @@
#elif __WINDOWS__
#define FLUID_LIB "fluidsynth.dll"
#else
#error "platform not recognized"
//#error "platform not recognized"
#endif
struct FluidFunctions fluid;
@ -22,6 +22,7 @@ static void *so;
void initFluidFunctions()
{
#if 0
#ifdef SHARED_FLUID
#define FLUID_FUN(name, type) \
@ -60,4 +61,7 @@ fail:
SDL_UnloadObject(so);
so = 0;
#endif
#endif
so = 0;
return;
}

View File

@ -316,7 +316,9 @@ private:
/* Nanoseconds per second */
#define NS_PER_S 1000000000
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#endif
struct FPSLimiter
{
uint64_t lastTickCount;
@ -418,6 +420,7 @@ struct FPSLimiter
private:
void delayTicks(uint64_t ticks)
{
#ifndef __EMSCRIPTEN__
#if defined(HAVE_NANOSLEEP)
struct timespec req;
uint64_t nsec = ticks / tickFreqNS;
@ -439,6 +442,10 @@ private:
}
#else
SDL_Delay(ticks / tickFreqMS);
#endif
#endif
#ifdef __EMSCRIPTEN
emscripten_sleep(ticks /tickFreqMS);
#endif
}
};

View File

@ -232,7 +232,7 @@ int main(int argc, char *argv[])
}
conf.readGameINI();
printf("%s\n", conf.gameFolder.c_str());
if (conf.windowTitle.empty())
conf.windowTitle = conf.game.title;
@ -325,8 +325,9 @@ int main(int argc, char *argv[])
rtData.bindingUpdateMsg.post(loadBindings(conf));
/* Start RGSS thread */
SDL_Thread *rgssThread =
SDL_CreateThread(rgssThreadFun, "rgss", &rtData);
//SDL_Thread *rgssThread =
// SDL_CreateThread(rgssThreadFun, "rgss", &rtData);
::rgssThreadFun(&rtData);
/* Start event processing */
eventThread.process(rtData);
@ -350,8 +351,8 @@ int main(int argc, char *argv[])
/* If RGSS thread ack'd request, wait for it to shutdown,
* otherwise abandon hope and just end the process as is. */
if (rtData.rqTermAck)
SDL_WaitThread(rgssThread, 0);
if (rtData.rqTermAck){}
//SDL_WaitThread(rgssThread, 0);
else
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, conf.windowTitle.c_str(),
"The RGSS script seems to be stuck and mkxp will now force quit", win);