Atlas - SDL_emscriptenopengles.c

Home / ext / SDL / src / video / emscripten Lines: 1 | Size: 5201 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)]
[FILE BEGIN]
1/* 2 Simple DirectMedia Layer 3 Copyright (C) 1997-2025 Sam Lantinga <[email protected]> 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#ifdef SDL_VIDEO_DRIVER_EMSCRIPTEN 24 25#include <emscripten/emscripten.h> 26#include <emscripten/html5_webgl.h> 27#include <GLES2/gl2.h> 28 29#include "SDL_emscriptenvideo.h" 30#include "SDL_emscriptenopengles.h" 31#include "../../main/SDL_main_callbacks.h" 32 33bool Emscripten_GLES_LoadLibrary(SDL_VideoDevice *_this, const char *path) 34{ 35 return true; 36} 37 38void Emscripten_GLES_UnloadLibrary(SDL_VideoDevice *_this) 39{ 40} 41 42SDL_FunctionPointer Emscripten_GLES_GetProcAddress(SDL_VideoDevice *_this, const char *proc) 43{ 44 return emscripten_webgl_get_proc_address(proc); 45} 46 47bool Emscripten_GLES_SetSwapInterval(SDL_VideoDevice *_this, int interval) 48{ 49 if (interval < 0) { 50 return SDL_SetError("Late swap tearing currently unsupported"); 51 } 52 53 if (Emscripten_ShouldSetSwapInterval(interval)) { 54 // don't change the mainloop timing if the app is also driving a main callback with this hint, 55 // as we assume that was the more deliberate action. 56 if (!SDL_HasMainCallbacks() || !SDL_GetHint(SDL_HINT_MAIN_CALLBACK_RATE)) { 57 if (interval == 0) { 58 emscripten_set_main_loop_timing(EM_TIMING_SETTIMEOUT, 0); 59 } else { 60 emscripten_set_main_loop_timing(EM_TIMING_RAF, interval); 61 } 62 } 63 } 64 65 return true; 66} 67 68bool Emscripten_GLES_GetSwapInterval(SDL_VideoDevice *_this, int *interval) 69{ 70 int mode, value; 71 72 emscripten_get_main_loop_timing(&mode, &value); 73 74 if (mode == EM_TIMING_RAF) { 75 *interval = value; 76 return true; 77 } else { 78 *interval = 0; 79 return true; 80 } 81} 82 83SDL_GLContext Emscripten_GLES_CreateContext(SDL_VideoDevice *_this, SDL_Window *window) 84{ 85 SDL_WindowData *window_data; 86 87 EmscriptenWebGLContextAttributes attribs; 88 EMSCRIPTEN_WEBGL_CONTEXT_HANDLE context; 89 90 emscripten_webgl_init_context_attributes(&attribs); 91 92 attribs.alpha = _this->gl_config.alpha_size > 0; 93 attribs.depth = _this->gl_config.depth_size > 0; 94 attribs.stencil = _this->gl_config.stencil_size > 0; 95 attribs.antialias = _this->gl_config.multisamplebuffers == 1; 96 97 if (_this->gl_config.major_version == 3) 98 attribs.majorVersion = 2; // WebGL 2.0 ~= GLES 3.0 99 100 window_data = window->internal; 101 102 if (window_data->gl_context) { 103 SDL_SetError("Cannot create multiple webgl contexts per window"); 104 return NULL; 105 } 106 107 context = emscripten_webgl_create_context(window_data->canvas_id, &attribs); 108 109 if (!context) { 110 SDL_SetError("Could not create webgl context"); 111 return NULL; 112 } 113 114 if (emscripten_webgl_make_context_current(context) != EMSCRIPTEN_RESULT_SUCCESS) { 115 emscripten_webgl_destroy_context(context); 116 return NULL; 117 } 118 119 window_data->gl_context = (SDL_GLContext)context; 120 121 return (SDL_GLContext)context; 122} 123 124bool Emscripten_GLES_DestroyContext(SDL_VideoDevice *_this, SDL_GLContext context) 125{ 126 SDL_Window *window; 127 128 // remove the context from its window 129 for (window = _this->windows; window; window = window->next) { 130 SDL_WindowData *window_data = window->internal; 131 132 if (window_data->gl_context == context) { 133 window_data->gl_context = NULL; 134 } 135 } 136 137 emscripten_webgl_destroy_context((EMSCRIPTEN_WEBGL_CONTEXT_HANDLE)context); 138 return true; 139} 140 141bool Emscripten_GLES_SwapWindow(SDL_VideoDevice *_this, SDL_Window *window) 142{ 143 if (emscripten_has_asyncify() && SDL_GetHintBoolean(SDL_HINT_EMSCRIPTEN_ASYNCIFY, true)) { 144 // give back control to browser for screen refresh 145 emscripten_sleep(0); 146 } 147 return true; 148} 149 150bool Emscripten_GLES_MakeCurrent(SDL_VideoDevice *_this, SDL_Window *window, SDL_GLContext context) 151{ 152 // it isn't possible to reuse contexts across canvases 153 if (window && context) { 154 SDL_WindowData *window_data = window->internal; 155 156 if (context != window_data->gl_context) { 157 return SDL_SetError("Cannot make context current to another window"); 158 } 159 } 160 161 if (emscripten_webgl_make_context_current((EMSCRIPTEN_WEBGL_CONTEXT_HANDLE)context) != EMSCRIPTEN_RESULT_SUCCESS) { 162 return SDL_SetError("Unable to make context current"); 163 } 164 return true; 165} 166 167#endif // SDL_VIDEO_DRIVER_EMSCRIPTEN 168
[FILE END]
(C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.