# Path: PyOpenGLng/GlApi/ApiNumber.py
# class ApiNumber(object):
# 
#     """ This class implements a basic API number suitable for OpenGL.
# 
#     Use case::
# 
#       >>> api_number = ApiNumber('4.4')
#       >>> str(api_number)
#       '4.4'
#       >>> ApiNumber('4.0') < ApiNumber('4.1')
#       True
#       >>> ApiNumber('4.0') <= ApiNumber('4.0')
#       True
# 
#     """
# 
#     ##############################################
# 
#     def __init__(self, number):
# 
#         """ The argument *number* must be of the form "x.y". """
# 
#         try:
#             self.major, self.minor = [int(x) for x in number.split('.')]
#         except ValueError:
#             # self.major, self.minor = int(number), 0
#             raise NameError("Version number must be of the form x.y")
# 
#     ##############################################
# 
#     def __int__(self):
# 
#         return self.major * 1000 + self.minor
# 
#     ##############################################
# 
#     def __str__(self):
# 
#         return "%u.%u" % (self.major, self.minor)
# 
#     ##############################################
# 
#     def __le__(self, api_number):
# 
#         return int(self) <= int(api_number)
# 
#     ##############################################
# 
#     def __lt__(self, api_number):
# 
#         return int(self) < int(api_number)
# 
# Path: PyOpenGLng/Tools/Timer.py
# class TimerContextManager(object):
# 
#     ##############################################
# 
#     def __init__(self, logger, title):
# 
#         self._logger = logger
#         self._title = title
# 
#     ##############################################
# 
#     def __enter__(self):
# 
#         self._start = time.clock()
# 
#     ##############################################
#     
#     def __exit__(self, type_, value, traceback):
# 
#         dt = time.clock() - self._start
#         self._logger.info("{} dt = {} s".format(self._title, dt))


import logging
import sys
import PyGlfwCffi as glfw
import PyOpenGLng.Wrapper as GlWrapper
from PyOpenGLng.GlApi import ApiNumber
from PyOpenGLng.Tools.Timer import TimerContextManager
api_number = ApiNumber('2.1')
glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, api_number.major)
glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, api_number.minor)
glfw.window_hint(glfw.CLIENT_API, glfw.OPENGL_API)
window = glfw.create_window(640, 480, "Hello World")
if not window:
    glfw.terminate()
    sys.exit()

# Create the OpenGL wrapper
with TimerContextManager(logging, "GlWrapper.init"):
    # orig: 0.618096 s
    # best: 0.123589 s
    gl = GlWrapper.init(api_number=str(api_number), profile='compat',
                        check_api_number=False,
                        wrapper='ctypes') # or cffi

# Make the window's context current
glfw.make_context_current(window)

# Install a key handler
glfw.set_key_callback(window, on_key)

# Loop until the user closes the window
while not glfw.window_should_close(window):
    # Render here
    width, height = glfw.get_framebuffer_size(window)
    ratio = width / float(height)
    gl.glViewport(0, 0, width, height)
    gl.glClear(gl.GL_COLOR_BUFFER_BIT)

# Path: PyOpenGLng/GlApi/ApiNumber.py
# class ApiNumber(object):
# 
#     """ This class implements a basic API number suitable for OpenGL.
# 
#     Use case::
# 
#       >>> api_number = ApiNumber('4.4')
#       >>> str(api_number)
#       '4.4'
#       >>> ApiNumber('4.0') < ApiNumber('4.1')
#       True
#       >>> ApiNumber('4.0') <= ApiNumber('4.0')
#       True
# 
#     """
# 
#     ##############################################
# 
#     def __init__(self, number):
# 
#         """ The argument *number* must be of the form "x.y". """
# 
#         try:
#             self.major, self.minor = [int(x) for x in number.split('.')]
#         except ValueError:
#             # self.major, self.minor = int(number), 0
#             raise NameError("Version number must be of the form x.y")
# 
#     ##############################################
# 
#     def __int__(self):
# 
#         return self.major * 1000 + self.minor
# 
#     ##############################################
# 
#     def __str__(self):
# 
#         return "%u.%u" % (self.major, self.minor)
# 
#     ##############################################
# 
#     def __le__(self, api_number):
# 
#         return int(self) <= int(api_number)
# 
#     ##############################################
# 
#     def __lt__(self, api_number):
# 
#         return int(self) < int(api_number)
# 
# Path: PyOpenGLng/Tools/Timer.py
# class TimerContextManager(object):
# 
#     ##############################################
# 
#     def __init__(self, logger, title):
# 
#         self._logger = logger
#         self._title = title
# 
#     ##############################################
# 
#     def __enter__(self):
# 
#         self._start = time.clock()
# 
#     ##############################################
#     
#     def __exit__(self, type_, value, traceback):
# 
#         dt = time.clock() - self._start
#         self._logger.info("{} dt = {} s".format(self._title, dt))


# Path: examples/basic-wrapper-test.py
import logging
import sys
import PyGlfwCffi as glfw
import PyOpenGLng.Wrapper as GlWrapper
from PyOpenGLng.GlApi import ApiNumber
from PyOpenGLng.Tools.Timer import TimerContextManager

api_number = ApiNumber('2.1')
glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, api_number.major)
glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, api_number.minor)
glfw.window_hint(glfw.CLIENT_API, glfw.OPENGL_API)
window = glfw.create_window(640, 480, "Hello World")
if not window:
    glfw.terminate()
    sys.exit()

# Create the OpenGL wrapper
with TimerContextManager(logging, "GlWrapper.init"):
    # orig: 0.618096 s
    # best: 0.123589 s
    gl = GlWrapper.init(api_number=str(api_number), profile='compat',
                        check_api_number=False,
                        wrapper='ctypes') # or cffi

# Make the window's context current
glfw.make_context_current(window)

# Install a key handler
glfw.set_key_callback(window, on_key)

# Loop until the user closes the window
while not glfw.window_should_close(window):
    # Render here
    width, height = glfw.get_framebuffer_size(window)
    ratio = width / float(height)
    gl.glViewport(0, 0, width, height)
    gl.glClear(gl.GL_COLOR_BUFFER_BIT)

    gl.glMatrixMode(gl.GL_PROJECTION)