cscg22-gearboy

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

SDL_uikitopengles.m (6692B)


      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_UIKIT
     24
     25#include "SDL_uikitopengles.h"
     26#include "SDL_uikitopenglview.h"
     27#include "SDL_uikitappdelegate.h"
     28#include "SDL_uikitmodes.h"
     29#include "SDL_uikitwindow.h"
     30#include "../SDL_sysvideo.h"
     31#include "../../events/SDL_keyboard_c.h"
     32#include "../../events/SDL_mouse_c.h"
     33#include "../../power/uikit/SDL_syspower.h"
     34#include "SDL_loadso.h"
     35#include <dlfcn.h>
     36
     37static int UIKit_GL_Initialize(_THIS);
     38
     39void *
     40UIKit_GL_GetProcAddress(_THIS, const char *proc)
     41{
     42    /* Look through all SO's for the proc symbol.  Here's why:
     43       -Looking for the path to the OpenGL Library seems not to work in the iPhone Simulator.
     44       -We don't know that the path won't change in the future.
     45    */
     46    return dlsym(RTLD_DEFAULT, proc);
     47}
     48
     49/*
     50    note that SDL_GL_Delete context makes it current without passing the window
     51*/
     52int UIKit_GL_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context)
     53{
     54    [EAGLContext setCurrentContext: context];
     55    return 0;
     56}
     57
     58int
     59UIKit_GL_LoadLibrary(_THIS, const char *path)
     60{
     61    /*
     62        shouldn't be passing a path into this function
     63        why?  Because we've already loaded the library
     64        and because the SDK forbids loading an external SO
     65    */
     66    if (path != NULL) {
     67        return SDL_SetError("iPhone GL Load Library just here for compatibility");
     68    }
     69    return 0;
     70}
     71
     72void UIKit_GL_SwapWindow(_THIS, SDL_Window * window)
     73{
     74#if SDL_POWER_UIKIT
     75    /* Check once a frame to see if we should turn off the battery monitor. */
     76    SDL_UIKit_UpdateBatteryMonitoring();
     77#endif
     78
     79    SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
     80
     81    if (nil == data->view) {
     82        return;
     83    }
     84    [data->view swapBuffers];
     85
     86    /* You need to pump events in order for the OS to make changes visible.
     87       We don't pump events here because we don't want iOS application events
     88       (low memory, terminate, etc.) to happen inside low level rendering.
     89     */
     90}
     91
     92SDL_GLContext UIKit_GL_CreateContext(_THIS, SDL_Window * window)
     93{
     94    SDL_uikitopenglview *view;
     95    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
     96    SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
     97    SDL_DisplayData *displaydata = display->driverdata;
     98    SDL_DisplayModeData *displaymodedata = display->current_mode.driverdata;
     99    UIWindow *uiwindow = data->uiwindow;
    100    EAGLSharegroup *share_group = nil;
    101
    102    if (_this->gl_config.share_with_current_context) {
    103        SDL_uikitopenglview *view = (SDL_uikitopenglview *) SDL_GL_GetCurrentContext();
    104        share_group = [view.context sharegroup];
    105    }
    106
    107    /* construct our view, passing in SDL's OpenGL configuration data */
    108    CGRect frame;
    109    if (window->flags & (SDL_WINDOW_FULLSCREEN|SDL_WINDOW_BORDERLESS)) {
    110        frame = [displaydata->uiscreen bounds];
    111    } else {
    112        frame = [displaydata->uiscreen applicationFrame];
    113    }
    114    view = [[SDL_uikitopenglview alloc] initWithFrame: frame
    115                                    scale: displaymodedata->scale
    116                                    retainBacking: _this->gl_config.retained_backing
    117                                    rBits: _this->gl_config.red_size
    118                                    gBits: _this->gl_config.green_size
    119                                    bBits: _this->gl_config.blue_size
    120                                    aBits: _this->gl_config.alpha_size
    121                                    depthBits: _this->gl_config.depth_size
    122                                    stencilBits: _this->gl_config.stencil_size
    123                                    majorVersion: _this->gl_config.major_version
    124                                    shareGroup: share_group];
    125    if (!view) {
    126        return NULL;
    127    }
    128
    129    data->view = view;
    130    view->viewcontroller = data->viewcontroller;
    131    if (view->viewcontroller != nil) {
    132        [view->viewcontroller setView:view];
    133        [view->viewcontroller retain];
    134    }
    135    [uiwindow addSubview: view];
    136
    137    /* The view controller needs to be the root in order to control rotation on iOS 6.0 */
    138    if (uiwindow.rootViewController == nil) {
    139        uiwindow.rootViewController = view->viewcontroller;
    140    }
    141
    142    EAGLContext *context = view.context;
    143    if (UIKit_GL_MakeCurrent(_this, window, context) < 0) {
    144        UIKit_GL_DeleteContext(_this, context);
    145        return NULL;
    146    }
    147
    148    /* Make this window the current mouse focus for touch input */
    149    if (displaydata->uiscreen == [UIScreen mainScreen]) {
    150        SDL_SetMouseFocus(window);
    151        SDL_SetKeyboardFocus(window);
    152    }
    153
    154    return context;
    155}
    156
    157void UIKit_GL_DeleteContext(_THIS, SDL_GLContext context)
    158{
    159    SDL_Window *window;
    160
    161    /* Find the view associated with this context */
    162    for (window = _this->windows; window; window = window->next) {
    163        SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
    164        SDL_uikitopenglview *view = data->view;
    165        if (view.context == context) {
    166            /* the delegate has retained the view, this will release him */
    167            if (view->viewcontroller) {
    168                UIWindow *uiwindow = (UIWindow *)view.superview;
    169                if (uiwindow.rootViewController == view->viewcontroller) {
    170                    uiwindow.rootViewController = nil;
    171                }
    172                [view->viewcontroller setView:nil];
    173                [view->viewcontroller release];
    174            }
    175            [view removeFromSuperview];
    176
    177            /* FIXME: This doesn't actually call view dealloc - what is holding a reference to it? */
    178            [view release];
    179            return;
    180        }
    181    }
    182
    183    /* View not found... delete the context anyway? */
    184    [(EAGLContext *)context release];
    185}
    186
    187#endif /* SDL_VIDEO_DRIVER_UIKIT */
    188
    189/* vi: set ts=4 sw=4 expandtab: */