def draw(self, targetWindowSurf): if self.rightJustified: sdl2.SDL_BlitSurface( self.surf, None, targetWindowSurf, sdl2.SDL_Rect(self.x - self.surf.w, self.y, self.surf.w, self.surf.h)) else: sdl2.SDL_BlitSurface( self.surf, None, targetWindowSurf, sdl2.SDL_Rect(self.x, self.y, self.surf.w, self.surf.h))
def нашкрабај(бре): for _ in range(100): y = random.randint(0, бре.висина - 1) x = random.randint(0, бре.ширина - 1) индекс = y * бре.корак + x * 4 бре.пиксели[индекс + random.randint(0, 3)] = random.randint(0, 255) sdl2.SDL_BlitSurface(бре.шарено, None, бре.површ, None)
def run(): sdl2.SDL_Init(sdl2.SDL_INIT_VIDEO) window = sdl2.SDL_CreateWindow(b"Hello World", sdl2.SDL_WINDOWPOS_CENTERED, sdl2.SDL_WINDOWPOS_CENTERED, 592, 460, sdl2.SDL_WINDOW_SHOWN) fname = os.path.join(os.path.dirname(os.path.abspath(__file__)), "resources", "hello.bmp") image = sdl2.SDL_LoadBMP(fname.encode("utf-8")) windowsurface = sdl2.SDL_GetWindowSurface(window) sdl2.SDL_BlitSurface(image, None, windowsurface, None) sdl2.SDL_UpdateWindowSurface(window) sdl2.SDL_FreeSurface(image) running = True event = sdl2.SDL_Event() while running: while sdl2.SDL_PollEvent(ctypes.byref(event)) != 0: if event.type == sdl2.SDL_QUIT: running = False break sdl2.SDL_Delay(10) sdl2.SDL_DestroyWindow(window) sdl2.SDL_Quit() return 0
def blitSurf(srcSurf, dst, dstSurf, xOffset=0, yOffset=0): x = dst.size[0] / 2 + xOffset - srcSurf.w / 2 y = dst.size[1] / 2 + yOffset - srcSurf.h / 2 sdl2.SDL_BlitSurface(srcSurf, None, dstSurf, sdl2.SDL_Rect(x, y, srcSurf.w, srcSurf.h)) sdl2.SDL_UpdateWindowSurface( dst.window ) #should this really be here? (will it cause immediate update?)
def _status_text(self, text): text_surface = self.font_manager.render(text) text_rect = text_surface.clip_rect # FIXME: draw a box instead of tons of lines. Sheesh. for i in range(0, min(self.surface.h, text_rect.h + 2*TEXT_MARGIN)): sdl2.ext.line(self.surface, BLACK, (0, i, self.surface.w, i)) sdl2.SDL_BlitSurface(text_surface, None, self.surface, sdl2.SDL_Rect(TEXT_MARGIN, TEXT_MARGIN))
def refresh(self, image): bytes_io = io.BytesIO() size = image.size image.resize((size[0] * self.scale, size[1] * self.scale)).save(bytes_io, "PPM") image_bytes = bytes_io.getvalue() rw_ops = sdl2.SDL_RWFromMem(image_bytes, len(image_bytes)) sdl_image = sdl2.sdlimage.IMG_Load_RW(rw_ops, 1) sdl2.SDL_BlitSurface(sdl_image, None, self.window_surface, None) sdl2.SDL_UpdateWindowSurface(self.window) sdl2.SDL_FreeSurface(sdl_image)
def _render_clock(self): """Draw a clock in the top-right corner of the screen.""" now = datetime.now().strftime(CLOCK_FORMAT) text_surface = self.font_manager.render(now) text_rect = text_surface.clip_rect dst_x = max(self.surface.w - text_surface.w - TEXT_MARGIN, 0) dst_y = min(TEXT_MARGIN, self.surface.h) sdl2.SDL_BlitSurface(text_surface, None, self.surface, sdl2.SDL_Rect(dst_x, dst_y))
def _do_flip(self): """Draw the canvas to the screen.""" sdl2.SDL_FillRect(self.work_surface, None, self.border_attr) if self.composite_artifacts: self.work_pixels[:] = video_graphical.apply_composite_artifacts( self.pixels[self.vpagenum], 4 // self.bitsperpixel) sdl2.SDL_SetSurfacePalette(self.work_surface, self.composite_palette) else: self.work_pixels[:] = self.pixels[self.vpagenum] sdl2.SDL_SetSurfacePalette(self.work_surface, self.show_palette[self.blink_state]) # apply cursor to work surface self._show_cursor(True) # convert 8-bit work surface to (usually) 32-bit display surface format pixelformat = self.display_surface.contents.format conv = sdl2.SDL_ConvertSurface(self.work_surface, pixelformat, 0) # scale converted surface and blit onto display if not self.smooth: sdl2.SDL_BlitScaled(conv, None, self.display_surface, None) else: # smooth-scale converted surface w, h = self.window_width, self.window_height zoomx = ctypes.c_double(w / (self.size[0] + 2.0 * self.border_x)) zoomy = ctypes.c_double(h / (self.size[1] + 2.0 * self.border_y)) # only free the surface just before zoomSurface needs to re-allocate # so that the memory block is highly likely to be easily available # this seems to avoid unpredictable delays sdl2.SDL_FreeSurface(self.zoomed) self.zoomed = sdl2.sdlgfx.zoomSurface(conv, zoomx, zoomy, sdl2.sdlgfx.SMOOTHING_ON) # blit onto display sdl2.SDL_BlitSurface(self.zoomed, None, self.display_surface, None) # create clipboard feedback if self.clipboard.active(): rects = (sdl2.SDL_Rect(r[0] + self.border_x, r[1] + self.border_y, r[2], r[3]) for r in self.clipboard.selection_rect) sdl_rects = (sdl2.SDL_Rect * len(self.clipboard.selection_rect))(*rects) sdl2.SDL_FillRect( self.overlay, None, sdl2.SDL_MapRGBA(self.overlay.contents.format, 0, 0, 0, 0)) sdl2.SDL_FillRects( self.overlay, sdl_rects, len(sdl_rects), sdl2.SDL_MapRGBA(self.overlay.contents.format, 128, 0, 128, 0)) sdl2.SDL_BlitScaled(self.overlay, None, self.display_surface, None) # flip the display sdl2.SDL_UpdateWindowSurface(self.display) # destroy the temporary surface sdl2.SDL_FreeSurface(conv)
def blit(self, source, dest, area=None, special_flags=0): """...""" sdl2.SDL_SetSurfaceBlendMode(source.surface, sdl2.SDL_BLENDMODE_BLEND) dest_rect = to_sdl_rect(dest) area_rect = to_sdl_rect(area) if area is not None else None err = sdl2.SDL_BlitSurface(source.surface, area_rect, self.surface, dest_rect) if err: raise sdl2.SDLError() dirty = Rect(dest[0], dest[1], source.w, source.h) return dirty.clip(self.get_rect())
def refresh(self, area=None): if area is None: area = ((0, self.NCOLS, 1), (0, self.NROWS, 1)) rcols, rrows = area if isinstance(rcols, tuple): rcols = slice(*rcols).indices(self.NCOLS) elif isinstance(rcols, slice): rcols = rcols.indices(self.NCOLS) else: rcols = int(rcols) rcols = (rcols, rcols + 1, 1) if isinstance(rrows, tuple): rrows = slice(*rrows).indices(self.NROWS) elif isinstance(rrows, slice): rrows = rrows.indices(self.NROWS) else: rrows = int(rrows) rrows = (rrows, rrows + 1, 1) rrows = xrange(*rrows) rcols = range(*rcols) for r in rrows: y = r * self.cellheight for c in rcols: x = c * self.cellwidth sdl2.SDL_BlitSurface( self.charsurf[self.abuf[c, r]], sdl2.SDL_Rect( 0, self.cellheight * self.cbuf[c, r], self.cellwidth, self.cellheight), self.scrsurf, sdl2.SDL_Rect( self.cellwidth * c, self.cellheight * r, 0, 0)) self.refreshfn(*self.rfnparams) return
sdl2.SDL_Init(sdl2.SDL_INIT_VIDEO) #This will return a handle to an open 'Notepad.exe' window. window_handle = get_windows_bytitle("Untitled", False) #Create a window so that the hint below can be set a = sdl2.SDL_CreateWindow("test window", sdl2.SDL_WINDOWPOS_UNDEFINED, sdl2.SDL_WINDOWPOS_UNDEFINED, 200, 200, 0) #Set hint as recommended by SDL documentation: https://wiki.libsdl.org/SDL_CreateWindowFrom#Remarks result = sdl2.SDL_SetHint(sdl2.SDL_HINT_VIDEO_WINDOW_SHARE_PIXEL_FORMAT, hex(id(a))) print(sdl2.SDL_GetError()) np_window = sdl2.SDL_CreateWindowFrom(window_handle[0]) print(sdl2.SDL_GetError()) np_sur = sdl2.SDL_GetWindowSurface(np_window) print(sdl2.SDL_GetError()) save_sur = sdl2.SDL_CreateRGBSurface(0, np_sur[0].w, np_sur[0].h, 32, 0, 0, 0, 0) print(sdl2.SDL_GetError()) r = sdl2.SDL_BlitSurface(np_sur, None, save_sur, None) print(sdl2.SDL_GetError()) result = sdl2.SDL_SaveBMP(save_sur, 'test.bmp') print(sdl2.SDL_GetError()) sdl2.SDL_FreeSurface(save_sur) print(sdl2.SDL_GetError())
SKIN['right_pad'] = SKIN['left_pad'] + SKIN['button_profile'] SKIN['top_pad'] = 3 SKIN['bottom_pad'] = 3 SKIN['window'] = (SKIN['size'][0] + SKIN['left_pad'] + SKIN['right_pad'], SKIN['size'][1] + SKIN['top_pad'] + SKIN['bottom_pad']) SKIN['adjust'] = (SKIN['offset'][0] + SKIN['left_pad'], SKIN['offset'][1] + SKIN['top_pad']) sdl2.ext.init() window = sdl2.ext.Window("ST7789", size=SKIN['window']) window.show() windowsurface = window.get_surface() sdl2.ext.fill(windowsurface, (0xff, 0xff, 0xff)) skin = sdl2.ext.load_image(SKIN['fname']) sdl2.SDL_BlitSurface( skin, None, windowsurface, sdl2.SDL_Rect(SKIN['left_pad'], SKIN['top_pad'], SKIN['size'][0], SKIN['size'][1])) sdl2.SDL_FreeSurface(skin) window.refresh() spi_st7789_sim = ST7789Sim() i2c_cst816s_sim = CST816SSim() def save_image(surface, fname): """Save a surface as an image.""" raw = sdl2.ext.pixels2d(surface) # Crop and swap the axes to ensure the final rotation is correct cropped = raw[SKIN['top_pad']:-SKIN['bottom_pad']] cropped = np.swapaxes(cropped, 0, 1)
def нашкрабај(бре): бре.пуо.x = бре.положај.x бре.пуо.y = бре.положај.y sdl2.SDL_BlitSurface(бре.плава, None, бре.површ, бре.пуо)
def _window_callback(hwnd, all_windows): all_windows.append((hwnd, win32gui.GetWindowText(hwnd))) windows = [] win32gui.EnumWindows(_window_callback, windows) if exact: return [hwnd for hwnd, title in windows if title_text == title] else: return [hwnd for hwnd, title in windows if title_text in title] sdl2.SDL_Init(sdl2.SDL_INIT_VIDEO) ''' a = sdl2.SDL_CreateWindow("test window", sdl2.SDL_WINDOWPOS_UNDEFINED,sdl2.SDL_WINDOWPOS_UNDEFINED, 200,200, 0 ) test_sur = sdl2.SDL_GetWindowSurface(a) #print test_sur image = sdl2.SDL_LoadBMP("testimage.bmp") sdl2.SDL_BlitSurface(image, None, test_sur, None) sdl2.SDL_FreeSurface(image) sdl2.SDL_UpdateWindowSurface(a) ''' #window_handle = get_windows_bytitle("Dwarf Fortress", True) window_handle = get_windows_bytitle("Untitled", False) #window_handle = get_windows_bytitle("test window", True) #hwndDC = win32gui.GetWindowDC(window_handle[0]) #mfcDC = win32ui.CreateDCFromHandle(hwndDC) #print window_handle
# upload rgb data buf = pkt.swsFrame.contents.data[0] surface = sdl2.SDL_CreateRGBSurfaceFrom( buf, size[0], size[1], 24, size[0] * 3, rmask, gmask, bmask, amask) #print sdl2.SDL_GetPixelFormatName(surface.contents.format.contents.format) if useTexture: texture = sdl2.SDL_CreateTextureFromSurface( renderer, surface) res = sdl2.SDL_RenderCopy(renderer, texture, None, None) if res < 0: raise RuntimeError(sdl2.SDL_GetError()) sdl2.SDL_RenderPresent(renderer) else: sdl2.SDL_BlitSurface(surface, None, windowSurface, None) sdl2.SDL_UpdateWindowSurface(window) sdl2.SDL_FreeSurface(surface) sdl2.SDL_Delay(10) sdl2.SDL_DestroyWindow(window) sdl2.SDL_Quit()
def drawText(myText, myFont, textColor, textWidth=.9): lineHeight = sdl2.sdlttf.TTF_RenderText_Blended( myFont, 'T', textColor).contents.h textWidthMax = int(stimDisplay.size[0]) paragraphs = myText.splitlines() renderList = [] textHeight = 0 for thisParagraph in paragraphs: words = thisParagraph.split(' ') if len(words) == 1: renderList.append(words[0]) if (thisParagraph != paragraphs[len(paragraphs) - 1]): renderList.append(' ') textHeight = textHeight + lineHeight else: thisWordIndex = 0 while thisWordIndex < (len(words) - 1): lineStart = thisWordIndex lineWidth = 0 while (thisWordIndex < (len(words) - 1)) and (lineWidth <= textWidthMax): thisWordIndex = thisWordIndex + 1 lineWidth = sdl2.sdlttf.TTF_RenderText_Blended( myFont, ' '.join(words[lineStart:(thisWordIndex + 1)]), textColor).contents.w if thisWordIndex < (len(words) - 1): #last word went over, paragraph continues renderList.append(' '.join( words[lineStart:(thisWordIndex - 1)])) textHeight = textHeight + lineHeight thisWordIndex = thisWordIndex - 1 else: if lineWidth <= textWidthMax: #short final line renderList.append(' '.join( words[lineStart:(thisWordIndex + 1)])) textHeight = textHeight + lineHeight else: #full line then 1 word final line renderList.append(' '.join( words[lineStart:thisWordIndex])) textHeight = textHeight + lineHeight renderList.append(words[thisWordIndex]) textHeight = textHeight + lineHeight #at end of paragraph, check whether a inter-paragraph space should be added if (thisParagraph != paragraphs[len(paragraphs) - 1]): renderList.append(' ') textHeight = textHeight + lineHeight numLines = len(renderList) * 1.0 for thisLine in range(len(renderList)): thisRender = sdl2.sdlttf.TTF_RenderText_Blended( myFont, renderList[thisLine], textColor).contents x = int(stimDisplay.size[0] / 2.0 - thisRender.w / 2.0) y = int(stimDisplay.size[1] / 2.0 - thisRender.h / 2.0 + 1.0 * thisLine / numLines * textHeight) sdl2.SDL_BlitSurface( thisRender, None, stimDisplaySurf, sdl2.SDL_Rect(x, y, thisRender.w, thisRender.h)) sdl2.SDL_UpdateWindowSurface( stimDisplay.window ) #should this really be here? (will it cause immediate update?)
def нашкрабај(бре): sdl2.SDL_BlitSurface(бре.главна_површ, бре.извор, бре.фокус, бре.извор) sdl2.SDL_BlitScaled(бре.фокус, бре.извор, бре.стаклена_површ, бре.притока)