Skip to content

Commit

Permalink
Misc. iOS code improvements.
Browse files Browse the repository at this point in the history
- Use @autoreleasepool instead of NSAutoReleasePool.

- Code style fixups.
  • Loading branch information
slime73 committed Jul 14, 2014
1 parent 96b613e commit 734b523
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 116 deletions.
3 changes: 3 additions & 0 deletions .hgignore
Expand Up @@ -8,6 +8,9 @@ Makefile
sdl-config
SDL2.spec
build
Build
*xcuserdata*
*xcworkspacedata*

# for Xcode
*.orig
Expand Down
12 changes: 10 additions & 2 deletions include/SDL_syswm.h
Expand Up @@ -208,13 +208,21 @@ struct SDL_SysWMinfo
#if defined(SDL_VIDEO_DRIVER_COCOA)
struct
{
NSWindow *window; /* The Cocoa window */
#if defined(__OBJC__) && __has_feature(objc_arc)
NSWindow __unsafe_unretained *window; /* The Cocoa window */
#else
NSWindow *window; /* The Cocoa window */
#endif
} cocoa;
#endif
#if defined(SDL_VIDEO_DRIVER_UIKIT)
struct
{
UIWindow *window; /* The UIKit window */
#if defined(__OBJC__) && __has_feature(objc_arc)
UIWindow __unsafe_unretained *window; /* The UIKit window */
#else
UIWindow *window; /* The UIKit window */
#endif
} uikit;
#endif
#if defined(SDL_VIDEO_DRIVER_WAYLAND)
Expand Down
31 changes: 14 additions & 17 deletions src/file/cocoa/SDL_rwopsbundlesupport.m
Expand Up @@ -41,27 +41,24 @@
return fopen(file, mode);
}

NSAutoreleasePool* autorelease_pool = [[NSAutoreleasePool alloc] init];


NSFileManager* file_manager = [NSFileManager defaultManager];
NSString* resource_path = [[NSBundle mainBundle] resourcePath];

NSString* ns_string_file_component = [file_manager stringWithFileSystemRepresentation:file length:strlen(file)];

NSString* full_path_with_file_to_try = [resource_path stringByAppendingPathComponent:ns_string_file_component];
if([file_manager fileExistsAtPath:full_path_with_file_to_try]) {
fp = fopen([full_path_with_file_to_try fileSystemRepresentation], mode);
}
else {
fp = fopen(file, mode);
@autoreleasepool {
NSFileManager* file_manager = [NSFileManager defaultManager];
NSString* resource_path = [[NSBundle mainBundle] resourcePath];

NSString* ns_string_file_component = [file_manager stringWithFileSystemRepresentation:file length:strlen(file)];

NSString* full_path_with_file_to_try = [resource_path stringByAppendingPathComponent:ns_string_file_component];
if([file_manager fileExistsAtPath:full_path_with_file_to_try]) {
fp = fopen([full_path_with_file_to_try fileSystemRepresentation], mode);
}
else {
fp = fopen(file, mode);
}
}

[autorelease_pool drain];

return fp;
}

#endif /* __MACOSX__ */
#endif /* __APPLE__ */

