<commit_before>/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2014-2015 <NAME>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package com.shc.silenceengine.graphics;

import com.shc.silenceengine.core.Display;
import com.shc.silenceengine.core.Game;
import com.shc.silenceengine.graphics.cameras.BaseCamera;
import com.shc.silenceengine.graphics.cameras.OrthoCam;
import com.shc.silenceengine.graphics.opengl.Primitive;
import com.shc.silenceengine.graphics.opengl.Texture;
import com.shc.silenceengine.math.Matrix4;
import com.shc.silenceengine.math.Transform;
import com.shc.silenceengine.math.Vector2;
import com.shc.silenceengine.math.Vector3;
import com.shc.silenceengine.math.geom2d.Polygon;
import com.shc.silenceengine.utils.MathUtils;

/**
 * @author <NAME>
 */
public class Graphics2D
{
    private static Graphics2D   instance;

    private OrthoCam     camera;
    private Paint        paint;
    private TrueTypeFont font;
    private Transform    transform;

    /* Utility Methods */
    private BaseCamera originalCamera;
    private Texture    originalTexture;

    protected Graphics2D()
    {
        camera = new OrthoCam().initProjection(Display.getWidth(), Display.getHeight());

        paint = new Paint();
        transform = new Transform();
    }

    public static Graphics2D getInstance()
    {
        if (instance == null)
            instance = new Graphics2D();

        return instance;
    }

    public void drawRect(Vector2 pos, float w, float h)
    {
        drawRect(pos.x, pos.y, w, h);
    }

    public void drawRect(float x, float y, float w, float h)
    {
        rect(x, y, w, h, Primitive.LINE_LOOP);
    }

    /* SHAPE DRAWING METHODS */
    private void rect(float x, float y, float w, float h, Primitive primitive)
    {
        Batcher batcher = Game.getBatcher();
        batcher.applyTransform(transform);

        startPainting();
        {
            batcher.begin(primitive);
            {
                batcher.vertex(x, y);
                batcher.color(paint.getTopLeftColor());
                batcher.normal(Vector3.AXIS_Z);

                batcher.vertex(x + w, y);
                batcher.color(paint.getTopRightColor());
                batcher.normal(Vector3.AXIS_Z);

                batcher.vertex(x + w, y + h);
                batcher.color(paint.getBottomRightColor());
                batcher.normal(Vector3.AXIS_Z);

                batcher.vertex(x, y + h);
                batcher.color(paint.getBottomLeftColor());
                batcher.normal(Vector3.AXIS_Z);
            }
            batcher.end();
        }
        endPainting();
    }

    protected void startPainting()
    {
        originalCamera = BaseCamera.CURRENT;
        camera.apply();

        originalTexture = Texture.CURRENT;
    }

    protected void endPainting()
    {
        originalTexture.bind();

        if (originalCamera != null)
            originalCamera.apply();
    }

    public void drawRect(Vector2 min, Vector2 max)
    {
        Vector2 size = max.subtract(min);
        drawRect(min.x, min.y, size.x, size.y);
    }

    public void fillRect(Vector2 pos, float w, float h)
    {
        fillRect(pos.x, pos.y, w, h);
    }

    public void fillRect(float x, float y, float w, float h)
    {
        rect(x, y, w, h, Primitive.TRIANGLE_FAN);
    }

    public void fillRect(Vector2 min, Vector2 max)
    {
        Vector2 size = max.subtract(min);
        fillRect(min.x, min.y, size.x, size.y);
    }

    public void drawOval(Vector2 pos, float rx, float ry)
    {
        drawOval(pos.x, pos.y, rx, ry);
    }

    public void drawOval(float x, float y, float rx, float ry)
    {
        oval(x, y, rx, ry, Primitive.LINE_LOOP);
    }

    private void oval(float x, float y, float rx, float ry, Primitive primitive)
    {
        Batcher batcher = Game.getBatcher();
        batcher.applyTransform(transform);

        Vector2 vertex = Vector2.REUSABLE_STACK.pop();
        Color color = Color.REUSABLE_STACK.pop();

        startPainting();
        {
            float width = 2 * rx;
            float height = 2 * ry;

            batcher.begin(primitive);
            {
                for (int i = 0; i < 360; i++)
                {
                    vertex.set(MathUtils.cos(i) * rx, MathUtils.sin(i) * ry);
                    batcher.vertex(x + vertex.x, y + vertex.y);

                    batcher.color(paint.getColor(vertex.x / width, vertex.y / height, color));
                    batcher.normal(Vector3.AXIS_Z);
                }
            }
            batcher.end();
        }
        endPainting();

        Vector2.REUSABLE_STACK.push(vertex);
        Color.REUSABLE_STACK.push(color);
    }

    public void fillOval(Vector2 pos, float rx, float ry)
    {
        fillOval(pos.x, pos.y, rx, ry);
    }

    public void fillOval(float x, float y, float rx, float ry)
    {
        oval(x, y, rx, ry, Primitive.TRIANGLE_FAN);
    }

    public void drawCircle(Vector2 pos, float r)
    {
        drawCircle(pos.x, pos.y, r);
    }

