From 22ada2ea34b703c58a57b3e1a93c925a0ae20ccc Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Fri, 22 Jun 2012 19:19:18 -0400 Subject: [PATCH] Created a header file for system dependent API functions, and added SDL_iPhoneSetAnimationCallback() --- README.iOS | 2 +- include/SDL.h | 1 + .../SDL_system.h | 24 +++++++++++-- include/SDL_touch.h | 2 +- src/video/uikit/SDL_uikitappdelegate.m | 2 ++ src/video/uikit/SDL_uikitopenglview.h | 14 ++++++++ src/video/uikit/SDL_uikitopenglview.m | 35 +++++++++++++++++++ src/video/uikit/SDL_uikitview.m | 1 - src/video/uikit/SDL_uikitwindow.m | 14 ++++++++ 9 files changed, 89 insertions(+), 6 deletions(-) rename src/video/uikit/SDL_uikitkeyboard.h => include/SDL_system.h (78%) mode change 100755 => 100644 diff --git a/README.iOS b/README.iOS index 4903f6e4d..cf5eb677a 100644 --- a/README.iOS +++ b/README.iOS @@ -78,7 +78,7 @@ Finally, if your application completely redraws the screen each frame, you may f Notes -- Keyboard ============================================================================== -SDL for iPhone contains several additional functions related to keyboard visibility. These functions are not part of the SDL standard API, but are necessary for revealing and hiding the iPhone's virtual onscreen keyboard. You can use them in your own applications by including a copy of the SDL_uikitkeyboard.h header (located in src/video/uikit) in your project. +SDL for iPhone contains several additional functions related to keyboard visibility. These functions are not part of the SDL standard API, but are necessary for revealing and hiding the iPhone's virtual onscreen keyboard. int SDL_iPhoneKeyboardShow(SDL_Window * window) -- reveals the onscreen keyboard. Returns 0 on success and -1 on error. diff --git a/include/SDL.h b/include/SDL.h index 6b584b7bd..aabbabaa3 100644 --- a/include/SDL.h +++ b/include/SDL.h @@ -86,6 +86,7 @@ #include "SDL_power.h" #include "SDL_render.h" #include "SDL_rwops.h" +#include "SDL_system.h" #include "SDL_thread.h" #include "SDL_timer.h" #include "SDL_version.h" diff --git a/src/video/uikit/SDL_uikitkeyboard.h b/include/SDL_system.h old mode 100755 new mode 100644 similarity index 78% rename from src/video/uikit/SDL_uikitkeyboard.h rename to include/SDL_system.h index 5ed736173..0ff5ced56 --- a/src/video/uikit/SDL_uikitkeyboard.h +++ b/include/SDL_system.h @@ -19,9 +19,18 @@ 3. This notice may not be removed or altered from any source distribution. */ -#ifndef sdl_uikitkeyboard_h -#define sdl_uikitkeyboard_h +/** + * \file SDL_system.h + * + * Include file for platform specific SDL API functions + */ +#ifndef _SDL_system_h +#define _SDL_system_h + +#include "SDL_stdinc.h" + +#include "begin_code.h" /* Set up for C function definitions, even when using C++ */ #ifdef __cplusplus /* *INDENT-OFF* */ @@ -29,18 +38,27 @@ extern "C" { /* *INDENT-ON* */ #endif +#if __IPHONEOS__ + +#include "SDL_video.h" + +extern DECLSPEC int SDLCALL SDL_iPhoneSetAnimationCallback(SDL_Window * window, int interval, void (*callback)(void*), void *callbackParam); + extern DECLSPEC int SDLCALL SDL_iPhoneKeyboardShow(SDL_Window * window); extern DECLSPEC int SDLCALL SDL_iPhoneKeyboardHide(SDL_Window * window); extern DECLSPEC SDL_bool SDLCALL SDL_iPhoneKeyboardIsShown(SDL_Window * window); extern DECLSPEC int SDLCALL SDL_iPhoneKeyboardToggle(SDL_Window * window); +#endif + /* Ends C function definitions when using C++ */ #ifdef __cplusplus /* *INDENT-OFF* */ } /* *INDENT-ON* */ #endif +#include "close_code.h" -#endif /* sdl_uikitkeyboard_h */ +#endif /* _SDL_system_h */ /* vi: set ts=4 sw=4 expandtab: */ diff --git a/include/SDL_touch.h b/include/SDL_touch.h index 727f82af8..6fc0c03cc 100644 --- a/include/SDL_touch.h +++ b/include/SDL_touch.h @@ -119,6 +119,6 @@ struct SDL_Touch { #endif #include "close_code.h" -#endif /* _SDL_mouse_h */ +#endif /* _SDL_touch_h */ /* vi: set ts=4 sw=4 expandtab: */ diff --git a/src/video/uikit/SDL_uikitappdelegate.m b/src/video/uikit/SDL_uikitappdelegate.m index 9c5440220..f0998ec8b 100755 --- a/src/video/uikit/SDL_uikitappdelegate.m +++ b/src/video/uikit/SDL_uikitappdelegate.m @@ -108,6 +108,8 @@ - (void)postFinishLaunch exit_status = SDL_main(forward_argc, forward_argv); /* exit, passing the return status from the user's application */ + // We don't actually exit to support applications that do setup in + // their main function and then allow the Cocoa event loop to run. // exit(exit_status); } diff --git a/src/video/uikit/SDL_uikitopenglview.h b/src/video/uikit/SDL_uikitopenglview.h index 8c1ba3c84..255ff6a4a 100755 --- a/src/video/uikit/SDL_uikitopenglview.h +++ b/src/video/uikit/SDL_uikitopenglview.h @@ -46,6 +46,11 @@ /* format of depthRenderbuffer */ GLenum depthBufferFormat; + + id displayLink; + int animationInterval; + void (*animationCallback)(void*); + void *animationCallbackParam; } @property (nonatomic, retain, readonly) EAGLContext *context; @@ -66,6 +71,15 @@ - (void)updateFrame; +- (void)setAnimationCallback:(int)interval + callback:(void (*)(void*))callback + callbackParam:(void*)callbackParam; + +- (void)startAnimation; +- (void)stopAnimation; + +- (void)doLoop:(id)sender; + @end /* vi: set ts=4 sw=4 expandtab: */ diff --git a/src/video/uikit/SDL_uikitopenglview.m b/src/video/uikit/SDL_uikitopenglview.m index d38b26c9b..2d17031a6 100755 --- a/src/video/uikit/SDL_uikitopenglview.m +++ b/src/video/uikit/SDL_uikitopenglview.m @@ -147,6 +147,41 @@ - (void)updateFrame } } +- (void)setAnimationCallback:(int)interval + callback:(void (*)(void*))callback + callbackParam:(void*)callbackParam +{ + [self stopAnimation]; + + animationInterval = interval; + animationCallback = callback; + animationCallbackParam = callbackParam; + + if (animationCallback) + [self startAnimation]; +} + +- (void)startAnimation +{ + // CADisplayLink is API new to iPhone SDK 3.1. Compiling against earlier versions will result in a warning, but can be dismissed + // if the system version runtime check for CADisplayLink exists in -initWithCoder:. + + displayLink = [NSClassFromString(@"CADisplayLink") displayLinkWithTarget:self selector:@selector(doLoop:)]; + [displayLink setFrameInterval:animationInterval]; + [displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; +} + +- (void)stopAnimation +{ + [displayLink invalidate]; + displayLink = nil; +} + +- (void)doLoop:(id)sender +{ + animationCallback(animationCallbackParam); +} + - (void)setCurrentContext { [EAGLContext setCurrentContext:context]; diff --git a/src/video/uikit/SDL_uikitview.m b/src/video/uikit/SDL_uikitview.m index d15aaaa33..b6c4cf867 100755 --- a/src/video/uikit/SDL_uikitview.m +++ b/src/video/uikit/SDL_uikitview.m @@ -31,7 +31,6 @@ #if SDL_IPHONE_KEYBOARD #import "keyinfotable.h" #import "SDL_uikitappdelegate.h" -#import "SDL_uikitkeyboard.h" #import "SDL_uikitwindow.h" #endif diff --git a/src/video/uikit/SDL_uikitwindow.m b/src/video/uikit/SDL_uikitwindow.m index 60037b518..632494a79 100755 --- a/src/video/uikit/SDL_uikitwindow.m +++ b/src/video/uikit/SDL_uikitwindow.m @@ -279,6 +279,20 @@ static int SetupWindowData(_THIS, SDL_Window *window, UIWindow *uiwindow, SDL_bo } } +int +SDL_iPhoneSetAnimationCallback(SDL_Window * window, int interval, void (*callback)(void*), void *callbackParam) +{ + SDL_WindowData *data = window ? (SDL_WindowData *)window->driverdata : NULL; + + if (!data || !data->view) { + SDL_SetError("Invalid window or view not set"); + return -1; + } + + [data->view setAnimationCallback:interval callback:callback callbackParam:callbackParam]; + return 0; +} + #endif /* SDL_VIDEO_DRIVER_UIKIT */ /* vi: set ts=4 sw=4 expandtab: */