我尝试将SDL 2用于iOS应用程序。我正在构建libSDL2.a并将其添加到我的项目中。此外,我将所有标题添加到应用程序项目。 对于初始化,我使用下一个代码:
/* initialize SDL */
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
fatalError("Could not initialize SDL");
}
window = SDL_CreateWindow(NULL, 0, 0, m_curScreenResolutions.deviceScreenResolution.size.height,
m_curScreenResolutions.deviceScreenResolution.size.width,
SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN |
SDL_WINDOW_BORDERLESS);
renderer = SDL_CreateRenderer(window, -1, 0);
然后为了绘制我的图像,我使用下一个代码:
struct displayImage
{
float x, y;
SDL_Texture *texture;
long width, height;
};
SDL_Rect srcRect;
SDL_Rect dstRect;
for (int i = 0; i < [m_imagesForDisplaying count]; i++)
{
NSValue *value = [m_imagesForDisplaying objectAtIndex:i];
displayImage preImage;
[value getValue:&preImage];
srcRect.x = 0;
srcRect.y = 0;
srcRect.w = preImage.width;
srcRect.h = preImage.height;
dstRect.w = preImage.width;
dstRect.h = preImage.height;
dstRect.x = preImage.x;
dstRect.y = preImage.y;
SDL_RenderCopy(inputRenderer, preImage.texture, &srcRect, &dstRect);
}
/* update screen */
SDL_RenderPresent(inputRenderer);
图片绘制完美,但在绘图后我看不到任何iOS原生对象(UIView
,UIButton
等等)没有应用程序UI,即使我以编程方式添加此对象。在SDL的窗口+渲染器创建之后,我尝试添加UIKit控件。要添加UIKit控件,例如UIView
,我将使用下一个代码:
UIView* newView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
newView.backgroundColor = [UIColor yellowColor];
[self.view addSubView:newView];
其中self是UIViewController。
然后我尝试使用代码
从UIWindow
获取SDL_SysWMinfo
// Retrieve SDL's root UIViewController (iOS only!)
// This function is completely NOT guaranteed to work in the future.
// Use it at your own risk!
UIViewController * GetSDLViewController(SDL_Window * sdlWindow)
{
SDL_SysWMinfo systemWindowInfo;
SDL_VERSION(&systemWindowInfo.version);
if ( ! SDL_GetWindowWMInfo(sdlWindow, &systemWindowInfo)) {
// consider doing some kind of error handling here
return nil;
}
UIWindow * appWindow = systemWindowInfo.info.uikit.window;
UIViewController * rootViewController = appWindow.rootViewController;
return rootViewController;
}
但是当我尝试初始化SDL_SysWMinfo
时,我遇到了一个嵌套问题:
https://docs.google.com/open?id=0B1IEde1HB7cUSVE1NGpVNDRHTUU
有人有什么想法吗?