def cacheall(): cache = {} rawcache = {} for img in os.listdir('data'): image = pyglet.resource.image('data/'+img) rawcache[img] = image tiles = [] for y in xrange(0,image.height//32): for x in xrange(0,image.width//32): tiles.append(image.get_region(x*32,y*32,32,32)) cache[img] = tuple(tiles) tiles = [] return (cache,rawcache)
def get_animation(name): img_name, width, height, num_frames, duration = all[name] image = game.imagecache.get_sprite(img_name) frames = list() for i in xrange(num_frames): frame_img = image.get_region(i * width, 0, width, height) frame_img.anchor_x = width / 2 frames.append(pyglet.image.AnimationFrame(frame_img, duration)) animation = pyglet.image.Animation(frames) return animation
def get_pixel_from_image(image, x, y): """Returns the color of pixel (x,y) in the image as a tuple (r, g, b, a).""" #Grab 1x1-pixel image. Converting entire image to ImageData takes much longer than just #grabbing the single pixel with get_region() and converting just that. image_data = image.get_region(x,y,1,1).get_image_data() #Get (very small) image as a string. The magic number '4' is just len('RGBA'). data = image_data.get_data('RGBA',4) #Convert data strings to integers. Provided by Alex Holkner on the mailing list. components = map(ord, list(data)) #components only contains one pixel. I want to return a color that I can pass to #pyglet.gl.glColor4f(), so I need to put it in the 0.0-1.0 range. return [float(c) / 255.0 for c in components]
def paste(self, destX, destY, srcX, srcY, width, height, collisionType): if self.image is None: return image = self.imageData newImage = image.get_region(srcX, image.height - height - srcY, width, height) sprite = self.make_sprite(newImage) sprite.alpha = self.image.alpha x, y = self.player.get_window_coordinates_python(destX, destY) sprite.x = x sprite.y = y sprite.shape.bottom = -newImage.height self.player.frame.paste_sprite(sprite, collisionType, self.parent.layerIndex)
def render(self, rect): image = self.image if image is None: return ir = util.Rect(image.x, image.y, image.width, image.height) if ir.clippedBy(rect): rect = ir.intersect(rect) if rect is None: return image = image.get_region(rect.x, rect.y, rect.width, rect.height) attrib = 0 if not self.isEnabled(): attrib = GL_CURRENT_BIT elif self.blend_color and self.color is not None: attrib = GL_CURRENT_BIT if self.is_blended: attrib |= GL_ENABLE_BIT if attrib: glPushAttrib(attrib) if attrib & GL_ENABLE_BIT: # blend with background glEnable(GL_BLEND) if attrib & GL_CURRENT_BIT: if not self.isEnabled(): # blend with gray colour to wash out glColor4f(.7, .7, .7, 1.) else: glColor4f(*self.color) # TODO: alignment # blit() handles enabling GL_TEXTURE_2D and binding image.blit(rect.x, rect.y, 0) if attrib: glPopAttrib()
def render(self, rect): image = self.image if image is None: return ir = util.Rect(image.x, image.y, image.width, image.height) if ir.clippedBy(rect): rect = ir.intersect(rect) if rect is None: return image = image.get_region(rect.x, rect.y, rect.width, rect.height) attrib = 0 if not self.isEnabled(): attrib = GL_CURRENT_BIT elif self.blend_color and self.color is not None: attrib = GL_CURRENT_BIT if self.is_blended: attrib |= GL_ENABLE_BIT if attrib: glPushAttrib(attrib) if attrib & GL_ENABLE_BIT: # blend with background glEnable(GL_BLEND) if attrib & GL_CURRENT_BIT: if not self.isEnabled(): # blend with gray colour to wash out glColor4f(.7, .7, .7, 1.) else: glColor4f(*self.color) # XXX alignment # blit() handles enabling GL_TEXTURE_2D and binding image.blit(rect.x, rect.y, 0) if attrib: glPopAttrib()
def make_animation(image,framewidth,frameheight, anchorx=None, anchory=None,animationlength=0.05): sequence = [] animations = [] for y in xrange(0,image.height // frameheight): for x in xrange(1,image.width // framewidth): frame = image.get_region(x*framewidth,y*frameheight,framewidth,frameheight) if anchorx == 'center': anchorx = frame.width//2 if anchorx is not None: frame.anchor_x = anchorx if anchory is not None: frame.anchor_y = anchory sequence.append(frame) animations.append(sequence) sequence = [] output = [] for animation in animations: animation = pyglet.image.Animation.from_image_sequence(animation,animationlength,loop=False) output.append(animation) return output
def render(self, text): # Convert text to UCS2 text_len = len(text) text_ucs2 = str_ucs2(text) # Create layout override handler to extract device advance value. override_spec = ATSULayoutOperationOverrideSpecifier() override_spec.operationSelector = \ kATSULayoutOperationPostLayoutAdjustment override_spec.overrideUPP = \ ATSUDirectLayoutOperationOverrideUPP(self._layout_callback) # Create ATSU text layout for this text and font layout = c_void_p() carbon.ATSUCreateTextLayout(byref(layout)) set_layout_attributes( layout, { kATSUCGContextTag: self._bitmap_context, kATSULayoutOperationOverrideTag: override_spec }) carbon.ATSUSetTextPointerLocation(layout, text_ucs2, kATSUFromTextBeginning, kATSUToTextEnd, text_len) carbon.ATSUSetRunStyle(layout, self.font.atsu_style, kATSUFromTextBeginning, kATSUToTextEnd) # Turning on transient font matching screws up font layout # predictability when strange fonts are installed # <ah> Don't believe this. Can't get foreign/special characters # without transient on. carbon.ATSUSetTransientFontMatching(layout, True) # Get bitmap dimensions required rect = Rect() carbon.ATSUMeasureTextImage(layout, kATSUFromTextBeginning, kATSUToTextEnd, 0, 0, byref(rect)) image_width = rect.right - rect.left + 2 image_height = rect.bottom - rect.top + 2 baseline = rect.bottom + 1 lsb = rect.left # Resize Quartz context if necessary if (image_width > self._bitmap_rect.size.width or image_height > self._bitmap_rect.size.height): self._create_bitmap_context( int(max(image_width, self._bitmap_rect.size.width)), int(max(image_height, self._bitmap_rect.size.height))) set_layout_attributes(layout, {kATSUCGContextTag: self._bitmap_context}) # Draw to the bitmap carbon.CGContextClearRect(self._bitmap_context, self._bitmap_rect) carbon.ATSUDrawText(layout, 0, kATSUToTextEnd, fixed(-lsb + 1), fixed(baseline)) advance = self._glyph_advance # Round advance to nearest int. It actually looks good with sub-pixel # advance as well -- Helvetica at 12pt is more tightly spaced, but # Times New Roman at 12pt is too light. With integer positioning # overall look seems darker and perhaps more uniform. It's also more # similar (programmatically) to Win32 and FreeType. Still, worth # messing around with (comment out next line) if you're interested. advance = int(round(advance)) # Fix advance for zero-width space if text == u'\u200b': advance = 0 # A negative pitch is required, but it is much faster to load the # glyph upside-down and flip the tex_coords. Note region used # to start at top of glyph image. pitch = int(4 * self._bitmap_rect.size.width) image = pyglet.image.ImageData(image_width, self._bitmap_rect.size.height, 'RGBA', self._bitmap, pitch) skip_rows = int(self._bitmap_rect.size.height - image_height) image = image.get_region(0, skip_rows, image.width, image_height) glyph = self.font.create_glyph(image) glyph.set_bearings(baseline, lsb - 1, int(advance)) t = list(glyph.tex_coords) glyph.tex_coords = t[9:12] + t[6:9] + t[3:6] + t[:3] return glyph
def render(self, text): # Convert text to UCS2 text_len = len(text) text_ucs2 = str_ucs2(text) # Create layout override handler to extract device advance value. override_spec = ATSULayoutOperationOverrideSpecifier() override_spec.operationSelector = \ kATSULayoutOperationPostLayoutAdjustment override_spec.overrideUPP = \ ATSUDirectLayoutOperationOverrideUPP(self._layout_callback) # Create ATSU text layout for this text and font layout = c_void_p() carbon.ATSUCreateTextLayout(byref(layout)) set_layout_attributes(layout, { kATSUCGContextTag: self._bitmap_context, kATSULayoutOperationOverrideTag: override_spec}) carbon.ATSUSetTextPointerLocation(layout, text_ucs2, kATSUFromTextBeginning, kATSUToTextEnd, text_len) carbon.ATSUSetRunStyle(layout, self.font.atsu_style, kATSUFromTextBeginning, kATSUToTextEnd) # Turning on transient font matching screws up font layout # predictability when strange fonts are installed # <ah> Don't believe this. Can't get foreign/special characters # without transient on. carbon.ATSUSetTransientFontMatching(layout, True) # Get bitmap dimensions required rect = Rect() carbon.ATSUMeasureTextImage(layout, kATSUFromTextBeginning, kATSUToTextEnd, 0, 0, byref(rect)) image_width = rect.right - rect.left + 2 image_height = rect.bottom - rect.top + 2 baseline = rect.bottom + 1 lsb = rect.left # Resize Quartz context if necessary if (image_width > self._bitmap_rect.size.width or image_height > self._bitmap_rect.size.height): self._create_bitmap_context( int(max(image_width, self._bitmap_rect.size.width)), int(max(image_height, self._bitmap_rect.size.height))) set_layout_attributes(layout, { kATSUCGContextTag: self._bitmap_context}) # Draw to the bitmap carbon.CGContextClearRect(self._bitmap_context, self._bitmap_rect) carbon.ATSUDrawText(layout, 0, kATSUToTextEnd, fixed(-lsb + 1), fixed(baseline)) advance = self._glyph_advance # Round advance to nearest int. It actually looks good with sub-pixel # advance as well -- Helvetica at 12pt is more tightly spaced, but # Times New Roman at 12pt is too light. With integer positioning # overall look seems darker and perhaps more uniform. It's also more # similar (programmatically) to Win32 and FreeType. Still, worth # messing around with (comment out next line) if you're interested. advance = int(round(advance)) # Fix advance for zero-width space if text == '\u200b': advance = 0 # A negative pitch is required, but it is much faster to load the # glyph upside-down and flip the tex_coords. Note region used # to start at top of glyph image. pitch = int(4 * self._bitmap_rect.size.width) image = pyglet.image.ImageData(image_width, self._bitmap_rect.size.height, 'RGBA', self._bitmap, pitch) skip_rows = int(self._bitmap_rect.size.height - image_height) image = image.get_region(0, skip_rows, image.width, image_height) glyph = self.font.create_glyph(image) glyph.set_bearings(baseline, lsb - 1, int(advance)) t = list(glyph.tex_coords) glyph.tex_coords = t[9:12] + t[6:9] + t[3:6] + t[:3] return glyph
def getRegion(image: AbstractImage, x, y, size: Vector2) -> AbstractImage: image = image.get_region(x, int(image.height - size.y - y), *size.toIntTuple()) image.anchor_x = image.width // 2 image.anchor_y = image.height // 2 return image
def render(self, text): # Convert text to UCS2 text_len = len(text) text = str_ucs2(text) # Create ATSU text layout for this text and font layout = c_void_p() carbon.ATSUCreateTextLayout(byref(layout)) set_layout_attributes(layout, { kATSUCGContextTag: self._bitmap_context}) carbon.ATSUSetTextPointerLocation(layout, text, kATSUFromTextBeginning, kATSUToTextEnd, text_len) carbon.ATSUSetRunStyle(layout, self.font.atsu_style, kATSUFromTextBeginning, kATSUToTextEnd) # Turning on transient font matching screws up font layout # predictability when strange fonts are installed carbon.ATSUSetTransientFontMatching(layout, False) # Get bitmap dimensions required rect = Rect() carbon.ATSUMeasureTextImage(layout, kATSUFromTextBeginning, kATSUToTextEnd, 0, 0, byref(rect)) image_width = rect.right - rect.left + 2 image_height = rect.bottom - rect.top + 2 baseline = rect.bottom + 1 lsb = rect.left # Resize Quartz context if necessary if (image_width > self._bitmap_rect.size.width or image_height > self._bitmap_rect.size.height): self._create_bitmap_context( int(max(image_width, self._bitmap_rect.size.width)), int(max(image_height, self._bitmap_rect.size.height))) set_layout_attributes(layout, { kATSUCGContextTag: self._bitmap_context}) # Get typographic box, which gives advance. bounds_actual = c_uint32() bounds = ATSTrapezoid() carbon.ATSUGetGlyphBounds( layout, 0, 0, kATSUFromTextBeginning, kATSUToTextEnd, kATSUseDeviceOrigins, 1, byref(bounds), byref(bounds_actual)) advance = fix2float(bounds.lowerRight.x) - fix2float(bounds.lowerLeft.x) # Draw to the bitmap carbon.CGContextClearRect(self._bitmap_context, self._bitmap_rect) carbon.ATSUDrawText(layout, 0, kATSUToTextEnd, fixed(-lsb + 1), fixed(baseline)) # A negative pitch is required, but it is much faster to load the # glyph upside-down and flip the tex_coords. Note region used # to start at top of glyph image. pitch = int(4 * self._bitmap_rect.size.width) image = pyglet.image.ImageData(image_width, self._bitmap_rect.size.height, 'RGBA', self._bitmap, pitch) skip_rows = int(self._bitmap_rect.size.height - image_height) image = image.get_region(0, skip_rows, image.width, image_height) glyph = self.font.create_glyph(image) glyph.set_bearings(baseline, lsb - 1, advance) glyph.tex_coords = (glyph.tex_coords[3], glyph.tex_coords[2], glyph.tex_coords[1], glyph.tex_coords[0]) return glyph
def render(self, text): # Convert text to UCS2 text_len = len(text) text = str_ucs2(text) # Create ATSU text layout for this text and font layout = c_void_p() carbon.ATSUCreateTextLayout(byref(layout)) set_layout_attributes(layout, {kATSUCGContextTag: self._bitmap_context}) carbon.ATSUSetTextPointerLocation(layout, text, kATSUFromTextBeginning, kATSUToTextEnd, text_len) carbon.ATSUSetRunStyle(layout, self.font.atsu_style, kATSUFromTextBeginning, kATSUToTextEnd) # Turning on transient font matching screws up font layout # predictability when strange fonts are installed carbon.ATSUSetTransientFontMatching(layout, False) # Get bitmap dimensions required rect = Rect() carbon.ATSUMeasureTextImage(layout, kATSUFromTextBeginning, kATSUToTextEnd, 0, 0, byref(rect)) image_width = rect.right - rect.left + 2 image_height = rect.bottom - rect.top + 2 baseline = rect.bottom + 1 lsb = rect.left # Resize Quartz context if necessary if (image_width > self._bitmap_rect.size.width or image_height > self._bitmap_rect.size.height): self._create_bitmap_context( int(max(image_width, self._bitmap_rect.size.width)), int(max(image_height, self._bitmap_rect.size.height))) set_layout_attributes(layout, {kATSUCGContextTag: self._bitmap_context}) # Get typographic box, which gives advance. bounds_actual = c_uint32() bounds = ATSTrapezoid() carbon.ATSUGetGlyphBounds(layout, 0, 0, kATSUFromTextBeginning, kATSUToTextEnd, kATSUseDeviceOrigins, 1, byref(bounds), byref(bounds_actual)) advance = fix2float(bounds.lowerRight.x) - fix2float( bounds.lowerLeft.x) # Draw to the bitmap carbon.CGContextClearRect(self._bitmap_context, self._bitmap_rect) carbon.ATSUDrawText(layout, 0, kATSUToTextEnd, fixed(-lsb + 1), fixed(baseline)) # A negative pitch is required, but it is much faster to load the # glyph upside-down and flip the tex_coords. Note region used # to start at top of glyph image. pitch = int(4 * self._bitmap_rect.size.width) image = pyglet.image.ImageData(image_width, self._bitmap_rect.size.height, 'RGBA', self._bitmap, pitch) skip_rows = int(self._bitmap_rect.size.height - image_height) image = image.get_region(0, skip_rows, image.width, image_height) glyph = self.font.create_glyph(image) glyph.set_bearings(baseline, lsb - 1, int(advance)) t = list(glyph.tex_coords) glyph.tex_coords = t[9:12] + t[6:9] + t[3:6] + t[:3] return glyph