#!/usr/bin/python

import util;
from util import *;
import pygame
from pygame.locals import *
from Light import *
from random import *
from OpenGL.GL import *
from OpenGL.GLU import *
from Player import *

from constants import *
#import Texture
#from Texture import *


count = 250
width = 800
height = 600
max_tex_size = 1024

groupTexts = 1

frames = 60
frameCount = 0
totalFrames = 0

texWidth = width * 1.0/max_tex_size
texHeight = height * 1.0/max_tex_size

pygame.init()
util.initializeDisplay(width, height)

texture = loadTextureG('data/marine.png')
bg = loadTexture('data/testbg.png')

GL_CLAMP_TO_EDGE = 0x812F




player = Player()





sprites = []
for i in range(0, count):
    x = int(random() * width)
    y = int(random() * height)
    scale = int(random() * 3)

    sprites.append((x, y, scale))

     
lastTime = pygame.time.get_ticks()
bgtex = 0
setTex(bgtex)
#glBindTexture(GL_TEXTURE_2D, bgtex)
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
(x1, y1),(x2,y2),(x3,y3),(x4,y4) = (10.0, 40.0),(20.0,10.0),(900.0,0.0),(850.0,400.0)


lights = []
lights.append(Light((600,300), -110, base_width = 90, length=300))
lights.append(Light((100,300), 90, length=300, color=[.5,1,.5]))
lights.append(Light((200,200), 20))
lights.append(Light((300,300), 180,color=[.5,1,0]))
lights.append(Light((400,400), 176, length=250, fov=70))

while totalFrames < 10 * frames + 1:
    glClear(GL_COLOR_BUFFER_BIT)
    glLoadIdentity()

    #glViewport(0, 0, max_tex_size, max_tex_size)

    glLoadIdentity()
    #glScalef(1.0,-1.0,1.0)
    drawTile(width=width, height=height, texture=bg)
 
    for (x,y,scale) in sprites:
        glLoadIdentity()
        #glScalef(1.0,-1.0,1.0)
        glTranslate(x,y,0)
        drawTileG(width = 16 * scale, height = 16 * scale, texture=texture)

    glLoadIdentity()
    if (bgtex != 0):
        setTex(bgtex)
        glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, max_tex_size, max_tex_size)

    else:
        bgtex = glGenTextures(1)
        setTex(bgtex)
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
        
        emptyList = "\x00" * max_tex_size*max_tex_size*4	
        glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, max_tex_size, max_tex_size, 0, GL_RGBA, GL_UNSIGNED_BYTE, emptyList )
        
        glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 0, 0, max_tex_size, max_tex_size, 0);


    glClear(GL_COLOR_BUFFER_BIT);

    glLoadIdentity()
    #setTex(bg)


    """glBegin(GL_QUADS)
    
    
    glColor4f(1,1,1,1);
    glTexCoord2f(x1 / width*texWidth, (1.0 - y1 / height)*texHeight ); glVertex2f(x1, y1)    # Bottom Left Of The Texture and Quad
    glColor4f(1,1,1,1);
    glTexCoord2f(x2/ width*texWidth , (1.0 - y2 / height)*texHeight ); glVertex2f(x2, y2)    # Bottom Right Of The Texture and Quad
    glColor4f(1,1,1,0);
    glTexCoord2f(x3/ width*texWidth , (1.0 - y3 / height)*texHeight ); glVertex2f( x3, y3)    # Top Right Of The Texture and Quad
    glColor4f(1,1,1,0);
    glTexCoord2f(x4/ width*texWidth , (1.0 - y4 / height)*texHeight ); glVertex2f(x4, y4)    # Top Left Of The Texture and Quad

    glEnd()"""
 
    for myLight in lights:
        myLight.draw(bgtex)
    player.draw()
    pygame.display.flip()

    frameCount += 1
    totalFrames += 1
    if frameCount == frames:
        thisTime = pygame.time.get_ticks() 
        print "fps:  %.2f" % ((frameCount*1000.0)/(thisTime - lastTime))
        lastTime = thisTime
        frameCount = 0
