<commit_before>package com.ezardlabs.dethsquare;

import com.ezardlabs.dethsquare.TextureAtlas.Sprite;
import com.ezardlabs.dethsquare.util.GameListeners;
import com.ezardlabs.dethsquare.util.RenderUtils;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;

public class Renderer extends BoundedComponent {
	// Add hook into game loop
	static {
		GameListeners.addRenderListener(Renderer::renderAll);
	}
	static HashMap<String, int[]> textures = new HashMap<>();

	private static QuadTree<Renderer> qt = new QuadTree<>(30);
	private static ArrayList<Renderer> renderers = new ArrayList<>();

	private static ArrayList<Integer> idsRendered = new ArrayList<>();

	private static ArrayList<Renderer> visible = new ArrayList<>();

	private static float[] vertices = new float[0];
	private static short[] indices = new short[0];
	private static float[] uvs = new float[0];
	private static FloatBuffer vertexBuffer;
	private static ShortBuffer indexBuffer;
	private static FloatBuffer uvBuffer;

	Sprite sprite;
	public float width;
	public float height;
	private int zIndex = 0;
	public boolean hFlipped = false;
	public boolean vFlipped = false;

	public int textureName = -1;
	public Mode mode = Mode.NONE;
	public float xOffset;
	public float yOffset;

	private enum Mode {
		NONE,
		IMAGE,
		SPRITE
	}

	public Renderer() {
	}

	public Renderer(String imagePath, float width, float height) {
		setImage(imagePath, width, height);
	}

	public Renderer(TextureAtlas textureAtlas, Sprite sprite, float width, float height) {
		setTextureAtlas(textureAtlas, width, height);
		this.sprite = sprite;
	}

	public Renderer setFlipped(boolean hFlipped, boolean vFlipped) {
		this.hFlipped = hFlipped;
		this.vFlipped = vFlipped;
		return this;
	}

	public void setImage(String imagePath, float width, float height) {
		mode = Mode.IMAGE;
		if (textures.containsKey(imagePath)) {
			textureName = textures.get(imagePath)[0];
		} else {
			int[] data = RenderUtils.loadImage(imagePath);
			textures.put(imagePath, data);
			textureName = data[0];
		}
		this.width = width;
		this.height = height;
	}

	public void setSprite(Sprite sprite) {
		this.sprite = sprite;
	}

	public void setSize(float width, float height) {
		this.width = width;
		this.height = height;
	}

	public void setOffsets(float xOffset, float yOffset) {
		this.xOffset = xOffset;
		this.yOffset = yOffset;
	}

	public void setTextureAtlas(TextureAtlas textureAtlas, float spriteWidth, float spriteHeight) {
		textureName = textureAtlas.textureName;
		mode = Mode.SPRITE;
		width = spriteWidth;
		height = spriteHeight;
	}

	public Renderer setzIndex(int zIndex) {
		this.zIndex = zIndex;
		return this;
	}

	protected float getXPos() {
		return transform.position.x + xOffset;
	}

	protected float getYPos() {
		return transform.position.y + yOffset;
	}

	protected int getZIndex() {
		return zIndex;
	}

	@Override
	public void start() {
		renderers.add(this);
		vertices = new float[vertices.length + 12];
		indices = new short[indices.length + 6];
		uvs = new float[uvs.length + 8];
		vertexBuffer = ByteBuffer.allocateDirect(vertices.length * 4).order(ByteOrder.nativeOrder()).asFloatBuffer();
		indexBuffer = ByteBuffer.allocateDirect(indices.length * 2).order(ByteOrder.nativeOrder()).asShortBuffer();
		uvBuffer = ByteBuffer.allocateDirect(uvs.length * 4).order(ByteOrder.nativeOrder()).asFloatBuffer();
	}

	@Override
	protected void destroy() {
		renderers.remove(this);
		vertices = new float[vertices.length - 12];
		indices = new short[indices.length - 6];
		uvs = new float[uvs.length - 8];
	}

	public static void init() {
		ArrayList<Renderer> staticRenderers = new ArrayList<>();
		for (Renderer r : renderers.toArray(new Renderer[renderers.size()])) {
			r.bounds.set(r.transform.position.x, r.transform.position.y, r.transform.position.x + r.width, r.transform.position.y + r.height);
			if (r.gameObject.isStatic) {
				staticRenderers.add(r);
				renderers.remove(r);
			}
		}
		qt.init(staticRenderers.toArray(new Renderer[staticRenderers.size()]));
	}

	static void clearAll() {
		renderers.clear();
	}

	static void destroyAllTextures() {
		RenderUtils.destroyAllTextures(textures);
		textures.clear();
	}

	static void clearQuadTree() {
		qt = new QuadTree<>(30);
	}

	static HashMap<Integer, ArrayList<Renderer>> map = new HashMap<>();

