def _init_thread(self): """ Complete SDL2 interface initialisation. """ # initialise SDL sdl2.SDL_Init(sdl2.SDL_INIT_EVERYTHING) # set clipboard handler to SDL2 backend.clipboard_handler = SDL2Clipboard() # display palettes for blink states 0, 1 self.show_palette = [ sdl2.SDL_AllocPalette(256), sdl2.SDL_AllocPalette(256) ] # get physical screen dimensions (needs to be called before set_mode) display_mode = sdl2.SDL_DisplayMode() sdl2.SDL_GetCurrentDisplayMode(0, ctypes.byref(display_mode)) self.physical_size = display_mode.w, display_mode.h # create the window initially, size will be corrected later self.display = None # create window in same thread that manipulates it # "NOTE: You should not expect to be able to create a window, render, or receive events on any thread other than the main one" # https://wiki.libsdl.org/CategoryThread # http://stackoverflow.com/questions/27751533/sdl2-threading-seg-fault self._do_create_window(640, 400) # load an all-black 16-colour game palette to get started self.set_palette([(0, 0, 0)] * 16, None) self.move_cursor(1, 1) self.set_page(0, 0) self.set_mode(self.kwargs['initial_mode']) # support for CGA composite self.composite_palette = sdl2.SDL_AllocPalette(256) composite_colors = video_graphical.composite_640.get( self.composite_card, video_graphical.composite_640['cga']) colors = (sdl2.SDL_Color * 256)( *[sdl2.SDL_Color(r, g, b, 255) for (r, g, b) in composite_colors]) sdl2.SDL_SetPaletteColors(self.composite_palette, colors, 0, 256) # check if we can honour scaling=smooth if self.smooth: # pointer to the zoomed surface self.zoomed = None pixelformat = self.display_surface.contents.format if pixelformat.contents.BitsPerPixel != 32: logging.warning( 'Smooth scaling not available on this display of %d-bit colour depth: needs 32-bit', self.display_surface.format.contents.BitsPerPixel) self.smooth = False if not hasattr(sdl2, 'sdlgfx'): logging.warning( 'Smooth scaling not available: SDL_GFX extension not found.' ) self.smooth = False # available joysticks num_joysticks = sdl2.SDL_NumJoysticks() for j in range(num_joysticks): sdl2.SDL_JoystickOpen(j) # if a joystick is present, its axes report 128 for mid, not 0 for axis in (0, 1): backend.input_queue.put( backend.Event(backend.STICK_MOVED, (j, axis, 128)))
def __enter__(self): """Complete SDL2 interface initialisation.""" # set clipboard handler to SDL2 self.clipboard_handler = get_clipboard_handler() # display palettes for blink states 0, 1 self.show_palette = [ sdl2.SDL_AllocPalette(256), sdl2.SDL_AllocPalette(256) ] # get physical screen dimensions (needs to be called before set_mode) # load an all-black 16-colour game palette to get started self.set_palette([(0, 0, 0)] * 16, None) self.move_cursor(1, 1) self.set_page(0, 0) # set_mode should be first event on queue # support for CGA composite self.composite_palette = sdl2.SDL_AllocPalette(256) composite_colors = video_graphical.composite_640.get( self.composite_card, video_graphical.composite_640['cga']) colors = (sdl2.SDL_Color * 256)( *[sdl2.SDL_Color(r, g, b, 255) for (r, g, b) in composite_colors]) sdl2.SDL_SetPaletteColors(self.composite_palette, colors, 0, 256) # check if we can honour scaling=smooth if self.smooth: # pointer to the zoomed surface self.zoomed = None pixelformat = self.display_surface.contents.format if pixelformat.contents.BitsPerPixel != 32: logging.warning( 'Smooth scaling not available on this display of %d-bit colour depth: needs 32-bit', self.display_surface.format.contents.BitsPerPixel) self.smooth = False if not hasattr(sdl2, 'sdlgfx'): logging.warning( 'Smooth scaling not available: SDL_GFX extension not found.' ) self.smooth = False # available joysticks num_joysticks = sdl2.SDL_NumJoysticks() for j in range(num_joysticks): sdl2.SDL_JoystickOpen(j) # if a joystick is present, its axes report 128 for mid, not 0 for axis in (0, 1): self.input_queue.put( signals.Event(signals.STICK_MOVED, (j, axis, 128))) # enable IME sdl2.SDL_StartTextInput() return video_graphical.VideoGraphical.__enter__(self)
def _set_icon(self): """Set the icon on the SDL window.""" mask = numpy.array(self.icon).T.repeat(2, 0).repeat(2, 1) icon = sdl2.SDL_CreateRGBSurface(0, mask.shape[0], mask.shape[1], 8, 0, 0, 0, 0) pixels2d(icon.contents)[:] = mask # icon palette (black & white) icon_palette = sdl2.SDL_AllocPalette(256) icon_colors = [ sdl2.SDL_Color(x, x, x, 255) for x in [0, 255] + [255]*254 ] sdl2.SDL_SetPaletteColors(icon_palette, (sdl2.SDL_Color * 256)(*icon_colors), 0, 2) sdl2.SDL_SetSurfacePalette(icon, icon_palette) sdl2.SDL_SetWindowIcon(self.display, icon) sdl2.SDL_FreeSurface(icon) sdl2.SDL_FreePalette(icon_palette)