Skip to content
This repository has been archived by the owner on Oct 2, 2020. It is now read-only.

Commit

Permalink
Image filters are now stored as an enum
Browse files Browse the repository at this point in the history
  • Loading branch information
WhyAreAllTheseTaken committed Nov 29, 2019
1 parent f6046da commit da97332
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 33 deletions.
67 changes: 55 additions & 12 deletions RPGOnline2/src/io/github/tomaso2468/rpgonline/Image.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,6 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
*
*/
public class Image implements Cloneable {
/**
* A filter that uses nearest neighbour interpolation.
*/
public static final int FILTER_NEAREST = 0;
/**
* A filter that uses linear interpolation.
*/
public static final int FILTER_LINEAR = 1;

/**
* The renderer for this image.
*/
Expand Down Expand Up @@ -97,7 +88,7 @@ public class Image implements Cloneable {
/**
* The filter this image uses.
*/
private int filter = FILTER_LINEAR;
private ImageFilter filter = RPGConfig.getFilterMode();

/**
* Constructs a new blank image.
Expand Down Expand Up @@ -175,7 +166,7 @@ public Image(Renderer renderer, Image img) {
* @see #FILTER_LINEAR
* @see #FILTER_NEAREST
*/
public void setFilter(int filterMode) {
public void setFilter(ImageFilter filterMode) {
renderer.setFilter(texture, filterMode);
this.filter = filterMode;
}
Expand Down Expand Up @@ -330,30 +321,69 @@ public float getTextureWidth() {
return textureWidth;
}

/**
* Gets the Y offset from within the texture.
*
* @return A float value.
*/
public float getTextureOffsetY() {
return textureY;
}

/**
* Gets the X offset from within the texture.
*
* @return A float value.
*/
public float getTextureHeight() {
return textureHeight;
}

/**
* Gets the width of the image in pixels.
*
* @return A float value.
*/
public float getWidth() {
return width;
}

/**
* Gets the height of the image in pixels.
*
* @return A float value.
*/
public float getHeight() {
return height;
}

/**
* Gets a copy of the image at the specified width and height. The backing texture is shared and not resized.
* @param w The width of the new image.
* @param h The height of the new image.
* @return A new scaled image.
*/
public Image getScaledCopy(float w, float h) {
return new Image(renderer, texture, textureX, textureY, textureWidth, textureHeight, w, h);
}

/**
* Gets a copy of the image scaled by the amount specified. The backing texture is shared and not resized.
* @param scale The amount to scale by (1 = original image).
* @return A new scaled image.
*/
public Image getScaledCopy(float scale) {
return getScaledCopy(width * scale, height * scale);
}

/**
* Gets a sub image of this image.
* @param x The x position to start at.
* @param y The y position to start at.
* @param w The width of the sub image.
* @param h The height of the sub image.
* @return A new sub image.
*/
public Image getSubImage(float x, float y, float w, float h) {
float newTextureOffsetX = ((x / (float) this.width) * textureWidth) + textureX;
float newTextureOffsetY = ((y / (float) this.height) * textureHeight) + textureY;
Expand All @@ -366,22 +396,35 @@ public Image getSubImage(float x, float y, float w, float h) {
return sub;
}

/**
* Creates a copy of this image with the same backing texture.
*/
public Image clone() {
return new Image(renderer, texture, textureX, textureY, textureWidth, textureHeight, width, height);
}

/**
* Destroys the texture backing this image.
*/
public void destroy() {
texture.destroy();
}

/**
* Flips texture coordinates on systems that need it.
*/
public void ensureInverted() {
if (textureHeight > 0) {
textureY = textureY + textureHeight;
textureHeight = -textureHeight;
}
}

public int getFilter() {
/**
* Gets the filter effect of this image.
* @return A value filter.
*/
public ImageFilter getFilter() {
return filter;
}
}
69 changes: 69 additions & 0 deletions RPGOnline2/src/io/github/tomaso2468/rpgonline/ImageFilter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
BSD 3-Clause License
Copyright (c) 2019, Tomas
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package io.github.tomaso2468.rpgonline;

import org.lwjgl.opengl.GL11;

/**
* An enum of image filtering modes.
* @author Tomaso2468
*
*/
public enum ImageFilter {
/**
* A filter that uses nearest neighbour interpolation.
*/
NEAREST(GL11.GL_NEAREST, org.newdawn.slick.Image.FILTER_NEAREST),
/**
* A filter that uses linear interpolation.
*/
LINEAR(GL11.GL_LINEAR, org.newdawn.slick.Image.FILTER_LINEAR),
;
/**
* The opengl constant for this filtering method.
*/
public final int glMapping;
/**
* The slick2d constant for this filtering method.
*/
public final int slickMapping;
/**
* Construct enum value.
* @param glMapping opengl
* @param slickMapping slick2d
*/
private ImageFilter(int glMapping, int slickMapping) {
this.glMapping = glMapping;
this.slickMapping = slickMapping;
}

}
18 changes: 5 additions & 13 deletions RPGOnline2/src/io/github/tomaso2468/rpgonline/RPGConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ private RPGConfig() {
* @see org.newdawn.slick.Image#setFilter(int)
* @see io.github.tomaso2468.rpgonline.TextureMap
*/
private static int filterMode = Image.FILTER_NEAREST;
private static ImageFilter filterMode = ImageFilter.NEAREST;

/**
* The number of threads to use for pathfinding.
Expand Down Expand Up @@ -536,33 +536,25 @@ public static void setHitbox(boolean hitbox) {
* Determines the default filter mode for textures. <b>This must be set before
* textures are initialised.</b>
*
* @return <a href=
* "http://slick.ninjacave.com/javadoc/org/newdawn/slick/Image.html#FILTER_NEAREST">FILTER_NEAREST</a>
* if nearest filtering is used and <a href=
* "http://slick.ninjacave.com/javadoc/org/newdawn/slick/Image.html#FILTER_LINEAR">FILTER_LINEAR</a>
* if bilinear filtering is used.
* @return An ImageFilter enum.
*
* @see org.newdawn.slick.Image#setFilter(int)
* @see io.github.tomaso2468.rpgonline.TextureMap
*/
public static int getFilterMode() {
public static ImageFilter getFilterMode() {
return filterMode;
}

/**
* Sets the default filter mode for textures. <b>This must be set before
* textures are initialised.</b>
*
* @param filterMode <a href=
* "http://slick.ninjacave.com/javadoc/org/newdawn/slick/Image.html#FILTER_NEAREST">FILTER_NEAREST</a>
* if nearest filtering is used and <a href=
* "http://slick.ninjacave.com/javadoc/org/newdawn/slick/Image.html#FILTER_LINEAR">FILTER_LINEAR</a>
* if bilinear filtering is used.
* @param filterMode An ImageFilter enum.
*
* @see org.newdawn.slick.Image#setFilter(int)
* @see io.github.tomaso2468.rpgonline.TextureMap
*/
public static void setFilterMode(int filterMode) {
public static void setFilterMode(ImageFilter filterMode) {
RPGConfig.filterMode = filterMode;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
import io.github.tomaso2468.rpgonline.Font;
import io.github.tomaso2468.rpgonline.Game;
import io.github.tomaso2468.rpgonline.Image;
import io.github.tomaso2468.rpgonline.ImageFilter;
import io.github.tomaso2468.rpgonline.TextureReference;
import io.github.tomaso2468.rpgonline.input.Input;
import io.github.tomaso2468.rpgonline.post.LUT;
Expand Down Expand Up @@ -221,7 +222,7 @@ public default Font loadFont(URL url, int type, float size) throws RenderExcepti

public Input getInput();

public void setFilter(TextureReference texture, int filterMode);
public void setFilter(TextureReference texture, ImageFilter filterMode);
public void writeImage(Image image, OutputStream out, boolean writeAlpha) throws IOException, RenderException;

public Shader createShader(URL vertex, URL fragment) throws RenderException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE

import io.github.tomaso2468.rpgonline.Font;
import io.github.tomaso2468.rpgonline.Image;
import io.github.tomaso2468.rpgonline.ImageFilter;
import io.github.tomaso2468.rpgonline.TextureReference;
import io.github.tomaso2468.rpgonline.render.BasicGraphics;
import io.github.tomaso2468.rpgonline.render.ColorMode;
Expand Down Expand Up @@ -375,7 +376,7 @@ public void copyArea(Image buffer, int x, int y) {
@Override
public TextureReference createEmptyTexture(int width, int height) throws TextureCreateException {
try {
return new SlickTexture(InternalTextureLoader.get().createTexture(width, height, Image.FILTER_NEAREST));
return new SlickTexture(InternalTextureLoader.get().createTexture(width, height, ImageFilter.NEAREST.slickMapping));
} catch (IOException e) {
throw new TextureCreateException("Failed to create empty image", e);
}
Expand Down Expand Up @@ -530,8 +531,8 @@ public Font loadFont(URL url, int type, float size, int[] codepages) throws Rend
}

@Override
public void setFilter(TextureReference texture, int filterMode) {
int filter = filterMode == Image.FILTER_LINEAR ? GL11.GL_LINEAR : GL11.GL_NEAREST;
public void setFilter(TextureReference texture, ImageFilter filterMode) {
int filter = filterMode.glMapping;
((SlickTexture) texture).texture.bind();
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, filter);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, filter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ protected void bindBuffer(Pbuffer pbuffer, Image image) throws RenderException {
Texture tex;
try {
tex = InternalTextureLoader.get().createTexture((int) image.getWidth(), (int) image.getHeight(),
image.getFilter());
image.getFilter().slickMapping);
} catch (IOException e) {
throw new RenderResourceException("An error occured creating a texture.", e);
}
Expand Down Expand Up @@ -328,7 +328,7 @@ public void setRenderTarget(Image image) throws RenderException {
Texture tex;
try {
tex = InternalTextureLoader.get().createTexture((int) image.getWidth(), (int) image.getHeight(),
image.getFilter());
image.getFilter().slickMapping);
} catch (IOException e) {
throw new RenderResourceException("An error occured creating a texture.", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,12 @@ public void setRenderTarget(Image image) throws RenderException {

Texture tex;
try {
tex = hdr ? createHDR((int) image.getWidth(), (int) image.getHeight()) : InternalTextureLoader.get().createTexture((int) image.getWidth(), (int) image.getHeight(), image.getFilter());
tex = hdr ? createHDR((int) image.getWidth(), (int) image.getHeight()) : InternalTextureLoader.get().createTexture((int) image.getWidth(), (int) image.getHeight(), image.getFilter().slickMapping);
} catch (IOException e) {
throw new RenderResourceException("Failed to create texture.");
}

int filter = image.getFilter() == Image.FILTER_LINEAR ? GL11.GL_LINEAR : GL11.GL_NEAREST;
int filter = image.getFilter().glMapping;
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, filter);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, filter);

Expand Down

0 comments on commit da97332

Please sign in to comment.