From 0201173b5a9fbdae7b32a86f32d4e8fee9df0051 Mon Sep 17 00:00:00 2001 From: Robert Vokac Date: Sat, 12 Oct 2024 16:24:34 +0200 Subject: [PATCH] Bug 18: Separated ShapeRenderer --- .../api/graphics/ShapeRenderer.java | 48 ++++++++- .../api/graphics/SpriteBatch.java | 2 + .../pixelgamelibrary/api/math/AngleUnit.java | 32 ++++++ .../api/math/AngleUnitConverter.java | 102 ++++++++++++++++++ .../pixelgamelibrary/api/math/Rectangle.java | 33 ++++++ .../pixelgamelibrary/api/math/Rotation.java | 45 ++++++++ 6 files changed, 259 insertions(+), 3 deletions(-) create mode 100644 src/main/java/com/pixelgamelibrary/api/math/AngleUnit.java create mode 100644 src/main/java/com/pixelgamelibrary/api/math/AngleUnitConverter.java create mode 100644 src/main/java/com/pixelgamelibrary/api/math/Rectangle.java create mode 100644 src/main/java/com/pixelgamelibrary/api/math/Rotation.java diff --git a/src/main/java/com/pixelgamelibrary/api/graphics/ShapeRenderer.java b/src/main/java/com/pixelgamelibrary/api/graphics/ShapeRenderer.java index 2576b4a..eee8853 100644 --- a/src/main/java/com/pixelgamelibrary/api/graphics/ShapeRenderer.java +++ b/src/main/java/com/pixelgamelibrary/api/graphics/ShapeRenderer.java @@ -17,13 +17,55 @@ // or write to the Free Software // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. /////////////////////////////////////////////////////////////////////////////////////////////// - package com.pixelgamelibrary.api.graphics; +import com.pixelgamelibrary.api.Disposable; +import com.pixelgamelibrary.api.math.Rectangle; +import com.pixelgamelibrary.api.math.Rotation; + /** * * @author robertvokac */ -public interface ShapeRenderer { - +public interface ShapeRenderer extends Disposable { + + void setColor(Color color); + + default void setColor(float r, float g, float b) { + setColor(new Color(r, g, b)); + } + + default void setColor(float r, float g, float b, float a) { + setColor(new Color(r, g, b, a)); + } + + Color getColor(); + + void filledRectangle(float x, float y, float width, float height, float rotation, Color color); + + default void filledRectangle(float x, float y, float width, float height, Color color) { + filledRectangle(x, y, width, height, Rotation.DEFAULT_ROTATION_IN_ANGLES, color); + } + + default void filledRectangle(float x, float y, float width, float height, float rotation) { + filledRectangle(x, y, width, height, rotation, getColor()); + } + + default void filledRectangle(float x, float y, float width, float height) { + filledRectangle(x, y, width, height, Rotation.DEFAULT_ROTATION_IN_ANGLES, getColor()); + } + + default void filledRectangle(Rectangle rectangle, Color color) { + filledRectangle(rectangle.getX(), rectangle.getY(), rectangle.getWidth(), rectangle.getHeight(), color); + } + + default void filledRectangle(Rectangle rectangle, float rotation) { + filledRectangle(rectangle.getX(), rectangle.getY(), rectangle.getWidth(), rectangle.getHeight(), rotation); + } + + default void filledRectangle(Rectangle rectangle) { + filledRectangle(rectangle.getX(), rectangle.getY(), rectangle.getWidth(), rectangle.getHeight()); + } + + } diff --git a/src/main/java/com/pixelgamelibrary/api/graphics/SpriteBatch.java b/src/main/java/com/pixelgamelibrary/api/graphics/SpriteBatch.java index 0540f9f..1851a09 100644 --- a/src/main/java/com/pixelgamelibrary/api/graphics/SpriteBatch.java +++ b/src/main/java/com/pixelgamelibrary/api/graphics/SpriteBatch.java @@ -34,5 +34,7 @@ public interface SpriteBatch extends Disposable { void draw(Texture texture, int x, int y, int width, int height); void draw(Texture texture, int x, int y); + + ShapeRenderer drawShape(); } diff --git a/src/main/java/com/pixelgamelibrary/api/math/AngleUnit.java b/src/main/java/com/pixelgamelibrary/api/math/AngleUnit.java new file mode 100644 index 0000000..1e43548 --- /dev/null +++ b/src/main/java/com/pixelgamelibrary/api/math/AngleUnit.java @@ -0,0 +1,32 @@ +/////////////////////////////////////////////////////////////////////////////////////////////// +// Pixel: Game library. +// Copyright (C) 2024 the original author or authors. +// +// This program is free software: you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation, either version 3 +// of the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see +// or write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +/////////////////////////////////////////////////////////////////////////////////////////////// +package com.pixelgamelibrary.api.math; + +/** + * + * @author robertvokac + */ +public enum AngleUnit { + + DEGREE, NORMALIZED, RADIAN, GRADIAN; + public float convert(float value, AngleUnit inputAngleUnit, AngleUnit outputAngleUnit) { + return AngleUnitConverter.convert(value, inputAngleUnit, outputAngleUnit); + } +} diff --git a/src/main/java/com/pixelgamelibrary/api/math/AngleUnitConverter.java b/src/main/java/com/pixelgamelibrary/api/math/AngleUnitConverter.java new file mode 100644 index 0000000..348dc88 --- /dev/null +++ b/src/main/java/com/pixelgamelibrary/api/math/AngleUnitConverter.java @@ -0,0 +1,102 @@ +/////////////////////////////////////////////////////////////////////////////////////////////// +// Pixel: Game library. +// Copyright (C) 2024 the original author or authors. +// +// This program is free software: you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation, either version 3 +// of the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see +// or write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +/////////////////////////////////////////////////////////////////////////////////////////////// +package com.pixelgamelibrary.api.math; + +import com.pixelgamelibrary.api.PixelException; +import static com.pixelgamelibrary.api.math.AngleUnit.GRADIAN; +import static com.pixelgamelibrary.api.math.AngleUnit.NORMALIZED; +import static com.pixelgamelibrary.api.math.AngleUnit.RADIAN; + +/** + * + * @author robertvokac + */ +class AngleUnitConverter { + + public static final float _360_DEGREES = 360f; + public static final float _180_DEGREES = 180f; + public static final float _90_DEGREES = 90f; + public static final float DEGREES_PER_RADIAN = _180_DEGREES / (float) Math.PI; + public static final float RADIANS_PER_DEGREE = (float) Math.PI / _180_DEGREES; + public static final float GRADIANS_PER_90_DEGREES = 100f; + + private AngleUnitConverter() { + //Not meant to be instantiated. + } + + public static float convertDegreesToRadians(float degrees) { + return degrees * RADIANS_PER_DEGREE; + } + + public static float convertDegreesToGradians(float degrees) { + return degrees / _90_DEGREES * GRADIANS_PER_90_DEGREES; + } + + public static float convertDegreesToNormalized(float degrees) { + return degrees / _360_DEGREES; + } + + public static float convertRadiansToDegrees(float radians) { + return radians * DEGREES_PER_RADIAN; + } + + public static float convertGradiansToDegrees(float gradians) { + return gradians / GRADIANS_PER_90_DEGREES * 90f; + } + + public static float convertNormalizedToDegrees(float normalized) { + return normalized * _360_DEGREES; + } + + public static float convert(float value, AngleUnit inputAngleUnit, AngleUnit outputAngleUnit) { + if (inputAngleUnit == outputAngleUnit) { + //nothing to do + return value; + } + if (inputAngleUnit == AngleUnit.DEGREE) { + switch (outputAngleUnit) { + case NORMALIZED: + return convertDegreesToNormalized(value); + case RADIAN: + return convertDegreesToRadians(value); + case GRADIAN: + return convertDegreesToGradians(value); + default: + throw new PixelException("Unsupported operation."); + } + } + + if (outputAngleUnit == AngleUnit.DEGREE) { + switch (inputAngleUnit) { + case NORMALIZED: + return convertNormalizedToDegrees(value); + case RADIAN: + return convertRadiansToDegrees(value); + case GRADIAN: + return convertGradiansToDegrees(value); + default: + throw new PixelException("Unsupported operation."); + } + } + float degrees = convert(value, inputAngleUnit, AngleUnit.DEGREE); + return convert(degrees, AngleUnit.DEGREE, outputAngleUnit); + + } +} diff --git a/src/main/java/com/pixelgamelibrary/api/math/Rectangle.java b/src/main/java/com/pixelgamelibrary/api/math/Rectangle.java new file mode 100644 index 0000000..88c7dc6 --- /dev/null +++ b/src/main/java/com/pixelgamelibrary/api/math/Rectangle.java @@ -0,0 +1,33 @@ +/////////////////////////////////////////////////////////////////////////////////////////////// +// Pixel: Game library. +// Copyright (C) 2024 the original author or authors. +// +// This program is free software: you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation, either version 3 +// of the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see +// or write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +/////////////////////////////////////////////////////////////////////////////////////////////// +package com.pixelgamelibrary.api.math; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.Setter; + +/** + * + * @author robertvokac + */ +@AllArgsConstructor @Getter @Setter +public class Rectangle { + private float x, y, width, height; +} diff --git a/src/main/java/com/pixelgamelibrary/api/math/Rotation.java b/src/main/java/com/pixelgamelibrary/api/math/Rotation.java new file mode 100644 index 0000000..57058c2 --- /dev/null +++ b/src/main/java/com/pixelgamelibrary/api/math/Rotation.java @@ -0,0 +1,45 @@ +/////////////////////////////////////////////////////////////////////////////////////////////// +// Pixel: Game library. +// Copyright (C) 2024 the original author or authors. +// +// This program is free software: you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation, either version 3 +// of the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see +// or write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +/////////////////////////////////////////////////////////////////////////////////////////////// +package com.pixelgamelibrary.api.math; + +/** + * + * @author robertvokac + */ +public class Rotation { + + public static final float DEFAULT_ROTATION_IN_ANGLES = 0f; + + private float rotatioInAngles; + public static Rotation ofAngles(float angles) { + return new Rotation(angles); + } + + public static Rotation ofRadians(float radians) { + return new Rotation(convertRadiansToAngles(radians)); + } + + private Rotation(float rotatioInAnglesIn) { + this.rotatioInAngles = rotatioInAnglesIn; + } + public float getAsRadians() { + return convertAnglesToRadians(rotatioInAngles); + } +}