<commit_before>// Copyright 2014 <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 twodee

import (
	"fmt"
	"github.com/go-gl/gl"
)

type Renderer struct {
	worldBounds  Rectangle
	screenBounds Rectangle
	projection   *Matrix4
	inverse      *Matrix4
}

func NewRenderer(world, screen Rectangle) (r *Renderer, err error) {
	r = &Renderer{}
	r.SetScreenBounds(screen)
	err = r.SetWorldBounds(world)
	return
}

func (r *Renderer) SetScreenBounds(bounds Rectangle) {
	r.screenBounds = bounds
}

func (r *Renderer) SetWorldBounds(bounds Rectangle) (err error) {
	r.worldBounds = bounds
	r.projection = GetOrthoMatrix(bounds.Min.X, bounds.Max.X, bounds.Min.Y, bounds.Max.Y, 1, 0)
	r.inverse, err = GetInverseMatrix(r.projection)
	return
}

func (r *Renderer) ScreenToWorldCoords(x, y float32) (wx, wy float32) {
	// http://stackoverflow.com/questions/7692988/
	var (
		halfw = r.screenBounds.Max.X / 2.0
		halfh = r.screenBounds.Max.Y / 2.0
		xpct  = (x - halfw) / halfw
		ypct  = (halfh - y) / halfh
	)
	return Unproject(r.inverse, xpct, ypct)
}

func (r *Renderer) WorldToScreenCoords(x, y float32) (sx, sy float32) {
	var pctx, pcty = returProject(r.projection, x, y)
	sx = pctx * r.screenBounds.Max.X + (r.screenBounds.Max.X / 2.0)
	sy = pcty * r.screenBounds.Max.Y + (r.screenBounds.Max.Y / 2.0)
	return
}

func CreateVAO() (array gl.VertexArray, err error) {
	array = gl.GenVertexArray()
	if e := gl.GetError(); e != 0 {
		err = fmt.Errorf("ERROR gl.GenVertexArray %X", e)
		return
	}
	array.Bind()
	if e := gl.GetError(); e != 0 {
		err = fmt.Errorf("ERROR array.Bind %X", e)
		return
	}
	return
}

func CreateVBO(size int, data interface{}, usage gl.GLenum) (buffer gl.Buffer, err error) {
	buffer = gl.GenBuffer()
	if e := gl.GetError(); e != 0 {
		err = fmt.Errorf("ERROR gl.GenBuffer %X", e)
		return
	}
	buffer.Bind(gl.ARRAY_BUFFER)
	if e := gl.GetError(); e != 0 {
		err = fmt.Errorf("ERROR buffer.Bind %X", e)
		return
	}
	gl.BufferData(gl.ARRAY_BUFFER, size, data, usage)
	if e := gl.GetError(); e != 0 {
		err = fmt.Errorf("ERROR gl.BufferData %X", e)
		return
	}
	buffer.Unbind(gl.ARRAY_BUFFER)
	if e := gl.GetError(); e != 0 {
		err = fmt.Errorf("ERROR buffer.Unbind %X", e)
		return
	}
	return
}

func CompileShader(stype gl.GLenum, source string) (shader gl.Shader, err error) {
	shader = gl.CreateShader(stype)
	shader.Source(source)
	shader.Compile()
	if status := shader.Get(gl.COMPILE_STATUS); status == 0 {
		err = fmt.Errorf("ERROR shader compile:\n%s", shader.GetInfoLog())
	}
	return
}

func LinkProgram(vertex gl.Shader, fragment gl.Shader) (program gl.Program, err error) {
	program = gl.CreateProgram()
	program.AttachShader(vertex)
	program.AttachShader(fragment)
	program.BindFragDataLocation(0, "v_FragData")
	if e := gl.GetError(); e != 0 {
		err = fmt.Errorf("ERROR program.BindFragDataLocation %X", e)
		return
	}
	program.Link()
	if status := program.Get(gl.LINK_STATUS); status == 0 {
		err = fmt.Errorf("ERROR program link:\n%s", program.GetInfoLog())
	}
	return
}

func BuildProgram(vsrc string, fsrc string) (program gl.Program, err error) {
	var (
		vertex   gl.Shader
		fragment gl.Shader
	)
	if vertex, err = CompileShader(gl.VERTEX_SHADER, vsrc); err != nil {
		return
	}
	if fragment, err = CompileShader(gl.FRAGMENT_SHADER, fsrc); err != nil {
		return
	}
	return LinkProgram(vertex, fragment)
}
<commit_msg>Fixed translation of game coordinates to screen coordinates<commit_after>// Copyright 2014 <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 twodee

import (
	"fmt"
	"github.com/go-gl/gl"
)

