<commit_before>package engine

import (
	"github.com/vova616/gl"
	//"gl/glu"
	//"log"
	//"image/png"
	//"image"
	//"os"
	"log"

	"github.com/vova616/chipmunk/vect"
	//"glfw"
)

type OnAnimationEnd func(sprite *Sprite)

type Sprite struct {
	BaseComponent
	*Texture
	//buffer               gl.Buffer
	AnimationSpeed       float32
	texcoordsIndex       int
	endAnimation         int
	startAnimation       int
	animation            float32
	UVs                  AnimatedUV
	animMap              map[ID][2]int
	currentAnim          interface{}
	AnimationEndCallback OnAnimationEnd

	Tiling Vector

	Render bool

	Color Color

	align AlignType
}

func NewSprite(tex *Texture) *Sprite {
	return NewSprite3(tex, AnimatedUV{NewUV(0, 0, 1, 1, float32(tex.Width())/float32(tex.Height()))})
}

func NewSprite2(tex *Texture, uv UV) *Sprite {
	return NewSprite3(tex, AnimatedUV{uv})
}

func NewSprite3(tex *Texture, uv AnimatedUV) *Sprite {

	sp := &Sprite{
		BaseComponent: NewComponent(),
		Texture:       tex,
		//buffer:         gl.GenBuffer(),
		AnimationSpeed: 1,
		endAnimation:   len(uv),
		UVs:            uv,
		Render:         true,
		Color:          Color_White,
		align:          AlignCenter,
		Tiling:         Vector{1, 1, 0},
	}
	//sp.CreateVBO(uv...)

	return sp
}

func (p *Sprite) BindAnimations(animMap map[ID][2]int) {
	p.animMap = animMap
}

func (p *Sprite) SetAnimation(id ID) {
	a, e := p.animMap[id]
	if !e {
		panic("no such id")
	}
	p.currentAnim = id
	p.animation = float32(a[0])
	p.startAnimation = a[0]
	p.endAnimation = a[1]
}

func (p *Sprite) Align() AlignType {
	return p.align
}

func (p *Sprite) SetAlign(align AlignType) {
	p.align = align
}

func (p *Sprite) CurrentAnimation() interface{} {
	return p.currentAnim
}

func (sp *Sprite) OnComponentBind(binded *GameObject) {
	binded.Sprite = sp
}

/*
func (sp *Sprite) CreateVBO(uvs ...UV) {
	l := len(uvs)

	if l == 0 {
		sp.buffer.Bind(gl.ARRAY_BUFFER)
		gl.BufferData(gl.ARRAY_BUFFER, 1, []byte{1}, gl.STATIC_DRAW)
		return
	}

	lt := l * 12
	_ = lt
	data := make([]float32, 20*l)

	vertexCount := 0
	texcoordsIndex := lt * 4
	for i, uv := range uvs {

		yratio := float32(1)
		xratio := uv.Ratio
		ygrid := float32(-0.5)
		xgrid := float32(-uv.Ratio / 2)

		vertexCount += 4

		data[(i*12)+0] = xgrid
		data[(i*12)+1] = ygrid
		data[(i*12)+2] = 1
		data[(i*12)+3] = (xratio) + xgrid
		data[(i*12)+4] = ygrid
		data[(i*12)+5] = 1
		data[(i*12)+6] = (xratio) + xgrid
		data[(i*12)+7] = (yratio) + ygrid
		data[(i*12)+8] = 1
		data[(i*12)+9] = xgrid
		data[(i*12)+10] = (yratio) + ygrid
		data[(i*12)+11] = 1

		data[lt+(i*8)+0] = uv.U1
		data[lt+(i*8)+1] = uv.V2
		data[lt+(i*8)+2] = uv.U2
		data[lt+(i*8)+3] = uv.V2

		data[lt+(i*8)+4] = uv.U2
		data[lt+(i*8)+5] = uv.V1
		data[lt+(i*8)+6] = uv.U1
		data[lt+(i*8)+7] = uv.V1
	}

	sp.texcoordsIndex = texcoordsIndex
	sp.buffer.Bind(gl.ARRAY_BUFFER)
	gl.BufferData(gl.ARRAY_BUFFER, len(data)*4, data, gl.STATIC_DRAW)
}
*/

func (sp *Sprite) Start() {

}

func (sp *Sprite) CurrentAnimationIndex() int {
	return int(sp.animation) - sp.startAnimation
}

func (sp *Sprite) AnimationLength() int {
	return sp.endAnimation - sp.startAnimation
}

func (sp *Sprite) SetAnimationIndex(index int) {
	if index <= 0 {
		sp.animation = float32(sp.startAnimation)
	} else {
		sp.animation = float32((index % (sp.endAnimation - sp.startAnimation)) + sp.startAnimation)
	}
}

var renders = 0

