<commit_before>// Copyright 2015 <NAME>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package sprite provides functions for managing sprites.

package sprite

import (
	"bytes"
	"fmt"
	"image"
	"image/draw"
	"runtime"

	"github.com/go-gl/gl/v4.1-core/gl"
	"github.com/go-gl/mathgl/mgl32"
	"github.com/hurricanerix/FlappyDisk/gen"
)

func init() {
	// GLFW event handling must run on the main OS thread
	runtime.LockOSThread()
}

// New returns Sprite
func New(assetName string) (*Sprite, error) {
	data, err := gen.Asset(assetName)
	if err != nil {
		return nil, err
	}

	tex, err := newTexture(data)
	if err != nil {
		return nil, err
	}

	s := Sprite{
		AssetName: assetName,
		Pos:       mgl32.Vec3{0.0, 0.0, 0.0},
		Scale:     1.0,
		Rot:       0.0,
		Texture:   tex,
		data:      data,
		model:     mgl32.Ident4(),
	}

	return &s, nil
}

// Sprite represents position, rotation and scale for a given asset.
type Sprite struct {
	AssetName    string
	Pos          mgl32.Vec3
	Scale        float64
	Rot          float64
	Texture      uint32
	data         []byte
	vao          uint32
	model        mgl32.Mat4
	modelUniform int32
}

func (s *Sprite) Bind(program uint32) error {
	gl.GenVertexArrays(1, &s.vao)
	gl.BindVertexArray(s.vao)

	var vbo uint32
	gl.GenBuffers(1, &vbo)
	gl.BindBuffer(gl.ARRAY_BUFFER, vbo)
	gl.BufferData(gl.ARRAY_BUFFER, len(vertices)*4, gl.Ptr(vertices), gl.STATIC_DRAW)

	vertAttrib := uint32(gl.GetAttribLocation(program, gl.Str("MCVertex\x00")))
	gl.EnableVertexAttribArray(vertAttrib)
	gl.VertexAttribPointer(vertAttrib, 3, gl.FLOAT, false, 5*4, gl.PtrOffset(0))

	texCoordAttrib := uint32(gl.GetAttribLocation(program, gl.Str("TexCoord0\x00")))
	gl.EnableVertexAttribArray(texCoordAttrib)
	gl.VertexAttribPointer(texCoordAttrib, 2, gl.FLOAT, false, 5*4, gl.PtrOffset(3*4))

	textureUniform := gl.GetUniformLocation(program, gl.Str("ColorMap\x00"))
	gl.Uniform1i(textureUniform, 0)

	s.modelUniform = gl.GetUniformLocation(program, gl.Str("ModelMatrix\x00"))
	gl.UniformMatrix4fv(s.modelUniform, 1, false, &s.model[0])
	return nil
}

func (s *Sprite) Draw() {
	gl.Enable(gl.DEPTH_TEST)
	s.model = (mgl32.Scale3D(float32(s.Scale), float32(s.Scale), 0.0)).Mul4(
		mgl32.Translate3D(s.Pos[0], s.Pos[1], s.Pos[2])).Mul4(
		mgl32.HomogRotate3DZ(float32(s.Rot)))

	gl.UniformMatrix4fv(s.modelUniform, 1, false, &s.model[0])

	gl.BindVertexArray(s.vao)

	gl.ActiveTexture(gl.TEXTURE0)
	gl.BindTexture(gl.TEXTURE_2D, s.Texture)

	gl.DrawArrays(gl.TRIANGLES, 0, 6*2*3)<commit_msg>Update var names<commit_after>// Copyright 2015 <NAME>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package sprite provides functions for managing sprites.

package sprite

import (
	"bytes"
	"fmt"
	"image"
	"image/draw"
	"runtime"

	"github.com/go-gl/gl/v4.1-core/gl"
	"github.com/go-gl/mathgl/mgl32"
	"github.com/hurricanerix/FlappyDisk/gen"
)

func init() {
	// GLFW event handling must run on the main OS thread
	runtime.LockOSThread()
}

// New returns Sprite
func New(assetName string) (*Sprite, error) {
	data, err := gen.Asset(assetName)
	if err != nil {
		return nil, err
	}

	tex, err := newTexture(data)
	if err != nil {
		return nil, err
	}

	s := Sprite{
		AssetName: assetName,
		Pos:       mgl32.Vec3{0.0, 0.0, 0.0},
		Scale:     1.0,
		Rot:       0.0,
		Texture:   tex,
		data:      data,
		model:     mgl32.Ident4(),
	}

	return &s, nil
}

// Sprite represents position, rotation and scale for a given asset.
type Sprite struct {
	AssetName   string
	Pos         mgl32.Vec3
	Scale       float64
	Rot         float64
	Texture     uint32
	data        []byte
	vao         uint32
	model       mgl32.Mat4
	modelMatrix int32
}

func (s *Sprite) Bind(program uint32) error {
	gl.GenVertexArrays(1, &s.vao)
	gl.BindVertexArray(s.vao)

	var vbo uint32
	gl.GenBuffers(1, &vbo)
	gl.BindBuffer(gl.ARRAY_BUFFER, vbo)
	gl.BufferData(gl.ARRAY_BUFFER, len(vertices)*4, gl.Ptr(vertices), gl.STATIC_DRAW)

	mcVertex := uint32(gl.GetAttribLocation(program, gl.Str("MCVertex\x00")))
	gl.EnableVertexAttribArray(mcVertex)
	gl.VertexAttribPointer(mcVertex, 3, gl.FLOAT, false, 5*4, gl.PtrOffset(0))

	texCoord0 := uint32(gl.GetAttribLocation(program, gl.Str("TexCoord0\x00")))
	gl.EnableVertexAttribArray(texCoord0)
	gl.VertexAttribPointer(texCoord0, 2, gl.FLOAT, false, 5*4, gl.PtrOffset(3*4))

	colorMap := gl.GetUniformLocation(program, gl.Str("ColorMap\x00"))
	gl.Uniform1i(colorMap, 0)

	s.modelMatrix = gl.GetUniformLocation(program, gl.Str("ModelMatrix\x00"))
	gl.UniformMatrix4fv(s.modelMatrix, 1, false, &s.model[0])
	return nil
}

func (s *Sprite) Draw() {
	gl.Enable(gl.DEPTH_TEST)
	s.model = (mgl32.Scale3D(float32(s.Scale), float32(s.Scale), 0.0)).Mul4(
		mgl32.Translate3D(s.Pos[0], s.Pos[1], s.Pos[2])).Mul4(
		mgl32.HomogRotate3DZ(float32(s.Rot)))

	gl.UniformMatrix4fv(s.modelMatrix, 1, false, &s.model[0])

	gl.BindVertexArray(s.vao)

	gl.ActiveTexture(gl.TEXTURE0)
	gl.BindTexture(gl.TEXTURE_2D, s.Texture)

	gl.DrawArrays(gl.TRIANGLES, 0, 6*2*3)
{'fraction_non_alphanumeric': 0.1344678011344678, 'fraction_numerical': 0.036870203536870205, 'mean_word_length': 3.761715647339158, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6671171', 'n_tokens_mistral': 2355, 'n_tokens_neox': 2227, 'n_words': 539}