I have a small SDL2 C/C++ test program that displays a window for 10 seconds and quits. It works on both Windows and Linux. I've managed to get it to link successfully on MacOSX, but when I run it the program pauses for 10 seconds but no window is displayed, it then returns without error to the command prompt.
The program is:
#include <SDL2/SDL.h>
#include <stdio.h>
#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480
int main(int argc, char** argv) {
(void)argc;
(void)argv;
SDL_Window* window = NULL;
SDL_Surface* screenSurface = NULL;
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
fprintf(stderr, "could not initialize sdl2: %s\n", SDL_GetError());
return 1;
}
window = SDL_CreateWindow("hello_sdl2", SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH,
SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if (window == NULL) {
fprintf(stderr, "could not create window: %s\n", SDL_GetError());
return 1;
}
screenSurface = SDL_GetWindowSurface(window);
SDL_FillRect(screenSurface, NULL,
SDL_MapRGB(screenSurface->format, 0xFF, 0x00, 0xFF));
SDL_UpdateWindowSurface(window);
SDL_Delay(2000);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
I link it on MacOSX with:
% g++ -std=c++17 -O2 -g -Werror -Wall -Wextra -o sdl_test sdl_test.o -framework CoreAudio -framework Cocoa -lSDL2main -lSDL2 -framework IOKit -framework AudioUnit -framework AudioToolbox -framework CoreVideo -framework ForceFeedback -framework Carbon -liconv
It links successfully. But when I run it:
% ./sdl_test
Nothing happens, no window appears. After 10 seconds the prompt returns without any output or error.
The fact that it pauses for 10 seconds means SDL is initializing successfully and the window is created in code, so main is getting run.
Could this be something to do with "app bundling" possibly? Or how the program is run? I really don't know that much about modern MacOSX - or how graphical programs are supposed to work on that platform.