def run():
    print 'hello, this is python code speaking'

    count = 0

    s3e.s3eSurfaceClear(00,0xFF,0x12)
 
    width = s3e.s3eSurfaceGetInt(s3e.S3E_SURFACE_WIDTH)
    height = s3e.s3eSurfaceGetInt(s3e.S3E_SURFACE_HEIGHT)
    pitch = s3e.s3eSurfaceGetInt(s3e.S3E_SURFACE_PITCH)
    
    print "Width %d Height %d Pitch %d" % (width, height, pitch)
    bytes = height * pitch

    surface = s3e.s3eSurfacePtr()
    print type(surface)
    print len(surface)

    colours = 360
    duration = 3.0
   
    starttime = time.time()
    # cycle through colour spectrum for duration
    for r in range(colours):
        val = getRgb565Bytes(*colorsys.hsv_to_rgb(r/float(colours), 1.0, 1.0))
        s3e.s3eSurfaceClear(*map(lambda c : int(c * 0xFF), colorsys.hsv_to_rgb(r/float(colours), 1.0, 1.0)))
        s3e.s3eSurfaceShow()

        if time.time < r * duration / colours:
            time.sleep(0.01)
    
    # copy to surface
    for i in range(1000):
        drawCircle(30,100, (0x200,0x30,0x66))
        s3e.s3eSurfaceShow()
        s3e.s3eDeviceYield(0)
    s3e.s3eSurfaceClear(0)
    s3e.s3eVideoPlay("angel_fish.jpg", 0, 0, 0, 320, 320)
    s3e.s3eAudioPlay("trumpet.wav", 0)
    time.sleep(3)
def run():
    print 'hello, this is python code speaking'

    iwgl.IwGLInit()

    print "Screen BPP  : %d\n" % (s3e.s3eSurfaceGetInt(s3e.S3E_SURFACE_PIXEL_TYPE) & s3e.S3E_SURFACE_PIXEL_SIZE_MASK)
    print "\n"
    print "Vendor     : %s\n" % glGetString(GL_VENDOR)
    print "Renderer   : %s\n" % glGetString(GL_RENDERER)
    print "Version    : %s\n" % glGetString(GL_VERSION)
    print "Extensions : %s\n" % glGetString(GL_EXTENSIONS)
    print "\n"

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glEnable(GL_DEPTH_TEST);

    glDepthFunc(GL_LESS);

    glShadeModel(GL_SMOOTH);

    start_time = datetime.now()
    frames = 0;
    done = False
    cube, color = getCube()

    while not done:
        #To take advantage of IwGL's automatic screen rotation support, the
        #projection matrix and viewport should be set up every frame.
        w = iwgl.IwGLGetInt(iwgl.IW_GL_WIDTH);
        h = iwgl.IwGLGetInt(iwgl.IW_GL_HEIGHT);
        glViewport(0, 0, w, h);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();

        glOrthof(-2.0, 2.0, -2.0, 2.0, -20.0, 20.0);

        # Do our drawing, too.
        glClearColor(0.0, 0.0, 0.0, 1.0);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        glEnableClientState(GL_VERTEX_ARRAY);
        glEnableClientState(GL_COLOR_ARRAY);
        glColorPointer(4, GL_FLOAT, 0, color);
        glVertexPointer(3, GL_FLOAT, 0, cube);
        glDrawArrays(GL_TRIANGLE_STRIP, 0, 24);
        glDisableClientState(GL_VERTEX_ARRAY);
        glDisableClientState(GL_COLOR_ARRAY);
        glMatrixMode(GL_MODELVIEW);
        glRotatef(5.0, 1.0, 1.0, 1.0);

        # Call IwGL swap instead of egl directly
        iwgl.IwGLSwapBuffers();

        # Check for error conditions. #
        gl_error = glGetError();

        if gl_error != GL_NO_ERROR:
            fprintf(stderr, "testgl: OpenGL error: %#x\n", gl_error);

        frames += 1
        # Allow the user to see what's happening 
        #time.sleep(0.01)
        s3e.s3eDeviceYield(10);
        duration = datetime.now() - start_time
        if duration.seconds > 3:
            print duration
            print frames
            print "%f FPS" % (frames/duration.seconds)

    iwgl.IwGLTerminate()
import sys
import time
import threading

import s3e
import colorsys

def getRgb565Bytes(r,g,b): 
    red   = int(r * 0xFF)
    green = int(g * 0xFF)
    blue  = int(b * 0xFF)
    return ( ((red >> 3) << 11) | ((green >> 2) << 5) | (blue >> 3))

def drawCircle(x,y, (r,g,b), radius=20):
    surface = s3e.s3eSurfacePtr();
    width   = s3e.s3eSurfaceGetInt(s3e.S3E_SURFACE_WIDTH);
    height  = s3e.s3eSurfaceGetInt(s3e.S3E_SURFACE_HEIGHT);
    pitch   = s3e.s3eSurfaceGetInt(s3e.S3E_SURFACE_PITCH);

    # clip circular region
    startx  = max(x - radius, 0)
    endx    = min(width, x + radius)
    starty  = max(0, y - radius)
    endy    = min(height, y + radius)

    radiusSq = radius * radius

    for iy in range (starty, endy):
        pos = iy * pitch + startx;
        for ix in range(startx, endx):
            distx = x - ix