0

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.

Andrew Tomazos
  • 66,139
  • 40
  • 186
  • 319
  • Please don't use the term "C/C++". There simply isn't any such language. Only the two distinct and *very*different languages C and C++. With that said, there's nothing specific to C++ in the code you show. If you want to program in C++, please learn it properly. – Some programmer dude Nov 16 '20 at 02:40
  • @Someprogrammerdude: You can read "C/C++" as "C and/or C++". C and C++ are not very different languages, C is mostly a subset of C++. The C++ specific thing is "g++ -std=c++17" so technically this is compiled as a C++17 program. It could be conceivable that this was relevant to the issue. – Andrew Tomazos Nov 16 '20 at 03:42
  • Did you try putting logs after every line? – OLNG Jan 12 '21 at 05:12
  • @OLNG: The issue was the same as the one in the linked duplicate. On MacOS you have to enter the SDL event loop before the window will show up. – Andrew Tomazos Jan 12 '21 at 09:23

0 Answers0