Closed ghost closed 2 years ago
Hi! Thanks for you detailed issue description. I'm afraid I can't help you, I don't have enough experience with EMSDK and how different cross-platform things work there. I will have to forward you to the Raylib discord server, there it should be relatively easy to get hints on how to solve the issue. I will leave this issue open in case you would want to post here a solution.
Raylib discord: https://discord.gg/raylib
Raylib C is working loadMusicStream() in emscripten. I think this is maybe nim Binding problem not emsdk, but I have no experience using nim. Can I close this issue right?
Raylib C is working loadMusicStream() in emscripten.
Do you have an example of C code which works in emscripten? I can make sure that the translation to Nim is correct. Currently it looks just fine since you said that it works on desktop. Although I might not be setting emscripten correctly. So far looking at the source code of the LoadMusicStream
function there's no hint as to why it wasn't opened.
This is my main.c and soundmanager.c Soundplayer_playmusic() and Soundplayer_update() are part of loadMusicStream().
#include "raylib.h"
#include "soundplayer.h"
#include "screens.h"
#if defined(PLATFORM_WEB)
#include <emscripten/emscripten.h>
#endif
int main(void)
{
InitWindow(screenWidth, screenHeight, "raylib game template");
InitAudioDevice(); // Initialize audio device
font = LoadFont("resources/mecha.png");
Soundplayer_playmusic("resources/sounds/PixelTunes/Pixel_Tune_2.ogg");
currentScreen = GAMEPLAY;
logo_init(&logo);
gameplay_init(&gameplay);
option_init(&option);
current = (Screen*)&gameplay;
#if defined(PLATFORM_WEB)
emscripten_set_main_loop(UpdateDrawFrame, 0, 1);
#else
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
while (!WindowShouldClose()) // Detect window close button or ESC key
{
UpdateDrawFrame();
}
#endif
current->interface->unload(current);
UnloadFont(font);
Soundplayer_unload();
CloseAudioDevice(); // Close audio context
CloseWindow(); // Close window and OpenGL context
return 0;
}
static void UpdateDrawFrame(void)
{
// Update
// NOTE: Music keeps playing between screens
Soundplayer_update();
if (IsWindowResized()){
SetWindowSize(GetScreenWidth(), GetScreenHeight());
}
if (!onTransition)
{
current->interface->update(current,0.0167f);
if (current->interface->finish(current) == 1) TransitionToScreen(TITLE);
if (current->interface->finish(current) == 2) TransitionToScreen(GAMEPLAY);
if (current->interface->finish(current) == 3) TransitionToScreen(ENDING);
}
else UpdateTransition(); // Update transition (fade-in, fade-out)
BeginDrawing();
ClearBackground(RAYWHITE);
current->interface->draw(current);
if (onTransition) DrawTransition();
EndDrawing();
}
#include "soundplayer.h"
Music music = { 0 };
void Musicplayer_load(char *key)
{
music = LoadMusicStream(key);
}
void Soundplayer_playmusic(char *key)
{
Musicplayer_load(key);
SetMusicVolume(music, 1.0f);
PlayMusicStream(music);
}
void Soundplayer_update(void)
{
UpdateMusicStream(music);
}
void Soundplayer_unload(void)
{
UnloadMusicStream(music);
}
If the translation to Nim is correct,it might be emscripten setting problem,like you said.Thank you.
Alright, thank you for the example! I will take a look at it this week, I don't have enough time to deal with it sooner, sorry for that.
Could you also share soundplayer.h
file?
This is my soundplayer.h
#include "raylib.h"
extern Music music;
void Musicplayer_load(char*);
void Soundplayer_playmusic(char*);
void Soundplayer_update(void);
void Soundplayer_unload(void);
No problem,give priority to your convenience.
Could you give me the full source code and commands for your working C copy which compiles via emsdk and it has sound in it? According to emscripten packaging docs, the files must be explicitly passed on the command line to be included in the emscripten virtual file system. If you manage to do this via C code, you can easily pass any commands to the C compiler via nim --passC:"your C flags here"
.
This is minimal official Raylib C game template include sound and loadMusicStream(). I confirmed build and working this template in my pc(Windows10). My game full source is little complicated because of forcibly OOP in C.
This is command.
cd raylib-game-template
make PLATFORM=PLATFORM_WEB -B
But, to build this template,need to confirm your RAYLIB_PATH and EMSDK_PATH in makefile. And raylib library must be recompiled for HTML5, generating libraylib.a, follow the official tutorial https://github.com/raysan5/raylib/wiki/Working-for-Web-%28HTML5%29 A little troublesome.
I think Might be --preload-file and -s FORCE_FILESYSTEM=1 frags in makefile help to load resource(png,ttf,audio etc) for nimraylib_now. There is a detailed explanation in this tutorial 4.1 related emscripten virtual file system.
@tiki48 thanks for bringing up this issue, it is now fixed, you will have your resources loaded by default from the "resources" directory, see compile options for more flexibility.
Thank you for your help and nimraylib_now.
Probably,loadMusicStream() file loading is failed in emscripten build. This is bug report in Chrome.
wav and mp3 are also failed. And desktop(windows10) build is already succeeded in same code.