Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

do not call loop from displaylink callback and pause displaylink befo… #1

Merged
merged 1 commit into from
Nov 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/SDL_system.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ extern DECLSPEC int SDLCALL SDL_LinuxSetThreadPriority(Sint64 threadID, int prio
*/
extern DECLSPEC int SDLCALL SDL_iPhoneSetAnimationCallback(SDL_Window * window, int interval, void (*callback)(void*), void *callbackParam);

extern DECLSPEC void SDLCALL SDL_iPhoneRunOnMainLoop(void (*callback)(void));

#define SDL_iOSSetEventPump(enabled) SDL_iPhoneSetEventPump(enabled)

/**
Expand Down
3 changes: 3 additions & 0 deletions include/SDL_video.h
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,9 @@ typedef enum

/* Function prototypes */

extern DECLSPEC void SDLCALL SDL_PauseDisplayLink(void);
extern DECLSPEC void SDLCALL SDL_ResumeDisplayLink(void);

/**
* Get the number of video drivers compiled into SDL.
*
Expand Down
3 changes: 3 additions & 0 deletions src/video/SDL_sysvideo.h
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,9 @@ struct SDL_VideoDevice
void (*ShowScreenKeyboard) (_THIS, SDL_Window *window);
void (*HideScreenKeyboard) (_THIS, SDL_Window *window);
SDL_bool (*IsScreenKeyboardShown) (_THIS, SDL_Window *window);

void (*PauseDisplayLink)(_THIS, SDL_Window *window);
void (*ResumeDisplayLink)(_THIS, SDL_Window *window);

/* Clipboard */
int (*SetClipboardText) (_THIS, const char *text);
Expand Down
20 changes: 20 additions & 0 deletions src/video/SDL_video.c
Original file line number Diff line number Diff line change
Expand Up @@ -4043,6 +4043,24 @@ SDL_StartTextInput(void)
}
}

void SDL_PauseDisplayLink(void) {
SDL_Window *window;
window = SDL_GetFocusWindow();

if (window && _this && _this->PauseDisplayLink) {
_this->PauseDisplayLink(_this, window);
}
}

void SDL_ResumeDisplayLink(void) {
SDL_Window *window;
window = SDL_GetFocusWindow();

if (window && _this && _this->ResumeDisplayLink) {
_this->ResumeDisplayLink(_this, window);
}
}

SDL_bool
SDL_IsTextInputActive(void)
{
Expand Down Expand Up @@ -4213,11 +4231,13 @@ SDL_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid)
}
#endif
#if SDL_VIDEO_DRIVER_UIKIT
SDL_PauseDisplayLink();
if (retval == -1 &&
SDL_MessageboxValidForDriver(messageboxdata, SDL_SYSWM_UIKIT) &&
UIKit_ShowMessageBox(messageboxdata, buttonid) == 0) {
retval = 0;
}
SDL_ResumeDisplayLink();
#endif
#if SDL_VIDEO_DRIVER_X11
if (retval == -1 &&
Expand Down
3 changes: 3 additions & 0 deletions src/video/uikit/SDL_uikitvideo.m
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ static void UIKit_DeleteDevice(SDL_VideoDevice * device)
device->IsScreenKeyboardShown = UIKit_IsScreenKeyboardShown;
device->SetTextInputRect = UIKit_SetTextInputRect;
#endif

device->PauseDisplayLink = UIKit_PauseDisplayLink;
device->ResumeDisplayLink = UIKit_ResumeDisplayLink;

device->SetClipboardText = UIKit_SetClipboardText;
device->GetClipboardText = UIKit_GetClipboardText;
Expand Down
5 changes: 5 additions & 0 deletions src/video/uikit/SDL_uikitviewcontroller.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@

- (void)startAnimation;
- (void)stopAnimation;
- (void)pauseAnimation;
- (void)resumeAnimation;

- (void)doLoop:(CADisplayLink*)sender;

Expand Down Expand Up @@ -89,3 +91,6 @@ void UIKit_HideScreenKeyboard(_THIS, SDL_Window *window);
SDL_bool UIKit_IsScreenKeyboardShown(_THIS, SDL_Window *window);
void UIKit_SetTextInputRect(_THIS, SDL_Rect *rect);
#endif

void UIKit_PauseDisplayLink(_THIS, SDL_Window *window);
void UIKit_ResumeDisplayLink(_THIS, SDL_Window *window);
24 changes: 24 additions & 0 deletions src/video/uikit/SDL_uikitviewcontroller.m
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,16 @@ - (void)stopAnimation
displayLink = nil;
}

- (void)pauseAnimation
{
[displayLink removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
}

- (void)resumeAnimation
{
[displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
}

- (void)doLoop:(CADisplayLink*)sender
{
/* Don't run the game loop while a messagebox is up */
Expand Down Expand Up @@ -547,6 +557,20 @@ - (BOOL)textFieldShouldReturn:(UITextField*)_textField
return data.viewcontroller;
}

void UIKit_PauseDisplayLink(_THIS, SDL_Window *window) {
@autoreleasepool {
SDL_uikitviewcontroller *vc = GetWindowViewController(window);
[vc pauseAnimation];
}
}

void UIKit_ResumeDisplayLink(_THIS, SDL_Window *window) {
@autoreleasepool {
SDL_uikitviewcontroller *vc = GetWindowViewController(window);
[vc resumeAnimation];
}
}

SDL_bool
UIKit_HasScreenKeyboardSupport(_THIS)
{
Expand Down
7 changes: 7 additions & 0 deletions src/video/uikit/SDL_uikitwindow.m
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,13 @@ - (void)layoutSubviews

return 0;
}

void SDL_iPhoneRunOnMainLoop(void (*callback)(void))
{
dispatch_async(dispatch_get_main_queue(), ^{
callback();
});
}

#endif /* SDL_VIDEO_DRIVER_UIKIT */

Expand Down
7 changes: 5 additions & 2 deletions test/testsprite2.c
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,10 @@ loop()
next_fps_check = now + fps_check_delay;
frames = 0;
}
}

void LoopWrapper() {
SDL_iPhoneRunOnMainLoop(loop);
}

int
Expand Down Expand Up @@ -659,7 +662,7 @@ main(int argc, char *argv[])
{
// Set up the game to run in the window animation callback on iOS
// so that Game Center and so forth works correctly.
SDL_iPhoneSetAnimationCallback(state->windows[0], 1, loop, NULL);
SDL_iPhoneSetAnimationCallback(state->windows[0], 1, LoopWrapper, NULL);
//while (!done) {
//loop();
//SDL_iPhoneSetAnimationCallback(state->windows[0], 1, loop, NULL);
Expand All @@ -669,10 +672,10 @@ main(int argc, char *argv[])
while (!done) {
loop();
}
quit(0);
}
#endif

//quit(0);
return 0;
}

Expand Down