	private static void renderAll() {
		visible.clear();<commit_msg>Move field declaration to top of file<commit_after>package com.ezardlabs.dethsquare;

import com.ezardlabs.dethsquare.TextureAtlas.Sprite;
import com.ezardlabs.dethsquare.util.GameListeners;
import com.ezardlabs.dethsquare.util.RenderUtils;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;

public class Renderer extends BoundedComponent {
	// Add hook into game loop
	static {
		GameListeners.addRenderListener(Renderer::renderAll);
	}
	static HashMap<String, int[]> textures = new HashMap<>();

	private static QuadTree<Renderer> qt = new QuadTree<>(30);
	private static ArrayList<Renderer> renderers = new ArrayList<>();

	private static ArrayList<Integer> idsRendered = new ArrayList<>();
	private static HashMap<Integer, ArrayList<Renderer>> map = new HashMap<>();

	private static ArrayList<Renderer> visible = new ArrayList<>();

	private static float[] vertices = new float[0];
	private static short[] indices = new short[0];
	private static float[] uvs = new float[0];
	private static FloatBuffer vertexBuffer;
	private static ShortBuffer indexBuffer;
	private static FloatBuffer uvBuffer;

	Sprite sprite;
	public float width;
	public float height;
	private int zIndex = 0;
	public boolean hFlipped = false;
	public boolean vFlipped = false;

	public int textureName = -1;
	public Mode mode = Mode.NONE;
	public float xOffset;
	public float yOffset;

	private enum Mode {
		NONE,
		IMAGE,
		SPRITE
	}

	public Renderer() {
	}

	public Renderer(String imagePath, float width, float height) {
		setImage(imagePath, width, height);
	}

	public Renderer(TextureAtlas textureAtlas, Sprite sprite, float width, float height) {
		setTextureAtlas(textureAtlas, width, height);
		this.sprite = sprite;
	}

	public Renderer setFlipped(boolean hFlipped, boolean vFlipped) {
		this.hFlipped = hFlipped;
		this.vFlipped = vFlipped;
		return this;
	}

	public void setImage(String imagePath, float width, float height) {
		mode = Mode.IMAGE;
		if (textures.containsKey(imagePath)) {
			textureName = textures.get(imagePath)[0];
		} else {
			int[] data = RenderUtils.loadImage(imagePath);
			textures.put(imagePath, data);
			textureName = data[0];
		}
		this.width = width;
		this.height = height;
	}

	public void setSprite(Sprite sprite) {
		this.sprite = sprite;
	}

	public void setSize(float width, float height) {
		this.width = width;
		this.height = height;
	}

	public void setOffsets(float xOffset, float yOffset) {
		this.xOffset = xOffset;
		this.yOffset = yOffset;
	}

	public void setTextureAtlas(TextureAtlas textureAtlas, float spriteWidth, float spriteHeight) {
		textureName = textureAtlas.textureName;
		mode = Mode.SPRITE;
		width = spriteWidth;
		height = spriteHeight;
	}

	public Renderer setzIndex(int zIndex) {
		this.zIndex = zIndex;
		return this;
	}

	protected float getXPos() {
		return transform.position.x + xOffset;
	}

	protected float getYPos() {
		return transform.position.y + yOffset;
	}

	protected int getZIndex() {
		return zIndex;
	}

	@Override
	public void start() {
		renderers.add(this);
		vertices = new float[vertices.length + 12];
		indices = new short[indices.length + 6];
		uvs = new float[uvs.length + 8];
		vertexBuffer = ByteBuffer.allocateDirect(vertices.length * 4).order(ByteOrder.nativeOrder()).asFloatBuffer();
		indexBuffer = ByteBuffer.allocateDirect(indices.length * 2).order(ByteOrder.nativeOrder()).asShortBuffer();
		uvBuffer = ByteBuffer.allocateDirect(uvs.length * 4).order(ByteOrder.nativeOrder()).asFloatBuffer();
	}

	@Override
	protected void destroy() {
		renderers.remove(this);
		vertices = new float[vertices.length - 12];
		indices = new short[indices.length - 6];
		uvs = new float[uvs.length - 8];
	}

	public static void init() {
		ArrayList<Renderer> staticRenderers = new ArrayList<>();
		for (Renderer r : renderers.toArray(new Renderer[renderers.size()])) {
			r.bounds.set(r.transform.position.x, r.transform.position.y, r.transform.position.x + r.width, r.transform.position.y + r.height);
			if (r.gameObject.isStatic) {
				staticRenderers.add(r);
				renderers.remove(r);
			}
		}
		qt.init(staticRenderers.toArray(new Renderer[staticRenderers.size()]));
	}

	static void clearAll() {
		renderers.clear();
	}

	static void destroyAllTextures() {
		RenderUtils.destroyAllTextures(textures);
		textures.clear();
	}

	static void clearQuadTree() {
		qt = new QuadTree<>(30);
	}

	private static void renderAll() {
		visible.clear();
{'fraction_non_alphanumeric': 0.11566794942275976, 'fraction_numerical': 0.004837822979659154, 'mean_word_length': 4.6113510178901915, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 35, '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': '5826368', 'n_tokens_mistral': 3119, 'n_tokens_neox': 2907, 'n_words': 724}