cscg22-gearboy

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

SDL_cocoavideo.m (7420B)


      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_COCOA
     24
     25#include "SDL.h"
     26#include "SDL_endian.h"
     27#include "SDL_cocoavideo.h"
     28#include "SDL_cocoashape.h"
     29#include "SDL_assert.h"
     30
     31/* Initialization/Query functions */
     32static int Cocoa_VideoInit(_THIS);
     33static void Cocoa_VideoQuit(_THIS);
     34
     35/* Cocoa driver bootstrap functions */
     36
     37static int
     38Cocoa_Available(void)
     39{
     40    return (1);
     41}
     42
     43static void
     44Cocoa_DeleteDevice(SDL_VideoDevice * device)
     45{
     46    SDL_free(device->driverdata);
     47    SDL_free(device);
     48}
     49
     50static SDL_VideoDevice *
     51Cocoa_CreateDevice(int devindex)
     52{
     53    SDL_VideoDevice *device;
     54    SDL_VideoData *data;
     55
     56    Cocoa_RegisterApp();
     57
     58    /* Initialize all variables that we clean on shutdown */
     59    device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice));
     60    if (device) {
     61        data = (struct SDL_VideoData *) SDL_calloc(1, sizeof(SDL_VideoData));
     62    } else {
     63        data = NULL;
     64    }
     65    if (!data) {
     66        SDL_OutOfMemory();
     67        SDL_free(device);
     68        return NULL;
     69    }
     70    device->driverdata = data;
     71
     72    /* Set the function pointers */
     73    device->VideoInit = Cocoa_VideoInit;
     74    device->VideoQuit = Cocoa_VideoQuit;
     75    device->GetDisplayBounds = Cocoa_GetDisplayBounds;
     76    device->GetDisplayModes = Cocoa_GetDisplayModes;
     77    device->SetDisplayMode = Cocoa_SetDisplayMode;
     78    device->PumpEvents = Cocoa_PumpEvents;
     79
     80    device->CreateWindow = Cocoa_CreateWindow;
     81    device->CreateWindowFrom = Cocoa_CreateWindowFrom;
     82    device->SetWindowTitle = Cocoa_SetWindowTitle;
     83    device->SetWindowIcon = Cocoa_SetWindowIcon;
     84    device->SetWindowPosition = Cocoa_SetWindowPosition;
     85    device->SetWindowSize = Cocoa_SetWindowSize;
     86    device->SetWindowMinimumSize = Cocoa_SetWindowMinimumSize;
     87    device->SetWindowMaximumSize = Cocoa_SetWindowMaximumSize;
     88    device->ShowWindow = Cocoa_ShowWindow;
     89    device->HideWindow = Cocoa_HideWindow;
     90    device->RaiseWindow = Cocoa_RaiseWindow;
     91    device->MaximizeWindow = Cocoa_MaximizeWindow;
     92    device->MinimizeWindow = Cocoa_MinimizeWindow;
     93    device->RestoreWindow = Cocoa_RestoreWindow;
     94    device->SetWindowBordered = Cocoa_SetWindowBordered;
     95    device->SetWindowFullscreen = Cocoa_SetWindowFullscreen;
     96    device->SetWindowGammaRamp = Cocoa_SetWindowGammaRamp;
     97    device->GetWindowGammaRamp = Cocoa_GetWindowGammaRamp;
     98    device->SetWindowGrab = Cocoa_SetWindowGrab;
     99    device->DestroyWindow = Cocoa_DestroyWindow;
    100    device->GetWindowWMInfo = Cocoa_GetWindowWMInfo;
    101    device->SetWindowHitTest = Cocoa_SetWindowHitTest;
    102
    103    device->shape_driver.CreateShaper = Cocoa_CreateShaper;
    104    device->shape_driver.SetWindowShape = Cocoa_SetWindowShape;
    105    device->shape_driver.ResizeWindowShape = Cocoa_ResizeWindowShape;
    106
    107#if SDL_VIDEO_OPENGL_CGL
    108    device->GL_LoadLibrary = Cocoa_GL_LoadLibrary;
    109    device->GL_GetProcAddress = Cocoa_GL_GetProcAddress;
    110    device->GL_UnloadLibrary = Cocoa_GL_UnloadLibrary;
    111    device->GL_CreateContext = Cocoa_GL_CreateContext;
    112    device->GL_MakeCurrent = Cocoa_GL_MakeCurrent;
    113    device->GL_GetDrawableSize = Cocoa_GL_GetDrawableSize;
    114    device->GL_SetSwapInterval = Cocoa_GL_SetSwapInterval;
    115    device->GL_GetSwapInterval = Cocoa_GL_GetSwapInterval;
    116    device->GL_SwapWindow = Cocoa_GL_SwapWindow;
    117    device->GL_DeleteContext = Cocoa_GL_DeleteContext;
    118#endif
    119
    120    device->StartTextInput = Cocoa_StartTextInput;
    121    device->StopTextInput = Cocoa_StopTextInput;
    122    device->SetTextInputRect = Cocoa_SetTextInputRect;
    123
    124    device->SetClipboardText = Cocoa_SetClipboardText;
    125    device->GetClipboardText = Cocoa_GetClipboardText;
    126    device->HasClipboardText = Cocoa_HasClipboardText;
    127
    128    device->free = Cocoa_DeleteDevice;
    129
    130    return device;
    131}
    132
    133VideoBootStrap COCOA_bootstrap = {
    134    "cocoa", "SDL Cocoa video driver",
    135    Cocoa_Available, Cocoa_CreateDevice
    136};
    137
    138
    139int
    140Cocoa_VideoInit(_THIS)
    141{
    142    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
    143
    144    Cocoa_InitModes(_this);
    145    Cocoa_InitKeyboard(_this);
    146    Cocoa_InitMouse(_this);
    147
    148    const char *hint = SDL_GetHint(SDL_HINT_VIDEO_MAC_FULLSCREEN_SPACES);
    149    data->allow_spaces = ( (floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_6) && (!hint || (*hint != '0')) );
    150
    151    return 0;
    152}
    153
    154void
    155Cocoa_VideoQuit(_THIS)
    156{
    157    Cocoa_QuitModes(_this);
    158    Cocoa_QuitKeyboard(_this);
    159    Cocoa_QuitMouse(_this);
    160}
    161
    162/* This function assumes that it's called from within an autorelease pool */
    163NSImage *
    164Cocoa_CreateImage(SDL_Surface * surface)
    165{
    166    SDL_Surface *converted;
    167    NSBitmapImageRep *imgrep;
    168    Uint8 *pixels;
    169    int i;
    170    NSImage *img;
    171
    172    converted = SDL_ConvertSurfaceFormat(surface,
    173#if SDL_BYTEORDER == SDL_BIG_ENDIAN
    174                                         SDL_PIXELFORMAT_RGBA8888,
    175#else
    176                                         SDL_PIXELFORMAT_ABGR8888,
    177#endif
    178                                         0);
    179    if (!converted) {
    180        return nil;
    181    }
    182
    183    imgrep = [[[NSBitmapImageRep alloc] initWithBitmapDataPlanes: NULL
    184                    pixelsWide: converted->w
    185                    pixelsHigh: converted->h
    186                    bitsPerSample: 8
    187                    samplesPerPixel: 4
    188                    hasAlpha: YES
    189                    isPlanar: NO
    190                    colorSpaceName: NSDeviceRGBColorSpace
    191                    bytesPerRow: converted->pitch
    192                    bitsPerPixel: converted->format->BitsPerPixel] autorelease];
    193    if (imgrep == nil) {
    194        SDL_FreeSurface(converted);
    195        return nil;
    196    }
    197
    198    /* Copy the pixels */
    199    pixels = [imgrep bitmapData];
    200    SDL_memcpy(pixels, converted->pixels, converted->h * converted->pitch);
    201    SDL_FreeSurface(converted);
    202
    203    /* Premultiply the alpha channel */
    204    for (i = (surface->h * surface->w); i--; ) {
    205        Uint8 alpha = pixels[3];
    206        pixels[0] = (Uint8)(((Uint16)pixels[0] * alpha) / 255);
    207        pixels[1] = (Uint8)(((Uint16)pixels[1] * alpha) / 255);
    208        pixels[2] = (Uint8)(((Uint16)pixels[2] * alpha) / 255);
    209        pixels += 4;
    210    }
    211
    212    img = [[[NSImage alloc] initWithSize: NSMakeSize(surface->w, surface->h)] autorelease];
    213    if (img != nil) {
    214        [img addRepresentation: imgrep];
    215    }
    216    return img;
    217}
    218
    219/*
    220 * Mac OS X log support.
    221 *
    222 * This doesn't really have aything to do with the interfaces of the SDL video
    223 *  subsystem, but we need to stuff this into an Objective-C source code file.
    224 */
    225
    226void SDL_NSLog(const char *text)
    227{
    228    NSLog(@"%s", text);
    229}
    230
    231#endif /* SDL_VIDEO_DRIVER_COCOA */
    232
    233/* vim: set ts=4 sw=4 expandtab: */