Skip to main content Link Search Menu Expand Document (external link)

Theme: auto

Making a Standalone Build

If you’d like to make a standalone package for one of your games so that it’s easy for you to redistribute your game and send it to people to play, we’ve created these instructions for you!

PC

The PC script will create a zip file that contains everything needed to run your game. You can simply send the zip file to people and they can extract it and run the LabXX.exe file in the zip.

Building the Package

  1. In Windows Explorer, open up your repo root folder. Click on the address bar and copy the path.

  2. In the Start Menu, search for “x64 Native Tools Command Prompt for VS 2022” and run it.

  3. In the command prompt, type cd followed by a space, and then right click to paste the path. Hit enter.

  4. Run the following command, replacing the XX with the lab number (for example 02 for Lab 2 or 10 for Lab 10):
    Scripts\win-package.bat LabXX
    
  5. After it finishes running, if succesful, you should see a message like “Package Releases\Packages\LabXX.zip successfully created!”

  6. You can then get this zip file from your repo’s Releases\Packages folder and upload it somewhere, etc.

Mac

On Mac, most programs are distributed as a .app bundle which contains both the executable and the resources. It turns out a .app bundle is really just a special directory layout where you need to place certain files for it to work. We’ve created a script for you to create an app bundle in the correct layout. As an added bonus, the Mac package script we’ve written will build a Universal binary so it should work on both Intel and ARM Macs, regardless of what type of Mac you have.

Source Code Changes

App bundles act oddly when it comes to loading files. By default, it will use / as the current working directory which means that all the assets for your game will fail to load. To fix this, on initialization of the game we need to change the current working directory to the app bundle’s Resources directory. This requires some changes to the Game.cpp file for any game you wish to create an app bundle package for.

First, add the following code after the include statements and before the implementation of the Game constructor:

#if defined(__APPLE__) && defined(NDEBUG)
#include <CoreFoundation/CoreFoundation.h>
#include <sys/syslimits.h>

static void ChangeToResourcesDirectory()
{
	char resourcesPath[PATH_MAX];

	CFBundleRef bundle = CFBundleGetMainBundle();
	if (!bundle)
		return;

	CFURLRef resourcesURL = CFBundleCopyResourcesDirectoryURL(bundle);

	CFStringRef last = CFURLCopyLastPathComponent(resourcesURL);
	if (CFStringCompare(CFSTR("Resources"), last, 0) != kCFCompareEqualTo)
	{
		CFRelease(last);
		CFRelease(resourcesURL);
		return;
	}

	CFRelease(last);

	if (!CFURLGetFileSystemRepresentation(resourcesURL, true, (UInt8*)resourcesPath, PATH_MAX))
	{
		CFRelease(resourcesURL);
		return;
	}

	CFRelease(resourcesURL);

	chdir(resourcesPath);
}
#endif

Next, at the very start of Game::Initialize you need to add the following code:

#if defined(__APPLE__) && defined(NDEBUG)
	ChangeToResourcesDirectory();
#endif

The source code for ChangeToResourcesDirectory is from the GLFW project (source), which is released under the permissive zlib license.

Building the Package

  1. In terminal, cd to your repo directory

  2. Run the following command, replacing the XX with the lab number (for example 02 for Lab 2 or 10 for Lab 10):
    Scripts/mac-package.sh LabXX
    
  3. If everything works, you will see a message on the terminal saying that it successfully created a Releases/Packagaes/LabXX.app package (in your repo folder). If you browse to this in Finder, you can confirm it works by double-clicking on the app.

  4. You can then just redistribute this .app to anyone else. If you want to be fancy, you could make a compressed DMG file and put the .app in that, as outlined here.

Commercially distributed app bundles on Mac are signed using a certificate. However, the app bundle you are making is unsigned, so running it on other computers will yield a warning about it being potentially dangerous to run. However, this warning can be bypassed (see instructions here). Code signing the app for Mac is quite complicated to do (and requires registering as a verified Apple Developer), so it’s not covered here.