<commit_before>package sprite

import (
	. "github.com/losinggeneration/hge-go/helpers/rect"
	hge "github.com/losinggeneration/hge-go/hge"
	. "github.com/losinggeneration/hge-go/legacy"
	"math"
)

type Sprite struct {
	*HGE

	hge.Quad
	TX, TY, W, H         float64
	TexW, TexH           float64
	HotX, HotY           float64
	XFlip, YFlip, HSFlip bool
}

func NewSprite(texture hge.Texture, texx, texy, w, h float64) Sprite {
	var sprite Sprite

	sprite.HGE = Create(hge.VERSION)

	sprite.TX, sprite.TY = texx, texy
	sprite.W, sprite.H = w, h

	if texture != 0 {
		sprite.TexW = float64(sprite.HGE.Texture_GetWidth(texture))
		sprite.TexH = float64(sprite.HGE.Texture_GetHeight(texture))
	} else {
		sprite.TexW = 1.0
		sprite.TexH = 1.0
	}

	sprite.Quad.Tex = texture

	texx1 := texx / sprite.TexW
	texy1 := texy / sprite.TexH
	texx2 := (texx + w) / sprite.TexW
	texy2 := (texy + h) / sprite.TexH

	sprite.Quad.V[0].TX, sprite.Quad.V[0].TY = float32(texx1), float32(texy1)
	sprite.Quad.V[1].TX, sprite.Quad.V[1].TY = float32(texx2), float32(texy1)
	sprite.Quad.V[2].TX, sprite.Quad.V[2].TY = float32(texx2), float32(texy2)
	sprite.Quad.V[3].TX, sprite.Quad.V[3].TY = float32(texx1), float32(texy2)

	sprite.Quad.V[0].Z = 0.5
	sprite.Quad.V[1].Z = 0.5
	sprite.Quad.V[2].Z = 0.5
	sprite.Quad.V[3].Z = 0.5

	sprite.Quad.V[0].Col = 0xffffffff
	sprite.Quad.V[1].Col = 0xffffffff
	sprite.Quad.V[2].Col = 0xffffffff
	sprite.Quad.V[3].Col = 0xffffffff

	sprite.Quad.Blend = hge.BLEND_DEFAULT

	return sprite
}

func (sprite *Sprite) Render(x, y float64) {
	tempx1 := x - sprite.HotX
	tempy1 := y - sprite.HotY
	tempx2 := x + sprite.W - sprite.HotX
	tempy2 := y + sprite.H - sprite.HotY

	sprite.Quad.V[0].X, sprite.Quad.V[0].Y = float32(tempx1), float32(tempy1)
	sprite.Quad.V[1].X, sprite.Quad.V[1].Y = float32(tempx2), float32(tempy1)
	sprite.Quad.V[2].X, sprite.Quad.V[2].Y = float32(tempx2), float32(tempy2)
	sprite.Quad.V[3].X, sprite.Quad.V[3].Y = float32(tempx1), float32(tempy2)

	sprite.HGE.Gfx_RenderQuad(&sprite.Quad)
}

func (sprite *Sprite) RenderEx(x, y float64, rot float64, arg ...interface{}) {
	var tx1, ty1, tx2, ty2 float64
	var sint, cost float64

	hscale, vscale := 1.0, 0.0

	for i := 0; i < len(arg); i++ {
		if i == 0 {
			if h, ok := arg[i].(float64); ok {
				hscale = h
			}
		}
		if i == 1 {
			if v, ok := arg[i].(float64); ok {
				vscale = v
			}
		}
	}

	if vscale == 0 {
		vscale = hscale
	}

	tx1 = -sprite.HotX * hscale
	ty1 = -sprite.HotY * vscale
	tx2 = (sprite.W - sprite.HotX) * hscale
	ty2 = (sprite.H - sprite.HotY) * vscale

	if rot != 0.0 {
		cost = math.Cos(rot)
		sint = math.Sin(rot)

		sprite.Quad.V[0].X = float32(tx1*cost - ty1*sint + x)
		sprite.Quad.V[0].Y = float32(tx1*sint + ty1*cost + y)

		sprite.Quad.V[1].X = float32(tx2*cost - ty1*sint + x)
		sprite.Quad.V[1].Y = float32(tx2*sint + ty1*cost + y)

		sprite.Quad.V[2].X = float32(tx2*cost - ty2*sint + x)
		sprite.Quad.V[2].Y = float32(tx2*sint + ty2*cost + y)

		sprite.Quad.V[3].X = float32(tx1*cost - ty2*sint + x)
		sprite.Quad.V[3].Y = float32(tx1*sint + ty2*cost + y)
	} else {
		sprite.Quad.V[0].X = float32(tx1 + x)
		sprite.Quad.V[0].Y = float32(ty1 + y)
		sprite.Quad.V[1].X = float32(tx2 + x)
		sprite.Quad.V[1].Y = float32(ty1 + y)
		sprite.Quad.V[2].X = float32(tx2 + x)
		sprite.Quad.V[2].Y = float32(ty2 + y)
		sprite.Quad.V[3].X = float32(tx1 + x)
		sprite.Quad.V[3].Y = float32(ty2 + y)
	}

	sprite.HGE.Gfx_RenderQuad(&sprite.Quad)
}

