from sdl2 import *
from sdl2.keycode import *
from gl import *
from glconstants import *
import globs
from ctypes import *
from math3d import *
from Texture import *
from Buffer import *
import random
from Program import *
from math import *


class Background:

    def __init__(self):
        self.pos= vec2(-0.5,0)
        Background.setupVao()

    @staticmethod
    def setupVao():
        shipTex = array.array("B", [0,0,255,255, 0,0,255,255,0,0,255,255,0,0,255,255,0,0,255,255,0,0,255,255,0,0,255,255, 0,0,0,0,
                                    0,0,0,0, 0,0,0,0, 0,0,255,255,0,0,255,255,0,0,255,255,0,0,255,255,0,0,255,255,0,0,255,255,
                                    0,0,0,0,0,0,0,0, 0,0,255,255,0,0,255,255, 255,0,0,255,255,0,0,255,0,0,255,255,0,0,255,255,
                                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 0, 0, 255, 255, 255, 0, 0, 255, 255, 0, 0,
                                    255, 0, 0, 255, 255, 0, 0, 255, 255,
                                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 0, 0, 255, 255, 255, 0, 0, 255, 255, 0, 0,
                                    255, 0, 0, 255, 255, 0, 0, 255, 255,
                                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 0, 0, 255, 255, 255, 0, 0, 255, 255, 0, 0,
                                    255, 0, 0, 255, 255, 0, 0, 255, 255,
                                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255,
                                    255, 0, 0, 255, 255, 0, 0, 255, 255,
                                    0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0,
                                    0, 255, 255, 0, 0, 255, 255, 0, 0, 0, 0,
                                    ])
        globs.shiptex = DataTexture2DArray(8, 8, 1, shipTex)
        enemyTex = array.array("B",
                               [128, 0, 128, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255,
                                255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 0, 0, 0, 255,
                                128, 0, 128, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255,
                                255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255,
                                0, 0, 0, 255, 0, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255,
                                255, 0, 0, 255, 255, 0, 0, 255, 0, 0, 255, 255, 0, 0, 255, 255,
                                0, 0, 0, 255, 0, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255,
                                255, 0, 0, 255, 255, 0, 0, 255, 0, 0, 255, 255, 0, 0, 255, 255,
                                0, 0, 0, 255, 0, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255,
                                255, 0, 0, 255, 255, 0, 0, 255, 0, 0, 255, 255, 0, 0, 255, 255,
                                0, 0, 0, 255, 0, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255,
                                255, 0, 0, 255, 255, 0, 0, 255, 0, 0, 255, 255, 0, 0, 255, 255,
                                128, 0, 128, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255,
                                255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255,
                                128, 0, 128, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255,
                                255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 0, 0, 0, 255,
                                ])
        globs.enemyTex = DataTexture2DArray(8, 8, 1, enemyTex)
        sinTex = array.array("B",
                             [0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255,
                              0, 255, 0, 255,0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255,
                              0, 255, 0, 255,0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255,
                              0, 255, 0, 255,0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255,
                              0, 0, 0, 255, 0, 0, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255,
                              0, 255, 0, 255, 0, 255, 0, 255, 0, 0, 0, 255, 0, 0, 0, 255,
                              0, 0, 0, 255, 0, 0, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255,
                              0, 255, 0, 255, 0, 255, 0, 255, 0, 0, 0, 255, 0, 0, 0, 255,
                              0, 0, 0, 255, 0, 0, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255,
                              0, 255, 0, 255, 0, 255, 0, 255, 0, 0, 0, 255, 0, 0, 0, 255,
                              0, 0, 0, 255, 0, 0, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255,
                              0, 255, 0, 255, 0, 255, 0, 255, 0, 0, 0, 255, 0, 0, 0, 255,
                              0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255,
                              0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255,
                              0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255,
                              0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255
                              ])
        globs.sinTex = DataTexture2DArray(8, 8, 1, sinTex)
        globs.samp = Sampler()
        shipPoints = [1,-1, 1,1, -1,1, -1,-1]
        vbuff = Buffer( array.array("f", shipPoints))
        shipIndices = [0,1,3, 3,2,1]
        ibuff = Buffer(array.array("I",shipIndices))
        tbuff = Buffer(array.array("f", [1,0, 1,1, 0,1, 0,0]))
        tmp = array.array("I", [0])
        glGenVertexArrays(1, tmp)
        Background.vao = tmp[0]
        glBindVertexArray(Background.vao)
        vbuff.bind(GL_ARRAY_BUFFER)
        ibuff.bind(GL_ELEMENT_ARRAY_BUFFER)
        glEnableVertexAttribArray(0)
        glVertexAttribPointer(0, 2, GL_FLOAT, False, 2 * 4, 0)
        tbuff.bind(GL_ARRAY_BUFFER)
        glEnableVertexAttribArray(1)
        glVertexAttribPointer(1, 2, GL_FLOAT, False, 2 * 4, 0)
        glBindVertexArray(0)

    def draw(self):
        globs.samp.bind(0)
        globs.shiptex.bind(0)
        glBindVertexArray(Background.vao)
        Program.setUniform("translation", self.pos)
        Program.updateUniforms()
        glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0)
from sdl2 import *
from sdl2.keycode import *
from gl import *
from glconstants import *
import globs
from ctypes import *
from math3d import *
from Texture import *
from Buffer import *
import random
from Program import *
from math import *


class Background:

    def __init__(self):
        self.pos= vec2(-0.5,0)
        Background.setupVao()

    @staticmethod
    def setupVao():
        shipTex = array.array("B", [0,0,255,255, 0,0,255,255,0,0,255,255,0,0,255,255,0,0,255,255,0,0,255,255,0,0,255,255, 0,0,0,0,
                                    0,0,0,0, 0,0,0,0, 0,0,255,255,0,0,255,255,0,0,255,255,0,0,255,255,0,0,255,255,0,0,255,255,
                                    0,0,0,0,0,0,0,0, 0,0,255,255,0,0,255,255, 255,0,0,255,255,0,0,255,0,0,255,255,0,0,255,255,
                                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 0, 0, 255, 255, 255, 0, 0, 255, 255, 0, 0,
                                    255, 0, 0, 255, 255, 0, 0, 255, 255,
                                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 0, 0, 255, 255, 255, 0, 0, 255, 255, 0, 0,
                                    255, 0, 0, 255, 255, 0, 0, 255, 255,
                                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 0, 0, 255, 255, 255, 0, 0, 255, 255, 0, 0,
                                    255, 0, 0, 255, 255, 0, 0, 255, 255,
                                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255,
                                    255, 0, 0, 255, 255, 0, 0, 255, 255,
                                    0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0,
                                    0, 255, 255, 0, 0, 255, 255, 0, 0, 0, 0,
                                    ])
        globs.shiptex = DataTexture2DArray(8, 8, 1, shipTex)
        enemyTex = array.array("B",
                               [128, 0, 128, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255,
                                255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 0, 0, 0, 255,
                                128, 0, 128, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255,
                                255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255,
                                0, 0, 0, 255, 0, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255,
                                255, 0, 0, 255, 255, 0, 0, 255, 0, 0, 255, 255, 0, 0, 255, 255,
                                0, 0, 0, 255, 0, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255,
                                255, 0, 0, 255, 255, 0, 0, 255, 0, 0, 255, 255, 0, 0, 255, 255,
                                0, 0, 0, 255, 0, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255,
                                255, 0, 0, 255, 255, 0, 0, 255, 0, 0, 255, 255, 0, 0, 255, 255,
                                0, 0, 0, 255, 0, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255,
                                255, 0, 0, 255, 255, 0, 0, 255, 0, 0, 255, 255, 0, 0, 255, 255,
                                128, 0, 128, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255,
                                255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255,
                                128, 0, 128, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255,
                                255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 0, 0, 0, 255,
                                ])
        globs.enemyTex = DataTexture2DArray(8, 8, 1, enemyTex)
        sinTex = array.array("B",
                             [0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255,
                              0, 255, 0, 255,0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255,
                              0, 255, 0, 255,0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255,
                              0, 255, 0, 255,0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255,
                              0, 0, 0, 255, 0, 0, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255,
                              0, 255, 0, 255, 0, 255, 0, 255, 0, 0, 0, 255, 0, 0, 0, 255,
                              0, 0, 0, 255, 0, 0, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255,
                              0, 255, 0, 255, 0, 255, 0, 255, 0, 0, 0, 255, 0, 0, 0, 255,
                              0, 0, 0, 255, 0, 0, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255,
                              0, 255, 0, 255, 0, 255, 0, 255, 0, 0, 0, 255, 0, 0, 0, 255,
                              0, 0, 0, 255, 0, 0, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255,
                              0, 255, 0, 255, 0, 255, 0, 255, 0, 0, 0, 255, 0, 0, 0, 255,
                              0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255,
                              0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255,
                              0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255,
                              0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255
                              ])
        globs.sinTex = DataTexture2DArray(8, 8, 1, sinTex)
        globs.samp = Sampler()
        shipPoints = [1,-1, 1,1, -1,1, -1,-1]
        vbuff = Buffer( array.array("f", shipPoints))
        shipIndices = [0,1,3, 3,2,1]
        ibuff = Buffer(array.array("I",shipIndices))
        tbuff = Buffer(array.array("f", [1,0, 1,1, 0,1, 0,0]))
        tmp = array.array("I", [0])
        glGenVertexArrays(1, tmp)
        Background.vao = tmp[0]
        glBindVertexArray(Background.vao)
        vbuff.bind(GL_ARRAY_BUFFER)
        ibuff.bind(GL_ELEMENT_ARRAY_BUFFER)
        glEnableVertexAttribArray(0)
        glVertexAttribPointer(0, 2, GL_FLOAT, False, 2 * 4, 0)
        tbuff.bind(GL_ARRAY_BUFFER)
        glEnableVertexAttribArray(1)
        glVertexAttribPointer(1, 2, GL_FLOAT, False, 2 * 4, 0)
        glBindVertexArray(0)

    def draw(self):
        globs.samp.bind(0)
        globs.shiptex.bind(0)
        glBindVertexArray(Background.vao)
        Program.setUniform("translation", self.pos)
        Program.updateUniforms()
        glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0)