def main(): pygame.init() pygame.display.gl_set_attribute(pygame.GL_STENCIL_SIZE, 2) srf = pygame.display.set_mode((640,480), pygame.OPENGL | pygame.DOUBLEBUF) pygame.display.set_caption("Paint test") VG.create_context((640, 480)) VG.set(VG_CLEAR_COLOR, (1.0, 1.0, 1.0, 1.0)) VG.set(VG_STROKE_LINE_WIDTH, 3.0) p = VG.Path(capabilities=VG_PATH_CAPABILITY_APPEND_TO) VGU.ellipse(p, (0, 0), (64*2,64*2)) solid_paint = VG.ColorPaint((1.0, 0.0, 1.0)) stops = [(0.0,(1.0,1.0,1.0,1.0)), (0.333,(1.0,0.0,0.0,1.0)), (0.666,(0.0,1.0,0.0,1.0)), (1.0,(0.0,0.0,1.0,1.0))] linear_gradient = [(-60,0), (60,0)] linear_paint = VG.GradientPaint(linear_gradient, linear=True) linear_paint.spread_mode = VG_COLOR_RAMP_SPREAD_REFLECT linear_paint.stops = stops radial_gradient = [(0,0), (0,0), 64] radial_paint = VG.GradientPaint(radial_gradient, linear=False) radial_paint.stops = stops running = True while running: events = pygame.event.get() for e in events: if e.type == pygame.QUIT: running = False elif e.type == pygame.KEYDOWN: if e.key == pygame.K_ESCAPE: running = False VG.clear((0, 0), (640, 480)) VG.set(VG_MATRIX_MODE, VG_MATRIX_PATH_USER_TO_SURFACE) VG.load_identity() VG.translate(68, 240-64) VG.set_paint(solid_paint, VG_FILL_PATH) p.draw(VG_STROKE_PATH | VG_FILL_PATH) VG.translate(192, 0) VG.set_paint(linear_paint, VG_FILL_PATH) p.draw(VG_STROKE_PATH | VG_FILL_PATH) VG.translate(192, 0) VG.set_paint(radial_paint, VG_FILL_PATH) p.draw(VG_STROKE_PATH | VG_FILL_PATH) pygame.display.flip()
def main(width, height): pygame.init() pygame.display.gl_set_attribute(pygame.GL_STENCIL_SIZE, 2) srf = pygame.display.set_mode((width, height), pygame.OPENGL | pygame.DOUBLEBUF) pygame.display.set_caption("Rainbow Gradient Test") VG.create_context((width, height)) VG.set(VG_CLEAR_COLOR, (1.0, 1.0, 1.0, 1.0)) canvas = VG.Path(capabilities=VG_PATH_CAPABILITY_APPEND_TO) VGU.rect(canvas, (0, 0), (width, height)) rgb_gradient = VG.GradientPaint([(0,0), (width,0)], linear=True) rgb_gradient.spread_mode = VG_COLOR_RAMP_SPREAD_REFLECT rgb_gradient.stops = generate_rgb_stops(7) alpha_stops = [(0.0, (0.0, 0.0, 0.0, 0.0)), (1.0, (1.0, 1.0, 1.0, 1.0))] alpha_gradient = VG.GradientPaint([(0,0), (0,height)], linear=True) alpha_gradient.spread_mode = VG_COLOR_RAMP_SPREAD_REFLECT alpha_gradient.stops = alpha_stops n = 7 running = True while running: events = pygame.event.get() for e in events: if e.type == pygame.QUIT: running = False elif e.type == pygame.KEYDOWN: if e.key == pygame.K_ESCAPE: running = False elif e.type == pygame.MOUSEBUTTONDOWN: if e.button == 4: n += 1 rgb_gradient.stops = generate_rgb_stops(n) elif e.button == 5: if n > 2: n -= 1 rgb_gradient.stops = generate_rgb_stops(n) VG.clear((0, 0), (640, 480)) VG.set_paint(rgb_gradient, VG_FILL_PATH) canvas.draw(VG_FILL_PATH) VG.set_paint(alpha_gradient, VG_FILL_PATH) canvas.draw(VG_FILL_PATH) pygame.display.flip()
def main(width, height): pygame.init() pygame.display.gl_set_attribute(pygame.GL_STENCIL_SIZE, 2) srf = pygame.display.set_mode((width, height), pygame.OPENGL | pygame.DOUBLEBUF) pygame.display.set_caption("Polyline test") VG.create_context((width, height)) VG.set(VG_CLEAR_COLOR, (1.0, 1.0, 1.0, 1.0)) polyline = VG.Path() stroke_paint = VG.ColorPaint((0.5, 0.2, 0.8, 0.6)) VG.set_paint(stroke_paint, VG_STROKE_PATH) fill_paint = VG.ColorPaint((0.3, 1.0, 0.0, 0.6)) VG.set_paint(fill_paint, VG_FILL_PATH) polyline.style = VG.Style(VG_STROKE_LINE_WIDTH = 4.0, VG_STROKE_JOIN_STYLE = VG_JOIN_MITER, VG_STROKE_CAP_STYLE = VG_CAP_ROUND) print "Usage" print "Left click: LINE_TO" print "Right click: MOVE_TO" running = True first_click = True while running: events = pygame.event.get() for e in events: if e.type == pygame.QUIT: running = False elif e.type == pygame.KEYDOWN: if e.key == pygame.K_ESCAPE: running = False elif e.type == pygame.MOUSEBUTTONDOWN: if e.button == 1: #left button if first_click: polyline.move_to((e.pos[0], height-e.pos[1]), rel=False) first_click = False else: polyline.line_to((e.pos[0], height-e.pos[1]), rel=False) elif e.button == 3:#right button polyline.move_to((e.pos[0], height-e.pos[1]), rel=False) VG.clear((0, 0), (width, height)) polyline.draw(VG_STROKE_PATH) pygame.display.flip()
def main(width, height, path, flags=0): pygame.init() pygame.display.gl_set_attribute(pygame.GL_STENCIL_SIZE, 2) flags |= pygame.OPENGL | pygame.DOUBLEBUF screen = pygame.display.set_mode((width, height), flags) pygame.display.set_caption("Pygame Image Draw Test") VG.create_context((width, height)) VG.set(VG_CLEAR_COLOR, (1.0, 1.0, 1.0, 1.0)) im = load_image(path) dump_image(im) dest_x = (width-im.width)/2.0 dest_y = (height-im.height)/2.0 dragging = False running = True while running: events = pygame.event.get() for e in events: if e.type == pygame.QUIT: running = False elif e.type == pygame.KEYDOWN: if e.key == pygame.K_ESCAPE: running = False elif e.type == pygame.MOUSEBUTTONDOWN: if e.button == 1: dragging = True elif e.type == pygame.MOUSEBUTTONUP: if e.button == 1: dragging = False elif e.type == pygame.MOUSEMOTION: if dragging: dest_x += e.rel[0] dest_y -= e.rel[1] VG.clear((0, 0), (width, height)) VG.set(VG_MATRIX_MODE, VG_MATRIX_IMAGE_USER_TO_SURFACE) VG.load_identity() VG.translate(dest_x, dest_y) im.draw() pygame.display.flip()
def main(width, height, message, rpm=20): pygame.init() pygame.display.gl_set_attribute(pygame.GL_STENCIL_SIZE, 2) srf = pygame.display.set_mode((width, height), pygame.OPENGL | pygame.DOUBLEBUF) pygame.display.set_caption("Freetype Font test") VG.create_context((width, height)) VG.set(VG_CLEAR_COLOR, (1.0, 1.0, 1.0, 1.0)) font = Font("data/fonts/Vera.ttf", 64) red_paint = VG.ColorPaint((1.0, 0.0, 0.0, 1.0)) text = font.build_path(message) (x, y), (w, h) = text.bounds() text.style = VG.Style(VG_STROKE_LINE_WIDTH = 1.0, fill_paint = red_paint) clock = pygame.time.Clock() dt = 0 cx = width/2.0 - w/2.0 cy = height/2.0 - h/2.0 running = True while running: events = pygame.event.get() for e in events: if e.type == pygame.QUIT: running = False elif e.type == pygame.KEYDOWN: if e.key == pygame.K_ESCAPE: running = False elif e.type == pygame.MOUSEBUTTONDOWN: pos = e.pos cx = pos[0] - w/2.0 cy = height-pos[1] - h/2.0 VG.clear((0, 0), (width, height)) VG.load_identity() VG.translate(cx, cy) rotate_about((x+w/2.0, y+h/2.0), dt*360.0*rpm / (60*1000.0)) text.draw(VG_STROKE_PATH | VG_FILL_PATH) pygame.display.flip() dt += clock.tick(60)
def main(width, height): pygame.init() pygame.display.gl_set_attribute(pygame.GL_STENCIL_SIZE, 2) pygame.display.gl_set_attribute(pygame.GL_MULTISAMPLEBUFFERS, 1) pygame.display.gl_set_attribute(pygame.GL_MULTISAMPLESAMPLES, 4) screen = pygame.display.set_mode((width, height), pygame.OPENGL | pygame.DOUBLEBUF) pygame.display.set_caption("Flower test") VG.create_context((width, height)) VG.set(VG_CLEAR_COLOR, (0.0, 0.0, 0.0, 1.0)) vera = Font("data/fonts/Vera.ttf", 32) message = vera.build_path("Hold down LMB to create flowers") message.style = VG.Style(fill_paint=VG.ColorPaint((1.0, 1.0, 1.0, 0.7))) doc = parse_svg("data/svg/flower.svg") flower = doc.getroot() (x,y), (w,h) = flower.bounds() cx,cy = x+w/2.0, y+h/2.0 particles = [] to_remove = [] clock = pygame.time.Clock() dt = 0 running = True while running: for e in pygame.event.get(): if e.type == pygame.QUIT: running = False elif e.type == pygame.KEYDOWN: if e.key == pygame.K_ESCAPE: running = False if pygame.mouse.get_pressed()[0]: x,y = pygame.mouse.get_pos() m = random.randint(-60, 60) angle = random.randint(0,359) pos = (x-cx, height-y-cy) vel = (m*math.cos(math.radians(angle)), m*math.sin(math.radians(angle))) rot = random.randint(-30, 30) scale = random.randint(8, 12)/10.0 particles.append([pos, vel, random.randint(0,359), rot, scale]) VG.clear((0,0), (width, height)) for particle in particles: VG.load_identity() pos, vel, angle, rot, scale = particle VG.translate(pos[0]+cx, pos[1]+cy) VG.scale(scale, scale) VG.rotate(angle) VG.translate(-cx, -cy) flower.draw() particle[0] = (pos[0]+vel[0]*dt/1000.0,pos[1]+vel[1]*dt/1000.0) particle[2] += rot * dt/1000.0 if particle[0][0] + w < 0 or particle[0][1] + h < 0: to_remove.append(particle) elif particle[0][0] - w > width or particle[0][1] - h > height: to_remove.append(particle) particles = [particle for particle in particles if particle not in to_remove] del to_remove[:] VG.load_identity() message.draw(VG_FILL_PATH) dt = clock.tick(30) pygame.display.flip()
def main(width, height): pygame.init() pygame.display.gl_set_attribute(pygame.GL_STENCIL_SIZE, 2) srf = pygame.display.set_mode((width, height), pygame.OPENGL | pygame.DOUBLEBUF) pygame.display.set_caption("Interpolation test") VG.create_context((width, height)) VG.set(VG_CLEAR_COLOR, (1.0, 1.0, 1.0, 1.0)) paths = [] path_tag = ".//{http://www.w3.org/2000/svg}path" VG.set(VG_MATRIX_MODE, VG_MATRIX_PATH_USER_TO_SURFACE) VG.scale(5.0, 5.0) doc = parse_svg("data/svg/shapes.svg") for element in doc.findall(path_tag): path = element.path.transform() path.style = element.style if not path.style: path.style = VG.Style(VG_STROKE_LINE_WIDTH=5.0) else: path.style[VG_STROKE_LINE_WIDTH] *= 2 paths.append(path) VG.load_identity() morph = VG.Path() start = paths[0] end = paths[1] morph.style = start.style VG.set(VG_MATRIX_MODE, VG_MATRIX_PATH_USER_TO_SURFACE) VG.translate(220, 140) clock = pygame.time.Clock() running = True dt = 0 i = 0 while running: events = pygame.event.get() for e in events: if e.type == pygame.QUIT: running = False elif e.type == pygame.KEYDOWN: if e.key == pygame.K_ESCAPE: running = False VG.clear((0, 0), (width, height)) morph.clear() VG.interpolate(start, end, morph, dt/float(MORPH_TIME)) morph.draw(VG_STROKE_PATH) pygame.display.flip() dt += clock.tick(60) if dt >= MORPH_TIME: dt -= MORPH_TIME i += 1 start = paths[i % len(paths)] end = paths[(i+1) % len(paths)] morph.style = start.style
def main(width, height, radius, count, flags=0): pygame.init() pygame.display.gl_set_attribute(pygame.GL_STENCIL_SIZE, 2) flags |= pygame.OPENGL | pygame.DOUBLEBUF srf = pygame.display.set_mode((width, height), flags) pygame.display.set_caption("Blending test") VG.create_context((width, height)) VG.set(VG_CLEAR_COLOR, (1.0, 1.0, 1.0, 1.0)) orange_paint = VG.ColorPaint((1.0, 0.5, 0.0, 0.5)) blue_paint = VG.ColorPaint((0.0, 0.0, 1.0, 0.5)) black_paint = VG.ColorPaint((0.0, 0.0, 0.0)) blend_modes = [VG_BLEND_SRC, VG_BLEND_SRC_OVER, VG_BLEND_DST_OVER, VG_BLEND_SRC_IN, VG_BLEND_DST_IN, VG_BLEND_MULTIPLY, VG_BLEND_SCREEN, VG_BLEND_DARKEN, VG_BLEND_LIGHTEN, VG_BLEND_ADDITIVE] blend_index = 0 VG.set(VG_BLEND_MODE, blend_modes[0]) font = Font("data/fonts/Vera.ttf", 30) src_path = font.build_path("SRC") dst_path = font.build_path("DST") message = font.build_path("Scroll to change the blend mode") circle = VG.Path() VGU.ellipse(circle, (0, 0), (radius*2, radius*2)) center_and_concat(circle, dst_path) square = VG.Path() VGU.rect(square, (-radius, -radius), (radius*2, radius*2)) center_and_concat(square, src_path) circle2 = VG.Path() VGU.ellipse(circle2, (0, 0), (radius*2, radius*2)) running = True while running: events = pygame.event.get() for e in events: if e.type == pygame.QUIT: running = False elif e.type == pygame.KEYDOWN: if e.key == pygame.K_ESCAPE: running = False elif e.type == pygame.MOUSEBUTTONDOWN: if e.button == 4: blend_index += 1 VG.set(VG_BLEND_MODE, blend_modes[blend_index % len(blend_modes)]) elif e.button == 5: blend_index -= 1 VG.set(VG_BLEND_MODE, blend_modes[blend_index % len(blend_modes)]) VG.clear((0, 0), (width, height)) VG.set(VG_MATRIX_MODE, VG_MATRIX_PATH_USER_TO_SURFACE) VG.load_identity() VG.translate(width - radius*1.5, radius * 2.5) circle.draw(VG_FILL_PATH | VG_STROKE_PATH) VG.translate(-radius, -radius) VG.set_paint(orange_paint, VG_FILL_PATH) square.draw(VG_FILL_PATH | VG_STROKE_PATH) VG.set_paint(blue_paint, VG_FILL_PATH) for i in xrange(count): angle = i*2*math.pi/count VG.load_identity() VG.translate(width//2, height//2) VG.translate(radius*math.cos(angle), radius*math.sin(angle)) circle2.draw(VG_FILL_PATH) VG.load_identity() (x,y), (w,h) = message.bounds() VG.translate(-x, height-y-h) message.draw(VG_STROKE_PATH | VG_FILL_PATH) pygame.display.flip()
def main(width, height, directory): pygame.init() pygame.display.gl_set_attribute(pygame.GL_STENCIL_SIZE, 2) pygame.display.gl_set_attribute(pygame.GL_MULTISAMPLEBUFFERS, 1) pygame.display.gl_set_attribute(pygame.GL_MULTISAMPLESAMPLES, 4) screen = pygame.display.set_mode((width, height), pygame.OPENGL | pygame.DOUBLEBUF) pygame.display.set_caption("SVG Viewer") VG.create_context((width, height)) VG.set(VG_CLEAR_COLOR, (1.0, 1.0, 1.0, 1.0)) register_font_finder(pygame.font.match_font) @register_font_finder def fallback_font(name): fname = pygame.font.get_default_font() return os.path.join(os.path.dirname(pygame.font.__file__), fname) vera = Font("data/fonts/Vera.ttf", 16) text = vera.build_path("Scroll to change svg files. Drag to see more.") drawings = [] for root, dirs, files in os.walk(directory): for fname in files: if not fname.endswith(".svg"): continue path = os.path.join(root, fname) try: tree = parse_svg(path) name = vera.build_path(fname, 16) tree.getroot().fit(width, height) drawings.append((tree.getroot(), name)) except: print "Error in loading %s" % path traceback.print_exc(0) dragging = False dx = dy = 0 i = 0 scale = 1 drawing, name = drawings[0] (x,y), (w,h) = drawing.bounds() running = True while running: events = pygame.event.get() for e in events: if e.type == pygame.QUIT: running = False elif e.type == pygame.KEYDOWN: if e.key == pygame.K_ESCAPE: running = False elif e.key == pygame.K_1: scale = 1 elif e.key == pygame.K_2: scale = 2 elif e.key == pygame.K_3: scale = 3 elif e.key == pygame.K_4: scale = 4 elif e.key == pygame.K_5: scale = 5 elif e.key == pygame.K_6: scale = 6 elif e.key == pygame.K_7: scale = 7 elif e.key == pygame.K_8: scale = 8 elif e.key == pygame.K_9: scale = 9 elif e.key == pygame.K_EQUALS: i += 1 drawing, name = drawings[i % len(drawings)] (x,y), (w,h) = drawing.bounds() elif e.key == pygame.K_MINUS: i -= 1 drawing, name = drawings[i % len(drawings)] (x,y), (w,h) = drawing.bounds() elif e.type == pygame.MOUSEBUTTONDOWN: if e.button == 1 or e.button == 2 or e.button == 3: dragging = True else: if e.button == 4: i += 1 else: i -= 1 drawing, name = drawings[i % len(drawings)] (x,y), (w,h) = drawing.bounds() elif e.type == pygame.MOUSEBUTTONUP: if e.button == 1 or e.button == 2 or e.button == 3: dragging = False elif e.type == pygame.MOUSEMOTION: if dragging: dx += e.rel[0]/2.0**(scale-1) dy -= e.rel[1]/2.0**(scale-1) VG.clear((0, 0), (width, height)) VG.load_identity() VG.scale(2**(scale-1),2**(scale-1)) VG.translate(width/2.0-w/2.0-x+dx, height/2.0-h/2.0-y+dy) drawing.draw() VG.load_identity() VG.translate(10, 10) text.draw(VG_FILL_PATH) VG.translate(text.bounds()[1][0]+10, 0) name.draw(VG_FILL_PATH) pygame.display.flip()
def main(width, height, font_path, size): pygame.init() pygame.display.gl_set_attribute(pygame.GL_STENCIL_SIZE, 2) srf = pygame.display.set_mode((width, height), pygame.OPENGL | pygame.DOUBLEBUF) pygame.display.set_caption("Freetype Font test") VG.create_context((width, height)) VG.set(VG_CLEAR_COLOR, (1.0, 1.0, 1.0, 1.0)) font = Font(font_path, size) xmin, ymin, xmax, ymax = [x*font.scale for x in font.face.bbox] the_path = VG.Path() message = font.build_path("left click and drag to move") (x, y), (w, h) = message.bounds() VG.translate(width/2.0-w/2.0, height-h) message.transform(the_path) VG.load_identity() VG.translate(0-xmin, height-ymax-h) boxes_per_line = int(width/float(xmax-xmin)) i = 0 for char_code, glyph in sorted(font.glyph_table.items()): font.get_path_for_glyph(glyph).transform(the_path) VG.translate(xmax-xmin, 0) i += 1 if i >= boxes_per_line: VG.translate((xmax-xmin)*-boxes_per_line, ymin-ymax) i = 0 VG.load_identity() red_paint = VG.ColorPaint((1.0, 0.0, 0.0, 1.0)) style = VG.Style(VG_STROKE_LINE_WIDTH = 1.0, fill_paint = red_paint) style.enable() dragging = False running = True while running: events = pygame.event.get() for e in events: if e.type == pygame.QUIT: running = False elif e.type == pygame.KEYDOWN: if e.key == pygame.K_ESCAPE: running = False elif e.type == pygame.MOUSEBUTTONDOWN: if e.button == 1: dragging = True elif e.type == pygame.MOUSEBUTTONUP: if e.button == 1: dragging = False elif e.type == pygame.MOUSEMOTION: if dragging: VG.translate(e.rel[0], -e.rel[1]) VG.clear((0, 0), (width, height)) the_path.draw(VG_STROKE_PATH | VG_FILL_PATH) pygame.display.flip()
def main(): WIDTH = 640 HEIGHT = 480 screen = setup("Pymunk + PyOpenVG test", WIDTH, HEIGHT, pygame.DOUBLEBUF | pygame.RESIZABLE) black = VG.ColorPaint((0.0, 0.0, 0.0, 1.0)) white = VG.ColorPaint((1.0, 1.0, 1.0, 1.0)) red = VG.ColorPaint((1.0, 0.0, 0.0, 1.0)) yellow = VG.ColorPaint((1.0, 1.0, 0.0, 1.0)) space = pm.Space() space.gravity = (0, -900) Ls = [StaticL(space, (300, 300), 200, 25), StaticL(space, (250, 150), 200, 25)] balls = [] running = True fps = 60 multiplier = 1 clock = pygame.time.Clock() while running: #Handle events for event in pygame.event.get(): if event.type == pygame.QUIT: running = False elif event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: running = False elif pygame.K_1 <= event.key <= pygame.K_3: multiplier = event.key - pygame.K_0 elif event.type == pygame.MOUSEBUTTONDOWN: if event.button == 1: pos = (event.pos[0], HEIGHT-event.pos[1]) balls.append(Ball(space, pos, 12*multiplier, smiley=True)) elif event.button == 3: pos = (event.pos[0], HEIGHT-event.pos[1]) balls.append(Ball(space, pos, 12*multiplier, smiley=False)) #Update world space.step(1.0/fps) #Redraw VG.clear((0,0), (WIDTH, HEIGHT)) for L in Ls: L.draw() offscreen = [] for ball in balls: if ball.body.position.y < 0: offscreen.append(ball) elif ball.body.position.x < 0 or ball.body.position.x > WIDTH: offscreen.append(ball) else: ball.draw() for ball in offscreen: balls.remove(ball) pygame.display.flip() clock.tick(fps)
VG.set(VG_CLEAR_COLOR, (1.0, 1.0, 1.0, 1.0)) world = MoonWorld() low_point = min(world.terrain_data) running = True clock = pygame.time.Clock() while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False elif event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: running = False left_down, middle_down, right_down = pygame.mouse.get_pressed() world.buggy.input_power = 1.0 * (left_down - right_down) world.update() VG.clear((0,0), (640,480)) VG.load_identity() pos = world.buggy.chassis.body.position VG.translate(-pos[0] + 300, min(-pos[1] + 200, -low_point)) world.draw() pygame.display.flip() clock.tick(60)