func (sprite *Sprite) RenderStretch(x1, y1, x2, y2 float64) {
	sprite.Quad.V[0].X, sprite.Quad.V[0].Y = float32(x1), float32(y1)
	sprite.Quad.V[1].X, sprite.Quad.V[1].Y = float32(x2), float32(y1)
	sprite.Quad.V[2].X, sprite.Quad.V[2].Y = float32(x2), float32(y2)
	sprite.Quad.V[3].X, sprite.Quad.V[3].Y = float32(x1), float32(y2)

	sprite.HGE.Gfx_RenderQuad(&sprite.Quad)
}

func (sprite *Sprite) Render4V(x0, y0, x1, y1, x2, y2, x3, y3 float64) {
	sprite.Quad.V[0].X, sprite.Quad.V[0].Y = float32(x0), float32(y0)
	sprite.Quad.V[1].X, sprite.Quad.V[1].Y = float32(x1), float32(y1)
	sprite.Quad.V[2].X, sprite.Quad.V[2].Y = float32(x2), float32(y2)
	sprite.Quad.V[3].X, sprite.Quad.V[3].Y = float32(x3), float32(y3)

	sprite.HGE.Gfx_RenderQuad(&sprite.Quad)
}

func (sprite *Sprite) SetTexture(tex hge.Texture) {
	var tx1, ty1, tx2, ty2 float64
	var tw, th float64

	sprite.Quad.Tex = tex

	if tex != 0 {
		tw = float64(sprite.HGE.Texture_GetWidth(tex))
		th = float64(sprite.HGE.Texture_GetHeight(tex))
	} else {
		tw, th = 1.0, 1.0
	}

	if tw != sprite.TexW || th != sprite.TexH {
		tx1 = float64(sprite.Quad.V[0].TX) * sprite.TexW
		ty1 = float64(sprite.Quad.V[0].TY) * sprite.TexH
		tx2 = float64(sprite.Quad.V[2].TX) * sprite.TexW
		ty2 = float64(sprite.Quad.V[2].TY) * sprite.TexH

		sprite.TexW, sprite.TexH = tw, th

		tx1 /= tw<commit_msg>Update sprite to use new API<commit_after>package sprite

import (
	. "github.com/losinggeneration/hge-go/helpers/rect"
	"github.com/losinggeneration/hge-go/hge"
	"math"
)

type Sprite struct {
	hge.Quad
	TX, TY, W, H         float64
	TexW, TexH           float64
	HotX, HotY           float64
	XFlip, YFlip, HSFlip bool
}

func NewSprite(texture hge.Texture, texx, texy, w, h float64) Sprite {
	var sprite Sprite

	sprite.TX, sprite.TY = texx, texy
	sprite.W, sprite.H = w, h

	if texture != 0 {
		sprite.TexW = float64(texture.Width())
		sprite.TexH = float64(texture.Height())
	} else {
		sprite.TexW = 1.0
		sprite.TexH = 1.0
	}

	sprite.Quad.Tex = texture

	texx1 := texx / sprite.TexW
	texy1 := texy / sprite.TexH
	texx2 := (texx + w) / sprite.TexW
	texy2 := (texy + h) / sprite.TexH

	sprite.Quad.V[0].TX, sprite.Quad.V[0].TY = float32(texx1), float32(texy1)
	sprite.Quad.V[1].TX, sprite.Quad.V[1].TY = float32(texx2), float32(texy1)
	sprite.Quad.V[2].TX, sprite.Quad.V[2].TY = float32(texx2), float32(texy2)
	sprite.Quad.V[3].TX, sprite.Quad.V[3].TY = float32(texx1), float32(texy2)

	sprite.Quad.V[0].Z = 0.5
	sprite.Quad.V[1].Z = 0.5
	sprite.Quad.V[2].Z = 0.5
	sprite.Quad.V[3].Z = 0.5

	sprite.Quad.V[0].Col = 0xffffffff
	sprite.Quad.V[1].Col = 0xffffffff
	sprite.Quad.V[2].Col = 0xffffffff
	sprite.Quad.V[3].Col = 0xffffffff

	sprite.Quad.Blend = hge.BLEND_DEFAULT

	return sprite
}

func (sprite *Sprite) Render(x, y float64) {
	tempx1 := x - sprite.HotX
	tempy1 := y - sprite.HotY
	tempx2 := x + sprite.W - sprite.HotX
	tempy2 := y + sprite.H - sprite.HotY

	sprite.Quad.V[0].X, sprite.Quad.V[0].Y = float32(tempx1), float32(tempy1)
	sprite.Quad.V[1].X, sprite.Quad.V[1].Y = float32(tempx2), float32(tempy1)
	sprite.Quad.V[2].X, sprite.Quad.V[2].Y = float32(tempx2), float32(tempy2)
	sprite.Quad.V[3].X, sprite.Quad.V[3].Y = float32(tempx1), float32(tempy2)

	sprite.Quad.Render()
}

func (sprite *Sprite) RenderEx(x, y float64, rot float64, arg ...interface{}) {
	var tx1, ty1, tx2, ty2 float64
	var sint, cost float64

	hscale, vscale := 1.0, 0.0

	for i := 0; i < len(arg); i++ {
		if i == 0 {
			if h, ok := arg[i].(float64); ok {
				hscale = h
			}
		}
		if i == 1 {
			if v, ok := arg[i].(float64); ok {
				vscale = v
			}
		}
	}

	if vscale == 0 {
		vscale = hscale
	}

	tx1 = -sprite.HotX * hscale
	ty1 = -sprite.HotY * vscale
	tx2 = (sprite.W - sprite.HotX) * hscale
	ty2 = (sprite.H - sprite.HotY) * vscale

	if rot != 0.0 {
		cost = math.Cos(rot)
		sint = math.Sin(rot)

		sprite.Quad.V[0].X = float32(tx1*cost - ty1*sint + x)
		sprite.Quad.V[0].Y = float32(tx1*sint + ty1*cost + y)

		sprite.Quad.V[1].X = float32(tx2*cost - ty1*sint + x)
		sprite.Quad.V[1].Y = float32(tx2*sint + ty1*cost + y)

		sprite.Quad.V[2].X = float32(tx2*cost - ty2*sint + x)
		sprite.Quad.V[2].Y = float32(tx2*sint + ty2*cost + y)

		sprite.Quad.V[3].X = float32(tx1*cost - ty2*sint + x)
		sprite.Quad.V[3].Y = float32(tx1*sint + ty2*cost + y)
	} else {
		sprite.Quad.V[0].X = float32(tx1 + x)
		sprite.Quad.V[0].Y = float32(ty1 + y)
		sprite.Quad.V[1].X = float32(tx2 + x)
		sprite.Quad.V[1].Y = float32(ty1 + y)
		sprite.Quad.V[2].X = float32(tx2 + x)
		sprite.Quad.V[2].Y = float32(ty2 + y)
		sprite.Quad.V[3].X = float32(tx1 + x)
		sprite.Quad.V[3].Y = float32(ty2 + y)
	}

	sprite.Quad.Render()
}

func (sprite *Sprite) RenderStretch(x1, y1, x2, y2 float64) {
	sprite.Quad.V[0].X, sprite.Quad.V[0].Y = float32(x1), float32(y1)
	sprite.Quad.V[1].X, sprite.Quad.V[1].Y = float32(x2), float32(y1)
	sprite.Quad.V[2].X, sprite.Quad.V[2].Y = float32(x2), float32(y2)
	sprite.Quad.V[3].X, sprite.Quad.V[3].Y = float32(x1), float32(y2)

	sprite.Quad.Render()
}

func (sprite *Sprite) Render4V(x0, y0, x1, y1, x2, y2, x3, y3 float64) {
	sprite.Quad.V[0].X, sprite.Quad.V[0].Y = float32(x0), float32(y0)
	sprite.Quad.V[1].X, sprite.Quad.V[1].Y = float32(x1), float32(y1)
	sprite.Quad.V[2].X, sprite.Quad.V[2].Y = float32(x2), float32(y2)
	sprite.Quad.V[3].X, sprite.Quad.V[3].Y = float32(x3), float32(y3)

	sprite.Quad.Render()
}

func (sprite *Sprite) SetTexture(tex hge.Texture) {
	var tx1, ty1, tx2, ty2 float64
	var tw, th float64

	sprite.Quad.Tex = tex

	if tex != 0 {
		tw = float64(tex.Width())
		th = float64(tex.Height())
	} else {
		tw, th = 1.0, 1.0
	}

	if tw != sprite.TexW || th != sprite.TexH {
		tx1 = float64(sprite.Quad.V[0].TX) * sprite.TexW
		ty1 = float64(sprite.Quad.V[0].TY) * sprite.TexH
		tx2 = float64(sprite.Quad.V[2].TX) * sprite.TexW
		ty2 = float64(sprite.Quad.V[2].TY) * sprite.TexH

		sprite.TexW, sprite.TexH = tw, th

		tx1 /= tw
{'fraction_non_alphanumeric': 0.18855434550501815, 'fraction_numerical': 0.07004057228272476, 'mean_word_length': 3.300734618916437, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 8, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2849348', 'n_tokens_mistral': 5014, 'n_tokens_neox': 4587, 'n_words': 781}