cscg22-gearboy

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

SDL_winrtopengles.cpp (3887B)


      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_WINRT && SDL_VIDEO_OPENGL_EGL
     24
     25/* EGL implementation of SDL OpenGL support */
     26
     27#include "SDL_winrtvideo_cpp.h"
     28extern "C" {
     29#include "SDL_winrtopengles.h"
     30#include "SDL_loadso.h"
     31}
     32
     33/* Windows includes */
     34#include <wrl/client.h>
     35using namespace Windows::UI::Core;
     36
     37/* ANGLE/WinRT constants */
     38static const int ANGLE_D3D_FEATURE_LEVEL_ANY = 0;
     39
     40
     41/*
     42 * SDL/EGL top-level implementation
     43 */
     44
     45extern "C" int
     46WINRT_GLES_LoadLibrary(_THIS, const char *path)
     47{
     48    SDL_VideoData *video_data = (SDL_VideoData *)_this->driverdata;
     49
     50    if (SDL_EGL_LoadLibrary(_this, path, EGL_DEFAULT_DISPLAY) != 0) {
     51        return -1;
     52    }
     53
     54    /* Load ANGLE/WinRT-specific functions */
     55    CreateWinrtEglWindow_Function CreateWinrtEglWindow = (CreateWinrtEglWindow_Function) SDL_LoadFunction(_this->egl_data->egl_dll_handle, "CreateWinrtEglWindow");
     56    if (!CreateWinrtEglWindow) {
     57        return SDL_SetError("Could not retrieve ANGLE/WinRT function CreateWinrtEglWindow");
     58    }
     59
     60    /* Create an ANGLE/WinRT EGL-window */
     61    /* TODO, WinRT: check for XAML usage before accessing the CoreWindow, as not doing so could lead to a crash */
     62    CoreWindow ^ native_win = CoreWindow::GetForCurrentThread();
     63    Microsoft::WRL::ComPtr<IUnknown> cpp_win = reinterpret_cast<IUnknown *>(native_win);
     64    HRESULT result = CreateWinrtEglWindow(cpp_win, ANGLE_D3D_FEATURE_LEVEL_ANY, &(video_data->winrtEglWindow));
     65    if (FAILED(result)) {
     66        return -1;
     67    }
     68
     69    /* Call eglGetDisplay and eglInitialize as appropriate.  On other
     70     * platforms, this would probably get done by SDL_EGL_LoadLibrary,
     71     * however ANGLE/WinRT's current implementation (as of Mar 22, 2014) of
     72     * eglGetDisplay requires that a C++ object be passed into it, so the
     73     * call will be made in this file, a C++ file, instead.
     74     */
     75    Microsoft::WRL::ComPtr<IUnknown> cpp_display = video_data->winrtEglWindow;
     76    _this->egl_data->egl_display = ((eglGetDisplay_Function)_this->egl_data->eglGetDisplay)(cpp_display);
     77    if (!_this->egl_data->egl_display) {
     78        return SDL_SetError("Could not get EGL display");
     79    }
     80    
     81    if (_this->egl_data->eglInitialize(_this->egl_data->egl_display, NULL, NULL) != EGL_TRUE) {
     82        return SDL_SetError("Could not initialize EGL");
     83    }
     84
     85    return 0;
     86}
     87
     88extern "C" void
     89WINRT_GLES_UnloadLibrary(_THIS)
     90{
     91    SDL_VideoData *video_data = (SDL_VideoData *)_this->driverdata;
     92
     93    /* Release SDL's own COM reference to the ANGLE/WinRT IWinrtEglWindow */
     94    if (video_data->winrtEglWindow) {
     95        video_data->winrtEglWindow->Release();
     96        video_data->winrtEglWindow = nullptr;
     97    }
     98
     99    /* Perform the bulk of the unloading */
    100    SDL_EGL_UnloadLibrary(_this);
    101}
    102
    103extern "C" {
    104SDL_EGL_CreateContext_impl(WINRT)
    105SDL_EGL_SwapWindow_impl(WINRT)
    106SDL_EGL_MakeCurrent_impl(WINRT)
    107}
    108
    109#endif /* SDL_VIDEO_DRIVER_WINRT && SDL_VIDEO_OPENGL_EGL */
    110
    111/* vi: set ts=4 sw=4 expandtab: */
    112