cscg22-gearboy

CSCG 2022 Challenge 'Gearboy'
git clone https://git.sinitax.com/sinitax/cscg22-gearboy
Log | Files | Refs | sfeed.txt

SDL_androidevents.c (4118B)


      1/*
      2  Simple DirectMedia Layer
      3  Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
      4
      5  This software is provided 'as-is', without any express or implied
      6  warranty.  In no event will the authors be held liable for any damages
      7  arising from the use of this software.
      8
      9  Permission is granted to anyone to use this software for any purpose,
     10  including commercial applications, and to alter it and redistribute it
     11  freely, subject to the following restrictions:
     12
     13  1. The origin of this software must not be misrepresented; you must not
     14     claim that you wrote the original software. If you use this software
     15     in a product, an acknowledgment in the product documentation would be
     16     appreciated but is not required.
     17  2. Altered source versions must be plainly marked as such, and must not be
     18     misrepresented as being the original software.
     19  3. This notice may not be removed or altered from any source distribution.
     20*/
     21#include "../../SDL_internal.h"
     22
     23#if SDL_VIDEO_DRIVER_ANDROID
     24
     25/* We're going to do this by default */
     26#define SDL_ANDROID_BLOCK_ON_PAUSE  1
     27
     28#include "SDL_androidevents.h"
     29#include "SDL_events.h"
     30#include "SDL_androidwindow.h"
     31
     32
     33void android_egl_context_backup();
     34void android_egl_context_restore();
     35void AndroidAUD_ResumeDevices(void);
     36void AndroidAUD_PauseDevices(void);
     37
     38void 
     39android_egl_context_restore() 
     40{
     41    SDL_Event event;
     42    SDL_WindowData *data = (SDL_WindowData *) Android_Window->driverdata;
     43    if (SDL_GL_MakeCurrent(Android_Window, (SDL_GLContext) data->egl_context) < 0) {
     44        /* The context is no longer valid, create a new one */
     45        data->egl_context = (EGLContext) SDL_GL_CreateContext(Android_Window);
     46        SDL_GL_MakeCurrent(Android_Window, (SDL_GLContext) data->egl_context);
     47        event.type = SDL_RENDER_DEVICE_RESET;
     48        SDL_PushEvent(&event);
     49    }
     50}
     51
     52void 
     53android_egl_context_backup() 
     54{
     55    /* Keep a copy of the EGL Context so we can try to restore it when we resume */
     56    SDL_WindowData *data = (SDL_WindowData *) Android_Window->driverdata;
     57    data->egl_context = SDL_GL_GetCurrentContext();
     58    /* We need to do this so the EGLSurface can be freed */
     59    SDL_GL_MakeCurrent(Android_Window, NULL);
     60}
     61
     62void
     63Android_PumpEvents(_THIS)
     64{
     65    static int isPaused = 0;
     66#if SDL_ANDROID_BLOCK_ON_PAUSE
     67    static int isPausing = 0;
     68#endif
     69    /* No polling necessary */
     70
     71    /*
     72     * Android_ResumeSem and Android_PauseSem are signaled from Java_org_libsdl_app_SDLActivity_nativePause and Java_org_libsdl_app_SDLActivity_nativeResume
     73     * When the pause semaphore is signaled, if SDL_ANDROID_BLOCK_ON_PAUSE is defined the event loop will block until the resume signal is emitted.
     74     */
     75
     76#if SDL_ANDROID_BLOCK_ON_PAUSE
     77    if (isPaused && !isPausing) {
     78        /* Make sure this is the last thing we do before pausing */
     79        android_egl_context_backup();
     80        AndroidAUD_PauseDevices();
     81        if(SDL_SemWait(Android_ResumeSem) == 0) {
     82#else
     83    if (isPaused) {
     84        if(SDL_SemTryWait(Android_ResumeSem) == 0) {
     85#endif
     86            isPaused = 0;
     87            AndroidAUD_ResumeDevices();
     88            /* Restore the GL Context from here, as this operation is thread dependent */
     89            if (!SDL_HasEvent(SDL_QUIT)) {
     90                android_egl_context_restore();
     91            }
     92        }
     93    }
     94    else {
     95#if SDL_ANDROID_BLOCK_ON_PAUSE
     96        if( isPausing || SDL_SemTryWait(Android_PauseSem) == 0 ) {
     97            /* We've been signaled to pause, but before we block ourselves, 
     98            we need to make sure that certain key events have reached the app */
     99            if (SDL_HasEvent(SDL_WINDOWEVENT) || SDL_HasEvent(SDL_APP_WILLENTERBACKGROUND) || SDL_HasEvent(SDL_APP_DIDENTERBACKGROUND) ) {
    100                isPausing = 1;
    101            }
    102            else {
    103                isPausing = 0;
    104                isPaused = 1;
    105            }
    106        }
    107#else
    108        if(SDL_SemTryWait(Android_PauseSem) == 0) {
    109            android_egl_context_backup();
    110            AndroidAUD_PauseDevices();
    111            isPaused = 1;
    112        }
    113#endif
    114    }
    115}
    116
    117#endif /* SDL_VIDEO_DRIVER_ANDROID */
    118
    119/* vi: set ts=4 sw=4 expandtab: */