Skip to content

Commit

Permalink
Use new EasyRPG Emscripten API
Browse files Browse the repository at this point in the history
  • Loading branch information
patapancakes committed Jan 8, 2024
1 parent 552f131 commit cf9cc69
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 57 deletions.
57 changes: 0 additions & 57 deletions src/multiplayer/game_multiplayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -438,65 +438,8 @@ void Game_Multiplayer::InitConnection() {
});
}

//this will only be called from outside
extern "C" {
using namespace Messages::C2S;

int* GetPlayerCoords() {
auto& player = *Main_Data::game_player;

int* coords = new int[2];
coords[0] = player.GetX();
coords[1] = player.GetY();

return coords;
}

void SetGameLanguage(const char* lang) {
Player::translation.SelectLanguage(lang);
}

void SessionReady() {
auto& i = GMI();
if (i.connection.IsConnected())
i.connection.Close(); // if SessionReady is called, the websocket must already be closed
i.session_active = true;
if (i.room_id != -1)
i.Connect(i.room_id);
}

void TogglePlayerSounds() {
auto& f = Game_Multiplayer::Instance().settings.enable_sounds;
f = !f;
Web_API::ReceiveInputFeedback(1);
}

void ToggleMute() {
auto& f = Game_Multiplayer::Instance().settings.mute_audio;
f = !f;
Web_API::ReceiveInputFeedback(2);
}

void SetSoundVolume(const int volume) {
Audio().SE_SetGlobalVolume(volume);
}

void SetMusicVolume(const int volume) {
Audio().BGM_SetGlobalVolume(volume);
}

void SetNametagMode(const int mode) {
Game_Multiplayer::Instance().SetNametagMode(mode);
Web_API::NametagModeUpdated(mode);
}

void SetSessionToken(const char* t) {
auto& i = Game_Multiplayer::Instance();
i.session_token.assign(t);
}

}

void Game_Multiplayer::Connect(int map_id, bool room_switch) {
Output::Debug("MP: connecting to id={}", map_id);
room_id = map_id;
Expand Down
71 changes: 71 additions & 0 deletions src/platform/emscripten/interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@
#include "scene_save.h"
#include "output.h"

#include <string>
#include "../../multiplayer/game_multiplayer.h"
#include "../../game_player.h"
#include "../../audio.h"
#include "../../web_api.h"

void Emscripten_Interface::Reset() {
Player::reset_flag = true;
}
Expand Down Expand Up @@ -91,6 +97,61 @@ bool Emscripten_Interface_Private::UploadSavegameStep2(int slot, int buffer_addr
return true;
}

// YNOproject

int* Emscripten_Interface::GetPlayerCoords() {
auto& player = *Main_Data::game_player;

int *coords = new int[2];
coords[0] = player.GetX();
coords[1] = player.GetY();

return coords;
}

void Emscripten_Interface::SetLanguage(std::string lang) {
Player::translation.SelectLanguage(lang);
}

void Emscripten_Interface::SessionReady() {
auto& i = GMI();
if (i.connection.IsConnected())
i.connection.Close(); // if SessionReady is called, the websocket must already be closed
i.session_active = true;
if (i.room_id != -1)
i.Connect(i.room_id);
}

void Emscripten_Interface::TogglePlayerSounds() {
auto& f = Game_Multiplayer::Instance().settings.enable_sounds;
f = !f;
Web_API::ReceiveInputFeedback(1);
}

void Emscripten_Interface::ToggleMute() {
auto& f = Game_Multiplayer::Instance().settings.mute_audio;
f = !f;
Web_API::ReceiveInputFeedback(2);
}

void Emscripten_Interface::SetSoundVolume(int volume) {
Audio().SE_SetGlobalVolume(volume);
}

void Emscripten_Interface::SetMusicVolume(int volume) {
Audio().BGM_SetGlobalVolume(volume);
}

void Emscripten_Interface::SetNametagMode(int mode) {
Game_Multiplayer::Instance().SetNametagMode(mode);
Web_API::NametagModeUpdated(mode);
}

void Emscripten_Interface::SetSessionToken(std::string t) {
auto& i = Game_Multiplayer::Instance();
i.session_token.assign(t);
}

// Binding code
EMSCRIPTEN_BINDINGS(player_interface) {
emscripten::class_<Emscripten_Interface>("api")
Expand All @@ -99,6 +160,16 @@ EMSCRIPTEN_BINDINGS(player_interface) {
.class_function("uploadSavegame", &Emscripten_Interface::UploadSavegame)
.class_function("refreshScene", &Emscripten_Interface::RefreshScene)
.class_function("takeScreenshot", &Emscripten_Interface::TakeScreenshot)

.class_function("getPlayerCoords", &Emscripten_Interface::GetPlayerCoords, emscripten::allow_raw_pointers())
.class_function("setLanguage", &Emscripten_Interface::SetLanguage, emscripten::allow_raw_pointers())
.class_function("sessionReady", &Emscripten_Interface::SessionReady)
.class_function("togglePlayerSounds", &Emscripten_Interface::TogglePlayerSounds)
.class_function("toggleMute", &Emscripten_Interface::ToggleMute)
.class_function("setSoundVolume", &Emscripten_Interface::SetSoundVolume)
.class_function("setMusicVolume", &Emscripten_Interface::SetMusicVolume)
.class_function("setNametagMode", &Emscripten_Interface::SetNametagMode)
.class_function("setSessionToken", &Emscripten_Interface::SetSessionToken, emscripten::allow_raw_pointers())
;

emscripten::class_<Emscripten_Interface_Private>("api_private")
Expand Down
12 changes: 12 additions & 0 deletions src/platform/emscripten/interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,25 @@
#ifndef EP_EMSCRIPTEN_INTERFACE_H
#define EP_EMSCRIPTEN_INTERFACE_H

#include <string>

class Emscripten_Interface {
public:
static bool DownloadSavegame(int slot);
static void UploadSavegame(int slot);
static void RefreshScene();
static void TakeScreenshot();
static void Reset();

static int* GetPlayerCoords();
static void SetLanguage(std::string lang);
static void SessionReady();
static void TogglePlayerSounds();
static void ToggleMute();
static void SetSoundVolume(int volume);
static void SetMusicVolume(int volume);
static void SetNametagMode(int mode);
static void SetSessionToken(std::string t);
};

class Emscripten_Interface_Private {
Expand Down

0 comments on commit cf9cc69

Please sign in to comment.