From 569bab3f7240303b8117656b9b3278ed62759d3c Mon Sep 17 00:00:00 2001 From: Alex Foulon Date: Thu, 11 Nov 2010 11:36:52 +0000 Subject: [PATCH] * Add examples: blit, frames and text. --- python/examples/blit.py | 66 ++++++++++++++++++++++++++++++++++ python/examples/frames.py | 74 +++++++++++++++++++++++++++++++++++++++ python/examples/text.py | 73 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 213 insertions(+) create mode 100755 python/examples/blit.py create mode 100755 python/examples/frames.py create mode 100755 python/examples/text.py diff --git a/python/examples/blit.py b/python/examples/blit.py new file mode 100755 index 0000000..e42e746 --- /dev/null +++ b/python/examples/blit.py @@ -0,0 +1,66 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +# figfont libcaca blit test program +# Copyright (c) 2010 Alex Foulon +# +# This file is a Python port of "examples/blit.c" +# which is: +# Copyright (c) 2006-2010 Sam Hocevar +# All Rights Reserverd +# +# This library is free software. It comes without any warranty, to +# the extent permitted by applicable law. You can redistribute it +# and/or modify it under the terms of the Do What The Fuck You Want +# To Public License, Version 2, as published by Sam Hocevar. See +# http://sam.zoy.org/wtfpl/COPYING for more details. +# + +import sys + +import caca +from caca.canvas import Canvas, CanvasError, NullCanvas +from caca.display import Display, DisplayError, Event + +THE_PIG="""\ + ,__ __, + \\)`\\_..._/`(/ + .' _ _ '. + / o\\ /o \\ + | .-.-. | _ + | /() ()\\ | (,`) + / \\ '-----' / \\ .' +| '-..___..-' | +| | +| | +; ; + \\ / \\ / + \\-..-/'-'\\-..-/ +jgs\\/\\/ \\/\\/""" + +def main(): + """ Main function. """ + try: + cv = Canvas(0, 0) + dp = Display(cv) + except (CanvasError, DisplayError), err: + sys.stderr.write("%s\n" % err) + sys.exit(2) + + sprite = Canvas(0, 0) + sprite.set_color_ansi(caca.COLOR_LIGHTRED, caca.COLOR_BLACK) + sprite.import_from_memory(THE_PIG, "text") + sprite.set_handle(sprite.get_width()/2, sprite.get_height()/2) + + cv.set_color_ansi(caca.COLOR_WHITE, caca.COLOR_BLUE) + cv.put_str(0, 0, "Centered sprite") + cv.blit(cv.get_width()/2, cv.get_height()/2, sprite, NullCanvas()) + + dp.refresh() + dp.get_event(caca.EVENT_KEY_PRESS, Event(), -1) + + sys.exit(0) + +if __name__ == "__main__": + main() + diff --git a/python/examples/frames.py b/python/examples/frames.py new file mode 100755 index 0000000..dd79c4b --- /dev/null +++ b/python/examples/frames.py @@ -0,0 +1,74 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +# frames canvas frame switching features +# Copyright (c) 2010 Alex Foulon +# +# This file is a Python port of "examples/frames.c" +# which is: +# Copyright (c) 2006-2010 Sam Hocevar +# All Rights Reserverd +# +# This library is free software. It comes without any warranty, to +# the extent permitted by applicable law. You can redistribute it +# and/or modify it under the terms of the Do What The Fuck You Want +# To Public License, Version 2, as published by Sam Hocevar. See +# http://sam.zoy.org/wtfpl/COPYING for more details. +# + +import sys + +import caca +from caca.canvas import Canvas, CanvasError +from caca.display import Display, DisplayError, Event + +def main(): + """ Main function. """ + + try: + cv = Canvas(0, 0) + except CanvasError, err: + sys.stderr.write("%s\n" % err) + sys.exit(2) + + for idx in range(1, 200): + cv.create_frame(idx) + + sys.stderr.write("canvas created, size is %dx%d\n" \ + % (cv.get_width(), cv.get_height())) + + cv.set_size(150, 80) + sys.stderr.write("canvas expanded, size is %dx%d\n" \ + % (cv.get_width(), cv.get_height())) + + for idx in range(0, 16): + cv.set_frame(idx) + cv.set_color_ansi(caca.COLOR_WHITE, idx) + cv.fill_box(0, 0, 40, 15, ':') + cv.set_color_ansi(caca.COLOR_WHITE, caca.COLOR_BLUE) + cv.put_str(idx * 5 / 2, idx, "カカ") + cv.set_color_ansi(caca.COLOR_DEFAULT, caca.COLOR_TRANSPARENT) + + cv.set_size(41, 16) + sys.stderr.write("canvas shrinked, size is %dx%d\n" \ + % (cv.get_width(), cv.get_height())) + + try: + dp = Display(cv) + except DisplayError, err: + sys.stderr.write("%s\n" % err) + sys.exit(2) + + dp.set_time(50000) + sys.stderr.write("display attached, size is %dx%d\n" \ + % (cv.get_width(), cv.get_height())) + + n = 0 + while not dp.get_event(caca.EVENT_KEY_PRESS, Event(), 0): + cv.set_frame(n % 16) + dp.refresh() + n += 1 + +if __name__ == "__main__": + main() + diff --git a/python/examples/text.py b/python/examples/text.py new file mode 100755 index 0000000..f8245bb --- /dev/null +++ b/python/examples/text.py @@ -0,0 +1,73 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +# text canvas text import/export +# Copyright (c) 2010 Alex Foulon +# +# This file is a Python port of "examples/text.c" +# which is: +# Copyright (c) 2006-2010 Sam Hocevar +# All Rights Reserverd +# +# This library is free software. It comes without any warranty, to +# the extent permitted by applicable law. You can redistribute it +# and/or modify it under the terms of the Do What The Fuck You Want +# To Public License, Version 2, as published by Sam Hocevar. See +# http://sam.zoy.org/wtfpl/COPYING for more details. +# + +import sys + +import caca +from caca.canvas import Canvas, CanvasError, NullCanvas +from caca.display import Display, DisplayError, Event + +STRING="""\ + |_| + _,----._ | | + (/ @ @ \\) __ + | OO | |_ + \\ `--' / |__ + `----' + |_| + Hello world! | + +""" + +def main(): + """ Main function. """ + + try: + pig = Canvas(0, 0) + pig.import_from_memory(STRING, "text") + cv = Canvas(pig.get_width() * 2, pig.get_height() * 2) + except CanvasError, err: + sys.stderr.write("%s\n" % err) + sys.exit(2) + + cv.blit(0, 0, pig, NullCanvas()) + pig.flip() + cv.blit(pig.get_width(), 0, pig, NullCanvas()) + pig.flip() + pig.flop() + cv.blit(0, pig.get_height(), pig, NullCanvas()) + pig.flop() + pig.rotate_180() + cv.blit(pig.get_width(), pig.get_height(), pig, NullCanvas()) + + for j in xrange(0, cv.get_height()): + for i in xrange(0, cv.get_width(), 2): + cv.set_color_ansi(caca.COLOR_LIGHTBLUE + (i + j) % 6, + caca.COLOR_DEFAULT) + + a = cv.get_attr(-1, -1) + cv.put_attr(i, j, a) + cv.put_attr(i+1, j, a) + + sys.stdout.write(cv.export_to_memory("utf8")) + cv.rotate_left() + sys.stdout.write(cv.export_to_memory("utf8")) + +if __name__ == "__main__": + main() +