src/video/emscripten/SDL_emscriptenopengles.c
author Charlie Birks <charlie@daftgames.net>
Tue, 29 Jan 2019 12:19:32 +0000
changeset 12579 8dc4e45fbe2b
parent 12503 806492103856
permissions -rw-r--r--
Emscripten: remove GLES_DeleteContext implementation
It was calling glClear without a context. The issue it was trying to
solve was actually that after destroying a window and creating a new one
, the contents of the old window were preserved. This no longer happens
since we resize the window to nothing on destroy.
icculus@9278
     1
/*
icculus@9278
     2
  Simple DirectMedia Layer
slouken@12503
     3
  Copyright (C) 1997-2019 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);
charlie@12323
    63
    LOAD_FUNC(eglQueryString);
charlie@12323
    64
    LOAD_FUNC(eglGetError);
charlie@12323
    65
icculus@9278
    66
    _this->egl_data->egl_display = _this->egl_data->eglGetDisplay(EGL_DEFAULT_DISPLAY);
icculus@9278
    67
    if (!_this->egl_data->egl_display) {
icculus@9278
    68
        return SDL_SetError("Could not get EGL display");
icculus@9278
    69
    }
icculus@9278
    70
    
icculus@9278
    71
    if (_this->egl_data->eglInitialize(_this->egl_data->egl_display, NULL, NULL) != EGL_TRUE) {
icculus@9278
    72
        return SDL_SetError("Could not initialize EGL");
icculus@9278
    73
    }
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
SDL_EGL_CreateContext_impl(Emscripten)
icculus@9278
    85
SDL_EGL_SwapWindow_impl(Emscripten)
icculus@9278
    86
SDL_EGL_MakeCurrent_impl(Emscripten)
icculus@9278
    87
icculus@9278
    88
void
icculus@9278
    89
Emscripten_GLES_GetDrawableSize(_THIS, SDL_Window * window, int * w, int * h)
icculus@9278
    90
{
icculus@9278
    91
    SDL_WindowData *data;
icculus@9278
    92
    if (window->driverdata) {
icculus@9278
    93
        data = (SDL_WindowData *) window->driverdata;
icculus@9278
    94
icculus@9278
    95
        if (w) {
icculus@9278
    96
            *w = window->w * data->pixel_ratio;
icculus@9278
    97
        }
icculus@9278
    98
icculus@9278
    99
        if (h) {
icculus@9278
   100
            *h = window->h * data->pixel_ratio;
icculus@9278
   101
        }
icculus@9278
   102
    }
icculus@9278
   103
}
icculus@9278
   104
icculus@9278
   105
#endif /* SDL_VIDEO_DRIVER_EMSCRIPTEN && SDL_VIDEO_OPENGL_EGL */
icculus@9278
   106
icculus@9278
   107
/* vi: set ts=4 sw=4 expandtab: */
icculus@9278
   108