src/video/emscripten/SDL_emscriptenopengles.c
author Ryan C. Gordon
Wed, 23 Nov 2016 17:20:28 -0500
changeset 10644 594c17c446f6
parent 9998 f67cf37e9cd4
child 10737 3406a0f8b041
permissions -rw-r--r--
Still more compiler warning fixes for various platforms.
icculus@9278
     1
/*
icculus@9278
     2
  Simple DirectMedia Layer
slouken@9998
     3
  Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
icculus@9278
     4
icculus@9278
     5
  This software is provided 'as-is', without any express or implied
icculus@9278
     6
  warranty.  In no event will the authors be held liable for any damages
icculus@9278
     7
  arising from the use of this software.
icculus@9278
     8
icculus@9278
     9
  Permission is granted to anyone to use this software for any purpose,
icculus@9278
    10
  including commercial applications, and to alter it and redistribute it
icculus@9278
    11
  freely, subject to the following restrictions:
icculus@9278
    12
icculus@9278
    13
  1. The origin of this software must not be misrepresented; you must not
icculus@9278
    14
     claim that you wrote the original software. If you use this software
icculus@9278
    15
     in a product, an acknowledgment in the product documentation would be
icculus@9278
    16
     appreciated but is not required.
icculus@9278
    17
  2. Altered source versions must be plainly marked as such, and must not be
icculus@9278
    18
     misrepresented as being the original software.
icculus@9278
    19
  3. This notice may not be removed or altered from any source distribution.
icculus@9278
    20
*/
icculus@9278
    21
#include "../../SDL_internal.h"
icculus@9278
    22
icculus@9278
    23
#if SDL_VIDEO_DRIVER_EMSCRIPTEN && SDL_VIDEO_OPENGL_EGL
icculus@9278
    24
icculus@9278
    25
#include <emscripten/emscripten.h>
icculus@9278
    26
#include <GLES2/gl2.h>
icculus@9278
    27
icculus@9278
    28
#include "SDL_emscriptenvideo.h"
icculus@9278
    29
#include "SDL_emscriptenopengles.h"
icculus@9278
    30
icculus@9278
    31
#define LOAD_FUNC(NAME) _this->egl_data->NAME = NAME;
icculus@9278
    32
icculus@9278
    33
/* EGL implementation of SDL OpenGL support */
icculus@9278
    34
icculus@9278
    35
int
icculus@9278
    36
Emscripten_GLES_LoadLibrary(_THIS, const char *path) {
icculus@9278
    37
    /*we can't load EGL dynamically*/
icculus@9278
    38
    _this->egl_data = (struct SDL_EGL_VideoData *) SDL_calloc(1, sizeof(SDL_EGL_VideoData));
icculus@9278
    39
    if (!_this->egl_data) {
icculus@9278
    40
        return SDL_OutOfMemory();
icculus@9278
    41
    }
icculus@10644
    42
icculus@10644
    43
    /* Emscripten forces you to manually cast eglGetProcAddress to the real
icculus@10644
    44
       function type; grep for "__eglMustCastToProperFunctionPointerType" in
icculus@10644
    45
       Emscripten's egl.h for details. */
icculus@10644
    46
    _this->egl_data->eglGetProcAddress = (void *(EGLAPIENTRY *)(const char *)) eglGetProcAddress;
icculus@10644
    47
icculus@9278
    48
    LOAD_FUNC(eglGetDisplay);
icculus@9278
    49
    LOAD_FUNC(eglInitialize);
icculus@9278
    50
    LOAD_FUNC(eglTerminate);
icculus@9278
    51
    LOAD_FUNC(eglChooseConfig);
icculus@9278
    52
    LOAD_FUNC(eglGetConfigAttrib);
icculus@9278
    53
    LOAD_FUNC(eglCreateContext);
icculus@9278
    54
    LOAD_FUNC(eglDestroyContext);
icculus@9278
    55
    LOAD_FUNC(eglCreateWindowSurface);
icculus@9278
    56
    LOAD_FUNC(eglDestroySurface);
icculus@9278
    57
    LOAD_FUNC(eglMakeCurrent);
icculus@9278
    58
    LOAD_FUNC(eglSwapBuffers);
icculus@9278
    59
    LOAD_FUNC(eglSwapInterval);
icculus@9278
    60
    LOAD_FUNC(eglWaitNative);
icculus@9278
    61
    LOAD_FUNC(eglWaitGL);
icculus@9278
    62
    LOAD_FUNC(eglBindAPI);
icculus@9278
    63
    
icculus@9278
    64
    _this->egl_data->egl_display = _this->egl_data->eglGetDisplay(EGL_DEFAULT_DISPLAY);
icculus@9278
    65
    if (!_this->egl_data->egl_display) {
icculus@9278
    66
        return SDL_SetError("Could not get EGL display");
icculus@9278
    67
    }
icculus@9278
    68
    
icculus@9278
    69
    if (_this->egl_data->eglInitialize(_this->egl_data->egl_display, NULL, NULL) != EGL_TRUE) {
icculus@9278
    70
        return SDL_SetError("Could not initialize EGL");
icculus@9278
    71
    }
icculus@9278
    72
icculus@9278
    73
    _this->gl_config.driver_loaded = 1;
icculus@9278
    74
icculus@9278
    75
    if (path) {
icculus@9278
    76
        SDL_strlcpy(_this->gl_config.driver_path, path, sizeof(_this->gl_config.driver_path) - 1);
icculus@9278
    77
    } else {
icculus@9278
    78
        *_this->gl_config.driver_path = '\0';
icculus@9278
    79
    }
icculus@9278
    80
    
icculus@9278
    81
    return 0;
icculus@9278
    82
}
icculus@9278
    83
icculus@9278
    84
void
icculus@9278
    85
Emscripten_GLES_DeleteContext(_THIS, SDL_GLContext context)
icculus@9278
    86
{
icculus@9278
    87
    /*
icculus@9278
    88
    WebGL contexts can't actually be deleted, so we need to reset it.
icculus@9278
    89
    ES2 renderer resets state on init anyway, clearing the canvas should be enough
icculus@9278
    90
    */
icculus@9278
    91
icculus@9278
    92
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
icculus@9278
    93
icculus@9278
    94
    SDL_EGL_DeleteContext(_this, context);
icculus@9278
    95
}
icculus@9278
    96
icculus@9278
    97
SDL_EGL_CreateContext_impl(Emscripten)
icculus@9278
    98
SDL_EGL_SwapWindow_impl(Emscripten)
icculus@9278
    99
SDL_EGL_MakeCurrent_impl(Emscripten)
icculus@9278
   100
icculus@9278
   101
void
icculus@9278
   102
Emscripten_GLES_GetDrawableSize(_THIS, SDL_Window * window, int * w, int * h)
icculus@9278
   103
{
icculus@9278
   104
    SDL_WindowData *data;
icculus@9278
   105
    if (window->driverdata) {
icculus@9278
   106
        data = (SDL_WindowData *) window->driverdata;
icculus@9278
   107
icculus@9278
   108
        if (w) {
icculus@9278
   109
            *w = window->w * data->pixel_ratio;
icculus@9278
   110
        }
icculus@9278
   111
icculus@9278
   112
        if (h) {
icculus@9278
   113
            *h = window->h * data->pixel_ratio;
icculus@9278
   114
        }
icculus@9278
   115
    }
icculus@9278
   116
}
icculus@9278
   117
icculus@9278
   118
#endif /* SDL_VIDEO_DRIVER_EMSCRIPTEN && SDL_VIDEO_OPENGL_EGL */
icculus@9278
   119
icculus@9278
   120
/* vi: set ts=4 sw=4 expandtab: */
icculus@9278
   121