nuclear@39: /* SimplyGL window system module for iOS */ nuclear@39: /* mac-framework: -framework UIKit */ nuclear@39: nuclear@39: #ifdef USE_WSYS_MODULE_IOS nuclear@39: nuclear@36: #include nuclear@36: #import nuclear@37: #import nuclear@36: #include nuclear@36: #include nuclear@40: #include "sgl.h" nuclear@40: #include "wsys.h" nuclear@36: nuclear@37: @interface SGLView : UIView { nuclear@36: @private nuclear@36: BOOL active; nuclear@36: BOOL use_disp_link; nuclear@36: NSInteger frame_interval; nuclear@36: id disp_link; nuclear@36: NSTimer *anim_timer; nuclear@36: EAGLContext *ctx; nuclear@36: unsigned int fbo, rbuf_color, rbuf_depth; nuclear@36: } nuclear@36: nuclear@37: @property (readonly, nonatomic, getter=isAnimating) BOOL active; nuclear@37: @property (nonatomic) NSInteger frame_interval; nuclear@37: nuclear@36: -(void)start_anim; nuclear@36: -(void)stop_anim; nuclear@37: -(void)drawView: (id)sender; nuclear@36: @end nuclear@36: nuclear@36: @interface SGLDelegate : NSObject { nuclear@36: UIWindow *win; nuclear@37: SGLView *view; nuclear@36: } nuclear@37: nuclear@40: nuclear@37: @property (nonatomic, retain) IBOutlet UIWindow *win; nuclear@37: @property (nonatomic, retain) IBOutlet SGLView *view; nuclear@36: @end nuclear@36: nuclear@36: nuclear@36: nuclear@39: static int init(void); nuclear@39: static void shutdown(void); nuclear@39: nuclear@39: /* video mode switching */ nuclear@39: static int set_vidmode(int xsz, int ysz); nuclear@39: static int get_vidmode(int *xsz, int *ysz); nuclear@39: nuclear@39: /* create/destroy windows */ nuclear@39: static int create_window(int xsz, int ysz, unsigned int flags); nuclear@39: static void close_window(int wid); nuclear@39: nuclear@39: /* window management */ nuclear@39: static int set_active(int wid); nuclear@39: static int set_title(const char *str); nuclear@39: static void redisplay(void); nuclear@39: static void swap_buffers(void); nuclear@39: nuclear@39: static int get_modifiers(void); nuclear@39: nuclear@39: /* event handling and friends */ nuclear@39: static void set_event(int idx, int enable); nuclear@39: static int process_events(void); nuclear@39: nuclear@39: nuclear@39: static struct wsys_module ws = { nuclear@39: "ios", 0, nuclear@39: init, nuclear@39: shutdown, nuclear@39: set_vidmode, nuclear@39: get_vidmode, nuclear@39: create_window, nuclear@39: close_window, nuclear@39: set_active, nuclear@39: set_title, nuclear@39: redisplay, nuclear@39: swap_buffers, nuclear@39: get_modifiers, nuclear@39: set_event, nuclear@39: process_events, nuclear@39: 0 nuclear@39: }; nuclear@39: nuclear@39: nuclear@39: void sgl_register_ios(void) nuclear@39: { nuclear@39: sgl_register_module(&ws); nuclear@39: } nuclear@39: nuclear@39: nuclear@37: @implementation SGLView nuclear@36: nuclear@37: @synthesize active; nuclear@36: @dynamic frame_interval; nuclear@36: nuclear@36: +(Class)layerClass nuclear@36: { nuclear@36: return [CAEAGLLayer class]; nuclear@36: } nuclear@36: nuclear@39: /*The EAGL view is stored in the nib file. When it's unarchived it's sent -initWithCoder: */ nuclear@36: -(id)initWithCoder: (NSCoder*)coder nuclear@36: { nuclear@36: if((self = [super initWithCoder: coder])) { nuclear@39: /* Get the layer */ nuclear@36: CAEAGLLayer *layer = (CAEAGLLayer*)self.layer; nuclear@36: nuclear@39: /*self.contentScaleFactor = 2.0; */ nuclear@36: nuclear@36: layer.opaque = TRUE; nuclear@36: layer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys: nuclear@36: [NSNumber numberWithBool: FALSE], nuclear@36: kEAGLDrawablePropertyRetainedBacking, kEAGLColorFormatRGBA8, nuclear@36: kEAGLDrawablePropertyColorFormat, nuclear@36: nil]; nuclear@36: nuclear@36: ctx = [[EAGLContext alloc] initWithAPI: kEAGLRenderingAPIOpenGLES2]; nuclear@36: if(!ctx || ![EAGLContext setCurrentContext: ctx]) { nuclear@36: [self release]; nuclear@36: return nil; nuclear@36: } nuclear@36: nuclear@39: /* initialize fbos etc... */ nuclear@36: glGenFramebuffers(1, &fbo); nuclear@36: glBindFramebuffer(GL_FRAMEBUFFER, fbo); nuclear@36: nuclear@36: glGenRenderbuffers(1, &rbuf_color); nuclear@36: glBindRenderbuffer(GL_RENDERBUFFER, rbuf_color); nuclear@36: glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, rbuf_color); nuclear@36: nuclear@36: glGenRenderbuffers(1, &rbuf_depth); nuclear@36: glBindRenderbuffer(GL_RENDERBUFFER, rbuf_depth); nuclear@36: glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rbuf_depth); nuclear@36: assert(glGetError() == GL_NO_ERROR); nuclear@36: nuclear@37: active = FALSE; nuclear@36: use_disp_link = FALSE; nuclear@36: frame_interval = 1; nuclear@36: disp_link = nil; nuclear@36: anim_timer = nil; nuclear@36: nuclear@39: /* A system version of 3.1 or greater is required to use CADisplayLink. The NSTimer */ nuclear@39: /* class is used as fallback when it isn't available. */ nuclear@36: NSString *req_ver = @"3.1"; nuclear@36: NSString *cur_ver = [[UIDevice currentDevice] systemVersion]; nuclear@37: if([cur_ver compare: req_ver options: NSNumericSearch] != NSOrderedAscending) { nuclear@36: use_disp_link = TRUE; nuclear@36: } nuclear@36: nuclear@39: /*self.multipleTouchEnabled = 1; */ nuclear@36: nuclear@39: /* TODO call user init ? */ nuclear@36: } nuclear@36: nuclear@36: return self; nuclear@36: } nuclear@36: nuclear@36: -(void)drawView: (id)sender nuclear@36: { nuclear@39: /* TODO call display */ nuclear@36: } nuclear@36: nuclear@36: -(void)layoutSubviews nuclear@36: { nuclear@39: /* TODO call reshape */ nuclear@39: /* XXX originally call to renderer resizeFromLayer */ nuclear@39: /* XXX originally call to [self drawView: nil] */ nuclear@36: } nuclear@36: nuclear@36: -(void)setAnimationFrameInterval: (NSInteger)interval nuclear@36: { nuclear@36: /* Frame interval defines how many display frames must pass nuclear@36: * between each time the display link fires. The display nuclear@36: * link will only fire 30 times a second when the frame nuclear@36: * internal is two on a display that refreshes 60 times a nuclear@36: * second. The default frame interval setting of one will nuclear@36: * fire 60 times a second when the display refreshes at 60 nuclear@36: * times a second. A frame interval setting of less than nuclear@36: * one results in undefined behavior. nuclear@36: */ nuclear@36: if(interval >= 1) { nuclear@36: frame_interval = frame_interval; nuclear@36: nuclear@36: if(active) nuclear@36: { nuclear@37: [self stop_anim]; nuclear@37: [self start_anim]; nuclear@36: } nuclear@36: } nuclear@36: } nuclear@36: nuclear@37: -(void)start_anim nuclear@36: { nuclear@36: if(!active) { nuclear@36: if(use_disp_link) { nuclear@39: /* CADisplayLink is API new to iPhone SDK 3.1. Compiling against earlier versions will result in a warning, but can be dismissed nuclear@39: * if the system version runtime check for CADisplayLink exists in -initWithCoder:. The runtime check ensures this code will nuclear@39: * not be called in system versions earlier than 3.1. nuclear@39: */ nuclear@36: nuclear@36: disp_link = [NSClassFromString(@"CADisplayLink") displayLinkWithTarget: self nuclear@36: selector: @selector(drawView:)]; nuclear@37: [disp_link setFrameInterval: frame_interval]; nuclear@37: [disp_link addToRunLoop: [NSRunLoop currentRunLoop] forMode: NSDefaultRunLoopMode]; nuclear@36: } else { nuclear@36: anim_timer = [NSTimer scheduledTimerWithTimeInterval: (NSTimeInterval)((1.0 / 60.0) * frame_interval) nuclear@36: target: self selector: @selector(drawView:) userInfo: nil nuclear@36: repeats: TRUE]; nuclear@36: } nuclear@36: nuclear@36: active = TRUE; nuclear@36: } nuclear@36: } nuclear@36: nuclear@37: - (void)stop_anim nuclear@36: { nuclear@36: if(active) { nuclear@36: if(use_disp_link) { nuclear@36: [disp_link invalidate]; nuclear@36: disp_link = nil; nuclear@36: } else { nuclear@36: [anim_timer invalidate]; nuclear@36: anim_timer = nil; nuclear@36: } nuclear@36: nuclear@36: active = FALSE; nuclear@36: } nuclear@36: } nuclear@36: nuclear@36: -(void)dealloc nuclear@36: { nuclear@39: /* XXX originally [renderer release]; */ nuclear@36: [super dealloc]; nuclear@36: } nuclear@36: @end nuclear@36: nuclear@36: @implementation SGLDelegate nuclear@36: nuclear@36: @synthesize win; nuclear@36: @synthesize view; nuclear@36: nuclear@36: -(BOOL)application: (UIApplication*)app didFinishLaunchingWithOptions: (NSDictionary*)opt nuclear@36: { nuclear@37: [view start_anim]; nuclear@36: return YES; nuclear@36: } nuclear@36: nuclear@36: -(void)applicationWillResignActive: (UIApplication*)app nuclear@36: { nuclear@37: [view stop_anim]; nuclear@36: } nuclear@36: nuclear@36: -(void)applicationDidBecomeActive: (UIApplication*)app nuclear@36: { nuclear@37: [view start_anim]; nuclear@36: } nuclear@36: nuclear@36: -(void)applicationWillTerminate: (UIApplication*)app nuclear@36: { nuclear@37: [view stop_anim]; nuclear@36: } nuclear@36: nuclear@36: -(void)dealloc nuclear@36: { nuclear@36: [win release]; nuclear@36: [view release]; nuclear@36: [super dealloc]; nuclear@36: } nuclear@36: @end nuclear@39: nuclear@39: nuclear@39: nuclear@39: static int init(void) nuclear@39: { nuclear@39: return -1; nuclear@39: } nuclear@39: nuclear@39: static void shutdown(void) nuclear@39: { nuclear@39: } nuclear@39: nuclear@39: /* video mode switching */ nuclear@39: static int set_vidmode(int xsz, int ysz) nuclear@39: { nuclear@39: return -1; nuclear@39: } nuclear@39: nuclear@39: static int get_vidmode(int *xsz, int *ysz) nuclear@39: { nuclear@39: return -1; nuclear@39: } nuclear@39: nuclear@39: /* create/destroy windows */ nuclear@39: static int create_window(int xsz, int ysz, unsigned int flags) nuclear@39: { nuclear@39: return -1; nuclear@39: } nuclear@39: nuclear@39: static void close_window(int wid) nuclear@39: { nuclear@39: } nuclear@39: nuclear@39: /* window management */ nuclear@39: static int set_active(int wid) nuclear@39: { nuclear@39: return -1; nuclear@39: } nuclear@39: nuclear@39: static int set_title(const char *str) nuclear@39: { nuclear@39: return -1; nuclear@39: } nuclear@39: nuclear@39: static void redisplay(void) nuclear@39: { nuclear@39: } nuclear@39: nuclear@39: static void swap_buffers(void) nuclear@39: { nuclear@39: } nuclear@39: nuclear@39: static int get_modifiers(void) nuclear@39: { nuclear@39: return 0; nuclear@39: } nuclear@39: nuclear@39: /* event handling and friends */ nuclear@39: static void set_event(int idx, int enable) nuclear@39: { nuclear@39: } nuclear@39: nuclear@39: static int process_events(void) nuclear@39: { nuclear@40: char *argv[] = {"foobar", 0}; nuclear@40: NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; nuclear@40: nuclear@40: int res = UIApplicationMain(1, argv, 0, @"SGLDelegate"); nuclear@40: nuclear@40: [pool release]; nuclear@40: return res; nuclear@39: } nuclear@39: nuclear@39: nuclear@39: nuclear@39: nuclear@39: nuclear@39: /* only compile the following if we're building in xcode and we nuclear@39: * don't have generated modules.c file nuclear@39: */ nuclear@39: #ifdef XCODE_BUILD nuclear@39: @interface sgl : NSObject nuclear@39: /* XXX no need for init yet... */ nuclear@39: @end nuclear@39: nuclear@39: @implementation sgl nuclear@39: nuclear@39: - (id)init nuclear@39: { nuclear@39: self = [super init]; nuclear@39: if(self) { nuclear@39: /* XXX Initialization code here. */ nuclear@39: } nuclear@39: return self; nuclear@39: } nuclear@39: nuclear@39: @end nuclear@39: nuclear@39: void sgl_modules_init(void) nuclear@39: { nuclear@40: sgl_register_ios(); nuclear@39: } nuclear@39: nuclear@39: #endif /* XCODE_BUILD */ nuclear@39: nuclear@39: nuclear@39: #else nuclear@39: int sgl_wsys_ios_silence_the_fucking_empty_file_warnings; nuclear@39: #endif /* USE_WSYS_MODULE_IOS */