Skip to content

Instantly share code, notes, and snippets.

@bluesentinelsec
Created January 19, 2024 16:17
Show Gist options
  • Save bluesentinelsec/6e6168c76014478fd4a12448541a727b to your computer and use it in GitHub Desktop.
Save bluesentinelsec/6e6168c76014478fd4a12448541a727b to your computer and use it in GitHub Desktop.
How to configure SDL2 with Xcode on macOS
/*
This explains the basic steps you need to perform to create a working .app bundle from a C program using SDL2.
This guide assumes you are a competent developer and that you have an Apple Developer account with
code signing properly configured.
1. Create a new project in Xcode; select the "App" template.
2. Delete the boiler plate files (choose move to trash when prompted):
AppDelegate.h
AppDelegate.m
MainMenu.xib
main.m
3. Open Assets.xcassets and select AppIcon; drag your app icon images onto the canvas
4. Add a main.c file and write an SDL starter program (example provided below)
5. Drag and drop your media folder into the project pane
6. Download SDL2.framework for macOS: https://github.com/libsdl-org/SDL/releases
7. Unpack the SDL .dmg file, then drag and drop SDL2.framework into the Xcode project pane; keep the default values when prompted and select finish
8. Go to the project settings and under the "Identity" row, give your program an App Category (Games).
9. Go to the project settings and find "Frameworks, Libraries, and Embedded Content"; SDL2.framework should be present.
Modify the embed column to "Embed & Sign"
10. Go to "Signing & Capabilities"; under Hardened Runtime, select "Disable Library Validation"
11. Select "Product > Archive > Validate App"; follow the prompts and fix any issues
12. Select "Product > Archive > Distribute App > Direct Distribution". Wait for your app to be notorized.
13. After notorization is complete, select your archive, then select "Export Notarized App".
If you lose the window, select the Xcode task bar > Window > Organizer
14. Zip or DMG your .app bundle; now you can distribute to end users
zip -r your_game.zip your_game.app
15. Make sure you test your .app on another mac computer to validate success.
In my experience, .app builds sometimes fail to run because of stale build artifacts or some other nonsense.
*/
#include <SDL2/SDL.h>
// SDL starter template
int main(int argc, char *argv[]) {
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
fprintf(stderr, "Error initializing SDL: %s\n", SDL_GetError());
return 1;
}
SDL_Window* window = SDL_CreateWindow(
"SDL Example",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
800, 600,
SDL_WINDOW_SHOWN
);
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
SDL_Event event;
int isRunning = 1;
while (isRunning) {
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
isRunning = 0;
}
}
// Update your game logic here
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
// Render your game objects here
SDL_RenderPresent(renderer);
}
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
@bluesentinelsec
Copy link
Author

Let me know if this is useful to you or you have a better method?
I would love to be able to do this without opening Xcode for automated builds.
Even better if this can be done entirely from cmake.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment