[4] | 1 | #!/usr/bin/python |
---|
| 2 | ############################################################################ |
---|
| 3 | # |
---|
| 4 | # py_sail: An example of how to write SAGE applications using python and py_sail |
---|
| 5 | # |
---|
| 6 | # Copyright (C) 2007 Electronic Visualization Laboratory, |
---|
| 7 | # University of Illinois at Chicago |
---|
| 8 | # |
---|
| 9 | # All rights reserved. |
---|
| 10 | # |
---|
| 11 | # Redistribution and use in source and binary forms, with or without |
---|
| 12 | # modification, are permitted provided that the following conditions are met: |
---|
| 13 | # |
---|
| 14 | # * Redistributions of source code must retain the above copyright |
---|
| 15 | # notice, this list of conditions and the following disclaimer. |
---|
| 16 | # * Redistributions in binary form must reproduce the above |
---|
| 17 | # copyright notice, this list of conditions and the following disclaimer |
---|
| 18 | # in the documentation and/or other materials provided with the distribution. |
---|
| 19 | # * Neither the name of the University of Illinois at Chicago nor |
---|
| 20 | # the names of its contributors may be used to endorse or promote |
---|
| 21 | # products derived from this software without specific prior written permission. |
---|
| 22 | # |
---|
| 23 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
---|
| 24 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
---|
| 25 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
---|
| 26 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
---|
| 27 | # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
---|
| 28 | # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
---|
| 29 | # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
---|
| 30 | # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
---|
| 31 | # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
---|
| 32 | # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
---|
| 33 | # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
| 34 | # |
---|
| 35 | # Author: Ratko Jagodic |
---|
| 36 | # |
---|
| 37 | ############################################################################ |
---|
| 38 | |
---|
| 39 | |
---|
| 40 | import os, sys, time, array |
---|
| 41 | |
---|
| 42 | |
---|
| 43 | # for importing py_sail properly |
---|
| 44 | sys.path.append(os.environ["SAGE_DIRECTORY"]+"/lib") |
---|
| 45 | import py_sail |
---|
| 46 | |
---|
| 47 | |
---|
| 48 | # size of SAGE buffer for this app |
---|
| 49 | BUFFER_WIDTH = 200 |
---|
| 50 | BUFFER_HEIGHT = 200 |
---|
| 51 | |
---|
| 52 | |
---|
| 53 | # initialize sail with appName, buffer width and height |
---|
| 54 | py_sail.initSail("pysail_test", BUFFER_WIDTH, BUFFER_HEIGHT) |
---|
| 55 | |
---|
| 56 | |
---|
| 57 | # make a pixel array of size BUFFER_WIDTH * BUFFER_HEIGHT * 3 |
---|
| 58 | a = array.array('B') # unsigned char type |
---|
| 59 | numPixels = BUFFER_WIDTH * BUFFER_HEIGHT |
---|
| 60 | for i in xrange(0, BUFFER_WIDTH * BUFFER_HEIGHT): |
---|
| 61 | p = 1-i/float(numPixels) |
---|
| 62 | a.append(int(p*255)); a.append(int(p*255)); a.append(int(p*255)) #R,G,B |
---|
| 63 | |
---|
| 64 | |
---|
| 65 | # convert the array to a string and send it to py_sail |
---|
| 66 | buf = a.tostring() |
---|
| 67 | py_sail.setBuffer(buf) |
---|
| 68 | |
---|
| 69 | |
---|
| 70 | # keep looping and checking for the quit message |
---|
| 71 | # you could also swap buffers here if you need |
---|
| 72 | # to update the image |
---|
| 73 | while True: |
---|
| 74 | time.sleep(0.5) |
---|
| 75 | msg = py_sail.checkMessages() |
---|
| 76 | if msg == -1: # -1 is returned when SAGE wants to close the app |
---|
| 77 | print "quitting..." |
---|
| 78 | break |
---|