greenfork / nimraylib_now

The Ultimate Raylib gaming library wrapper for Nim
MIT License
149 stars 16 forks source link

loadMusicStream() is not working in html5 #82

Closed ghost closed 2 years ago

ghost commented 2 years ago

Probably,loadMusicStream() file loading is failed in emscripten build. This is bug report in Chrome. 185789143-87e98f09-358f-4446-be79-9daeeb25463f

wav and mp3 are also failed. And desktop(windows10) build is already succeeded in same code.

greenfork commented 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

ghost commented 2 years ago

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?

greenfork commented 2 years ago

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.

ghost commented 2 years ago

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);
}
ghost commented 2 years ago

If the translation to Nim is correct,it might be emscripten setting problem,like you said.Thank you.

greenfork commented 2 years ago

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.

greenfork commented 2 years ago

Could you also share soundplayer.h file?

ghost commented 2 years ago

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.

greenfork commented 2 years ago

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".

ghost commented 2 years ago

raylib-game-template.zip

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.

greenfork commented 2 years ago

@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.

ghost commented 2 years ago

Thank you for your help and nimraylib_now.