【问题标题】:SDL and iOS main() methods conflictSDL 和 iOS main() 方法冲突
【发布时间】:2014-06-20 13:20:24
【问题描述】:

我了解到 SDL 项目需要 main() 方法来运行循环,代码如下:

#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;
}

而iOS项目也需要main()方法,代码如下:

#import <UIKit/UIKit.h>

#import "AppDelegate.h"

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

现在我需要将 SDL 库集成到 iOS 项目中,但需要两个 main() 方法。如何?如果是,有人可以显示更多代码吗?谢谢。

【问题讨论】:

    标签: ios graphics ffmpeg sdl main


    【解决方案1】:

    我已经更改了 main.m 文件中的 main() 函数,如下所示:

    #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;
        }
    }
    

    ViewController.m 中做 dome SDL 工作,所以不需要 AppDelegate.h/m。然后 SDL 的窗口和 UIKit 的窗口有时会相互交换。

    顺便说一句,在一个集成了SDL的iOS项目中,UIKit支持一些UIWindow,SDL支持一些SDL_Window,项目会生成SDLUIKitDelegateSDL_uikitopenglview

    【讨论】:

    • 您是如何从故事板导航到 sdl 主循环的?还是整个项目都是用 Sdl 完成的??
    【解决方案2】:

    你需要获取 SDLMain.m 和 SDLMain.h

    在此处了解更多信息 - http://beefchunk.com/documentation/lib/libSDL/faq/FAQ-MacOSX.html

    【讨论】:

      猜你喜欢
      • 2016-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多