Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
charlesneimog committed Nov 30, 2024
2 parents d9e15c8 + 1247505 commit 6055f74
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 45 deletions.
14 changes: 7 additions & 7 deletions Documentation/tests/3-body/WebPatch/sketch.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ function setup() {
createCanvas(w, h, WEBGL);

// Initialize the masses
mass1 = new Mass(-w / 2, 0, 8, 0.1, 0.1, color(255, 0, 0), 1);
mass2 = new Mass(-w / 3, 0, 10, -0.02, -0.01, color(0, 255, 0), 2);
mass3 = new Mass(w / 3, 0, 9, 0, 0, color(0, 150, 255), 3);
mass4 = new Mass(0, 0, 5, 0, 0, color(150, 255, 255), 4);
mass5 = new Mass(0, h / 4, 7, 0, 0, color(155, 255, 0), 5);
mass1 = new Mass(0, 0, 40, -0.02, -0.01, color(0, 255, 0), 2);
mass2 = new Mass(-w / 8, 0, 8, 0.1, 0.1, color(255, 0, 0), 1);
mass3 = new Mass(w / 8, 0, 9, 0, 0, color(0, 150, 255), 3);
mass4 = new Mass(0, -h / 8, 5, 0, 0, color(150, 255, 255), 4);
mass5 = new Mass(0, h / 8, 7, 0, 0, color(155, 255, 0), 5);
}

