Using SDL2 with iOS native objects

Hi all,

I’m try to using SDL 2 for iOS application. I’m build the libSDL2.a and add it to the my project. Also I’m add an all headers to application project.
For initialization I’m use next code:

/* 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);

and then for drawing my images I sue next code:

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);

Pictures draw perfectly, but after drawing I do not see any iOS native objects (UIView, UIButton, ect.) no the application UI, even if I add this objects programmatically.

Can any body explains me how I can use SDL with iOS native objects?

Thank you.–
Maxim Tartachnik

How, and more importantly, when (relative to SDL’s window + renderer creation calls), are you adding the UIKit controls?

leadro wrote:

Can any body explains me how I can use SDL with iOS native objects?

The short answer is you don’t. If you want to use UIKit, you may as well ditch SDL and just use UIKit.

Are you concerned about portability or just focusing on the iOS platform?

leadro wrote:

I’m try to add the UIKit controls after SDL’s window + renderer creation. For adding UIKit controls, for example UIView, I’m use next code:

Code:
UIView* newView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
newView.backgroundColor = [UIColor yellowColor];
[self.view addSubView:newView];

In the above bit of code, what is ‘self’? Is it a UIViewController, or a subclass of one? SDL2, as of this writing, has its own subclass of UIViewController. Assuming you’re creating everything in one UIViewController, then trying to display SDL’s view controller (via SDL_RenderPresent), SDL’s view could end up being overlaid on top of yours.

In my experience, it’s possible to overlay UIKit controls on top of SDL’s in either of two ways:

  1. by getting a pointer to SDL’s view controller, getting its view, then adding new controls as subviews. I’ve done this in the past to overlay a UIKit text field onto an SDL app, in order to get use of iOS’ cut, copy, paste, and deletion controls.
  2. by getting a pointer to SDL’s view controller, creating a new view controller, then displaying it modally over SDL’s view controller. This can be used to display a Game Center view controller, or a score-sharing view controller, for example.

Here is one way to get SDL’s view controller: create your window, create the renderer (using the OpenGL ES based renderer, not the software-only one), use SDL_GetWindowWMInfo to get a pointer to the app’s UIWindow, then use the ‘rootViewController’ method on the UIWindow to get the view controller. Here’s some extremely rough example code:

Code:

// 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 = mainWindowWMInfo.info.uikit.window;
UIViewController * rootViewController = appWindow.rootViewController;
return rootViewController;
}

With this, one or more UIKit controls could then be added to the screen as such:

Code:

void SetupIOSControls(SDL_Window * sdlWindow)
{
UIViewController * rootViewController = GetSDLViewController(sdlWindow);
if ( ! rootViewController ) {
// handle this error as appropriate
} else {
UIView * myView = /* create and configure a UIView, or an appropriate subclass of UIView, such as UITextField */
[rootViewController.view addSubview:myView];
[myView release]; // This line probably isn’t necessary if ARC is being used.
}
}

leadro wrote:

I need to integrate the SDL features for drawing images into iOS application.
But, as I say previously, I have a problem with showing UIKit controls over SDL_Window + SDL_Renderer

UIKit is very much capable of drawing images. :slight_smile: Is there some feature that SDL has that UIKit doesn’t have, outside of platform-independence, that you’re looking for? The code you posted looks fairly iOS-centric and non-portable. I’m wondering if you wouldn’t just be happier and/or better off using UIKit instead of SDL for the time being.

You can also subclass SDL_UIKitAppDelegate and have pure objective-c code
before launching the sdl view.
http://code.google.com/p/hedgewars/source/browse/project_files/HedgewarsMobile/Classes/HedgewarsAppDelegate.m

VittorioOn Fri, Jan 4, 2013 at 6:32 PM, DLudwig wrote:

**
How, and more importantly, when (relative to SDL’s window + renderer
creation calls), are you adding the UIKit controls?


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Vittorio Giovara wrote:

You can also subclass SDL_UIKitAppDelegate and have pure objective-c code before launching the sdl view.
http://code.google.com/p/hedgewars/source/browse/project_files/HedgewarsMobile/Classes/HedgewarsAppDelegate.m (http://code.google.com/p/hedgewars/source/browse/project_files/HedgewarsMobile/Classes/HedgewarsAppDelegate.m)

Neat! This looks like a remarkably useful technique, especially given iOS’ close ties to app delegates. Thanks for the info!

– David L.

leadro wrote:

I try this code, but I have a problem with initialization SDL_SysWMinfo. I’m share a screenshot of this problem:
https://docs.google.com/open?id=0B1IEde1HB7cUSVE1NGpVNDRHTUU

I’m not entirely sure what’s happening here. Maybe try #include’ing <SDL_syswm.h> ?

There is a small error in the code I posted before. It’s on a different line, specifically one that declares and sets ‘appWindow’. Here’s a modified version:

Code:

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

leadro wrote:

Also, maybe I have a problem with library,
DLudwig, can you send me, please, your variant of library and I try this code with your library.
Or maybe you can guide me how I can build SDL library (or framework).

While I usually use a variant of SDL in whatever project I’m working in, the code sample I listed does work in stock SDL 2.x, as far as I can tell. I tried it with a fresh snapshot of SDL 2.x code from hg.libsdl.org created yesterday afternoon.