Adapting old C++ Code to run on MacOS

First, I should mention that I am totally new to C++, so I apologize up front for my inexperience and improper use of terms, etc.

I have some C++ code that a friend of mine wrote way back in the day (1998ish) that compiled and ran on Windows 98. It's a text based game that he did while in school. There is no urgency or purpose around me doing this other than I'm trying to learn. :-) I actually fired up a Windows 98 VM and ran the compiled executable that he had, and it ran fine.

I would like to get this to compile and run on my Mac. I started by looking at the code and finding that some of the header files that are listed as includes aren't things that I have, or are Windows specific.

So, I decided to take the approach of commenting out things in the code until I could get it to compile in Xcode and at least run at some level. Some of the things the code does is format the console text, and play wav files. After I commented out those specific things, it does seem to compile and run.

Now I am taking the approach of trying to figure out how to reintegrate the things I commented out in a way that will run on MacOS. I'm tackling the playing of wav files first.

I'm trying to implement SDL2 and SDL_mixer to do this. I've found some good tutorials that have shown me how to download the proper IMG file and copy things into /Library/Frameworks, and then how to set that up in Xcode. I've even been able to add the include directives (?) and the code will still compile without issue. Where I am running into issues now is trying to actually initialize SDL and play the file. I'm using code like this (this is from an example I found doing some searches):

    if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048) < 0) {
      // Handle error
    }
    
    Mix_Chunk* sound = Mix_LoadWAV("sounds/rm1.wav");
    Mix_PlayChannel(-1, sound, 0);
    Mix_FreeChunk(sound);
    Mix_CloseAudio();

But when I compile the code, I'm getting these errors, even though it does actually compile and start running anyway:

2022-12-19 10:20:44.218888-0600 thecave[4476:94786] [plugin] AddInstanceForFactory: No factory registered for id <CFUUID 0x600000234020> F8BB1C28-BAE8-11D6-9C31-00039315CD46
2022-12-19 10:20:44.322404-0600 thecave[4476:95240] [aqsrv]              AQServer.cpp:80    Exception caught in AudioQueueInternalNotifyRunning - error -66671

Does anyone have any idea why this is happening? Or maybe even a different way I should be trying to play wav files? Thanks so much!

It's been 10+ years since I used SDL so I can't provide an answer to your specific question, but I can provide some help for you to accomplish your goal.

File loading code from SDL articles and tutorials aren't going to work well on Mac because Mac apps have a different structure. Mac apps are app bundles that look like a single file in the Finder but have folders and files inside them. You can examine the contents of app bundles in the Finder by selecting a file, right-clicking, and choosing Show Package Contents.

In a Mac game the sound and image files are in the app bundle. Windows and Linux do not have app bundles. If you use code to load a sound file, such as the following, in a Mac game:

Mix_Chunk* sound = Mix_LoadWAV("sounds/rm1.wav");

The game won't be able to find and load the file.

The big thing to get file loading to work properly with SDL on Mac is to create an Xcode project that creates an app bundle, not a command-line tool project. You may be able to find SDL Xcode project templates on GitHub or at the SDL site that take care of things for you. But you can also create an App project that uses Objective-C as the language and storyboards for the user interface. Delete the .m, .h, and storyboard files from the project. Add SDL and your C++ code.

The following tutorial is old so the screenshots are going to look very different, but it shows how to set up SDL2 with Xcode:

https://www.meandmark.com/blog/2012/01/using-sdl-with-xcode-4/

The following Reddit thread has a link to a newer article on using SDL with Xcode:

https://www.reddit.com/r/Xcode/comments/zi9oku/how_do_i_set_up_sdl_with_xcode/

Thanks so much for your reply!

I've tried a few different things since I posted this. It actually doesn't seem to throw the error (or maybe it never gets there) on the part that loads the wav file, it throws the error when trying to initialize SDL. It looks like the last thing I tried was this:

if( SDL_Init( SDL_INIT_AUDIO ) < 0 )
    {
     printf( "SDL could not initialize! SDL Error: %s\n", SDL_GetError() );
    }

It doesn't print anything, but it still generates the error message. I don't even have the line in the code to play the wav file.

I'm going to take a look at the links you provided and see if they give me any clue. Thank you!

Adapting old C&#43;&#43; Code to run on MacOS
 
 
Q