function draw() {
Expand Down Expand Up @@ -68,7 +68,7 @@ function draw() {
function checkCollision(m1, m2) {
let distance = dist(m1.pos.x, m1.pos.y, m2.pos.x, m2.pos.y);
let combinedRadius = m1.radius + m2.radius;
let proximityThreshold = 15; // Define "very close" threshold distance
let proximityThreshold = 7; // Define "very close" threshold distance

// Check if the objects are within the "very close" range
if (distance <= combinedRadius + proximityThreshold) {
Expand Down Expand Up @@ -121,7 +121,7 @@ class Mass {
}

function applyGravity(m1, m2) {
let G = 1; // Gravitational constant
let G = 0.7; // Gravitational constant
let r = p5.Vector.sub(m1.pos, m2.pos);
let distanceSq = r.magSq();
distanceSq = constrain(distanceSq, 100, 10000);
Expand Down
7 changes: 6 additions & 1 deletion Resources/Pd/pd4web.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,12 @@ static bool pd4web_check(Pd4Web *x) {
post("[pd4web] Installing pd4web, wait...");
}

std::string venv_cmd = x->pythonGlobal + " -m venv \"" + x->objRoot + "/.venv\"";
// create virtual environment
std::string objRoot = x->objRoot;
std::string venv_cmd = x->pythonGlobal + " -m venv \"" + objRoot + "/.venv\"";
#ifdef _WIN32
std::replace(objRoot.begin(), objRoot.end(), '\\', '/');
#endif
post("[pd4web] Creating virtual environment...");
result = pd4web_terminal(x, venv_cmd, false, false, false, false);
if (!result) {
Expand Down
23 changes: 21 additions & 2 deletions Sources/pd4web.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,29 @@ EM_JS(void, _JS_addSoundToggle, (void), {
});
});

// ─────────────────────────────────────
EM_JS(void, _JS_pd4webCppClass, (void *Pd4Web), {
console.log("Received Pd4Web pointer:", Pd4Web);
});

// ─────────────────────────────────────
EM_JS(void, _JS_setIcon, (const char *icon, const char *animation), {
let jsIcon = UTF8ToString(icon);
let jsAnimation = UTF8ToString(animation);
setSoundIcon(jsIcon, jsAnimation);

function tryCallSetSoundIcon() {
if (typeof setSoundIcon === 'function') {
setSoundIcon(jsIcon, jsAnimation);
} else {
setTimeout(() => {
if (typeof setSoundIcon === 'function') {
setSoundIcon(jsIcon, jsAnimation);
}
}, 200); // Retry after 200ms
}
}

setTimeout(() => tryCallSetSoundIcon(), 200);
});

// ─────────────────────────────────────
Expand Down Expand Up @@ -935,7 +953,6 @@ void Pd4Web::resumeAudio() { emscripten_resume_audio_context_sync(m_Context); }
*/
void Pd4Web::suspendAudio() { _JS_suspendAudioWorkLet(m_Context); }


/* Sound Switch */
void Pd4Web::soundToggle() {
if (m_audioSuspended) {
Expand Down Expand Up @@ -1012,6 +1029,8 @@ void Pd4Web::init() {
}
m_audioSuspended = false;

_JS_pd4webCppClass(this);

return;
}
// ╭─────────────────────────────────────╮
Expand Down
2 changes: 1 addition & 1 deletion Sources/pd4web.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ EMSCRIPTEN_BINDINGS(WebPd) {
.function("suspendAudio", &Pd4Web::suspendAudio)
.function("resumeAudio", &Pd4Web::resumeAudio)
.function("soundToggle", &Pd4Web::soundToggle)

// senders
.function("sendFloat", &Pd4Web::sendFloat)
.function("sendSymbol", &Pd4Web::sendSymbol)
Expand Down
52 changes: 18 additions & 34 deletions Sources/pd4web/Pd4Web.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
import pygit2

import shutil
import requests
import importlib.metadata as importlib_metadata


import certifi

os.environ["SSL_CERT_FILE"] = certifi.where()
Expand Down Expand Up @@ -62,12 +60,12 @@ def argParse(self):

parser = parser.parse_args()

self.get_mainPaths()
self.getMainPaths()
self.do_actions(parser)

self.Parser = parser
self.Patch = os.path.abspath(self.Parser.patch_file)

if not os.path.isfile(self.Patch):
self.exception("\n\nError: Patch file not found")

Expand Down Expand Up @@ -101,7 +99,7 @@ def InitVariables(self):
self.declaredLibsObjs = []
self.declaredPaths = []
self.processedAbs = []

# Versions
self.Version = {}
self.Version["python"] = f"{sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro}"
Expand All @@ -124,8 +122,7 @@ def InitVariables(self):
# add /bin and /usr/bin to PATH on macOS
if sys.platform == "darwin":
self.env["PATH"] += ":/bin:/usr/bin"



def Execute(self):
from .Builder import GetAndBuildExternals
from .Compilers import ExternalsCompiler
Expand All @@ -134,7 +131,7 @@ def Execute(self):
if self.Patch == "":
self.exception("You must set a patch file")

self.get_mainPaths()
self.getMainPaths()
self.InitVariables()

# ╭──────────────────────────────────────╮
Expand All @@ -161,24 +158,17 @@ def Execute(self):

def RunBrowser(self):
self.CWD = os.getcwd()
if sys.platform == "win32":
self.APPDATA = os.path.join(os.getenv("APPDATA"), "pd4web")
elif sys.platform == "darwin":
self.APPDATA = os.path.join(os.path.expanduser("~/Library/"), "pd4web")
elif sys.platform == "linux":
self.APPDATA = os.path.join(os.path.expanduser("~/.local/share"), "pd4web")
else:
self.exception("Unsupported platform")
self.PD4WEB_ROOT = os.path.dirname(os.path.realpath(__file__))
self.APPDATA = os.path.join(self.PD4WEB_ROOT, "data")

emccRun = self.APPDATA + "/emsdk/upstream/emscripten/emrun"
# check if emrun exists
if not os.path.exists(emccRun):
self.exception("emrun not found. Please install Emscripten")

projectRoot = os.path.realpath(self.Patch)
os.chdir(projectRoot)
subprocess.run(f"{emccRun} {projectRoot}/index.html", shell=True, env=self.env)

def get_mainPaths(self):
def getMainPaths(self):
self.PROJECT_ROOT = os.path.dirname(os.path.realpath(self.Patch))
self.PROJECT_PATCH = os.path.basename(self.Patch)
self.PD4WEB_ROOT = os.path.dirname(os.path.realpath(__file__))
Expand All @@ -187,15 +177,9 @@ def get_mainPaths(self):
self.PD4WEB_LIBRARIES = os.path.abspath(self.PD4WEB_LIBRARIES)

self.CWD = os.getcwd()
if sys.platform == "win32":
self.APPDATA = os.path.join(os.getenv("APPDATA"), "pd4web")
elif sys.platform == "darwin":
self.APPDATA = os.path.join(os.path.expanduser("~/Library/"), "pd4web")
elif sys.platform == "linux":
self.APPDATA = os.path.join(os.path.expanduser("~/.local/share"), "pd4web")
else:
self.exception("Unsupported platform")

if not os.path.exists(os.path.join(self.PD4WEB_ROOT, "data")):
os.makedirs(os.path.join(self.PD4WEB_ROOT, "data"))
self.APPDATA = os.path.join(self.PD4WEB_ROOT, "data")
if not os.path.exists(self.APPDATA):
os.makedirs(self.APPDATA)

Expand Down Expand Up @@ -305,7 +289,7 @@ def options_flags(self, parser):
type=int,
help="Initial memory size in MB",
)

parser.add_argument(
"--patch-file",
required=False,
Expand Down Expand Up @@ -349,7 +333,7 @@ def options_flags(self, parser):
type=str,
help="Pure Data version to use",
)

# Pd Object
parser.add_argument(
"--pd-external",
Expand All @@ -358,7 +342,7 @@ def options_flags(self, parser):
action="store_true",
help="If is it pd4web external",
)

parser.add_argument(
"--pd-external-version",
required=False,
Expand Down Expand Up @@ -391,13 +375,13 @@ def print(self, text, color=None, bright=False, silence=False, pd4web=False):
if pd4web:
if color == "red":
print("ERROR: " + text)
sys.stdout.flush() # Ensure immediate output
sys.stdout.flush()
elif color == "yellow":
print("WARNING: " + text)
sys.stdout.flush() # Ensure immediate output
sys.stdout.flush()
else:
print(text)
sys.stdout.flush() # Ensure immediate output
sys.stdout.flush()
return
if silence:
return
Expand Down

0 comments on commit 6055f74

Please sign in to comment.