我已经了解到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()方法。怎么样?如果是的话,任何人都可以显示更多代码吗?感谢。
答案 0 :(得分:1)
您需要获取SDLMain.m和SDLMain.h
在此处阅读更多相关信息 - http://beefchunk.com/documentation/lib/libSDL/faq/FAQ-MacOSX.html
答案 1 :(得分: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
执行圆顶SDL作业,因此不需要AppDelegate.h / m。然后SDL的窗口和UIKit的窗口有时互相交换。
SDLUIKitDelegate
,SDL_uikitopenglview
。