Emscripten and sdl2_mixer error: Mix_Init: no sound/music loaders supported ()

44 Views Asked by At

I have been trying to create a small webapp in C using emscripten and sdl2. I want to include the sdl2_mixer into the program in order to play the sound but the console keeps giving me the error: Mix_Init: no sound/music loaders supported (). I was able to use SDL2 and SDL2_image just fine, however as soon as I start importing SDL2_mixer (not even using it), it throws this error at me. Any help would be much appreciated. I have tried to find examples and tutorial for using emscripten and sdl2, but they seem to rarely use sdl2_mixer.

I have tried a lot of different commands to get it to compile (like including -sUSE_VORBIS=1 or -sUSE_OGG=1), but this is the current one I am using: emcc source/main.c -o main.html -s USE_SDL=2 -s USE_SDL_IMAGE=2 -s USE_SDL_MIXER=2 -s SDL2_IMAGE_FORMATS=["png"] -s ALLOW_MEMORY_GROWTH=1 --preload-file assets

Here is the code for the project (note I am not trying to play sound in the project. Just include it.):

#include <stdio.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_mixer.h>
#include <emscripten.h>
#include <stdlib.h>


SDL_Window *window = NULL;
SDL_Renderer *renderer = NULL;
SDL_Texture *texture = NULL;
SDL_Rect destinationRect = {100, 100, 0, 0};


void render_loop() {
    SDL_Event event;
    while (SDL_PollEvent(&event)) {
        if (event.type == SDL_QUIT) {
            emscripten_cancel_main_loop();
            break;
        }
    }
    
    SDL_RenderClear(renderer);
    SDL_RenderCopy(renderer, texture, NULL, &destinationRect);
    SDL_RenderPresent(renderer);
}

int main() {

    SDL_Init(SDL_INIT_VIDEO);
    IMG_Init(IMG_INIT_PNG);
       

    window = SDL_CreateWindow("SDL2_image Example",
                              SDL_WINDOWPOS_CENTERED,
                              SDL_WINDOWPOS_CENTERED,
                              800, 600,
                              0);
    renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
    
    SDL_Surface *surface = IMG_Load("assets/triangle.png");
    if (!surface) {
        printf("Failed to load image: %s\n", IMG_GetError());
        return 1;
    }
    
    destinationRect.w = surface->w;
    destinationRect.h = surface->h;
    
    texture = SDL_CreateTextureFromSurface(renderer, surface);
    if (!texture) {
        printf("Failed to create texture: %s\n", SDL_GetError());
        return 1;
    }
    
    SDL_FreeSurface(surface);

    emscripten_set_main_loop(render_loop, 0, 1);
    
    SDL_DestroyTexture(texture);
    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);
    IMG_Quit();
    SDL_Quit();
    
    return 0;
}


1

There are 1 best solutions below

2
max97 On

I was able to get it working. I had to revert to an older version of the emsdk(I used 3.1.44). I think there must be a bug of some sort.