/* vi: set ts=4 sw=4 expandtab: */
88 changes: 45 additions & 43 deletions src/filesystem/cocoa/SDL_sysfilesystem.m
Expand Up @@ -36,67 +36,69 @@
char *
SDL_GetBasePath(void)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSBundle *bundle = [NSBundle mainBundle];
const char* baseType = [[[bundle infoDictionary] objectForKey:@"SDL_FILESYSTEM_BASE_DIR_TYPE"] UTF8String];
const char *base = NULL;
char *retval = NULL;
if (baseType == NULL) {
baseType = "resource";
}
if (SDL_strcasecmp(baseType, "bundle")==0) {
base = [[bundle bundlePath] fileSystemRepresentation];
} else if (SDL_strcasecmp(baseType, "parent")==0) {
base = [[[bundle bundlePath] stringByDeletingLastPathComponent] fileSystemRepresentation];
} else {
/* this returns the exedir for non-bundled and the resourceDir for bundled apps */
base = [[bundle resourcePath] fileSystemRepresentation];
}
if (base) {
const size_t len = SDL_strlen(base) + 2;
retval = (char *) SDL_malloc(len);
if (retval == NULL) {
SDL_OutOfMemory();

@autoreleasepool {
const char *base = NULL;
NSBundle *bundle = [NSBundle mainBundle];
const char* baseType = [[[bundle infoDictionary] objectForKey:@"SDL_FILESYSTEM_BASE_DIR_TYPE"] UTF8String];
if (baseType == NULL) {
baseType = "resource";
}
if (SDL_strcasecmp(baseType, "bundle")==0) {
base = [[bundle bundlePath] fileSystemRepresentation];
} else if (SDL_strcasecmp(baseType, "parent")==0) {
base = [[[bundle bundlePath] stringByDeletingLastPathComponent] fileSystemRepresentation];
} else {
SDL_snprintf(retval, len, "%s/", base);
/* this returns the exedir for non-bundled and the resourceDir for bundled apps */
base = [[bundle resourcePath] fileSystemRepresentation];
}

if (base) {
const size_t len = SDL_strlen(base) + 2;
retval = (char *) SDL_malloc(len);
if (retval == NULL) {
SDL_OutOfMemory();
} else {
SDL_snprintf(retval, len, "%s/", base);
}
}
}

[pool release];
return retval;
}

char *
SDL_GetPrefPath(const char *org, const char *app)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSArray *array = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
char *retval = NULL;

if ([array count] > 0) { /* we only want the first item in the list. */
NSString *str = [array objectAtIndex:0];
const char *base = [str fileSystemRepresentation];
if (base) {
const size_t len = SDL_strlen(base) + SDL_strlen(org) + SDL_strlen(app) + 4;
retval = (char *) SDL_malloc(len);
if (retval == NULL) {
SDL_OutOfMemory();
} else {
char *ptr;
SDL_snprintf(retval, len, "%s/%s/%s/", base, org, app);
for (ptr = retval+1; *ptr; ptr++) {
if (*ptr == '/') {
*ptr = '\0';
mkdir(retval, 0700);
*ptr = '/';
@autoreleasepool {
NSArray *array = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);

if ([array count] > 0) { /* we only want the first item in the list. */
NSString *str = [array objectAtIndex:0];
const char *base = [str fileSystemRepresentation];
if (base) {
const size_t len = SDL_strlen(base) + SDL_strlen(org) + SDL_strlen(app) + 4;
retval = (char *) SDL_malloc(len);
if (retval == NULL) {
SDL_OutOfMemory();
} else {
char *ptr;
SDL_snprintf(retval, len, "%s/%s/%s/", base, org, app);
for (ptr = retval+1; *ptr; ptr++) {
if (*ptr == '/') {
*ptr = '\0';
mkdir(retval, 0700);
*ptr = '/';
}
}
mkdir(retval, 0700);
}
mkdir(retval, 0700);
}
}
}

[pool release];
return retval;
}

Expand Down
12 changes: 6 additions & 6 deletions src/video/uikit/SDL_uikitappdelegate.m
Expand Up @@ -44,7 +44,6 @@
int main(int argc, char **argv)
{
int i;
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

/* store arguments */
forward_argc = argc;
Expand All @@ -56,15 +55,16 @@ int main(int argc, char **argv)
forward_argv[i] = NULL;

/* Give over control to run loop, SDLUIKitDelegate will handle most things from here */
UIApplicationMain(argc, argv, NULL, [SDLUIKitDelegate getAppDelegateClassName]);
@autoreleasepool {
UIApplicationMain(argc, argv, NULL, [SDLUIKitDelegate getAppDelegateClassName]);
}

/* free the memory we used to hold copies of argc and argv */
for (i = 0; i < forward_argc; i++) {
free(forward_argv[i]);
}
free(forward_argv);

[pool release];
return exit_status;
}

Expand Down Expand Up @@ -151,8 +151,8 @@ - (void)updateSplashImage:(UIInterfaceOrientation)interfaceOrientation
} else {
image = self->splashPortrait;
}
if (image)
{

if (image) {
splash.image = image;
}
}
Expand All @@ -165,7 +165,7 @@ @implementation SDLUIKitDelegate
/* convenience method */
+ (id) sharedAppDelegate
{
/* the delegate is set in UIApplicationMain(), which is garaunteed to be called before this method */
/* the delegate is set in UIApplicationMain(), which is guaranteed to be called before this method */
return [[UIApplication sharedApplication] delegate];
}

Expand Down
3 changes: 2 additions & 1 deletion src/video/uikit/SDL_uikitevents.m
Expand Up @@ -40,8 +40,9 @@
void
UIKit_PumpEvents(_THIS)
{
if (!UIKit_EventPumpEnabled)
if (!UIKit_EventPumpEnabled) {
return;
}

/* Let the run loop run for a short amount of time: long enough for
touch events to get processed (which is important to get certain
Expand Down
50 changes: 24 additions & 26 deletions src/video/uikit/SDL_uikitmessagebox.m
Expand Up @@ -71,40 +71,38 @@ - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)
UIKit_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid)
{
int clicked;
int i;
const SDL_MessageBoxButtonData *buttons = messageboxdata->buttons;

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
@autoreleasepool {
UIAlertView* alert = [[UIAlertView alloc] init];

UIAlertView* alert = [[UIAlertView alloc] init];
alert.title = [NSString stringWithUTF8String:messageboxdata->title];
alert.message = [NSString stringWithUTF8String:messageboxdata->message];
alert.delegate = [[UIKit_UIAlertViewDelegate alloc] initWithButtonIndex:&clicked];

alert.title = [NSString stringWithUTF8String:messageboxdata->title];
alert.message = [NSString stringWithUTF8String:messageboxdata->message];
alert.delegate = [[UIKit_UIAlertViewDelegate alloc] initWithButtonIndex:&clicked];
for (i = 0; i < messageboxdata->numbuttons; ++i) {
[alert addButtonWithTitle:[[NSString alloc] initWithUTF8String:buttons[i].text]];
}

const SDL_MessageBoxButtonData *buttons = messageboxdata->buttons;
int i;
for (i = 0; i < messageboxdata->numbuttons; ++i) {
[alert addButtonWithTitle:[[NSString alloc] initWithUTF8String:buttons[i].text]];
}
/* Set up for showing the alert */
clicked = messageboxdata->numbuttons;

/* Set up for showing the alert */
clicked = messageboxdata->numbuttons;
[alert show];

[alert show];
/* Run the main event loop until the alert has finished */
/* Note that this needs to be done on the main thread */
s_showingMessageBox = SDL_TRUE;
while (clicked == messageboxdata->numbuttons) {
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
}
s_showingMessageBox = SDL_FALSE;

/* Run the main event loop until the alert has finished */
/* Note that this needs to be done on the main thread */
s_showingMessageBox = SDL_TRUE;
while (clicked == messageboxdata->numbuttons) {
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
}
s_showingMessageBox = SDL_FALSE;

*buttonid = messageboxdata->buttons[clicked].buttonid;
*buttonid = messageboxdata->buttons[clicked].buttonid;

[alert.delegate release];
[alert release];

[pool release];
[alert.delegate release];
[alert release];
}

return 0;
}
Expand Down

0 comments on commit 734b523

Please sign in to comment.