sgl

changeset 31:124195562f7e

FUCKING AUTORELEASE POOL
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 03 Jul 2011 04:33:32 +0300
parents 6242b03e7191
children fc2dba4c5a5f
files README src/log.c src/wsys.c src/wsys_cocoa.m tests/simple/simple.c
diffstat 5 files changed, 92 insertions(+), 17 deletions(-) [+]
line diff
     1.1 --- a/README	Tue Jun 28 13:52:02 2011 +0300
     1.2 +++ b/README	Sun Jul 03 04:33:32 2011 +0300
     1.3 @@ -12,3 +12,9 @@
     1.4  
     1.5  Finally the module source code must start by including "config.h", and the rest
     1.6  of it from that point on must be in a big ifdef USE_WSYS_MODULE_WHATEVER block.
     1.7 +
     1.8 +Building on windows
     1.9 +-------------------
    1.10 +If you decide to use the visual studio project files to build sgl, you still
    1.11 +need to run the configure script in order to generate config.h and modules.c.
    1.12 +Msys or cygwin should do the trick.
     2.1 --- a/src/log.c	Tue Jun 28 13:52:02 2011 +0300
     2.2 +++ b/src/log.c	Sun Jul 03 04:33:32 2011 +0300
     2.3 @@ -1,15 +1,27 @@
     2.4  #include <stdio.h>
     2.5  #include <stdlib.h>
     2.6 +#include <string.h>
     2.7 +#include <errno.h>
     2.8  #include <stdarg.h>
     2.9  #include "log.h"
    2.10  
    2.11  void sgl_log(const char *fmt, ...)
    2.12  {
    2.13 +	static int first_run = 1;
    2.14  	va_list ap;
    2.15  	const char *logfile;
    2.16  	FILE *fp;
    2.17  
    2.18 -	if(!(logfile = getenv("SGL_LOG")) || !(fp = fopen(logfile, "a"))) {
    2.19 +	if((logfile = getenv("SGL_LOG"))) {
    2.20 +		if(first_run) {
    2.21 +			remove(logfile);
    2.22 +			first_run = 0;
    2.23 +		}
    2.24 +		if(!(fp = fopen(logfile, "a"))) {
    2.25 +			fprintf(stderr, "failed to open logfile: %s: %s\n", logfile, strerror(errno));
    2.26 +			fp = stderr;
    2.27 +		}
    2.28 +	} else {
    2.29  		fp = stderr;
    2.30  	}
    2.31  
     3.1 --- a/src/wsys.c	Tue Jun 28 13:52:02 2011 +0300
     3.2 +++ b/src/wsys.c	Sun Jul 03 04:33:32 2011 +0300
     3.3 @@ -2,6 +2,7 @@
     3.4  #include <stdlib.h>
     3.5  #include <string.h>
     3.6  #include "wsys.h"
     3.7 +#include "log.h"
     3.8  
     3.9  static struct wsys_module *merge(struct wsys_module *list1, struct wsys_module *list2);
    3.10  static struct wsys_module *msort(struct wsys_module *list, int count);
    3.11 @@ -27,9 +28,9 @@
    3.12  {
    3.13  	struct wsys_module *ws = wslist;
    3.14  
    3.15 -	printf("window system modules:\n");
    3.16 +	sgl_log("window system modules:\n");
    3.17  	while(ws) {
    3.18 -		printf("- %s\n", ws->name);
    3.19 +		sgl_log("- %s\n", ws->name);
    3.20  		ws = ws->next;
    3.21  	}
    3.22  }
     4.1 --- a/src/wsys_cocoa.m	Tue Jun 28 13:52:02 2011 +0300
     4.2 +++ b/src/wsys_cocoa.m	Sun Jul 03 04:33:32 2011 +0300
     4.3 @@ -8,6 +8,9 @@
     4.4  #import <Cocoa/Cocoa.h>
     4.5  #include "sgl.h"
     4.6  #include "wsys.h"
     4.7 +#include "log.h"
     4.8 +
     4.9 +#define APPLE_SUCKS()	sgl_log("%s called\n", __func__)
    4.10  
    4.11  @interface OpenGLView : NSOpenGLView
    4.12  {
    4.13 @@ -38,8 +41,18 @@
    4.14  {
    4.15  	int foo;
    4.16  }
    4.17 +
    4.18 +-(void) applicationWillFinishLaunching: (NSNotification*) notification;
    4.19 +-(void) applicationDidFinishLaunching: (NSNotification*) notification;
    4.20 +
    4.21 +-(void) windowDidExpose: (NSNotification*) notification;
    4.22 +-(void) windowDidResize: (NSNotification*) notification;
    4.23 +
    4.24  -(BOOL) applicationShouldTerminate: (NSApplication*) app;
    4.25 --(BOOL) applicationShouldTerminateAfterLastWindowClosed: (NSApplication*)app;
    4.26 +-(BOOL) applicationShouldTerminateAfterLastWindowClosed: (NSApplication*) app;
    4.27 +-(void) applicationWillTerminate: (NSNotification*) notification;
    4.28 +-(BOOL) windowShouldClose: (id) win;
    4.29 +-(void) windowWillClose: (NSNotification*) notification;
    4.30  @end
    4.31  
    4.32  
    4.33 @@ -103,6 +116,7 @@
    4.34  static struct window *winlist, *active_win;
    4.35  static int quit_main_loop;
    4.36  
    4.37 +static NSAutoreleasePool *global_pool;
    4.38  
    4.39  void sgl_register_cocoa(void)
    4.40  {
    4.41 @@ -114,12 +128,14 @@
    4.42  
    4.43  -(id) initWithFrame: (NSRect) frame pixelFormat: (NSOpenGLPixelFormat*) pf
    4.44  {
    4.45 +	APPLE_SUCKS();
    4.46  	self = [super initWithFrame: frame pixelFormat: pf];
    4.47  	return self;
    4.48  }
    4.49  
    4.50  -(void) drawRect: (NSRect) rect
    4.51  {
    4.52 +	APPLE_SUCKS();
    4.53  	sgl_display_callback_t func = sgl_get_callback(SGL_DISPLAY);
    4.54  	if(func) {
    4.55  		func();
    4.56 @@ -128,6 +144,7 @@
    4.57  
    4.58  -(void) reshape
    4.59  {
    4.60 +	APPLE_SUCKS();
    4.61  	NSSize sz;
    4.62  	sgl_reshape_callback_t func;
    4.63  
    4.64 @@ -143,41 +160,69 @@
    4.65  
    4.66  -(BOOL) acceptsFirstResponder
    4.67  {
    4.68 +	APPLE_SUCKS();
    4.69  	return YES;
    4.70  }
    4.71  @end
    4.72  
    4.73  @implementation AppDelegate
    4.74 +-(void) applicationWillFinishLaunching: (NSNotification*) notification
    4.75 +{
    4.76 +	APPLE_SUCKS();
    4.77 +}
    4.78 +
    4.79 +-(void) applicationDidFinishLaunching: (NSNotification*) notification
    4.80 +{
    4.81 +	APPLE_SUCKS();
    4.82 +}
    4.83 +
    4.84 +-(void) windowDidExpose: (NSNotification*) notification
    4.85 +{
    4.86 +	APPLE_SUCKS();
    4.87 +}
    4.88 +
    4.89 +-(void) windowDidResize: (NSNotification*) notification
    4.90 +{
    4.91 +	APPLE_SUCKS();
    4.92 +}
    4.93 +
    4.94  -(BOOL) applicationShouldTerminate: (NSApplication*) app
    4.95  {
    4.96 +	APPLE_SUCKS();
    4.97  	return NSTerminateNow;
    4.98  }
    4.99  
   4.100  -(BOOL) applicationShouldTerminateAfterLastWindowClosed: (NSApplication*) app
   4.101  {
   4.102 +	APPLE_SUCKS();
   4.103  	return YES;
   4.104  }
   4.105  
   4.106  -(void) applicationWillTerminate: (NSNotification*) notification
   4.107  {
   4.108 -	printf("applicationWillTerminate\n");
   4.109 +	APPLE_SUCKS();
   4.110  }
   4.111  
   4.112  -(BOOL) windowShouldClose: (id) win
   4.113  {
   4.114 +	APPLE_SUCKS();
   4.115  	return YES;
   4.116  }
   4.117  
   4.118  -(void) windowWillClose: (NSNotification*) notification
   4.119  {
   4.120 -	[NSApp terminate: nil];
   4.121 +	APPLE_SUCKS();
   4.122 +	/*[NSApp terminate: nil];*/
   4.123  }
   4.124  @end
   4.125  
   4.126  static int init(void)
   4.127  {
   4.128 +	APPLE_SUCKS();
   4.129  	AppDelegate *delegate;
   4.130  
   4.131 +	global_pool = [[NSAutoreleasePool alloc] init];
   4.132 +
   4.133  	[NSApplication sharedApplication];
   4.134  
   4.135  	delegate = [[AppDelegate alloc] init];
   4.136 @@ -187,15 +232,14 @@
   4.137  
   4.138  static void shutdown(void)
   4.139  {
   4.140 +	APPLE_SUCKS();
   4.141  	while(winlist) {
   4.142 -		struct window *win = winlist;
   4.143 -		winlist = winlist->next;
   4.144 -
   4.145 -		/* TODO destroy window */
   4.146 -		free(win);
   4.147 +		close_window(winlist->wid);
   4.148  	}
   4.149  
   4.150  	quit_main_loop = 1;
   4.151 +	[NSApp setDelegate: nil];
   4.152 +	[global_pool drain];
   4.153  }
   4.154  
   4.155  
   4.156 @@ -214,6 +258,7 @@
   4.157  /* create/destroy windows */
   4.158  static int create_window(int xsz, int ysz, unsigned int flags)
   4.159  {
   4.160 +	APPLE_SUCKS();
   4.161  	NSAutoreleasePool *pool;
   4.162  	NSWindow *nswin;
   4.163  	NSRect rect;
   4.164 @@ -271,6 +316,7 @@
   4.165  
   4.166  static void close_window(int wid)
   4.167  {
   4.168 +	APPLE_SUCKS();
   4.169  	struct window *win, *prev, dummy;
   4.170  	sgl_close_callback_t close_func;
   4.171  
   4.172 @@ -284,17 +330,25 @@
   4.173  			}
   4.174  			[win->win close];
   4.175  
   4.176 +			prev->next = win->next;
   4.177 +
   4.178 +			if(!dummy.next) {
   4.179 +				winlist = 0;
   4.180 +			}
   4.181 +
   4.182  			if(active_win == win) {
   4.183  				activate_window(winlist);
   4.184  			}
   4.185 -
   4.186 -			prev->next = win->next;
   4.187  			free(win);
   4.188 -			return;
   4.189 +			break;
   4.190  		}
   4.191  		prev = win;
   4.192  		win = win->next;
   4.193  	}
   4.194 +
   4.195 +	if(!winlist) {
   4.196 +		shutdown();
   4.197 +	}
   4.198  }
   4.199  
   4.200  
   4.201 @@ -407,6 +461,8 @@
   4.202  
   4.203  	[runloop release];
   4.204  	[pool drain];
   4.205 +
   4.206 +	/*sgl_log("%s returning: %d\n", __func__, quit_main_loop ? -1 : 0);*/
   4.207  	return quit_main_loop ? -1 : 0;
   4.208  }
   4.209  
     5.1 --- a/tests/simple/simple.c	Tue Jun 28 13:52:02 2011 +0300
     5.2 +++ b/tests/simple/simple.c	Sun Jul 03 04:33:32 2011 +0300
     5.3 @@ -39,7 +39,7 @@
     5.4  
     5.5  void disp(void)
     5.6  {
     5.7 -	printf("redisplay\n");
     5.8 +	sgl_log("redisplay\n");
     5.9  
    5.10  	glClearColor(0.2, 0.2, 0.2, 1);
    5.11  	glClear(GL_COLOR_BUFFER_BIT);
    5.12 @@ -69,9 +69,9 @@
    5.13  	char *ststr = state ? "pressed" : "released";
    5.14  
    5.15  	if(key < 0xff && isprint(key)) {
    5.16 -		printf("keyboard: '%c' %s\n", (char)key, ststr);
    5.17 +		sgl_log("keyboard: '%c' %s\n", (char)key, ststr);
    5.18  	} else {
    5.19 -		printf("keyboard: %x %s\n", key, ststr);
    5.20 +		sgl_log("keyboard: %x %s\n", key, ststr);
    5.21  	}
    5.22  
    5.23  	if(key == 27) {