The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
Using SDL2 with iOS native objects
leadro


Joined: 27 Dec 2012
Posts: 9
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:

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:

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
leadro


Joined: 27 Dec 2012
Posts: 9
Anybody help me, please!!!
DLudwig


Joined: 09 Feb 2012
Posts: 179
How, and more importantly, when (relative to SDL's window + renderer creation calls), are you adding the UIKit controls?
Re: Using SDL2 with iOS native objects
msturner


Joined: 06 Mar 2011
Posts: 15
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?
Using SDL2 with iOS native objects
Maxim Tartachnik
Guest

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
leadro


Joined: 27 Dec 2012
Posts: 9
DLudwig wrote:
How, and more importantly, when (relative to SDL's window + renderer creation calls), are you adding the UIKit controls?

Hello,

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


Also I try to add the UILable to the current view via next code:
Code:
        UILabel *myLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(50, 200, 200, 80)];
   myLabel2.text = @"Test";
   myLabel2.textAlignment = UITextAlignmentCenter;
   myLabel2.textColor = [UIColor yellowColor];
   myLabel2.shadowColor = [UIColor whiteColor];
   myLabel2.shadowOffset = CGSizeMake(1,1);
   myLabel2.font = [UIFont fontWithName:@"Zapfino" size:20];
   myLabel2.backgroundColor = [UIColor greenColor];
   [self.view addSubview:myLabel2];
   [myLabel2 release];
Re: Using SDL2 with iOS native objects
leadro


Joined: 27 Dec 2012
Posts: 9
msturner wrote:
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?

Hello,

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
DLudwig


Joined: 09 Feb 2012
Posts: 179
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. :-) 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.
Using SDL2 with iOS native objects
Vittorio Giovara
Guest

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

Vittorio

On Fri, Jan 4, 2013 at 6:32 PM, DLudwig wrote:
Quote:
How, and more importantly, when (relative to SDL's window + renderer creation calls), are you adding the UIKit controls?


_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Re: Using SDL2 with iOS native objects
DLudwig


Joined: 09 Feb 2012
Posts: 179
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


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

-- David L.
leadro


Joined: 27 Dec 2012
Posts: 9
DLudwig wrote:
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;
}


Hello,
thank you for your answer,
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

DLudwig wrote:
UIKit is very much capable of drawing images. 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.

I know that the UIKit a very powerful for drawing images. But I have a very specific task and UIKit can't do some image processing[/img]
DLudwig


Joined: 09 Feb 2012
Posts: 179
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


Joined: 27 Dec 2012
Posts: 9
DLudwig wrote:
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;
}


I'm already included <SDL_syswm.h>, but nothing changes...
May be anybody have any idea, what is happening?
leadro


Joined: 27 Dec 2012
Posts: 9
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).
DLudwig


Joined: 09 Feb 2012
Posts: 179
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.
leadro


Joined: 27 Dec 2012
Posts: 9
DLudwig wrote:
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.


Hello,

I'm also try to use base variant of SDL from hg.libsdl.org. And I cloned it 2-3 hours ago and build it, but I steel have a problem with initializing SDL_SysWMinfo member Sad
Maybe it's problem with project...

What I did:
1. Create in the Xcode "Single View Application";
2. Add to this project libs: libSDL2.a and libSDL2main.a; and headers: All SDL headers after building;
3. Into method -(void)viewDidLoad I add code for initialization SDL:
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);

4. Then try to add native UIKit controls.

Maybe I'm do something wrong..... Most likely I'm doing something wrong, but I don't know what...
Can you help me?

Thank you.