Ejemplo n.º 1
0
Archivo: tetris.go Proyecto: dmac/gg
func NewTetris(vertShader, fragShader string) (*Tetris, error) {
	gg.Enable(gg.BLEND)
	gg.BlendFunc(gg.SRC_ALPHA, gg.ONE_MINUS_SRC_ALPHA)

	// Compile the global shader program
	vshader, err := gg.CreateShader([]byte(vertShader), gg.VERTEX_SHADER)
	if err != nil {
		return nil, err
	}
	fshader, err := gg.CreateShader([]byte(fragShader), gg.FRAGMENT_SHADER)
	if err != nil {
		return nil, err
	}
	program := gg.CreateProgram()
	gg.AttachShader(program, vshader)
	gg.AttachShader(program, fshader)
	if err := gg.LinkProgram(program); err != nil {
		return nil, err
	}

	// Set the global projection matrix
	gg.UseProgram(program)
	proj := mgl.Ortho(0, float32(WindowWidth), float32(WindowHeight), 0, 0, 1)
	projUniform, err := gg.GetUniformLocation(program, "proj")
	if err != nil {
		return nil, err
	}
	gg.UniformMatrix4fv(projUniform, proj[:])

	tetris := &Tetris{}

	tetris.textures, err = LoadTextures()
	if err != nil {
		return nil, err
	}

	tetris.bg = NewSprite(WindowWidth, WindowHeight, program, tetris.textures["bg"])
	tetris.board = NewBoard(WidthCells, HeightCells, program, tetris.textures)
	tetris.board.x = Padding
	tetris.board.y = Padding

	rand.Seed(time.Now().Unix())
	tetris.board.current = tetris.board.NewRandomPiece()

	go func() {
		t := time.NewTicker(time.Second)
		for range t.C {
			tetris.mu.Lock()
			if tetris.gameOver {
				return
			}
			tetris.mu.Unlock()
			tetris.HandleInput(inputDown)
		}
	}()

	return tetris, nil
}
Ejemplo n.º 2
0
Archivo: texture.go Proyecto: dmac/gg
func NewScene(vertShader, fragShader string, texture *gg.Texture) (*Scene, error) {
	gg.Enable(gg.DEPTH_TEST)
	gg.Enable(gg.CULL_FACE)
	gg.DepthFunc(gg.LESS)

	gg.Enable(gg.BLEND)
	gg.BlendFunc(gg.SRC_ALPHA, gg.ONE_MINUS_SRC_ALPHA)

	// Compile the global shader program
	vshader, err := gg.CreateShader([]byte(vertShader), gg.VERTEX_SHADER)
	if err != nil {
		return nil, err
	}
	fshader, err := gg.CreateShader([]byte(fragShader), gg.FRAGMENT_SHADER)
	if err != nil {
		return nil, err
	}
	program := gg.CreateProgram()
	gg.AttachShader(program, vshader)
	gg.AttachShader(program, fshader)
	if err := gg.LinkProgram(program); err != nil {
		return nil, err
	}

	// Set the global projection matrix
	gg.UseProgram(program)
	proj := mgl.Ortho(0, float32(WindowWidth), float32(WindowHeight), 0, 0, 1)
	projUniform, err := gg.GetUniformLocation(program, "proj")
	if err != nil {
		return nil, err
	}
	gg.UniformMatrix4fv(projUniform, proj[:])

	vertices := []float32{
		float32(WindowWidth)/2 - 50, float32(WindowHeight)/2 - 50, 0,
		float32(WindowWidth)/2 - 50, float32(WindowHeight)/2 + 50, 0,
		float32(WindowWidth)/2 + 50, float32(WindowHeight)/2 + 50, 0,
		float32(WindowWidth)/2 + 50, float32(WindowHeight)/2 - 50, 0,
	}

	// Initialize sprite
	sprite, err := NewSprite(vertices, program, texture)
	if err != nil {
		return nil, err
	}

	return &Scene{sprite: sprite}, nil
}