2 Simple DirectMedia Layer
3 Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
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.
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:
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.
21 #include "../../SDL_internal.h"
23 #if SDL_VIDEO_DRIVER_EMSCRIPTEN
25 #include "SDL_emscriptenvideo.h"
26 #include "SDL_emscriptenframebuffer.h"
29 int Emscripten_CreateWindowFramebuffer(_THIS, SDL_Window * window, Uint32 * format, void ** pixels, int *pitch)
32 const Uint32 surface_format = SDL_PIXELFORMAT_BGR888;
35 Uint32 Rmask, Gmask, Bmask, Amask;
37 /* Free the old framebuffer surface */
38 SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
39 surface = data->surface;
40 SDL_FreeSurface(surface);
42 /* Create a new one */
43 SDL_PixelFormatEnumToMasks(surface_format, &bpp, &Rmask, &Gmask, &Bmask, &Amask);
44 SDL_GetWindowSize(window, &w, &h);
46 surface = SDL_CreateRGBSurface(0, w, h, bpp, Rmask, Gmask, Bmask, Amask);
51 /* Save the info and return! */
52 data->surface = surface;
53 *format = surface_format;
54 *pixels = surface->pixels;
55 *pitch = surface->pitch;
59 int Emscripten_UpdateWindowFramebuffer(_THIS, SDL_Window * window, const SDL_Rect * rects, int numrects)
63 SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
64 surface = data->surface;
66 return SDL_SetError("Couldn't find framebuffer surface for window");
69 /* Send the data to the display */
72 //TODO: don't create context every update
73 var ctx = Module['canvas'].getContext('2d');
75 //library_sdl.js SDL_UnlockSurface
76 var image = ctx.createImageData($0, $1);
77 var data = image.data;
82 if (typeof CanvasPixelArray !== 'undefined' && data instanceof CanvasPixelArray) {
83 // IE10/IE11: ImageData objects are backed by the deprecated CanvasPixelArray,
84 // not UInt8ClampedArray. These don't have buffers, so we need to revert
85 // to copying a byte at a time. We do the undefined check because modern
86 // browsers do not define CanvasPixelArray anymore.
89 var val = HEAP32[src]; // This is optimized. Instead, we could do {{{ makeGetValue('buffer', 'dst', 'i32') }}};
90 data[dst ] = val & 0xff;
91 data[dst+1] = (val >> 8) & 0xff;
92 data[dst+2] = (val >> 16) & 0xff;
93 data[dst+3] = isScreen ? 0xff : ((val >> 24) & 0xff);
98 var data32 = new Uint32Array(data.buffer);
102 // HEAP32[src++] is an optimization. Instead, we could do {{{ makeGetValue('buffer', 'dst', 'i32') }}};
103 data32[dst++] = HEAP32[src++] | 0xff000000;
107 data32[dst++] = HEAP32[src++];
112 ctx.putImageData(image, 0, 0);
114 }, surface->w, surface->h, surface->pixels);
116 /*if (SDL_getenv("SDL_VIDEO_Emscripten_SAVE_FRAMES")) {
117 static int frame_number = 0;
119 SDL_snprintf(file, sizeof(file), "SDL_window%d-%8.8d.bmp",
120 SDL_GetWindowID(window), ++frame_number);
121 SDL_SaveBMP(surface, file);
126 void Emscripten_DestroyWindowFramebuffer(_THIS, SDL_Window * window)
128 SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
130 SDL_FreeSurface(data->surface);
131 data->surface = NULL;
134 #endif /* SDL_VIDEO_DRIVER_EMSCRIPTEN */
136 /* vi: set ts=4 sw=4 expandtab: */