sgl

diff src/wsys_cocoa.m @ 15:a16b34ac3f2a

bah
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 17 May 2011 11:21:09 +0300
parents 5b8fb89fe63f
children 01a576351090 ee7b3a898b6b
line diff
     1.1 --- a/src/wsys_cocoa.m	Mon May 16 23:09:53 2011 +0300
     1.2 +++ b/src/wsys_cocoa.m	Tue May 17 11:21:09 2011 +0300
     1.3 @@ -6,12 +6,14 @@
     1.4  
     1.5  #import <Cocoa/Cocoa.h>
     1.6  #include "sgl.h"
     1.7 +#include "wsys.h"
     1.8  
     1.9  @interface OpenGLView : NSOpenGLView
    1.10  {
    1.11 +	int foo;
    1.12  }
    1.13  
    1.14 -//-(id) initWithFrame: (NSRect) frameRect;
    1.15 +/*-(id) initWithFrame: (NSRect) frameRect;*/
    1.16  
    1.17  -(void) drawRect: (NSRect) rect;
    1.18  -(void) reshape;
    1.19 @@ -32,8 +34,11 @@
    1.20  
    1.21  struct window {
    1.22  	int wid;
    1.23 +	int width, height;
    1.24  	NSWindow *win;
    1.25  	OpenGLView *view;
    1.26 +	NSOpenGLContext *ctx;
    1.27 +	int needs_redisplay;
    1.28  	struct window *next;
    1.29  };
    1.30  
    1.31 @@ -89,7 +94,7 @@
    1.32  
    1.33  void sgl_register_cocoa(void)
    1.34  {
    1.35 -	sgl_register_cocoa(&ws);
    1.36 +	sgl_register_module(&ws);
    1.37  }
    1.38  
    1.39  
    1.40 @@ -105,9 +110,16 @@
    1.41  
    1.42  -(void) reshape
    1.43  {
    1.44 -	sgl_reshape_callback_t func = sgl_get_callback(SGL_RESHAPE);
    1.45 -	if(func) {
    1.46 -		func(x, y);
    1.47 +	NSSize sz;
    1.48 +	sgl_reshape_callback_t func;
    1.49 +
    1.50 +	sz = [self bounds].size;
    1.51 +
    1.52 +	if((func = sgl_get_callback(SGL_RESHAPE)) && (sz.width != active_win->width ||
    1.53 +				sz.height != active_win->height)) {
    1.54 +		active_win->width = sz.width;
    1.55 +		active_win->height = sz.height;
    1.56 +		func(sz.width, sz.height);
    1.57  	}
    1.58  }
    1.59  
    1.60 @@ -120,6 +132,7 @@
    1.61  static int init(void)
    1.62  {
    1.63  	app = [NSApplication sharedApplication];
    1.64 +	return 0;
    1.65  }
    1.66  
    1.67  static void shutdown(void)
    1.68 @@ -151,7 +164,7 @@
    1.69  {
    1.70  	NSWindow *nswin;
    1.71  	NSRect rect;
    1.72 -	NSView *view;
    1.73 +	OpenGLView *view;
    1.74  	unsigned int style;
    1.75  	struct window *win;
    1.76  	static int next_id = 1;
    1.77 @@ -176,10 +189,15 @@
    1.78  
    1.79  	win->win = nswin;
    1.80  	win->view = view;
    1.81 +	win->ctx = [view openGLContext];
    1.82  	win->wid = next_id++;
    1.83 +	win->needs_redisplay = 1;
    1.84  	win->next = winlist;
    1.85  	winlist = win;
    1.86  
    1.87 +	if(!active_win) {
    1.88 +		activate_window(win);
    1.89 +	}
    1.90  	return win->wid;
    1.91  }
    1.92  
    1.93 @@ -215,6 +233,8 @@
    1.94  /* window management */
    1.95  static int set_active(int wid)
    1.96  {
    1.97 +	struct window *win = find_window(wid);
    1.98 +	return activate_window(win);
    1.99  }
   1.100  
   1.101  static struct window *find_window(int wid)
   1.102 @@ -222,7 +242,7 @@
   1.103  	struct window *win = winlist;
   1.104  
   1.105  	while(win) {
   1.106 -		if(win->win == id) {
   1.107 +		if(win->wid == wid) {
   1.108  			return win;
   1.109  		}
   1.110  		win = win->next;
   1.111 @@ -247,16 +267,17 @@
   1.112  	nsstr = [[NSString alloc] initWithCString: str encoding: NSASCIIStringEncoding];
   1.113  	[active_win->win setTitle: nsstr];
   1.114  	[nsstr release];
   1.115 +	return 0;
   1.116  }
   1.117  
   1.118  static void redisplay(void)
   1.119  {
   1.120 -	[active_win->view setNeedsRedisplay: YES];
   1.121 +	active_win->needs_redisplay = 1;
   1.122  }
   1.123  
   1.124  static void swap_buffers(void)
   1.125  {
   1.126 -	[active_win->flushBuffer];
   1.127 +	[active_win->ctx flushBuffer];
   1.128  }
   1.129  
   1.130  
   1.131 @@ -274,11 +295,26 @@
   1.132  static int process_events(void)
   1.133  {
   1.134  	NSAutoreleasePool *pool;
   1.135 -	int state;
   1.136  	sgl_idle_callback_t idle;
   1.137 +	sgl_display_callback_t disp;
   1.138 +	struct window *win;
   1.139 +	int res = 0;
   1.140  
   1.141  	pool = [[NSAutoreleasePool alloc] init];
   1.142  
   1.143 +	idle = sgl_get_callback(SGL_IDLE);
   1.144 +	disp = sgl_get_callback(SGL_DISPLAY);
   1.145 +
   1.146 +	win = winlist;
   1.147 +	while(win) {
   1.148 +		if(win->needs_redisplay && disp) {
   1.149 +			activate_window(win);
   1.150 +			disp();
   1.151 +			win->needs_redisplay = 0;
   1.152 +		}
   1.153 +		win = win->next;
   1.154 +	}
   1.155 +
   1.156  	for(;;) {
   1.157  		NSEvent *ev = [app nextEventMatchingMask: NSAnyEventMask
   1.158  			untilDate: [NSDate distantPast] inMode: NSDefaultRunLoopMode dequeue: YES];
   1.159 @@ -286,8 +322,15 @@
   1.160  			break;
   1.161  		}
   1.162  
   1.163 -		handle_event(ev);
   1.164 +		res = handle_event(ev);
   1.165  	}
   1.166 +
   1.167 +	if(idle) {
   1.168 +		idle();
   1.169 +	}
   1.170 +
   1.171 +	[pool drain];
   1.172 +	return 0;
   1.173  }
   1.174  
   1.175  static int handle_event(NSEvent *ev)
   1.176 @@ -295,7 +338,13 @@
   1.177  	switch([ev type]) {
   1.178  	case NSKeyDown:
   1.179  	case NSKeyUp:
   1.180 +		printf("key pressed\n");
   1.181 +		break;
   1.182  
   1.183 +	default:
   1.184 +		break;
   1.185 +	}
   1.186 +	return 0;
   1.187  }
   1.188  
   1.189  #endif	/* USE_WSYS_MODULE_COCOA */