Support 4 npot tx, inlined some btlncks

v5stable
Ondřej Hruška 11 years ago
parent 0619f83bfb
commit e773a5307a
  1. BIN
      res/img/kitten_npot.png
  2. 13
      src/mightypork/gamecore/loading/DeferredResource.java
  3. 99
      src/mightypork/gamecore/render/Render.java
  4. 62
      src/mightypork/gamecore/render/fonts/impl/CachedFont.java
  5. 94
      src/mightypork/gamecore/render/textures/DeferredTexture.java
  6. 57
      src/mightypork/gamecore/render/textures/GLTexture.java
  7. 5
      src/mightypork/rogue/Res.java
  8. 2
      src/mightypork/rogue/screens/test_cat_sound/LayerFlyingCat.java
  9. 22
      src/mightypork/util/constraints/rect/caching/RectDigest.java

Binary file not shown.

After

Width:  |  Height:  |  Size: 84 KiB

@ -42,7 +42,8 @@ public abstract class DeferredResource implements Deferred, Destroyable {
loadFailed = false;
if (isNull()) return;
if (this instanceof NullResource) return; // don't even try
try {
if (resource == null) {
throw new NullPointerException("Resource string cannot be null for non-null resource.");
@ -61,8 +62,6 @@ public abstract class DeferredResource implements Deferred, Destroyable {
@Override
public synchronized final boolean isLoaded()
{
if (isNull()) return false;
return loadAttempted && !loadFailed;
}
@ -74,8 +73,6 @@ public abstract class DeferredResource implements Deferred, Destroyable {
*/
public synchronized final boolean ensureLoaded()
{
if (isNull()) return false;
if (isLoaded()) {
return true;
} else {
@ -132,10 +129,4 @@ public abstract class DeferredResource implements Deferred, Destroyable {
} else if (!resource.equals(other.resource)) return false;
return true;
}
private boolean isNull()
{
return this instanceof NullResource;
}
}

@ -18,7 +18,6 @@ import mightypork.util.math.color.Color;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureImpl;
import org.newdawn.slick.opengl.TextureLoader;
import org.newdawn.slick.util.ResourceLoader;
@ -299,7 +298,7 @@ public class Render {
* @param filtering filtering mode to use while loading.
* @return the loaded texture
*/
public synchronized static Texture loadTexture(String resourcePath, FilterMode filtering)
public synchronized static Texture loadSlickTexture(String resourcePath, FilterMode filtering)
{
try {
@ -322,29 +321,6 @@ public class Render {
}
/**
* Bind texture
*
* @param texture the texture
* @throws RuntimeException if not loaded yet
*/
private static void bindTexture(GLTexture texture) throws RuntimeException
{
texture.bind();
}
/**
* Unbind all
*/
private static void unbindTexture()
{
if (TextureImpl.getLastBind() != null) {
TextureImpl.bindNone();
}
}
/**
* Render quad 2D
*
@ -368,7 +344,7 @@ public class Render {
final RectDigest q = quad.digest();
// draw with color
glDisable(GL_TEXTURE_2D);
// quad
@ -381,47 +357,6 @@ public class Render {
}
/**
* Render textured rect (texture must be binded already)
*
* @param quad rectangle (px)
* @param uvs texture coords (0-1)
*/
public static void quadUV(Rect quad, Rect uvs)
{
glBegin(GL_QUADS);
quadUV_nobound(quad, uvs);
glEnd();
}
/**
* Draw quad without glBegin and glEnd.
*
* @param quad rectangle (px)
* @param uvs texture coords (0-1)
*/
public static void quadUV_nobound(Rect quad, Rect uvs)
{
final RectDigest q = quad.digest();
final RectDigest u = uvs.digest();
// quad with texture
glTexCoord2d(u.left, u.bottom);
glVertex2d(q.left, q.bottom);
glTexCoord2d(u.right, u.bottom);
glVertex2d(q.right, q.bottom);
glTexCoord2d(u.right, u.top);
glVertex2d(q.right, q.top);
glTexCoord2d(u.left, u.top);
glVertex2d(q.left, q.top);
}
/**
* Draw quad with horizontal gradient
*
@ -455,7 +390,7 @@ public class Render {
final RectDigest r = quad.digest();
// draw with color
glDisable(GL_TEXTURE_2D);
glBegin(GL_QUADS);
@ -498,10 +433,32 @@ public class Render {
public static void quadTextured(Rect quad, Rect uvs, GLTexture texture, Color tint)
{
glEnable(GL_TEXTURE_2D);
bindTexture(texture);
texture.bind();
setColor(tint);
quadUV(quad, uvs);
unbindTexture();
glBegin(GL_QUADS);
final RectDigest q = quad.digest();
final RectDigest u = uvs.digest();
final double w = texture.getWidth01();
final double h = texture.getHeight01();
// quad with texture
glTexCoord2d(u.x * w, u.bottom * h);
glVertex2d(q.left, q.bottom);
glTexCoord2d(u.right * w, u.bottom * h);
glVertex2d(q.right, q.bottom);
glTexCoord2d(u.right * w, u.top * h);
glVertex2d(q.right, q.top);
glTexCoord2d(u.left * w, u.top * h);
glVertex2d(q.left, q.top);
glEnd();
}

@ -334,30 +334,6 @@ public class CachedFont implements GLFont {
}
private void drawQuad(float xmin, float ymin, float xmax, float ymax, float txmin, float tymin, float txmax, float tymax)
{
final float draw_width = xmax - xmin;
final float draw_height = ymax - ymin;
final float txmin01 = txmin / textureWidth;
final float tymin01 = tymin / textureHeight;
final float twidth01 = ((txmax - txmin) / textureWidth);
final float theight01 = ((tymax - tymin) / textureHeight);
glTexCoord2f(txmin01, tymin01);
glVertex2f(xmin, ymin);
glTexCoord2f(txmin01, tymin01 + theight01);
glVertex2f(xmin, ymin + draw_height);
glTexCoord2f(txmin01 + twidth01, tymin01 + theight01);
glVertex2f(xmin + draw_width, ymin + draw_height);
glTexCoord2f(txmin01 + twidth01, tymin01);
glVertex2f(xmin + draw_width, ymin);
}
/**
* Get size needed to draw given string
*
@ -398,31 +374,57 @@ public class CachedFont implements GLFont {
{
GLUtils.checkGLContext();
// PUSH
glPushAttrib(GL_ENABLE_BIT);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, textureID);
glColor4d(color.red(), color.green(), color.blue(), color.alpha());
glBegin(GL_QUADS);
CharTile chtx = null;
char charCurrent;
int minx = 0;
glBegin(GL_QUADS);
int totalwidth = 0;
for (int i = 0; i < text.length(); i++) {
charCurrent = text.charAt(i);
chtx = chars.get(charCurrent);
if (chtx != null) {
drawQuad((totalwidth), 0, (totalwidth + chtx.width), (chtx.height), chtx.texPosX, chtx.texPosY, chtx.texPosX + chtx.width, chtx.texPosY
+ chtx.height);
totalwidth += chtx.width;
// draw quad
float txmin = chtx.texPosX;
float tymin = chtx.texPosY;
final float draw_width = minx + chtx.width - minx;
final float draw_height = (float) (chtx.height) - (float) 0;
final float txmin01 = txmin / textureWidth;
final float tymin01 = tymin / textureHeight;
final float twidth01 = ((chtx.texPosX + chtx.width - txmin) / textureWidth);
final float theight01 = ((chtx.texPosY + chtx.height - tymin) / textureHeight);
glTexCoord2f(txmin01, tymin01);
glVertex2f(minx, 0);
glTexCoord2f(txmin01, tymin01 + theight01);
glVertex2f(minx, 0 + draw_height);
glTexCoord2f(txmin01 + twidth01, tymin01 + theight01);
glVertex2f(minx + draw_width, 0 + draw_height);
glTexCoord2f(txmin01 + twidth01, tymin01);
glVertex2f(minx + draw_width, 0);
minx += chtx.width;
}
}
glEnd();
// POP
glPopAttrib();
}

@ -15,13 +15,15 @@ import org.lwjgl.opengl.GL11;
*
* @author MightyPork
*/
@MustLoadInMainThread
@LogAlias(name = "Texture")
@MustLoadInMainThread
public class DeferredTexture extends DeferredResource implements GLTexture {
private org.newdawn.slick.opengl.Texture backingTexture;
private FilterMode filter = FilterMode.NEAREST;
private WrapMode wrap = WrapMode.CLAMP;
private boolean alpha;
private boolean alphal;
/**
@ -33,12 +35,6 @@ public class DeferredTexture extends DeferredResource implements GLTexture {
}
/**
* Get a quad from this texture of given position/size
*
* @param uvs quad rect
* @return the quad
*/
@Override
public TxQuad makeQuad(Rect uvs)
{
@ -49,7 +45,7 @@ public class DeferredTexture extends DeferredResource implements GLTexture {
@Override
protected synchronized void loadResource(String path)
{
backingTexture = Render.loadTexture(path, filter);
backingTexture = Render.loadSlickTexture(path, filter);
}
@ -58,30 +54,22 @@ public class DeferredTexture extends DeferredResource implements GLTexture {
{
if (!ensureLoaded()) return false;
return backingTexture.hasAlpha();
}
/**
* Bind without adjusting parameters
*/
@Override
public void bindRaw()
{
if (!ensureLoaded()) return;
if (!alphal) {
alphal = true;
alpha = backingTexture.hasAlpha();
}
backingTexture.bind();
return alpha;
}
/**
* Bind and adjust parameters (filter, wrap)
*/
@Override
public void bind()
{
if (!ensureLoaded()) return;
GL11.glEnable(GL11.GL_TEXTURE_2D);
GL11.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_TEXTURE_ENV_MODE, GL11.GL_MODULATE);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, wrap.num);
@ -90,7 +78,7 @@ public class DeferredTexture extends DeferredResource implements GLTexture {
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, filter.num);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, filter.num);
bindRaw();
GL11.glBindTexture(GL11.GL_TEXTURE_2D, getTextureID());
}
@ -113,16 +101,7 @@ public class DeferredTexture extends DeferredResource implements GLTexture {
@Override
public String getTextureRef()
{
if (!ensureLoaded()) return null;
return backingTexture.getTextureRef();
}
@Override
public float getHeight()
public float getHeight01()
{
if (!ensureLoaded()) return 0;
@ -131,7 +110,7 @@ public class DeferredTexture extends DeferredResource implements GLTexture {
@Override
public float getWidth()
public float getWidth01()
{
if (!ensureLoaded()) return 0;
@ -140,25 +119,7 @@ public class DeferredTexture extends DeferredResource implements GLTexture {
@Override
public int getTextureHeight()
{
if (!ensureLoaded()) return 0;
return backingTexture.getTextureHeight();
}
@Override
public int getTextureWidth()
{
if (!ensureLoaded()) return 0;
return backingTexture.getTextureWidth();
}
@Override
public void release()
public void destroy()
{
if (!isLoaded()) return;
@ -175,31 +136,6 @@ public class DeferredTexture extends DeferredResource implements GLTexture {
}
@Override
public byte[] getTextureData()
{
if (!ensureLoaded()) return null;
return backingTexture.getTextureData();
}
@Override
public void setTextureFilter(int textureFilter)
{
if (!ensureLoaded()) return;
backingTexture.setTextureFilter(textureFilter);
}
@Override
public void destroy()
{
release();
}
@Override
public void setFilter(FilterMode filterMin)
{

@ -2,8 +2,7 @@ package mightypork.gamecore.render.textures;
import mightypork.util.constraints.rect.Rect;
import org.newdawn.slick.opengl.Texture;
import mightypork.util.control.Destroyable;
/**
@ -11,7 +10,7 @@ import org.newdawn.slick.opengl.Texture;
*
* @author MightyPork
*/
public interface GLTexture extends Texture {
public interface GLTexture extends Destroyable {
/**
* Set filter for scaling
@ -36,12 +35,6 @@ public interface GLTexture extends Texture {
TxQuad makeQuad(Rect uvs);
/**
* Bind without adjusting parameters
*/
void bindRaw();
/**
* Get a grid for given number of tiles
*
@ -50,4 +43,50 @@ public interface GLTexture extends Texture {
* @return grid
*/
QuadGrid grid(int x, int y);
/**
* @return OpenGL texture ID
*/
int getTextureID();
/**
* Get the height of the texture, 0..1.<br>
*
* @return height 0..1
*/
float getHeight01();
/**
* Get the width of the texture, 0..1.<br>
*
* @return width 0..1
*/
float getWidth01();
/**
* @return source image width (corresponding to width01)
*/
int getImageWidth();
/**
* @return source image height (corresponding to height01)
*/
int getImageHeight();
/**
* Bind to GL context, applying the filters prescribed.
*/
void bind();
/**
* @return true if the image is RGBA
*/
boolean hasAlpha();
}

@ -11,8 +11,6 @@ import mightypork.gamecore.render.fonts.Glyphs;
import mightypork.gamecore.render.fonts.impl.DeferredFont;
import mightypork.gamecore.render.textures.*;
import org.newdawn.slick.opengl.Texture;
/**
* Static resource repository
@ -65,6 +63,7 @@ public final class Res {
GLTexture texture;
texture = textures.loadTexture("test.kitten", "/res/img/kitten.png", FilterMode.LINEAR, WrapMode.CLAMP);
texture = textures.loadTexture("test.kitten2", "/res/img/kitten_npot.png", FilterMode.LINEAR, WrapMode.CLAMP);
texture = textures.loadTexture("gui1", "/res/img/gui1.png", FilterMode.NEAREST, WrapMode.CLAMP);
final QuadGrid gui = texture.grid(4, 4);
@ -107,7 +106,7 @@ public final class Res {
}
public static Texture getTexture(String key)
public static GLTexture getTexture(String key)
{
return textures.getTexture(key);
}

@ -42,7 +42,7 @@ public class LayerFlyingCat extends ScreenLayer implements MouseButtonEvent.List
cat_position.setTo(getDisplay().getCenter());
cat_position.setDefaultDuration(3);
final ImagePainter cat = new ImagePainter(Res.getTxQuad("test.kitten"));
final ImagePainter cat = new ImagePainter(Res.getTxQuad("test.kitten2"));
cat.setRect(Rect.make(size).centerTo(cat_position));
cat.enableCaching(false);

@ -2,7 +2,6 @@ package mightypork.util.constraints.rect.caching;
import mightypork.util.constraints.rect.Rect;
import mightypork.util.constraints.rect.RectConst;
public class RectDigest {
@ -19,20 +18,17 @@ public class RectDigest {
public RectDigest(Rect rect)
{
final RectConst frozen = rect.freeze();
this.x = frozen.x().value();
this.y = frozen.y().value();
{
this.x = rect.origin().x();
this.y = rect.origin().y();
this.width = frozen.width().value();
this.height = frozen.height().value();
this.width = rect.size().x();
this.height = rect.size().y();
this.left = frozen.left().value();
this.right = frozen.right().value();
this.top = frozen.top().value();
this.bottom = frozen.bottom().value();
this.left = x;
this.right = x + width;
this.top = y;
this.bottom = y + height;
}

Loading…
Cancel
Save