Bug 18: Separated ShapeRenderer

This commit is contained in:
Robert Vokac 2024-10-12 16:24:34 +02:00
parent cd20ac8ea6
commit 0201173b5a
Signed by: robertvokac
GPG Key ID: FB9CE8E20AADA55F
6 changed files with 259 additions and 3 deletions

@ -17,13 +17,55 @@
// <https://www.gnu.org/licenses/> 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());
}
}

@ -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();
}

@ -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
// <https://www.gnu.org/licenses/> 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);
}
}

@ -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
// <https://www.gnu.org/licenses/> 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);
}
}

@ -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
// <https://www.gnu.org/licenses/> 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;
}

@ -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
// <https://www.gnu.org/licenses/> 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);
}
}