func (sp *Sprite) Update() {<commit_msg>Added RealSize functions to Sprite.<commit_after>package engine

import (
	"github.com/vova616/gl"
	//"gl/glu"
	//"log"
	//"image/png"
	//"image"
	//"os"
	"log"

	"github.com/vova616/chipmunk/vect"
	//"glfw"
)

type OnAnimationEnd func(sprite *Sprite)

type Sprite struct {
	BaseComponent
	*Texture
	//buffer               gl.Buffer
	AnimationSpeed       float32
	texcoordsIndex       int
	endAnimation         int
	startAnimation       int
	animation            float32
	UVs                  AnimatedUV
	animMap              map[ID][2]int
	currentAnim          interface{}
	AnimationEndCallback OnAnimationEnd

	Tiling Vector

	Render bool

	Color Color

	align AlignType
}

func NewSprite(tex *Texture) *Sprite {
	return NewSprite3(tex, AnimatedUV{NewUV(0, 0, 1, 1, float32(tex.Width())/float32(tex.Height()))})
}

func NewSprite2(tex *Texture, uv UV) *Sprite {
	return NewSprite3(tex, AnimatedUV{uv})
}

func NewSprite3(tex *Texture, uv AnimatedUV) *Sprite {

	sp := &Sprite{
		BaseComponent: NewComponent(),
		Texture:       tex,
		//buffer:         gl.GenBuffer(),
		AnimationSpeed: 1,
		endAnimation:   len(uv),
		UVs:            uv,
		Render:         true,
		Color:          Color_White,
		align:          AlignCenter,
		Tiling:         Vector{1, 1, 0},
	}
	//sp.CreateVBO(uv...)

	return sp
}

func (p *Sprite) BindAnimations(animMap map[ID][2]int) {
	p.animMap = animMap
}

func (p *Sprite) SetAnimation(id ID) {
	a, e := p.animMap[id]
	if !e {
		panic("no such id")
	}
	p.currentAnim = id
	p.animation = float32(a[0])
	p.startAnimation = a[0]
	p.endAnimation = a[1]
}

func (p *Sprite) Align() AlignType {
	return p.align
}

func (p *Sprite) SetAlign(align AlignType) {
	p.align = align
}

func (p *Sprite) CurrentAnimation() interface{} {
	return p.currentAnim
}

func (sp *Sprite) OnComponentBind(binded *GameObject) {
	binded.Sprite = sp
}

/*
func (sp *Sprite) CreateVBO(uvs ...UV) {
	l := len(uvs)

	if l == 0 {
		sp.buffer.Bind(gl.ARRAY_BUFFER)
		gl.BufferData(gl.ARRAY_BUFFER, 1, []byte{1}, gl.STATIC_DRAW)
		return
	}

	lt := l * 12
	_ = lt
	data := make([]float32, 20*l)

	vertexCount := 0
	texcoordsIndex := lt * 4
	for i, uv := range uvs {

		yratio := float32(1)
		xratio := uv.Ratio
		ygrid := float32(-0.5)
		xgrid := float32(-uv.Ratio / 2)

		vertexCount += 4

		data[(i*12)+0] = xgrid
		data[(i*12)+1] = ygrid
		data[(i*12)+2] = 1
		data[(i*12)+3] = (xratio) + xgrid
		data[(i*12)+4] = ygrid
		data[(i*12)+5] = 1
		data[(i*12)+6] = (xratio) + xgrid
		data[(i*12)+7] = (yratio) + ygrid
		data[(i*12)+8] = 1
		data[(i*12)+9] = xgrid
		data[(i*12)+10] = (yratio) + ygrid
		data[(i*12)+11] = 1

		data[lt+(i*8)+0] = uv.U1
		data[lt+(i*8)+1] = uv.V2
		data[lt+(i*8)+2] = uv.U2
		data[lt+(i*8)+3] = uv.V2

		data[lt+(i*8)+4] = uv.U2
		data[lt+(i*8)+5] = uv.V1
		data[lt+(i*8)+6] = uv.U1
		data[lt+(i*8)+7] = uv.V1
	}

	sp.texcoordsIndex = texcoordsIndex
	sp.buffer.Bind(gl.ARRAY_BUFFER)
	gl.BufferData(gl.ARRAY_BUFFER, len(data)*4, data, gl.STATIC_DRAW)
}
*/

func (sp *Sprite) Start() {

}

func (sp *Sprite) RealWorldSize() (s Vector) {
	currentUV := sp.UVs[int(sp.animation)]
	s = sp.Transform().WorldScale()
	s.X *= currentUV.Ratio
	return
}

func (sp *Sprite) RealSize() (s Vector) {
	currentUV := sp.UVs[int(sp.animation)]
	s = sp.Transform().Scale()
	s.X *= currentUV.Ratio
	return
}

func (sp *Sprite) CurrentAnimationIndex() int {
	return int(sp.animation) - sp.startAnimation
}

func (sp *Sprite) AnimationLength() int {
	return sp.endAnimation - sp.startAnimation
}

func (sp *Sprite) SetAnimationIndex(index int) {
	if index <= 0 {
		sp.animation = float32(sp.startAnimation)
	} else {
		sp.animation = float32((index % (sp.endAnimation - sp.startAnimation)) + sp.startAnimation)
	}
}

var renders = 0

func (sp *Sprite) Update() {
{'fraction_non_alphanumeric': 0.1675458078845086, 'fraction_numerical': 0.03553581343697946, 'mean_word_length': 3.218384074941452, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '2307499', 'n_tokens_mistral': 3155, 'n_tokens_neox': 2944, 'n_words': 622}