Mixing OpenGL & UIKit and UIView drawing...

Posts: 10
Joined: 2009.01
Post: #1
Hi everyone,

I'm still working on my game and I have a little performance issue with mixing OpenGL & UIKit stuff.

I know it's not recommended by Apple but for various, pretty good, reasons (see at the bottom), I'd rather keep mixing them both.

I have a screenshot there so you can get an idea of what I'm trying to do:

As you can sort-of-see, there is 2D stuffs (background, status bar, cards) and "3D" (weapons, particules, hit anim) stuffs.

The layout is currently like that:

UIView Container
UIImageView: BG (opaque)
Various UIViews: cards, status bar etc.. (opaque)
OpenGL View: particules, weapons etc.. (transparent)

Unfortunatly, as expected, having a transparent OpenGL view above all others is a performance killer (like 10-20 times slower).

My guess is that I could get better performance (meaning, more acceptables) if I could layout this way:

UIView Container
OpenGLView (opaque)
Draw BG in GL
Draw Various UIViews
Draw particules etc.. on top.

I must stress that I cannot just draw textures instead of the various UIViews as they are updated during the animation, and they are quite composited.

So I tried to add the UIViews as childs of my GLView and override the drawRect: method of the GLView to something like that:

- (void) drawRect:(CGRect)rect
  // Draw BG
  [self drawBGTexture];

  // Draw UI
  [super drawRect: rect];

  // Draw Effects
  [self drawGLEffects];

Unfortunatly, the method doesn't seems to be called at all, and when I call it myself in my loop, the UIViews are still on top of the GL contents.

The reason for which I dont want to go full GL are:

- I'm switching from various states using UIViews and CoreAnimation and going full openGL on them would be very very hard and unconvenient. The "battle" part is very important, but also very "special" regarding the rest of the game.
- The cards and toolbars are composites UIViews that are used in the other states (and nicely setup using IB).
- I really dont want to composite twice my cards and all' (for UIView and for GL), it's a maintenance nightmare.

Can anyone help me there? Or maybe suggest a solution. I would really appreciate it.


Quote this message in a reply
Posts: 1,066
Joined: 2004.07
Post: #2
How about using Quartz and Core Animation for your weapons and particles? They're not as fast as OpenGL ES in and of themselves, but they might work better when overlayed on top of your scene.

I've only been able to successfully render OpenGL ES content below UIKit content, so unfortunately I can't offer any tips or tricks for you. Good luck on the adventure though.
Quote this message in a reply
Posts: 10
Joined: 2009.01
Post: #3
I started there.. but then I saw a lot of people saying (here) that open GL is the way to go...
I could go back there.. but I'd like to be sure it's better.. because there would be a lot of code to redo.
Quote this message in a reply
Posts: 446
Joined: 2002.09
Post: #4
Fraggle Wrote:I started there.. but then I saw a lot of people saying (here) that open GL is the way to go...
If you want maximum framerates then OpenGL is the way to go - just take Apple's advice and avoid mixing it with Quartz, UIKit, or Core Animation.
Quote this message in a reply
Posts: 283
Joined: 2006.05
Post: #5
You can draw a UIView to an OpenGL texture, which means you wouldn't have to stop using Interface Builder. I doubt you'd want to do that every frame though, which means you'd still have to recreate any Core Animation effects you want.
Quote this message in a reply
Posts: 10
Joined: 2009.01
Post: #6
Ah.. it's getting interresting Smile
Any idea how I can draw the UIVIew as a texture?
I dont mind doing it from time to time and/or recreate the CA effects.
Quote this message in a reply
Posts: 283
Joined: 2006.05
Post: #7
I haven't tried this, but it looks like it should work. Found linked from here with no additional info.

#import <QuartzCore/QuartzCore.h>

[myView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();

That should give you a UIImage which I assume you could load as a texture using the Texture2D class.
Quote this message in a reply
Posts: 8
Joined: 2008.12
Post: #8
Not exactly what you were asking for but GamesFromWithin blog has an interesting article about sharing images data from UIKit to OpenGL ES and vice versa.

Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  OpenGL Cube drawing: VBOs, instanced geometry, octrees Symbol$ 5 13,396 Dec 23, 2011 01:43 PM
Last Post: Skorche
  UIView opaque blending problems Joecoolage 0 4,737 Aug 2, 2010 12:58 AM
Last Post: Joecoolage
  UIView Subclass not displaying controls... flipflop 7 9,931 May 13, 2010 01:16 PM
Last Post: flipflop
  How to access EAGLView/UIView functions from C? Rasterman 10 9,857 Apr 8, 2010 10:47 AM
Last Post: Rasterman
  Drawing menu versus drawing game johncmurphy 4 6,294 Jan 6, 2010 09:14 AM
Last Post: AnotherJake