Mix_OpenAudio Closes Program Without Creating Window

60 Views Asked by At

I want to create a simple program that lets me move around a little sprite with some music.

I'm able to bring up a window, my next step is getting some music. I have everything setup properly as when I build my main.exe with make it builds perfectly. I've narrowed the issue down to the first instance when the mixer is used in the line Mix_OpenAudio(48000, AUDIO_S16SYS, 2, 1024.

I'm not sure why it's not working, but when this line is included, the program simply doesn't open the window and ends the program. On the contrary, when the line isn't included, then the window opens just fine. I've provided a screencap to further illustrate:

Screencap

Here is my main.cpp code:

#include <iostream>
#include <SDL2/SDL.h>
#include <SDl2/SDL_mixer.h>

//Define window size
const int WIDTH = 800;
const int HEIGHT = 600;

bool init();
void closeWindow();

SDL_Window *window = NULL;

bool init() {


    window = SDL_CreateWindow("test", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WIDTH, HEIGHT, SDL_WINDOW_ALLOW_HIGHDPI);
    if (NULL == window) {
        std::cout << "Could not create window: " << SDL_GetError() << std::endl;
        return 1;
    }
    else {
        return 0;
    }

}

void closeWindow() {

    SDL_DestroyWindow(window);
    SDL_Quit();
    return;

}



int main(int argc, char *argv[]) {

    /* Initialize the SDL library */
    if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
        fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
        exit(2);
    }
    /* Opens the audio device */
    if (Mix_OpenAudio(48000, AUDIO_S16SYS, 2, 1024) < 0 ) {
        fprintf(stderr, "Warning: Couldn't set 48000Hz 16-bit audio\n- Reason: %s\n", SDL_GetError());
    }

    init();

    SDL_Event windowEvent;
    while(true) {
        if(SDL_PollEvent(&windowEvent)) {
            if (SDL_QUIT == windowEvent.type) {
                break;
            }
        }

        
        

    }

    closeWindow();
    Mix_CloseAudio();
    return EXIT_SUCCESS;
}

How do I fix this issue?

0

There are 0 best solutions below