How to access EAGLView/UIView functions from C?

Member
Posts: 81
Joined: 2007.07
Post: #1
I need to call a UIView function to update the screen from a C function, for example in the GLSprite example I need to have a global function like this:

void EAGLUpdateScreen()
{
glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
[context presentRenderbuffer:GL_RENDERBUFFER_OES];
[EAGLContext setCurrentContext:context];
glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
}

This will allow me to update the screen inside of a frame (I use for progress bars while loading). The problem is I don't know Objective C and don't know how to access UIView functions from a global C function. Can I 'get a pointer' to the UIView class and call the function?
Quote this message in a reply
Moderator
Posts: 1,562
Joined: 2003.10
Post: #2
A few indirect answers to your question:
  • It generally works to call a C function from non-Objective-C code that calls Objective-C methods on the inside. You just have to be sure you're not including a header that defines an @interface or anything like that from a non-ObjC file.
  • It sounds like you're trying to update the screen during a synchronous, blocking loading function. It's generally inadvisable to block the main thread for long periods of time without yielding to the run loop. To avoid this, you could restructure your code to load asynchronously, either by breaking up the loading function so it only does a little bit at a time, or loading on a secondary thread while the main thread updates the display.
  • If you're programming for the iPhone, you really ought to have at least rudimentary working knowledge of Objective-C. It only takes an afternoon or two to learn it to a reasonable level of proficiency.
Quote this message in a reply
Member
Posts: 446
Joined: 2002.09
Post: #3
ThemsAllTook Wrote:If you're programming for the iPhone, you really ought to have at least rudimentary working knowledge of Objective-C. It only takes an afternoon or two to learn it to a reasonable level of proficiency.
Well, there's a difference between knowing Cocoa and wanting to use it for your game code Wink

@Rasterman: I use a lot of Cocoa->C wrappers in my iPhone framework, so I'll give you an example, but ThemsAllTook's points are still valid and you really do need to know Cocoa even if you're just wrapping it into C APIs...

Step #1: Take that void EAGLUpdateScreen() function you got and make it an instance method in EAGLView.m - so something like this:

Code:
- (void)EAGLUpdateScreen
{
  glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
  [context presentRenderbuffer:GL_RENDERBUFFER_OES];
  [EAGLContext setCurrentContext:context];
  glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
}

You'll need to also add the function prototype for that in EAGLView.h, and I'll also note that you may not need to do all that binding unless you're really switching contexts elsewhere.

Step #2: Create a new C header to include into your C files - lets call it "ceagl.h" and stick in the function prototype (use header guards as needed):
Code:
void cEAGLUpdateScreen(void);

Step #3: Create a a "ceagl.m" file to go along with that header. It'll look something like this:
Code:
#import "AppDelegate.h"
#import "EAGLView.h"

void
cEAGLUpdateScreen(void)
{
  AppDelegate *ad = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  [[ad glView] EAGLUpdateScreen];    
}

This assumes your app delegate is called "AppDelegate" and that it exposes a public EAGLView called "glView".

Step #4: There is no step 4 - just #include "ceagl.h" in any C files that need this functionality and call cEAGLUpdateScreen() as needed.

P.S. Anyone who didn't chuckle at "ceagl" is dead inside.
Quote this message in a reply
Member
Posts: 81
Joined: 2007.07
Post: #4
Thanks Frank, this is the bit I didn't know how to do

AppDelegate *ad = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[[ad glView] EAGLUpdateScreen];
Quote this message in a reply
Member
Posts: 81
Joined: 2007.07
Post: #5
I have it coded but am getting an error when calling cEAGLUpdateScreen

Code:
@class EAGLView;
@interface GLSpriteAppDelegate : NSObject <UIApplicationDelegate> {
    IBOutlet UIWindow *window;
    IBOutlet EAGLView *glView;
}
@property (nonatomic, retain) UIWindow *window;
@end

I am getting the error:
Quote:-[GLSpriteAppDelegate glView]: unrecognized selector sent to instance

I assume I need to put something like this in GLSpriteAppDelegate?
Code:
@property (nonatomic, readwrite) EAGLView *glView;
Quote this message in a reply
Member
Posts: 446
Joined: 2002.09
Post: #6
Yes - though you probably want (nonatomic, retain) attributes.
Quote this message in a reply
Member
Posts: 81
Joined: 2007.07
Post: #7
after adding the property code it is still giving me the -[GLSpriteAppDelegate glView]: unrecognized selector sent to instance error
Quote this message in a reply
Member
Posts: 446
Joined: 2002.09
Post: #8
Hopefully you aren't taking the code I posted literally - i.e. if your app delegate is named "GLSpriteAppDelegate" then you need to import its header and cast to "GLSpriteAppDelegate", not "AppDelegate" as written.
Quote this message in a reply
Member
Posts: 81
Joined: 2007.07
Post: #9
If it were "AppDelegate" I doubt it would even compile, here is the call I'm using

Code:
GLSpriteAppDelegate *ad = (GLSpriteAppDelegate *)[[UIApplication sharedApplication] delegate];
[[ad glView] updateScreen];
Quote this message in a reply
Member
Posts: 446
Joined: 2002.09
Post: #10
Not really sure then - did you @synthesize glView; in GLSpriteAppDelegate.m or create a getter method?
Quote this message in a reply
Member
Posts: 81
Joined: 2007.07
Post: #11
Yeah I didn't see the synthesize, that worked Smile Thanks for your help. It looks like inframe updates work on the simulator and the actual device.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  UIView opaque blending problems Joecoolage 0 3,744 Aug 2, 2010 12:58 AM
Last Post: Joecoolage
  UIView Subclass not displaying controls... flipflop 7 7,023 May 13, 2010 01:16 PM
Last Post: flipflop
  How do I get access to files that are created in the simulator? Toontingy 2 2,829 Apr 27, 2010 03:24 PM
Last Post: Toontingy
  Switching to another EAGLView help. jeonghyunhan 2 2,849 Jul 6, 2009 11:37 PM
Last Post: AnotherJake
  Direct screen surface access for iPhone Nosredna 15 7,387 Feb 15, 2009 10:35 AM
Last Post: Nosredna