How to access EAGLView/UIView functions from C?
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?
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?
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.
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
@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.
Thanks Frank, this is the bit I didn't know how to do
AppDelegate *ad = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[[ad glView] EAGLUpdateScreen];
AppDelegate *ad = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[[ad glView] EAGLUpdateScreen];
I have it coded but am getting an error when calling cEAGLUpdateScreen
I am getting the error:
I assume I need to put something like this in GLSpriteAppDelegate?
Code:
@class EAGLView;
@interface GLSpriteAppDelegate : NSObject <UIApplicationDelegate> {
IBOutlet UIWindow *window;
IBOutlet EAGLView *glView;
}
@property (nonatomic, retain) UIWindow *window;
@endI 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;
after adding the property code it is still giving me the -[GLSpriteAppDelegate glView]: unrecognized selector sent to instance error
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.
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];
Not really sure then - did you @synthesize glView; in GLSpriteAppDelegate.m or create a getter method?
Yeah I didn't see the synthesize, that worked
Thanks for your help. It looks like inframe updates work on the simulator and the actual device.
Thanks for your help. It looks like inframe updates work on the simulator and the actual device.
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| UIView opaque blending problems | Joecoolage | 0 | 3,042 |
Aug 2, 2010 12:58 AM Last Post: Joecoolage |
|
| UIView Subclass not displaying controls... | flipflop | 7 | 5,901 |
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,397 |
Apr 27, 2010 03:24 PM Last Post: Toontingy |
|
| Switching to another EAGLView help. | jeonghyunhan | 2 | 2,433 |
Jul 6, 2009 11:37 PM Last Post: AnotherJake |
|
| Direct screen surface access for iPhone | Nosredna | 15 | 6,667 |
Feb 15, 2009 10:35 AM Last Post: Nosredna |
|

