Consider the multiple copiesThe image exists in 3 places in the web page: program memory, canvas
ImageData, and the canvas element. Normally,
SDL_LockSurface()copies from the canvas, to
ImageDataand then to program memory, and
SDL_UnlockSurface()copies from program memory to
ImageDataand to the canvas. Conversion may be needed between program memory and
SDL.defaults.copyOnLock = false
SDL_LockSurface()will copy from canvas to
ImageDatabut not from
ImageDatainto program memory.
SDL.defaults.discardOnLock = true
createImageData() once to initially create
ImageData, and never copy from the canvas. Copying from
ImageDatato program memory is prevented regardless of
With normal SDL you can write only the RGB values and get opaque pixels of the requested colour. Canvas pixels also have an alpha value, which needs to be set to 255 to make pixels fully opaque.
SDL.defaults.opaqueFrontBuffer = false
SDL_UnlockSurface()set alpha values in
ImageDatato 255. This option prevents those operations. With it, the
SDL_HWPALETTE8 bpp mode works normally, your code that writes pixels into memory must set the alpha values. You can simply bitwise or pixels with
Amaskfrom the surface
It's possible to use 8 bpp without
SDL_HWPALETTE flag for 8 bpp modes
SDL_HWPALETTE. However, that uses less optimized code when converting to 32 bpp for the canvas, and doesn't work with
SDL.defaults.opaqueFrontBuffer = false.
SDL_LockSurface()copying is disabled. 8 bpp modes without the flag don't have that requirement, but you'll end up with 32 bpp RGB values copied back, which you probably don't want.
Module.screenIsReadOnly = true
SDL_LockSurface()copying. You could use it instead of SDL.defaults.discardOnLock = true. The only difference is that
ImageDatais copied from the canvas the first time
SDL_LockSurface()is called instead of being created via
create. It's probably better to use the
SDL.defaultsoptions instead because they're better documented and better named.
Sample codeHere is a code fragment which sets uses the recommended optimization settings and enters 8 bpp mode in the recommended way. Some of this is redundant as noted above, but there's no harm in that.
SDL.defaults.copyOnLock = false;
SDL.defaults.discardOnLock = true;
SDL.defaults.opaqueFrontBuffer = false;
surface = SDL_SetVideoMode(WIDTH, HEIGHT, 8, SDL_HWPALETTE);