def copy_rect(dst, dst_x, dst_y, src, src_x, src_y, width, height, op="copy", blendmode=None, alpha=None, dest_rect=None): """Static method which performs some type checking before calling :meth:`pinproc.DMDBuffer.copy_to_rect`.""" # print "copy_rect(dst_x=%d, dst_y=%d, src_x=%d, src_y=%d, width=%d, height=%d)" % (dst_x, dst_y, src_x, src_y, width, height) # src_rect = pygame.Rect(int(src_x),int(src_y),int(width),int(height)) # dst_rect = pygame.Rect(int(dst_x),int(dst_y),int(width),int(height)) src_rect = (int(src_x),int(src_y),int(width),int(height)) # if(width > src.width): # width = src.width # if(height > src.height): # height = src.height if(dest_rect is None): dest_rect = (int(dst_x),int(dst_y),int(width),int(height)) # """ 1.8.0: BLEND_ADD, BLEND_SUB, BLEND_MULT, BLEND_MIN, BLEND_MAX # in 1.8.1: BLEND_RGBA_ADD, BLEND_RGBA_SUB, BLEND_RGBA_MULT, BLEND_RGBA_MIN, # BLEND_RGBA_MAX BLEND_RGB_ADD, BLEND_RGB_SUB, BLEND_RGB_MULT, # BLEND_RGB_MIN, BLEND_RGB_MAX # """ # if (op=="copy"): # special_flags = 0 # elif(op=="add"): # special_flags = pygame.BLEND_ADD # elif(op == "sub"): # special_flags = pygame.BLEND_SUB # elif(op == "mult"): # special_flags = pygame.BLEND_MULT # elif(op == "min"): # special_flags = pygame.BLEND_MIN # elif(op == "max"): # special_flags = pygame.BLEND_MAX # elif(op=="rgb_add"): # special_flags = pygame.BLEND_RGB_ADD # elif(op == "rgb_sub"): # special_flags = pygame.BLEND_RGB_SUB # elif(op == "rgb_mult"): # special_flags = pygame.BLEND_RGB_MULT # elif(op == "blacksrc"): # special_flags = 0; # src.pySurface.set_colorkey((0,0,0)) # elif(op == "magentasrc"): # special_flags = 0; # src.pySurface.set_colorkey((255,0,255)) # elif(op == "greensrc"): # special_flags = 0; # src.pySurface.set_colorkey((0,255,0)) # elif(op == "alpha"): # special_flags = 0 # # TODO: THIS... # elif(op == "alphaboth"): # special_flags = 0 # # TODO: THIS... # else: # raise ValueError, "Operation type not recognized." #HD_copy_texture(src.pySurface, dst.pySurface, src_rect, dst_rect) sdl2_DisplayManager.inst().blit(source_tx=src.pySurface, dest_tx=dst.pySurface, dest=dest_rect, area=src_rect, special_flags = 0, blendmode=blendmode, alpha=alpha)
def fill_rect(self, x, y, w, h, c): if (len(c) == 3): c = (c[0], c[1], c[2], 255) old = sdl2_DisplayManager.inst().switch_target(self.pySurface) sdl2_DisplayManager.inst().fill((int(x), int(y), int(w), int(h)), c) sdl2_DisplayManager.inst().switch_target(old)
def main(): sdl2_DisplayManager.Init(450,225,2) # FIRE def emitter_dx(x): return random.randint(-10,10) def emitter_dy(x): return random.randint(-3,3) def dither_dx(x): return random.randint(-10,0) def dither_dy(x): return (x - 0.35) ps = ParticleSystem(450,225, max_life=20, max_particles=400, particles_per_update=20, emitter_dx_fn=emitter_dx, emitter_dy_fn=emitter_dy, dither_dx_fn=dither_dx, dither_dy_fn=dither_dy) # def emitter_dx(x): return random.randint(-10,10) # def emitter_dy(y): return random.randint(-6,6) # def dither_dx(x): return x #random.randint(-6,6) # def dither_dy(y): return y #(x - 0.35) # ps = ParticleSystem(450,200, max_life=20, max_particles=200, particles_per_update=5, emitter_dx_fn=emitter_dx, emitter_dy_fn=emitter_dy, dither_dx_fn=dither_dx, dither_dy_fn=dither_dy) running = True while running: events = sdl2.ext.get_events() for event in events: if event.type == sdl2.SDL_QUIT: running = False break ps.update() ps.draw() sdl2_DisplayManager.inst().flip() sdl2.SDL_Delay(33) sdl2_DisplayManager.inst().clear() return 0
def draw(self, destination_texture = None): # for p in self.particles: for x in xrange(0,len(self.particles)): #xrange(len(self.particles)-1,0,-1): p = self.particles[x] tx = None if(p.life > self.max_life * 0.55): tx = self.txImg16 (self.p_w, self.p_h) = (self.p16_w,self.p16_h) else: tx = self.txImg8 (self.p_w, self.p_h) = (self.p8_w,self.p8_h) if(p.color_changed): sdl2.SDL_SetTextureColorMod(tx.texture, p.r,p.g,p.b) p.color_changed = False # sdl2.SDL_SetTextureAlphaMod(tx.texture, 192) #int(p.a)) if(p.alpha_changed): sdl2.SDL_SetTextureAlphaMod(tx.texture, int(p.a)) p.alpha_changed = False if(destination_texture is None): sdl2_DisplayManager.inst().screen_blit(tx, x=p.x, y=p.y, expand_to_fill=False) else: sdl2_DisplayManager.inst().blit(source_tx = tx, dest_tx=destination_texture, dest=(p.x,p.y,self.p_w, self.p_h))
def fill_rect(self, x,y,w,h,c): if(len(c)==3): c = (c[0],c[1],c[2],255) old = sdl2_DisplayManager.inst().switch_target(self.pySurface) sdl2_DisplayManager.inst().fill((int(x),int(y),int(w),int(h)), c) sdl2_DisplayManager.inst().switch_target(old)
def copy(self): """Returns a copy of itself.""" # frame = Frame(self.width, self.height, from_surface=self.pySurface.copy()) #frame.pySurface.blit(self.pySurface, (0,0,self.width,self.height), (0,0,self.width,self.height), special_flags = 0) frame = Frame(self.width, self.height) sdl2_DisplayManager.inst().blit(source_tx=self.pySurface, dest_tx=frame.pySurface, dest=(0,0,self.width,self.height), area=(0,0,self.width,self.height), special_flags = 0) #frame.set_data(self.get_data()) return frame
def tint(self, r,g,b): """ returns a copy of a Frame, tinted with the set R, G, B amounts. Useful for cheap effects """ frame = Frame(self.width, self.height) dframe = self.copy() frame.fill_rect(0,0,self.width,self.height,(r,g,b)) # sdl2_DisplayManager.inst().blit(source_tx=self.pySurface, dest_tx=frame.pySurface, dest=(0,0,self.width,self.height), area=(0,0,self.width,self.height), special_flags = 0,blendmode='MOD') sdl2_DisplayManager.inst().blit(source_tx=frame.pySurface, dest_tx=dframe.pySurface, dest=(0,0,self.width,self.height), area=(0,0,self.width,self.height), special_flags = 0,blendmode='MOD') #frame.set_data(self.get_data()) return dframe
def rotozoom(self,rotation=0, scale=1, origin=None): """ Returns a rotated and possibly scaled version of the original frame """ # resizes a frame... (sw, sh) = (self.width*scale, self.height*scale) dstrect = (0, 0, int(sw), int(sh)) F = Frame(self.width, self.height) sdl2_DisplayManager.inst().roto_blit(self.pySurface, F.pySurface, dstrect, None, angle=rotation, origin=origin) return F
def main(): sdl2_DisplayManager.Init(450,225,2) # # FIRE # def emitter_dx(x): return random.randint(-10,10) # def emitter_dy(x): return random.randint(-3,3) # def dither_dx(x): return random.randint(-10,0) # def dither_dy(x): return (x - 0.35) #ps = ParticleEmitter(450,225, max_life=20, max_particles=400, particles_per_update=20, particle_class=Particle) ps1 = ParticleEmitter(450, 225, max_life=20, max_particles=200, particles_per_update=40, total_creations=None, particle_class=FireParticle) # ps2 = ParticleEmitter(250, 115, max_life=30, max_particles=500, particles_per_update=40, total_creations=2000, particle_class=SnowParticle) # ps2 = ParticleEmitter(450, 0, max_life=35, max_particles=500, particles_per_update=16, total_creations=None, particle_class=SnowParticle) ps2 = ParticleEmitter(20, 20, max_life=20, max_particles=300, particles_per_update=100, total_creations=300, particle_class=FireworkParticle, random_next=True) ps3 = ParticleEmitter(300, 220, max_life=20, max_particles=300, particles_per_update=100, total_creations=300, particle_class=FireworkParticle, random_next=True) # ps = ParticleSystem(emitters=[ps1,ps2, ps3]) ps = ParticleSystem(emitters=[ps1, ps2, ps3]) # def emitter_dx(x): return random.randint(-10,10) # def emitter_dy(y): return random.randint(-6,6) # def dither_dx(x): return x #random.randint(-6,6) # def dither_dy(y): return y #(x - 0.35) # ps = ParticleEmitter(450,200, max_life=20, max_particles=200, particles_per_update=5, emitter_dx_fn=emitter_dx, emitter_dy_fn=emitter_dy, dither_dx_fn=dither_dx, dither_dy_fn=dither_dy) running = True while running: events = sdl2.ext.get_events() for event in events: if event.type == sdl2.SDL_QUIT: running = False break if event.type == sdl2.SDL_KEYDOWN: if event.key.keysym.sym == sdl2.SDLK_r: ps.reset() if event.key.keysym.sym == sdl2.SDLK_ESCAPE: running = False break ps.update() ps.draw() sdl2_DisplayManager.inst().flip() sdl2.SDL_Delay(33) sdl2_DisplayManager.inst().clear() return 0
def main(): sdl2_DisplayManager.Init(450, 225, 2) # FIRE def emitter_dx(x): return random.randint(-10, 10) def emitter_dy(x): return random.randint(-3, 3) def dither_dx(x): return random.randint(-10, 0) def dither_dy(x): return x - 0.35 ps = ParticleSystem( 450, 225, max_life=20, max_particles=400, particles_per_update=20, emitter_dx_fn=emitter_dx, emitter_dy_fn=emitter_dy, dither_dx_fn=dither_dx, dither_dy_fn=dither_dy, ) # def emitter_dx(x): return random.randint(-10,10) # def emitter_dy(y): return random.randint(-6,6) # def dither_dx(x): return x #random.randint(-6,6) # def dither_dy(y): return y #(x - 0.35) # ps = ParticleSystem(450,200, max_life=20, max_particles=200, particles_per_update=5, emitter_dx_fn=emitter_dx, emitter_dy_fn=emitter_dy, dither_dx_fn=dither_dx, dither_dy_fn=dither_dy) running = True while running: events = sdl2.ext.get_events() for event in events: if event.type == sdl2.SDL_QUIT: running = False break ps.update() ps.draw() sdl2_DisplayManager.inst().flip() sdl2.SDL_Delay(33) sdl2_DisplayManager.inst().clear() return 0
def copy(self): """Returns a copy of itself.""" # frame = Frame(self.width, self.height, from_surface=self.pySurface.copy()) #frame.pySurface.blit(self.pySurface, (0,0,self.width,self.height), (0,0,self.width,self.height), special_flags = 0) frame = Frame(self.width, self.height) sdl2_DisplayManager.inst().blit(source_tx=self.pySurface, dest_tx=frame.pySurface, dest=(0, 0, self.width, self.height), area=(0, 0, self.width, self.height), special_flags=0) #frame.set_data(self.get_data()) return frame
def tint(self, r, g, b): """ returns a copy of a Frame, tinted with the set R, G, B amounts. Useful for cheap effects """ frame = Frame(self.width, self.height) dframe = self.copy() frame.fill_rect(0, 0, self.width, self.height, (r, g, b)) # sdl2_DisplayManager.inst().blit(source_tx=self.pySurface, dest_tx=frame.pySurface, dest=(0,0,self.width,self.height), area=(0,0,self.width,self.height), special_flags = 0,blendmode='MOD') sdl2_DisplayManager.inst().blit(source_tx=frame.pySurface, dest_tx=dframe.pySurface, dest=(0, 0, self.width, self.height), area=(0, 0, self.width, self.height), special_flags=0, blendmode='MOD') #frame.set_data(self.get_data()) return dframe
def populate_from_mp4_file(self, file): if (cv2 is None): raise ValueError, "MP4 is unavailable as OpenCV is not installed" vc = cv2.VideoCapture(file) self.width = int(vc.get(cv.CV_CAP_PROP_FRAME_WIDTH)) self.height = int(vc.get(cv.CV_CAP_PROP_FRAME_HEIGHT)) frame_count = int(vc.get(cv.CV_CAP_PROP_FRAME_COUNT)) #vc.set(cv.CV_CAP_PROP_CONVERT_RGB, True) #print "width:" + str(self.width) + " Height: " + str(self.height) + "frame count: " + str(frame_count) for i in range(frame_count): rval, video_frame = vc.read() if rval is not None: video_frame = cv2.cvtColor(video_frame, cv2.cv.CV_BGR2RGB) the_frame = cv.fromarray(video_frame) # surface = pygame.image.frombuffer(the_frame.tostring(), (self.movie.width, self.movie.height), 'RGB') surf = sdl2_DisplayManager.inst().make_texture_from_imagebits( bits=the_frame.tostring(), width=self.width, height=self.height, mode='RGB', composite_op=None) new_frame = Frame(self.width, self.height, from_surface=surf) self.frames.append(new_frame) vc.release()
def rotozoom(self, rotation=0, scale=1, origin=None): """ Returns a rotated and possibly scaled version of the original frame """ # resizes a frame... (sw, sh) = (self.width * scale, self.height * scale) dstrect = (0, 0, int(sw), int(sh)) F = Frame(self.width, self.height) sdl2_DisplayManager.inst().roto_blit(self.pySurface, F.pySurface, dstrect, None, angle=rotation, origin=origin) return F
def drawHD(self, frame, text, x, y, line_color, line_width, interior_color, fill_color): """Uses this font's characters to draw the given string at the given position.""" #t = self.pygFont.render(text,False,(255,0,255),(0,0,0)) # print("drawHD(%s) - line color=%s | line width=%d" % (text,line_color, line_width )) if(line_color is None or line_width == 0): return self.draw(frame, text, x, y, interior_color, fill_color) # if(interior_color is None): # interior_color=(255,255,255) # surf = self.pygFont.render(text,False,color,(0,0,0)) # (w,h) = surf.get_size() if(text is None or text==""): return x surf = sdl2_DisplayManager.inst().font_render_bordered_text(text, font_alias=self.name, size=self.font_size, width=None, color=interior_color, bg_color=fill_color, border_color=line_color, border_width=line_width) (w,h) = surf.size tmp = Frame(w,h, from_surface=surf) # tmp.composite_op = "blacksrc" # w = min(w, frame.width) # h = min(h, frame.height) Frame.copy_rect(dst=frame, dst_x=x, dst_y=y, src=tmp, src_x=0, src_y=0, width=w, height=h, op=self.composite_op) #Frame.copy_rect(dst=frame, dst_x=x, dst_y=y, src=self.bitmap, src_x=char_x, src_y=char_y, width=width, height=self.char_size, op=self.composite_op) return x+w
def populate_from_image_file(self, path, f, composite_op = None): if not Image: raise RuntimeError, 'Cannot open non-native image types without Python Imaging Library: %s' % (path) src = Image.open(f) (w, h) = src.size # print ("conversion of image, sized " + str(w) + "," + str(h)) if len(self.frames) > 0 and (w != self.width or h != self.height): raise ValueError, "Image sizes must be uniform! Anim is %dx%d, image is %dx%d" % (w, h, self.width, self.height) (self.width, self.height) = (w, h) # I'm punting on animated gifs, because they're too slow. If you coalesce them # via image magic then they are fast again. if path.endswith('.gif'): import animgif self.frames += animgif.gif_frames(src, composite_op = composite_op) else: (w,h) = src.size if src.mode == "P": src.convert("RGB") src.mode = "RGB" surf = sdl2_DisplayManager.inst().make_texture_from_imagebits(bits=src.tobytes(), width=w, height=h, mode=src.mode, composite_op = composite_op) frame = Frame(w,h,surf) self.frames.append(frame)
def draw(self, frame, text, x, y, color = None, bg_color = None): """Uses this font's characters to draw the given string at the given position.""" #t = self.pygFont.render(text,False,(255,0,255),(0,0,0)) if(color is None): color=(255,255,255) # surf = self.pygFont.render(text,False,color,(0,0,0)) # (w,h) = surf.get_size() if(text is None or text==""): return x surf = sdl2_DisplayManager.inst().font_render_text(text, font_alias=self.name, size=self.font_size, width=None, color=color, bg_color=bg_color) (w,h) = surf.size tmp = Frame(w,h, from_surface=surf) tmp.composite_op = "blacksrc" # w = min(w, frame.width) # h = min(h, frame.height) Frame.copy_rect(dst=frame, dst_x=x, dst_y=y, src=tmp, src_x=0, src_y=0, width=w, height=h, op=self.composite_op) #Frame.copy_rect(dst=frame, dst_x=x, dst_y=y, src=self.bitmap, src_x=char_x, src_y=char_y, width=width, height=self.char_size, op=self.composite_op) return x+w
def build_surface_from_8bit_dmd_string(self, str_data, composite_op=None): self.eight_to_RGB_map = VgaDMD.get_palette_ch() self.font_dots = str_data x = 0 y = 0 d = "" for dot in str_data: # get the Byte from the frame data dot = ord(dot) # convert it to the correct RGB pallette color (r, g, b) = self.eight_to_RGB_map[dot] d += r + g + b # # write out the color into the target buffer array # index = y*self.y_offset + x*self.x_offset # #self.pySurface[index:(index+bytes_per_pixel)] = (b,g,r,0) # buffer_interface[index:(index+4)] = (b,g,r,0) # #move to the next dot # x += 1 # if x == self.width: # x = 0 # y += 1 #self.pySurface = pygame.image.fromstring(d,(self.width,self.height),'RGB').convert() # surf = HD_load_file(path) self.pySurface = sdl2_DisplayManager.inst( ).make_texture_from_imagebits(bits=d, width=self.width, height=self.height, mode='RGB', composite_op=composite_op)
def build_surface_from_8bit_dmd_string(self, str_data, composite_op=None): self.eight_to_RGB_map = VgaDMD.get_palette_ch() self.font_dots = str_data x = 0 y = 0 d = "" for dot in str_data: # get the Byte from the frame data dot = ord(dot) # convert it to the correct RGB pallette color (r,g,b) = self.eight_to_RGB_map[dot] d +=r + g + b # # write out the color into the target buffer array # index = y*self.y_offset + x*self.x_offset # #self.pySurface[index:(index+bytes_per_pixel)] = (b,g,r,0) # buffer_interface[index:(index+4)] = (b,g,r,0) # #move to the next dot # x += 1 # if x == self.width: # x = 0 # y += 1 #self.pySurface = pygame.image.fromstring(d,(self.width,self.height),'RGB').convert() # surf = HD_load_file(path) self.pySurface = sdl2_DisplayManager.inst().make_texture_from_imagebits(bits=d, width=self.width, height=self.height, mode='RGB', composite_op=composite_op)
def main(): import font from font import Font import layers from layers import HDTextLayer, TextLayer import time import sdl2 t0 = time.clock() sdl2_DisplayManager.Init(512,512,1) sdl2_DisplayManager.inst().fonts_init("Courier", "HHSam") sdl2_DisplayManager.inst().font_add(font_path="assets/T2.ttf", font_alias="T2_30", size=30)#, color=None, bgcolor=None) f = sdl2_DisplayManager.inst().font_add(font_path="assets/HH_Samuel.ttf", font_alias="HHSam_30", size=48)#, color=None, bgcolor=None) lChars = [ chr(i+ord(' ')) for i in xrange(0,95)] char_size = 50 frame = Frame(width=512, height=512) interior_color = (255,0,255) line_width = 1 fill_color = (0,0,0) line_color = (0,255,0) for char_offset,c in zip(xrange(0,95),lChars): # surf = f.textHollow(c, line_color, interior_color, line_width, fill_color) surf = sdl2_DisplayManager.inst().font_render_bordered_text(c, font_alias='HHSam_30', size=48, border_width=2, border_color=(255,0,255), color=(0,255,0)) (w,h) = surf.size F = Frame(width=w, height=h, from_surface=surf) char_x = char_size * (char_offset % 10) char_y = char_size * (char_offset / 10) Frame.copy_rect(dst=frame, dst_x=char_x, dst_y=char_y, src=F, src_x=0, src_y=0, width=w, height=h, op='copy') #x += width + self.tracking sdl2_DisplayManager.inst().screen_blit(source_tx=frame.pySurface, expand_to_fill=True)#, area=(10,10,400,200)) ss = sdl2.ext.SoftwareSprite(frame.pySurface) sdl2.SDL_SaveBMP(ss.contents, "file.bmp") sdl2_DisplayManager.inst().flip() sdl2.SDL_Delay(2000)
def __init__(self, x, y, max_life=60, max_particles=200, particles_per_update=5, total_creations=None, particle_class=Particle, random_next=False, dx=0, dy=0): self.x = x self.y = y self.orig_x = x self.orig_y = y self.dx = dx self.dy = dy self.particle_class = particle_class self.random_next = random_next self.particles = list() self.particles_per_update = particles_per_update self.max_particles = max_particles self.max_life = max_life self.total_creations = total_creations self.creations_remaining = total_creations self.stopped = False for i in range(0,particles_per_update): p = self.particle_class(x,y, emitter=self) p.update() self.particles.append(p) if(self.total_creations is not None): self.creations_remaining = self.creations_remaining - particles_per_update else: self.creations_remaining = self.max_particles cwd = os.path.dirname(__file__) sprImg8 = sdl2_DisplayManager.inst().load_surface(os.path.join(cwd,"exp8.png")) sprImg16 = sdl2_DisplayManager.inst().load_surface(os.path.join(cwd,"exp16.png")) self.txImg8 = sdl2_DisplayManager.inst().texture_from_surface(sprImg8) self.txImg16 = sdl2_DisplayManager.inst().texture_from_surface(sprImg16) (self.p8_w,self.p8_h) = self.txImg8.size (self.p16_w,self.p16_h) = self.txImg16.size sdl2.SDL_SetTextureBlendMode(self.txImg8.texture, sdl2.SDL_BLENDMODE_BLEND) sdl2.SDL_SetTextureBlendMode(self.txImg16.texture, sdl2.SDL_BLENDMODE_BLEND) del sprImg8 del sprImg16
def drawHD(self, frame, text, x, y, line_color, line_width, interior_color, fill_color, font_size=None): """Uses this font's characters to draw the given string at the given position.""" #t = self.pygFont.render(text,False,(255,0,255),(0,0,0)) # print("drawHD(%s) - line color=%s | line width=%d" % (text,line_color, line_width )) if (line_color is None or line_width == 0): return self.draw(frame, text, x, y, interior_color, fill_color) # if(interior_color is None): # interior_color=(255,255,255) # surf = self.pygFont.render(text,False,color,(0,0,0)) # (w,h) = surf.get_size() if (text is None or text == ""): return x if (font_size is None): font_size = self.font_size surf = sdl2_DisplayManager.inst().font_render_bordered_text( text, font_alias=self.name, size=font_size, width=None, color=interior_color, bg_color=fill_color, border_color=line_color, border_width=line_width) (w, h) = surf.size tmp = Frame(w, h, from_surface=surf) # tmp.composite_op = "blacksrc" # w = min(w, frame.width) # h = min(h, frame.height) Frame.copy_rect(dst=frame, dst_x=x, dst_y=y, src=tmp, src_x=0, src_y=0, width=w, height=h, op=self.composite_op) #Frame.copy_rect(dst=frame, dst_x=x, dst_y=y, src=self.bitmap, src_x=char_x, src_y=char_y, width=width, height=self.char_size, op=self.composite_op) return x + w
def load_single_frame(self, idx): ## ONLY USE THIS FROM STREAMING LOAD!! path = self.filenames[idx] print("loading %s" % path) tx = sdl2_DisplayManager.inst().load_texture(path, self.composite_op) (self.width,self.height) = tx._size frame = Frame(self.width,self.height,tx) return frame
def scale(self, percentage, new_w=None, new_h=None): # resizes a frame... # raise ValueError, "Scale not yet programmed..." if(new_w is None): new_w = int(percentage * self.width) new_h = int(percentage * self.height) dstrect = (0, 0, int(new_w), int(new_h)) self.width = new_w self.height = new_h F = Frame(new_w, new_h) sdl2_DisplayManager.inst().roto_blit(self.pySurface, F.pySurface, dstrect, None) del self.pySurface self.pySurface = F.pySurface
def __init__( self, x, y, max_life=60, max_particles=200, particles_per_update=5, emitter_dx_fn=None, emitter_dy_fn=None, dither_dx_fn=None, dither_dy_fn=None, ): self.x = x self.y = y self.particles = list() self.particles_per_update = particles_per_update self.max_particles = max_particles self.max_life = max_life self.emitter_dx_fn = emitter_dx_fn self.emitter_dy_fn = emitter_dy_fn self.dither_dx_fn = dither_dx_fn self.dither_dy_fn = dither_dy_fn for i in range(0, particles_per_update): p = Particle(x, y, parent=self) p.update() self.particles.append(p) sprFire = sdl2_DisplayManager.inst().load_surface("assets/dmd/fire.png") sprWhite = sdl2_DisplayManager.inst().load_surface("assets/dmd/white.png") sprYellow = sdl2_DisplayManager.inst().load_surface("assets/dmd/fire_yellow.png") sprSmoke = sdl2_DisplayManager.inst().load_surface("assets/dmd/exp.png") self.txFire = sdl2_DisplayManager.inst().texture_from_surface(sprFire) self.txWhite = sdl2_DisplayManager.inst().texture_from_surface(sprWhite) self.txYellow = sdl2_DisplayManager.inst().texture_from_surface(sprYellow) self.txSmoke = sdl2_DisplayManager.inst().texture_from_surface(sprSmoke) (self.p_w, self.p_h) = self.txSmoke.size sdl2.SDL_SetTextureBlendMode(self.txFire.texture, sdl2.SDL_BLENDMODE_BLEND) sdl2.SDL_SetTextureBlendMode(self.txWhite.texture, sdl2.SDL_BLENDMODE_BLEND) sdl2.SDL_SetTextureBlendMode(self.txYellow.texture, sdl2.SDL_BLENDMODE_BLEND) sdl2.SDL_SetTextureBlendMode(self.txSmoke.texture, sdl2.SDL_BLENDMODE_BLEND) del sprFire del sprWhite del sprYellow del sprSmoke
def scale(self, percentage, new_w=None, new_h=None): # resizes a frame... # raise ValueError, "Scale not yet programmed..." if (new_w is None): new_w = int(percentage * self.width) new_h = int(percentage * self.height) dstrect = (0, 0, int(new_w), int(new_h)) self.width = new_w self.height = new_h F = Frame(new_w, new_h) sdl2_DisplayManager.inst().roto_blit(self.pySurface, F.pySurface, dstrect, None) del self.pySurface self.pySurface = F.pySurface
class HDFont(object): """Object wrapper for a PyGame font. Fonts can be loaded manually, using :meth:`load`, or with the :func:`font_named` utility function which supports searching a font path.""" char_widths = None """Array of dot widths for each character, 0-indexed from <space>. This array is populated by :meth:`load`. You may alter this array in order to update the font and then :meth:`save` it.""" tracking = 0 """Number of dots to adjust the horizontal position between characters, in addition to the last character's width.""" composite_op = 'copy' """Composite operation used by :meth:`draw` when calling :meth:`~pinproc.DMDBuffer.copy_rect`.""" pygFont = None def __init__(self, fontname, size, bold=False, font_file_path=None): super(HDFont, self).__init__() # init pyg # pygame.font.init() # p = pygame.font.match_font(fontname,bold) font_path = font_file_path if (font_path is None): font_path = match_font(fontname) try: p = sdl2_DisplayManager.inst().font_add(font_path=font_path, font_alias=fontname, size=None, color=None, bgcolor=None) except Exception, e: raise ValueError, "Specific font '%s' could not be found on your system or in path '%s' Please install/verify." % ( fontname, font_file_path) if (p == None): raise ValueError, "Specific font '%s' could not be found on your system or in path '%s' Please install/verify." % ( fontname, font_file_path) self.pygFont = p # pygame.font.Font(p,size) self.name = fontname self.font_size = size self.char_widths = [] for i in range(96): self.char_widths += [size] #self.char_widths += [ self.pygFont.size(str(chr(i+32)))[0] ] self.char_size = size #self.pygFont.get_height() (self.font_width, self.font_height) = sdl2_DisplayManager.inst().font_get_size( "Z", self.name, self.font_size)
def draw(self, destination_texture = None): # for p in self.particles: for x in xrange(0,len(self.particles)): #xrange(len(self.particles)-1,0,-1): p = self.particles[x] tx = None # if(p.life > self.max_life * 0.55): # tx = self.txYellow # elif(p.life > self.max_life * 0.25): # tx = self.txFire # else: # tx = self.txWhite tx = self.txSmoke sdl2.SDL_SetTextureColorMod(tx.texture, p.r,p.g,p.b) # sdl2.SDL_SetTextureAlphaMod(tx.texture, 192) #int(p.alpha_value)) sdl2.SDL_SetTextureAlphaMod(tx.texture, int(p.alpha_value)) if(destination_texture is None): sdl2_DisplayManager.inst().screen_blit(tx, x=p.x, y=p.y, expand_to_fill=False) else: sdl2_DisplayManager.inst().blit(source_tx = tx, dest_tx=destination_texture, dest=(p.x,p.y,self.p_w, self.p_h))
def draw_in_rect(self, frame, text, rect=(0,0,128,32), anchor=AnchorCenter): """Draw *text* on *frame* within the given *rect*, aligned in accordance with *anchor*. *rect* is a tuple of length 4: (origin_x, origin_y, height, width). 0,0 is in the upper left (NW) corner. *anchor* is one of: :attr:`~procgame.dmd.AnchorN`, :attr:`~procgame.dmd.AnchorE`, :attr:`~procgame.dmd.AnchorS`, :attr:`~procgame.dmd.AnchorW`, :attr:`~procgame.dmd.AnchorNE`, :attr:`~procgame.dmd.AnchorNW`, :attr:`~procgame.dmd.AnchorSE`, :attr:`~procgame.dmd.AnchorSW`, or :attr:`~procgame.dmd.AnchorCenter` (the default). """ origin_x, origin_y, width, height = rect surf = sdl2_DisplayManager.inst().font_render_text(text, font_alias=self.name, size=self.font_size, width=None, color=None, bg_color=None) text_width, text_height = surf.size tmp = Frame(text_width,text_height, from_surface=surf) tmp.composite_op = "blacksrc" x = 0 y = 0 # print "Size: %d x %d" % (text_height) if anchor & AnchorN: y = origin_y elif anchor & AnchorS: y = origin_y + (height - text_height) else: y = origin_y + (height/2.0 - text_height/2.0) if anchor & AnchorW: x = origin_x elif anchor & AnchorE: x = origin_x + (width - text_width) else: x = origin_x + (width/2.0 - text_width/2.0) # w = min(width, frame.width) # h = min(height, frame.height) w = text_width #min(width, frame.width) h = text_height#min(height, frame.height) # self.draw(frame=frame, text=text, x=x, y=y) Frame.copy_rect(dst=frame, dst_x=x, dst_y=y, src=tmp, src_x=0, src_y=0, width=w, height=h, op=tmp.composite_op)
def draw(self, destination_texture=None): # for p in self.particles: for x in xrange(0, len(self.particles)): # xrange(len(self.particles)-1,0,-1): p = self.particles[x] tx = None # if(p.life > self.max_life * 0.55): # tx = self.txYellow # elif(p.life > self.max_life * 0.25): # tx = self.txFire # else: # tx = self.txWhite tx = self.txSmoke sdl2.SDL_SetTextureColorMod(tx.texture, p.r, p.g, p.b) # sdl2.SDL_SetTextureAlphaMod(tx.texture, 192) #int(p.alpha_value)) sdl2.SDL_SetTextureAlphaMod(tx.texture, int(p.alpha_value)) if destination_texture is None: sdl2_DisplayManager.inst().screen_blit(tx, x=p.x, y=p.y, expand_to_fill=False) else: sdl2_DisplayManager.inst().blit( source_tx=tx, dest_tx=destination_texture, dest=(p.x, p.y, self.p_w, self.p_h) )
def __init__(self, fontname, size, bold = False, font_file_path = None): super(HDFont, self).__init__() # init pyg # pygame.font.init() # p = pygame.font.match_font(fontname,bold) font_path = font_file_path if(font_path is None): font_path = match_font(fontname) p = sdl2_DisplayManager.inst().font_add(font_path=font_path, font_alias=fontname, size=None, color=None, bgcolor=None) if(p==None): raise ValueError, "Specific font could not be found on your system. Please install '" + fontname + "'." self.pygFont = p # pygame.font.Font(p,size) self.name = fontname self.font_size=size self.char_widths = [] for i in range(96): self.char_widths += [size] #self.char_widths += [ self.pygFont.size(str(chr(i+32)))[0] ] self.char_size = size #self.pygFont.get_height() (self.font_width, self.font_height) = sdl2_DisplayManager.inst().font_get_size("Z", self.name, self.font_size)
def draw(self, frame, text, x, y, color=None, bg_color=None, font_size=None): """Uses this font's characters to draw the given string at the given position.""" #t = self.pygFont.render(text,False,(255,0,255),(0,0,0)) if (color is None): color = (255, 255, 255) # surf = self.pygFont.render(text,False,color,(0,0,0)) # (w,h) = surf.get_size() if (text is None or text == ""): return x if (font_size is None): font_size = self.font_size surf = sdl2_DisplayManager.inst().font_render_text( text, font_alias=self.name, size=font_size, width=None, color=color, bg_color=bg_color) (w, h) = surf.size tmp = Frame(w, h, from_surface=surf) tmp.composite_op = "blacksrc" # w = min(w, frame.width) # h = min(h, frame.height) Frame.copy_rect(dst=frame, dst_x=x, dst_y=y, src=tmp, src_x=0, src_y=0, width=w, height=h, op=self.composite_op) #Frame.copy_rect(dst=frame, dst_x=x, dst_y=y, src=self.bitmap, src_x=char_x, src_y=char_y, width=width, height=self.char_size, op=self.composite_op) return x + w
def __init__(self, width, height, from_surface=None): """Initializes the frame to the given `width` and `height`.""" # super(Frame, self).__init__(width, height) width = int(width) height = int(height) self.width = width self.height = height if(from_surface is None): self.pySurface = sdl2_DisplayManager.inst().new_texture(width, height) #pygame.surface.Surface((width, height)) #self.clear() -- don't need this, the new_texture function does this for us. else: self.pySurface = from_surface self.x_offset = 4 #bytes_per_pixel self.y_offset = self.width*4 # every y_offset Bytes is the next line self.font_dots = []
def populate_from_dmd_file(self, f, composite_op=None): f.seek(0, os.SEEK_END) # Go to the end of the file to get its length file_length = f.tell() f.seek(0) # Skip back to the 4 byte DMD header. dmd_version = struct.unpack("I", f.read(4))[0] dmd_style = 0 # old if (dmd_version == 0x00646D64): # print("old dmd style") pass elif (dmd_version == 0x00DEFACE): # print("full color dmd style") dmd_style = 1 frame_count = struct.unpack("I", f.read(4))[0] self.width = struct.unpack("I", f.read(4))[0] self.height = struct.unpack("I", f.read(4))[0] if (dmd_style == 0): if file_length != 16 + self.width * self.height * frame_count: logging.getLogger('game.dmdcache').warning(f) logging.getLogger('game.dmdcache').warning( "expected size = {%d} got {%d}", (16 + self.width * self.height * frame_count), (file_length)) raise ValueError, "File size inconsistent with original DMD format header information. Old or incompatible file format?" elif (dmd_style == 1): if file_length != 16 + self.width * self.height * frame_count * 3: logging.getLogger('game.dmdcache').warning(f) raise ValueError, "File size inconsistent with true-color DMD format header information. Old or incompatible file format?" for frame_index in range(frame_count): new_frame = Frame(self.width, self.height) if (dmd_style == 0): str_frame = f.read(self.width * self.height) new_frame.build_surface_from_8bit_dmd_string( str_frame, composite_op) elif (dmd_style == 1): str_frame = f.read(self.width * self.height * 3) new_frame.pySurface = sdl2_DisplayManager.inst( ).make_texture_from_imagebits(bits=str_frame, width=self.width, height=self.height, composite_op=composite_op) if (frame_index == 1): new_frame.font_dots = str_frame[0:97] self.frames.append(new_frame)
def __init__(self, width, height, from_surface=None): """Initializes the frame to the given `width` and `height`.""" # super(Frame, self).__init__(width, height) width = int(width) height = int(height) self.width = width self.height = height if (from_surface is None): self.pySurface = sdl2_DisplayManager.inst().new_texture( width, height) #pygame.surface.Surface((width, height)) #self.clear() -- don't need this, the new_texture function does this for us. else: self.pySurface = from_surface self.x_offset = 4 #bytes_per_pixel self.y_offset = self.width * 4 # every y_offset Bytes is the next line self.font_dots = []
def __init__(self, x, y, max_life=60, max_particles=200, particles_per_update=5, emitter_dx_fn=None, emitter_dy_fn=None, dither_dx_fn=None, dither_dy_fn=None): self.x = x self.y = y self.particles = list() self.particles_per_update = particles_per_update self.max_particles = max_particles self.max_life = max_life self.emitter_dx_fn = emitter_dx_fn self.emitter_dy_fn = emitter_dy_fn self.dither_dx_fn = dither_dx_fn self.dither_dy_fn = dither_dy_fn for i in range(0,particles_per_update): p = Particle(x,y, parent=self) p.update() self.particles.append(p) sprFire = sdl2_DisplayManager.inst().load_surface("assets/dmd/fire.png") sprWhite = sdl2_DisplayManager.inst().load_surface("assets/dmd/white.png") sprYellow = sdl2_DisplayManager.inst().load_surface("assets/dmd/fire_yellow.png") sprSmoke = sdl2_DisplayManager.inst().load_surface("assets/dmd/exp.png") self.txFire = sdl2_DisplayManager.inst().texture_from_surface(sprFire) self.txWhite = sdl2_DisplayManager.inst().texture_from_surface(sprWhite) self.txYellow = sdl2_DisplayManager.inst().texture_from_surface(sprYellow) self.txSmoke = sdl2_DisplayManager.inst().texture_from_surface(sprSmoke) (self.p_w,self.p_h) = self.txSmoke.size sdl2.SDL_SetTextureBlendMode(self.txFire.texture, sdl2.SDL_BLENDMODE_BLEND) sdl2.SDL_SetTextureBlendMode(self.txWhite.texture, sdl2.SDL_BLENDMODE_BLEND) sdl2.SDL_SetTextureBlendMode(self.txYellow.texture, sdl2.SDL_BLENDMODE_BLEND) sdl2.SDL_SetTextureBlendMode(self.txSmoke.texture, sdl2.SDL_BLENDMODE_BLEND) del sprFire del sprWhite del sprYellow del sprSmoke
def drawHD(self, frame, text, x, y, line_color, line_width, interior_color, fill_color, font_size=None): """Uses this font's characters to draw the given string at the given position.""" #t = self.pygFont.render(text,False,(255,0,255),(0,0,0)) # print("drawHD(%s) - line color=%s | line width=%d" % (text,line_color, line_width )) if (line_color is None or line_width == 0): return self.draw(frame, text, x, y, interior_color, fill_color) # if(interior_color is None): # interior_color=(255,255,255) # surf = self.pygFont.render(text,False,color,(0,0,0)) # (w,h) = surf.get_size() if (text is None or text == ""): return x if (font_size is None): font_size = self.font_size x = int(x) y = int(y) w = sdl2_DisplayManager.inst().font_render_bordered_text_Faster( frame.pySurface, { 'x': x, 'y': y }, text, font_alias=self.name, size=font_size, width=None, color=interior_color, bg_color=fill_color, border_color=line_color, border_width=line_width) return x + w
def __init__(self, fontname, size, bold=False, font_file_path=None): super(HDFont, self).__init__() # init pyg # pygame.font.init() # p = pygame.font.match_font(fontname,bold) font_path = font_file_path if (font_path is None): font_path = match_font(fontname) try: p = sdl2_DisplayManager.inst().font_add(font_path=font_path, font_alias=fontname, size=None, color=None, bgcolor=None) except Exception, e: raise ValueError, "Specific font '%s' could not be found on your system or in path '%s' Please install/verify." % ( fontname, font_file_path)
def populate_from_dmd_file(self, f, composite_op = None): f.seek(0, os.SEEK_END) # Go to the end of the file to get its length file_length = f.tell() ## MJO: Don't just skip the header! Check it. f.seek(0) # Skip over the 4 byte DMD header. dmd_version = struct.unpack("I", f.read(4))[0] dmd_style = 0 # old if(dmd_version == 0x00646D64): # print("old dmd style") pass elif(dmd_version == 0x00DEFACE): # print("full color dmd style") dmd_style = 1 frame_count = struct.unpack("I", f.read(4))[0] self.width = struct.unpack("I", f.read(4))[0] self.height = struct.unpack("I", f.read(4))[0] if(dmd_style==0): if file_length != 16 + self.width * self.height * frame_count: logging.getLogger('game.dmdcache').warning(f) logging.getLogger('game.dmdcache').warning("expected size = {%d} got {%d}", (16 + self.width * self.height * frame_count), (file_length)) raise ValueError, "File size inconsistent with original DMD format header information. Old or incompatible file format?" elif(dmd_style==1): if file_length != 16 + self.width * self.height * frame_count * 3: logging.getLogger('game.dmdcache').warning(f) raise ValueError, "File size inconsistent with true-color DMD format header information. Old or incompatible file format?" for frame_index in range(frame_count): new_frame = Frame(self.width, self.height) if(dmd_style==0): str_frame = f.read(self.width * self.height) new_frame.build_surface_from_8bit_dmd_string(str_frame, composite_op) elif(dmd_style==1): # print("LOADING FRAME...") str_frame = f.read(self.width * self.height * 3) # surface = pygame.image.fromstring(str_frame, (self.width, self.height), 'RGB') # new_frame.set_surface(surface) # https://wiki.libsdl.org/SDL_CreateRGBSurfaceFrom?? new_frame.pySurface = sdl2_DisplayManager.inst().make_texture_from_imagebits(bits=str_frame, width=self.width, height=self.height, composite_op=composite_op) # print("made texture for this frame -- contents: " + str(new_frame.pySurface)) if(frame_index==1): new_frame.font_dots = str_frame[0:97] self.frames.append(new_frame)
def populate_from_dmd_file(self, f, composite_op = None): f.seek(0, os.SEEK_END) # Go to the end of the file to get its length file_length = f.tell() ## MJO: Don't just skip the header! Check it. f.seek(0) # Skip over the 4 byte DMD header. dmd_version = struct.unpack("I", f.read(4))[0] dmd_style = 0 # old if(dmd_version == 0x00646D64): # print("old dmd style") pass elif(dmd_version == 0x00DEFACE): # print("full color dmd style") dmd_style = 1 frame_count = struct.unpack("I", f.read(4))[0] self.width = struct.unpack("I", f.read(4))[0] self.height = struct.unpack("I", f.read(4))[0] if(dmd_style==0): if file_length != 16 + self.width * self.height * frame_count: logging.getLogger('game.dmdcache').warning(f) logging.getLogger('game.dmdcache').warning("expected size = {%d} got {%d}", (16 + self.width * self.height * frame_count), (file_length)) raise ValueError, "File size inconsistent with original DMD format header information. Old or incompatible file format?" elif(dmd_style==1): if file_length != 16 + self.width * self.height * frame_count * 3: logging.getLogger('game.dmdcache').warning(f) raise ValueError, "File size inconsistent with true-color DMD format header information. Old or incompatible file format?" for frame_index in range(frame_count): new_frame = Frame(self.width, self.height) if(dmd_style==0): str_frame = f.read(self.width * self.height) new_frame.build_surface_from_8bit_dmd_string(str_frame, composite_op) elif(dmd_style==1): # print("LOADING FRAME...") str_frame = f.read(self.width * self.height * 3) # surface = pygame.image.fromstring(str_frame, (self.width, self.height), 'RGB') # new_frame.set_surface(surface) # https://wiki.libsdl.org/SDL_CreateRGBSurfaceFrom?? new_frame.pySurface = sdl2_DisplayManager.inst().make_texture_from_imagebits(bits=str_frame, width=self.width, height=self.height, composite_op=composite_op) # print("made texture for this frame -- contents: " + str(new_frame.pySurface)) if(frame_index==1): new_frame.font_dots = str_frame[0:97] self.frames.append(new_frame)
def populate_from_mp4_file(self,file): if(cv2 is None): raise ValueError, "MP4 is unavailable as OpenCV is not installed" vc = cv2.VideoCapture(file) self.width = int(vc.get(cv.CV_CAP_PROP_FRAME_WIDTH)) self.height = int(vc.get(cv.CV_CAP_PROP_FRAME_HEIGHT)) frame_count = int(vc.get(cv.CV_CAP_PROP_FRAME_COUNT)) #vc.set(cv.CV_CAP_PROP_CONVERT_RGB, True) #print "width:" + str(self.width) + " Height: " + str(self.height) + "frame count: " + str(frame_count) for i in range(frame_count): rval, video_frame = vc.read() if rval is not None: video_frame = cv2.cvtColor(video_frame,cv2.cv.CV_BGR2RGB) the_frame = cv.fromarray(video_frame) # surface = pygame.image.frombuffer(the_frame.tostring(), (self.movie.width, self.movie.height), 'RGB') surf = sdl2_DisplayManager.inst().make_texture_from_imagebits(bits=the_frame.tostring(), width=self.width, height=self.height, mode='RGB', composite_op = None) new_frame = Frame(self.width, self.height, from_surface=surf) self.frames.append(new_frame) vc.release()
def populate_from_image_file(self, path, f, composite_op = None): if not Image: raise RuntimeError, 'Cannot open non-native image types without Python Imaging Library: %s' % (path) src = Image.open(f) (w, h) = src.size # print ("conversion of image, sized " + str(w) + "," + str(h)) if len(self.frames) > 0 and (w != self.width or h != self.height): raise ValueError, "Image sizes must be uniform! Anim is %dx%d, image is %dx%d" % (w, h, self.width, self.height) (self.width, self.height) = (w, h) # I'm punting on animated gifs, because they're too slow. If you coalesce them # via image magic then they are fast again. if path.endswith('.gif'): import animgif self.frames += animgif.gif_frames(src, composite_op = composite_op) else: #frame = Animation.convertImage(src) (w,h) = src.size if src.mode == "P": src.convert("RGB") src.mode = "RGB" # surf = HD_load_file(path) surf = sdl2_DisplayManager.inst().make_texture_from_imagebits(bits=src.tostring(), width=w, height=h, mode=src.mode, composite_op = composite_op) #print("ConvertImage made texture for this frame -- contents: " + str(surf)) frame = Frame(w,h,surf) self.frames.append(frame)
def main(): # RESOURCES = sdl2.ext.Resources(__file__, "resources") # Uint32 * pixels = new Uint32[640 * 480]; # SDL_UpdateTexture(texture, NULL, pixels, 640 * sizeof(Uint32)); sdl2_DisplayManager.Init(450, 225, 2) sw_sprite = sdl2_DisplayManager.inst().load_surface("assets/dmd/smile.png") # sprite = HD_load_file("hello.bmp") # sprite2 = HD_load_file("transparent_test.png") # dot_sprite = HD_load_file("dmdgrid32x32_v1.png") sprite = sdl2_DisplayManager.inst().texture_from_surface(sw_sprite) f = Frame(20, 20, from_surface=sprite) f.x_offset = 400 sdl2_DisplayManager.inst().screen_blit(source_tx=f.pySurface) sdl2_DisplayManager.inst().flip() import sdl2 sdl2.SDL_Delay(1000) pass
def main(): # RESOURCES = sdl2.ext.Resources(__file__, "resources") # Uint32 * pixels = new Uint32[640 * 480]; # SDL_UpdateTexture(texture, NULL, pixels, 640 * sizeof(Uint32)); sdl2_DisplayManager.Init(450,225,2) sw_sprite = sdl2_DisplayManager.inst().load_surface("assets/dmd/smile.png") # sprite = HD_load_file("hello.bmp") # sprite2 = HD_load_file("transparent_test.png") # dot_sprite = HD_load_file("dmdgrid32x32_v1.png") sprite = sdl2_DisplayManager.inst().texture_from_surface(sw_sprite) f = Frame(20,20,from_surface=sprite) f.x_offset=400 sdl2_DisplayManager.inst().screen_blit(source_tx=f.pySurface) sdl2_DisplayManager.inst().flip() import sdl2 sdl2.SDL_Delay(1000) pass
def copy_rect(dst, dst_x, dst_y, src, src_x, src_y, width, height, op="copy", blendmode=None, alpha=None, dest_rect=None): """Static method which performs some type checking before calling :meth:`pinproc.DMDBuffer.copy_to_rect`.""" # print "copy_rect(dst_x=%d, dst_y=%d, src_x=%d, src_y=%d, width=%d, height=%d)" % (dst_x, dst_y, src_x, src_y, width, height) # src_rect = pygame.Rect(int(src_x),int(src_y),int(width),int(height)) # dst_rect = pygame.Rect(int(dst_x),int(dst_y),int(width),int(height)) src_rect = (int(src_x), int(src_y), int(width), int(height)) # if(width > src.width): # width = src.width # if(height > src.height): # height = src.height if (dest_rect is None): dest_rect = (int(dst_x), int(dst_y), int(width), int(height)) # """ 1.8.0: BLEND_ADD, BLEND_SUB, BLEND_MULT, BLEND_MIN, BLEND_MAX # in 1.8.1: BLEND_RGBA_ADD, BLEND_RGBA_SUB, BLEND_RGBA_MULT, BLEND_RGBA_MIN, # BLEND_RGBA_MAX BLEND_RGB_ADD, BLEND_RGB_SUB, BLEND_RGB_MULT, # BLEND_RGB_MIN, BLEND_RGB_MAX # """ # if (op=="copy"): # special_flags = 0 # elif(op=="add"): # special_flags = pygame.BLEND_ADD # elif(op == "sub"): # special_flags = pygame.BLEND_SUB # elif(op == "mult"): # special_flags = pygame.BLEND_MULT # elif(op == "min"): # special_flags = pygame.BLEND_MIN # elif(op == "max"): # special_flags = pygame.BLEND_MAX # elif(op=="rgb_add"): # special_flags = pygame.BLEND_RGB_ADD # elif(op == "rgb_sub"): # special_flags = pygame.BLEND_RGB_SUB # elif(op == "rgb_mult"): # special_flags = pygame.BLEND_RGB_MULT # elif(op == "blacksrc"): # special_flags = 0; # src.pySurface.set_colorkey((0,0,0)) # elif(op == "magentasrc"): # special_flags = 0; # src.pySurface.set_colorkey((255,0,255)) # elif(op == "greensrc"): # special_flags = 0; # src.pySurface.set_colorkey((0,255,0)) # elif(op == "alpha"): # special_flags = 0 # # TODO: THIS... # elif(op == "alphaboth"): # special_flags = 0 # # TODO: THIS... # else: # raise ValueError, "Operation type not recognized." #HD_copy_texture(src.pySurface, dst.pySurface, src_rect, dst_rect) sdl2_DisplayManager.inst().blit(source_tx=src.pySurface, dest_tx=dst.pySurface, dest=dest_rect, area=src_rect, special_flags=0, blendmode=blendmode, alpha=alpha)
def size(self, text): """Returns a tuple of the width and height of this text as rendered with this font.""" #raise ValueError, "Size is not supported in HDText (yet)" return sdl2_DisplayManager.inst().font_get_size( text, self.name, self.font_size)
def clear(self, color=(0, 0, 0, 0)): sdl2_DisplayManager.inst().texture_clear(self.pySurface, color)
def populate_from_image_file_sdl2(self, path, f, composite_op=None): # print("loading %s" % f) tx = sdl2_DisplayManager.inst().load_texture(path, composite_op) (self.width, self.height) = tx._size frame = Frame(self.width, self.height, tx) self.frames.append(frame)
def clear(self, color=(0,0,0,0)): sdl2_DisplayManager.inst().texture_clear(self.pySurface, color)
def size(self, text): """Returns a tuple of the width and height of this text as rendered with this font.""" #raise ValueError, "Size is not supported in HDText (yet)" return sdl2_DisplayManager.inst().font_get_size( text, self.name, self.font_size)
def main(): from os import sys if len(sys.argv) <= 1: show_commandline_help() import font from font import Font import layers from layers import HDTextLayer, TextLayer import time import sdl2 t0 = time.clock() import ctypes from ctypes import byref, cast, POINTER, c_int, c_float, sizeof, c_uint32, c_double, c_voidp, c_void_p from sdl2 import endian exp_font_name = sys.argv[1] font_size = 48 if len(sys.argv) > 2: font_size = int(sys.argv[2]) sdl2_DisplayManager.Init(10, 10, 1) sdl2_DisplayManager.inst().fonts_init("Courier", "HHSam") font_path = match_font(exp_font_name) #"coalition") sdl2_DisplayManager.inst().font_add( font_path=font_path, font_alias="export_font", size=font_size) #, color=None, bgcolor=None) char_size = 0 lChars = [chr(i + ord(' ')) for i in xrange(0, 95)] for char_offset, c in zip(xrange(0, 95), lChars): # surf = f.textHollow(c, line_color, interior_color, line_width, fill_color) (font_width, font_height) = sdl2_DisplayManager.inst().font_get_size( c, 'export_font', font_size) char_size = max(char_size, font_width, font_height) width = height = char_size * 10 font_sizes = '' # hack stuff to re-attach the correctly sized window sdl2_DisplayManager.inst().window = sdl2.ext.Window("Font Preview", size=(width, height)) sdl2_DisplayManager.inst().texture_renderer = sdl2.ext.Renderer( sdl2_DisplayManager.inst().window) sdl2_DisplayManager.inst().fill = sdl2_DisplayManager.inst( ).texture_renderer.fill sdl2_DisplayManager.inst().clear = sdl2_DisplayManager.inst( ).texture_renderer.clear sdl2_DisplayManager.inst().factory = sdl2.ext.SpriteFactory( renderer=sdl2_DisplayManager.inst().texture_renderer) sdl2_DisplayManager.inst().show_window() frame = Frame(width, height) #BGR? interior_color = (255, 255, 255) line_width = 0 #fill_color = (255,0,0) line_color = (1, 1, 1) for char_offset, c in zip(xrange(0, 95), lChars): # surf = f.textHollow(c, line_color, interior_color, line_width, fill_color) char_x = char_size * (char_offset % 10) char_y = char_size * (char_offset / 10) surf = sdl2_DisplayManager.inst().font_render_bordered_text_Faster( frame.pySurface, { 'x': char_x, 'y': char_y }, c, font_alias='export_font', size=font_size, border_width=line_width, border_color=line_color, color=interior_color) (font_width, font_height) = sdl2_DisplayManager.inst().font_get_size( c, 'export_font', font_size) #font_sizes += format(font_width,'x') font_sizes += str(font_width) font_sizes += "," sdl2_DisplayManager.inst().screen_blit( source_tx=frame.pySurface, expand_to_fill=True) #, area=(10,10,400,200)) texture_renderer = sdl2_DisplayManager.inst().texture_renderer bk = sdl2.SDL_GetRenderTarget(texture_renderer.renderer) t = sdl2.render.SDL_CreateTexture(texture_renderer.renderer, sdl2.pixels.SDL_PIXELFORMAT_RGBA8888, sdl2.render.SDL_TEXTUREACCESS_TARGET, width, height) #create a new texture and blit the frame to it, then grab bits from that texture_renderer.clear((0, 0, 0, 0)) sdl2.SDL_SetRenderTarget(texture_renderer.renderer, t) #sdl2_DisplayManager.inst().blit(source_tx=frame.pySurface, dest_tx = t, dest = (0,0,512,512)) texture_renderer.copy(frame.pySurface, (0, 0, width, height), (0, 0, width, height)) pitch = c_int() bytes = c_void_p() rect = sdl2.SDL_Rect(0, 0, width, height) sdl2.SDL_LockTexture(t, rect, ctypes.byref(bytes), ctypes.byref(pitch)) if endian.SDL_BYTEORDER == endian.SDL_LIL_ENDIAN: rmask = 0x000000FF gmask = 0x0000FF00 bmask = 0x00FF0000 amask = 0xFF000000 else: rmask = 0xFF000000 gmask = 0x00FF0000 bmask = 0x0000FF00 amask = 0x000000FF print rmask imgsurface = sdl2.surface.SDL_CreateRGBSurfaceFrom(bytes, width, height, 32, pitch, rmask, gmask, bmask, amask) if not imgsurface: raise sdl2.ext.SDLError() sdl2.SDL_RenderReadPixels(texture_renderer.renderer, rect, 0, bytes, pitch) sdl2.SDL_SaveBMP(imgsurface, 'image.png') #4) Restore renderer's texture target sdl2.SDL_SetRenderTarget(texture_renderer.renderer, bk) #ss = sdl2.ext.SoftwareSprite(surf, True) #ss = sdl2.ext.SoftwareSprite(imgsurface, True) #sdl2.SDL_SaveBMP(ss.contents, "file.bmp") # # print font_sizes sdl2_DisplayManager.inst().flip() sdl2.SDL_Delay(2)
def draw_in_rect(self, frame, text, rect=(0, 0, 128, 32), anchor=AnchorCenter, font_size=None): """Draw *text* on *frame* within the given *rect*, aligned in accordance with *anchor*. *rect* is a tuple of length 4: (origin_x, origin_y, height, width). 0,0 is in the upper left (NW) corner. *anchor* is one of: :attr:`~procgame.dmd.AnchorN`, :attr:`~procgame.dmd.AnchorE`, :attr:`~procgame.dmd.AnchorS`, :attr:`~procgame.dmd.AnchorW`, :attr:`~procgame.dmd.AnchorNE`, :attr:`~procgame.dmd.AnchorNW`, :attr:`~procgame.dmd.AnchorSE`, :attr:`~procgame.dmd.AnchorSW`, or :attr:`~procgame.dmd.AnchorCenter` (the default). """ origin_x, origin_y, width, height = rect if (font_size is None): font_size = self.font_size surf = sdl2_DisplayManager.inst().font_render_text( text, font_alias=self.name, size=font_size, width=None, color=None, bg_color=None) text_width, text_height = surf.size tmp = Frame(text_width, text_height, from_surface=surf) tmp.composite_op = "blacksrc" x = 0 y = 0 # print "Size: %d x %d" % (text_height) if anchor & AnchorN: y = origin_y elif anchor & AnchorS: y = origin_y + (height - text_height) else: y = origin_y + (height / 2.0 - text_height / 2.0) if anchor & AnchorW: x = origin_x elif anchor & AnchorE: x = origin_x + (width - text_width) else: x = origin_x + (width / 2.0 - text_width / 2.0) # w = min(width, frame.width) # h = min(height, frame.height) w = text_width #min(width, frame.width) h = text_height #min(height, frame.height) # self.draw(frame=frame, text=text, x=x, y=y) Frame.copy_rect(dst=frame, dst_x=x, dst_y=y, src=tmp, src_x=0, src_y=0, width=w, height=h, op=tmp.composite_op)