    public void drawCircle(float x, float y, float r)
    {
        drawOval(x, y, r, r);
    }
<commit_msg>Correction to the color of the drawn oval<commit_after>/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2014-2015 <NAME>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package com.shc.silenceengine.graphics;

import com.shc.silenceengine.core.Display;
import com.shc.silenceengine.core.Game;
import com.shc.silenceengine.graphics.cameras.BaseCamera;
import com.shc.silenceengine.graphics.cameras.OrthoCam;
import com.shc.silenceengine.graphics.opengl.Primitive;
import com.shc.silenceengine.graphics.opengl.Texture;
import com.shc.silenceengine.math.Matrix4;
import com.shc.silenceengine.math.Transform;
import com.shc.silenceengine.math.Vector2;
import com.shc.silenceengine.math.Vector3;
import com.shc.silenceengine.math.geom2d.Polygon;
import com.shc.silenceengine.utils.MathUtils;

/**
 * @author <NAME>
 */
public class Graphics2D
{
    private static Graphics2D   instance;

    private OrthoCam     camera;
    private Paint        paint;
    private TrueTypeFont font;
    private Transform    transform;

    /* Utility Methods */
    private BaseCamera originalCamera;
    private Texture    originalTexture;

    protected Graphics2D()
    {
        camera = new OrthoCam().initProjection(Display.getWidth(), Display.getHeight());

        paint = new Paint();
        transform = new Transform();
    }

    public static Graphics2D getInstance()
    {
        if (instance == null)
            instance = new Graphics2D();

        return instance;
    }

    public void drawRect(Vector2 pos, float w, float h)
    {
        drawRect(pos.x, pos.y, w, h);
    }

    public void drawRect(float x, float y, float w, float h)
    {
        rect(x, y, w, h, Primitive.LINE_LOOP);
    }

    /* SHAPE DRAWING METHODS */
    private void rect(float x, float y, float w, float h, Primitive primitive)
    {
        Batcher batcher = Game.getBatcher();
        batcher.applyTransform(transform);

        startPainting();
        {
            batcher.begin(primitive);
            {
                batcher.vertex(x, y);
                batcher.color(paint.getTopLeftColor());
                batcher.normal(Vector3.AXIS_Z);

                batcher.vertex(x + w, y);
                batcher.color(paint.getTopRightColor());
                batcher.normal(Vector3.AXIS_Z);

                batcher.vertex(x + w, y + h);
                batcher.color(paint.getBottomRightColor());
                batcher.normal(Vector3.AXIS_Z);

                batcher.vertex(x, y + h);
                batcher.color(paint.getBottomLeftColor());
                batcher.normal(Vector3.AXIS_Z);
            }
            batcher.end();
        }
        endPainting();
    }

    protected void startPainting()
    {
        originalCamera = BaseCamera.CURRENT;
        camera.apply();

        originalTexture = Texture.CURRENT;
    }

    protected void endPainting()
    {
        originalTexture.bind();

        if (originalCamera != null)
            originalCamera.apply();
    }

    public void drawRect(Vector2 min, Vector2 max)
    {
        Vector2 size = max.subtract(min);
        drawRect(min.x, min.y, size.x, size.y);
    }

    public void fillRect(Vector2 pos, float w, float h)
    {
        fillRect(pos.x, pos.y, w, h);
    }

    public void fillRect(float x, float y, float w, float h)
    {
        rect(x, y, w, h, Primitive.TRIANGLE_FAN);
    }

    public void fillRect(Vector2 min, Vector2 max)
    {
        Vector2 size = max.subtract(min);
        fillRect(min.x, min.y, size.x, size.y);
    }

    public void drawOval(Vector2 pos, float rx, float ry)
    {
        drawOval(pos.x, pos.y, rx, ry);
    }

    public void drawOval(float x, float y, float rx, float ry)
    {
        oval(x, y, rx, ry, Primitive.LINE_LOOP);
    }

    private void oval(float x, float y, float rx, float ry, Primitive primitive)
    {
        Batcher batcher = Game.getBatcher();
        batcher.applyTransform(transform);

        Vector2 vertex = Vector2.REUSABLE_STACK.pop();
        Color color = Color.REUSABLE_STACK.pop();

        startPainting();
        {
            float width = 2 * rx;
            float height = 2 * ry;

            batcher.begin(primitive);
            {
                for (int i = 0; i < 360; i++)
                {
                    vertex.set(MathUtils.cos(i) * rx, MathUtils.sin(i) * ry);
                    batcher.vertex(x + vertex.x, y + vertex.y);

                    batcher.color(paint.getColor((rx + vertex.x) / width, (ry + vertex.y) / height, color));
                    batcher.normal(Vector3.AXIS_Z);
                }
            }
            batcher.end();
        }
        endPainting();

        Vector2.REUSABLE_STACK.push(vertex);
        Color.REUSABLE_STACK.push(color);
    }

    public void fillOval(Vector2 pos, float rx, float ry)
    {
        fillOval(pos.x, pos.y, rx, ry);
    }

    public void fillOval(float x, float y, float rx, float ry)
    {
        oval(x, y, rx, ry, Primitive.TRIANGLE_FAN);
    }

    public void drawCircle(Vector2 pos, float r)
    {
        drawCircle(pos.x, pos.y, r);
    }

    public void drawCircle(float x, float y, float r)
    {
        drawOval(x, y, r, r);
    }

{'fraction_non_alphanumeric': 0.1023736476667205, 'fraction_numerical': 0.00678185047634426, 'mean_word_length': 2.2614533965244865, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, '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': '5561865', 'n_tokens_mistral': 3907, 'n_tokens_neox': 3693, 'n_words': 1051}