type Renderer struct {
	worldBounds  Rectangle
	screenBounds Rectangle
	projection   *Matrix4
	inverse      *Matrix4
}

func NewRenderer(world, screen Rectangle) (r *Renderer, err error) {
	r = &Renderer{}
	r.SetScreenBounds(screen)
	err = r.SetWorldBounds(world)
	return
}

func (r *Renderer) SetScreenBounds(bounds Rectangle) {
	r.screenBounds = bounds
}

func (r *Renderer) SetWorldBounds(bounds Rectangle) (err error) {
	r.worldBounds = bounds
	r.projection = GetOrthoMatrix(bounds.Min.X, bounds.Max.X, bounds.Min.Y, bounds.Max.Y, 1, 0)
	r.inverse, err = GetInverseMatrix(r.projection)
	return
}

func (r *Renderer) ScreenToWorldCoords(x, y float32) (wx, wy float32) {
	// http://stackoverflow.com/questions/7692988/
	var (
		halfw = r.screenBounds.Max.X / 2.0
		halfh = r.screenBounds.Max.Y / 2.0
		xpct  = (x - halfw) / halfw
		ypct  = (halfh - y) / halfh
	)
	return Unproject(r.inverse, xpct, ypct)
}

func (r *Renderer) WorldToScreenCoords(x, y float32) (sx, sy float32) {
	var pctx, pcty = Project(r.projection, x, y)
	var halfw = r.screenBounds.Max.X / 2.0
	var halfh = r.screenBounds.Max.Y / 2.0
	sx = pctx * halfw + halfw
	sy = pcty * halfh + halfh
	return
}

func CreateVAO() (array gl.VertexArray, err error) {
	array = gl.GenVertexArray()
	if e := gl.GetError(); e != 0 {
		err = fmt.Errorf("ERROR gl.GenVertexArray %X", e)
		return
	}
	array.Bind()
	if e := gl.GetError(); e != 0 {
		err = fmt.Errorf("ERROR array.Bind %X", e)
		return
	}
	return
}

func CreateVBO(size int, data interface{}, usage gl.GLenum) (buffer gl.Buffer, err error) {
	buffer = gl.GenBuffer()
	if e := gl.GetError(); e != 0 {
		err = fmt.Errorf("ERROR gl.GenBuffer %X", e)
		return
	}
	buffer.Bind(gl.ARRAY_BUFFER)
	if e := gl.GetError(); e != 0 {
		err = fmt.Errorf("ERROR buffer.Bind %X", e)
		return
	}
	gl.BufferData(gl.ARRAY_BUFFER, size, data, usage)
	if e := gl.GetError(); e != 0 {
		err = fmt.Errorf("ERROR gl.BufferData %X", e)
		return
	}
	buffer.Unbind(gl.ARRAY_BUFFER)
	if e := gl.GetError(); e != 0 {
		err = fmt.Errorf("ERROR buffer.Unbind %X", e)
		return
	}
	return
}

func CompileShader(stype gl.GLenum, source string) (shader gl.Shader, err error) {
	shader = gl.CreateShader(stype)
	shader.Source(source)
	shader.Compile()
	if status := shader.Get(gl.COMPILE_STATUS); status == 0 {
		err = fmt.Errorf("ERROR shader compile:\n%s", shader.GetInfoLog())
	}
	return
}

func LinkProgram(vertex gl.Shader, fragment gl.Shader) (program gl.Program, err error) {
	program = gl.CreateProgram()
	program.AttachShader(vertex)
	program.AttachShader(fragment)
	program.BindFragDataLocation(0, "v_FragData")
	if e := gl.GetError(); e != 0 {
		err = fmt.Errorf("ERROR program.BindFragDataLocation %X", e)
		return
	}
	program.Link()
	if status := program.Get(gl.LINK_STATUS); status == 0 {
		err = fmt.Errorf("ERROR program link:\n%s", program.GetInfoLog())
	}
	return
}

func BuildProgram(vsrc string, fsrc string) (program gl.Program, err error) {
	var (
		vertex   gl.Shader
		fragment gl.Shader
	)
	if vertex, err = CompileShader(gl.VERTEX_SHADER, vsrc); err != nil {
		return
	}
	if fragment, err = CompileShader(gl.FRAGMENT_SHADER, fsrc); err != nil {
		return
	}
	return LinkProgram(vertex, fragment)
}

{'fraction_non_alphanumeric': 0.1378159887078147, 'fraction_numerical': 0.011548825869369948, 'mean_word_length': 3.78159509202454, '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': '3247376', 'n_tokens_mistral': 2904, 'n_tokens_neox': 2795, 'n_words': 783}