#!/bin/env python
# -*- coding: utf-8 -*-
"""
FPS Meter/Framework

Public domain work by anatoly techtonik <techtonik@gmail.com>
Use MIT License if public domain doesn't make sense for you.
"""

# [x] create windsow 
# [x] run each test
# [x] print stats


import pyglet

# time to run each FPS test
TIME = 10

class FPS(object):
    counter = 0
    cumtime = 0
    fps = []              # list of FPSes for each second

    @classmethod
    def on_draw(cls):
        pass

class FPS_1_Empty(FPS):
    """No output"""
    pass

class FPS_2_Console(FPS):
    """Create and draw text.Label"""
    @classmethod
    def on_draw(cls):
        window.clear()
        label = pyglet.text.Label('FPS: %.5s' % (cls.counter/cls.cumtime),
                          font_name='Times New Roman',
                          font_size=36,
                          x=window.width//2, y=window.height//2,
                          anchor_x='center', anchor_y='center')
        # TODO [ ]: schedule the next event after this one completes to trigger
        #           event loop processing (it will wait for keypress otherwise)
        #   NOTE: it looks like due to this, clock scheduler can miss the exact
        #         time event should fire on
        #pyglet.app.platform_event_loop.post_event(window, 'on_draw')
        label.draw()

class FPS_3_Console(FPS):
    """Change text for pre-created text.Label"""
    label = pyglet.text.Label('FPS: 0',
                          font_name='Times New Roman',
                          font_size=36,
                          x=100, y=100,
                          anchor_x='center', anchor_y='center')
    @classmethod
    def on_draw(cls):
        window.clear()
        cls.label.text = 'FPS: %.5s' % (cls.counter/cls.cumtime)

        # TODO [ ]: schedule the next event after this one completes to trigger
        #           event loop processing (it will wait for keypress otherwise)
        #   NOTE: it looks like due to this, clock scheduler can miss the exact
        #         time event should fire on
        #pyglet.app.platform_event_loop.post_event(window, 'on_draw')
        cls.label.draw()


def update(dt, test, window):
    test.cumtime += dt
    test.counter += 1
    if test.cumtime > TIME:
        window.close()

print "Running each test for %s seconds." % TIME
for test in FPS.__subclasses__():
    print "%s:" % test.__doc__,
    window = pyglet.window.Window()
    window.set_handler('on_draw', test.on_draw)

    pyglet.clock.set_fps_limit(None)
    pyglet.clock.schedule(update, test, window)
    #pyglet.clock.schedule_interval(update, 0.000001, test, window)
    pyglet.app.run()

    #print test.counter, test.cumtime, test.counter / test.cumtime
    print "FPS %.6s" % (test.counter/test.cumtime)


"""
window = pyglet.window.Window()

counter = 0
@window.event
def on_draw():
    global counter
    counter += 1
    window.clear()
    label = pyglet.text.Label('Count: %s' % counter,
                          font_name='Times New Roman',
                          font_size=36,
                          x=window.width//2, y=window.height//2,
                          anchor_x='center', anchor_y='center')
    label.draw()

pyglet.app.run()

"""