src/video/emscripten/SDL_emscriptenopengles.c
author Sam Lantinga
Fri, 27 Jan 2017 21:23:27 -0800
changeset 10861 71d8f9afb690
parent 10741 a32fd6b7412b
child 11811 5d94cb6b24d3
permissions -rw-r--r--
Fixed bug 3569 - GL_UpdateViewport leaves PROJECTION matrix selected

Tom Seddon

GL_ActivateRenderer may call GL_UpdateViewport, which leaves the GL_PROJECTION matrix selected. But after GL_ResetState, the GL_MODELVIEW matrix is selected, suggesting that's the intended default state.

It seems at least like these should be consistent. Presumably GL_UpdateViewport should be doing a glMatrixMode(GL_MODELVIEW) before it finishes.
icculus@9278
     1
/*
icculus@9278
     2
  Simple DirectMedia Layer
slouken@10737
     3
  Copyright (C) 1997-2017 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
    if (path) {
icculus@9278
    74
        SDL_strlcpy(_this->gl_config.driver_path, path, sizeof(_this->gl_config.driver_path) - 1);
icculus@9278
    75
    } else {
icculus@9278
    76
        *_this->gl_config.driver_path = '\0';
icculus@9278
    77
    }
icculus@9278
    78
    
icculus@9278
    79
    return 0;
icculus@9278
    80
}
icculus@9278
    81
icculus@9278
    82
void
icculus@9278
    83
Emscripten_GLES_DeleteContext(_THIS, SDL_GLContext context)
icculus@9278
    84
{
icculus@9278
    85
    /*
icculus@9278
    86
    WebGL contexts can't actually be deleted, so we need to reset it.
icculus@9278
    87
    ES2 renderer resets state on init anyway, clearing the canvas should be enough
icculus@9278
    88
    */
icculus@9278
    89
icculus@9278
    90
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
icculus@9278
    91
icculus@9278
    92
    SDL_EGL_DeleteContext(_this, context);
icculus@9278
    93
}
icculus@9278
    94
icculus@9278
    95
SDL_EGL_CreateContext_impl(Emscripten)
icculus@9278
    96
SDL_EGL_SwapWindow_impl(Emscripten)
icculus@9278
    97
SDL_EGL_MakeCurrent_impl(Emscripten)
icculus@9278
    98
icculus@9278
    99
void
icculus@9278
   100
Emscripten_GLES_GetDrawableSize(_THIS, SDL_Window * window, int * w, int * h)
icculus@9278
   101
{
icculus@9278
   102
    SDL_WindowData *data;
icculus@9278
   103
    if (window->driverdata) {
icculus@9278
   104
        data = (SDL_WindowData *) window->driverdata;
icculus@9278
   105
icculus@9278
   106
        if (w) {
icculus@9278
   107
            *w = window->w * data->pixel_ratio;
icculus@9278
   108
        }
icculus@9278
   109
icculus@9278
   110
        if (h) {
icculus@9278
   111
            *h = window->h * data->pixel_ratio;
icculus@9278
   112
        }
icculus@9278
   113
    }
icculus@9278
   114
}
icculus@9278
   115
icculus@9278
   116
#endif /* SDL_VIDEO_DRIVER_EMSCRIPTEN && SDL_VIDEO_OPENGL_EGL */
icculus@9278
   117
icculus@9278
   118
/* vi: set ts=4 sw=4 expandtab: */
icculus@9278
   119