Question

I have learnt that SDL project need main() method to run loop, code like this:

#include "SDL.h"

int main(int argc, char *argv[])
{
    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
        printf("%s\n", SDL_GetError());
    }

    SDL_Window *window = SDL_CreateWindow(NULL, 0, 0, 320, 640, SDL_WINDOW_OPENGL | SDL_WINDOW_FULLSCREEN);
    SDL_Renderer *renderer = SDL_CreateRenderer(window, 0, 0);

    SDL_Surface *bmp_surface = SDL_LoadBMP("space.bmp");
    SDL_Texture *space = SDL_CreateTextureFromSurface(renderer, bmp_surface);
    SDL_FreeSurface(bmp_surface);

    SDL_RenderCopy(renderer, space, NULL, NULL);
    SDL_RenderPresent(renderer);

    int done = 0;
    while (!done) {
        Uint32 startFrame = SDL_GetTicks();
        SDL_Event event;
        while (SDL_PollEvent(&event)) {
            if (event.type == SDL_QUIT) {
                done = 1;
            }
        }
        Uint32 endFrame = SDL_GetTicks();

        Sint32 delay = MILLESECONDS_PER_FRAME - (endFrame - startFrame);
        if (delay < 0) {
            delay = 0;
        } else if (delay > MILLESECONDS_PER_FRAME) {
            delay = MILLESECONDS_PER_FRAME;
        }
        SDL_Delay(delay);
    }

    SDL_DestroyTexture(space);
    SDL_Quit();

    return 0;
}

And iOS project also need main() method, code like this:

#import <UIKit/UIKit.h>

#import "AppDelegate.h"

int main(int argc, char * argv[])
{
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}

Now I need integrate SDL library to an iOS project, but there need two main() methods. How? If yes, can anybody show some more codes? Thanks.

Was it helpful?

Solution 2

I have change the main() function in main.m file, like this:

#import <UIKit/UIKit.h>
#import "ViewController.h"
#import "SDL.h"

extern C_LINKAGE int SDL_main(int argc, char * argv[])
{
    @autoreleasepool {
        ViewController *viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
        [UIApplication sharedApplication].keyWindow.rootViewController = viewController;
        [[UIApplication sharedApplication].keyWindow makeKeyAndVisible];

        return 0;
    }
}

In ViewController.m do dome SDL jobs, so AppDelegate.h/m are no need. Then SDL's window and UIKit's windows swap each other sometimes.

BTW, in an iOS project integrated with SDL, UIKit support some UIWindow and SDL support some SDL_Window, project will generate SDLUIKitDelegate, SDL_uikitopenglview.

OTHER TIPS

You need to get SDLMain.m and SDLMain.h

Read more about it here - http://beefchunk.com/documentation/lib/libSDL/faq/FAQ-MacOSX.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top