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; loadFailed = false;
if (isNull()) return; if (this instanceof NullResource) return; // don't even try
try { try {
if (resource == null) { if (resource == null) {
throw new NullPointerException("Resource string cannot be null for non-null resource."); throw new NullPointerException("Resource string cannot be null for non-null resource.");
@ -61,8 +62,6 @@ public abstract class DeferredResource implements Deferred, Destroyable {
@Override @Override
public synchronized final boolean isLoaded() public synchronized final boolean isLoaded()
{ {
if (isNull()) return false;
return loadAttempted && !loadFailed; return loadAttempted && !loadFailed;
} }
@ -74,8 +73,6 @@ public abstract class DeferredResource implements Deferred, Destroyable {
*/ */
public synchronized final boolean ensureLoaded() public synchronized final boolean ensureLoaded()
{ {
if (isNull()) return false;
if (isLoaded()) { if (isLoaded()) {
return true; return true;
} else { } else {
@ -132,10 +129,4 @@ public abstract class DeferredResource implements Deferred, Destroyable {
} else if (!resource.equals(other.resource)) return false; } else if (!resource.equals(other.resource)) return false;
return true; 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.lwjgl.opengl.GL11;
import org.newdawn.slick.opengl.Texture; import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureImpl;
import org.newdawn.slick.opengl.TextureLoader; import org.newdawn.slick.opengl.TextureLoader;
import org.newdawn.slick.util.ResourceLoader; import org.newdawn.slick.util.ResourceLoader;
@ -299,7 +298,7 @@ public class Render {
* @param filtering filtering mode to use while loading. * @param filtering filtering mode to use while loading.
* @return the loaded texture * @return the loaded texture
*/ */
public synchronized static Texture loadTexture(String resourcePath, FilterMode filtering) public synchronized static Texture loadSlickTexture(String resourcePath, FilterMode filtering)
{ {
try { 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 * Render quad 2D
* *
@ -368,7 +344,7 @@ public class Render {
final RectDigest q = quad.digest(); final RectDigest q = quad.digest();
// draw with color // draw with color
glDisable(GL_TEXTURE_2D); glDisable(GL_TEXTURE_2D);
// quad // 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 * Draw quad with horizontal gradient
* *
@ -455,7 +390,7 @@ public class Render {
final RectDigest r = quad.digest(); final RectDigest r = quad.digest();
// draw with color // draw with color
glDisable(GL_TEXTURE_2D); glDisable(GL_TEXTURE_2D);
glBegin(GL_QUADS); glBegin(GL_QUADS);
@ -498,10 +433,32 @@ public class Render {
public static void quadTextured(Rect quad, Rect uvs, GLTexture texture, Color tint) public static void quadTextured(Rect quad, Rect uvs, GLTexture texture, Color tint)
{ {
glEnable(GL_TEXTURE_2D); glEnable(GL_TEXTURE_2D);
bindTexture(texture);
texture.bind();
setColor(tint); 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 * Get size needed to draw given string
* *
@ -398,31 +374,57 @@ public class CachedFont implements GLFont {
{ {
GLUtils.checkGLContext(); GLUtils.checkGLContext();
// PUSH
glPushAttrib(GL_ENABLE_BIT); glPushAttrib(GL_ENABLE_BIT);
glEnable(GL_TEXTURE_2D); glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, textureID); glBindTexture(GL_TEXTURE_2D, textureID);
glColor4d(color.red(), color.green(), color.blue(), color.alpha()); glColor4d(color.red(), color.green(), color.blue(), color.alpha());
glBegin(GL_QUADS); glBegin(GL_QUADS);
CharTile chtx = null; CharTile chtx = null;
char charCurrent; char charCurrent;
int minx = 0;
glBegin(GL_QUADS);
int totalwidth = 0;
for (int i = 0; i < text.length(); i++) { for (int i = 0; i < text.length(); i++) {
charCurrent = text.charAt(i); charCurrent = text.charAt(i);
chtx = chars.get(charCurrent); chtx = chars.get(charCurrent);
if (chtx != null) { if (chtx != null) {
drawQuad((totalwidth), 0, (totalwidth + chtx.width), (chtx.height), chtx.texPosX, chtx.texPosY, chtx.texPosX + chtx.width, chtx.texPosY
+ chtx.height); // draw quad
totalwidth += chtx.width;
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(); glEnd();
// POP
glPopAttrib(); glPopAttrib();
} }

@ -15,13 +15,15 @@ import org.lwjgl.opengl.GL11;
* *
* @author MightyPork * @author MightyPork
*/ */
@MustLoadInMainThread
@LogAlias(name = "Texture") @LogAlias(name = "Texture")
@MustLoadInMainThread
public class DeferredTexture extends DeferredResource implements GLTexture { public class DeferredTexture extends DeferredResource implements GLTexture {
private org.newdawn.slick.opengl.Texture backingTexture; private org.newdawn.slick.opengl.Texture backingTexture;
private FilterMode filter = FilterMode.NEAREST; private FilterMode filter = FilterMode.NEAREST;
private WrapMode wrap = WrapMode.CLAMP; 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 @Override
public TxQuad makeQuad(Rect uvs) public TxQuad makeQuad(Rect uvs)
{ {
@ -49,7 +45,7 @@ public class DeferredTexture extends DeferredResource implements GLTexture {
@Override @Override
protected synchronized void loadResource(String path) 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; if (!ensureLoaded()) return false;
return backingTexture.hasAlpha(); if (!alphal) {
} alphal = true;
alpha = backingTexture.hasAlpha();
}
/**
* Bind without adjusting parameters
*/
@Override
public void bindRaw()
{
if (!ensureLoaded()) return;
backingTexture.bind(); return alpha;
} }
/**
* Bind and adjust parameters (filter, wrap)
*/
@Override @Override
public void bind() public void bind()
{ {
if (!ensureLoaded()) return; if (!ensureLoaded()) return;
GL11.glEnable(GL11.GL_TEXTURE_2D);
GL11.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_TEXTURE_ENV_MODE, GL11.GL_MODULATE); 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); 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_MIN_FILTER, filter.num);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_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 @Override
public String getTextureRef() public float getHeight01()
{
if (!ensureLoaded()) return null;
return backingTexture.getTextureRef();
}
@Override
public float getHeight()
{ {
if (!ensureLoaded()) return 0; if (!ensureLoaded()) return 0;
@ -131,7 +110,7 @@ public class DeferredTexture extends DeferredResource implements GLTexture {
@Override @Override
public float getWidth() public float getWidth01()
{ {
if (!ensureLoaded()) return 0; if (!ensureLoaded()) return 0;
@ -140,25 +119,7 @@ public class DeferredTexture extends DeferredResource implements GLTexture {
@Override @Override
public int getTextureHeight() public void destroy()
{
if (!ensureLoaded()) return 0;
return backingTexture.getTextureHeight();
}
@Override
public int getTextureWidth()
{
if (!ensureLoaded()) return 0;
return backingTexture.getTextureWidth();
}
@Override
public void release()
{ {
if (!isLoaded()) return; 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 @Override
public void setFilter(FilterMode filterMin) public void setFilter(FilterMode filterMin)
{ {

@ -2,8 +2,7 @@ package mightypork.gamecore.render.textures;
import mightypork.util.constraints.rect.Rect; import mightypork.util.constraints.rect.Rect;
import mightypork.util.control.Destroyable;
import org.newdawn.slick.opengl.Texture;
/** /**
@ -11,7 +10,7 @@ import org.newdawn.slick.opengl.Texture;
* *
* @author MightyPork * @author MightyPork
*/ */
public interface GLTexture extends Texture { public interface GLTexture extends Destroyable {
/** /**
* Set filter for scaling * Set filter for scaling
@ -36,12 +35,6 @@ public interface GLTexture extends Texture {
TxQuad makeQuad(Rect uvs); TxQuad makeQuad(Rect uvs);
/**
* Bind without adjusting parameters
*/
void bindRaw();
/** /**
* Get a grid for given number of tiles * Get a grid for given number of tiles
* *
@ -50,4 +43,50 @@ public interface GLTexture extends Texture {
* @return grid * @return grid
*/ */
QuadGrid grid(int x, int y); 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.fonts.impl.DeferredFont;
import mightypork.gamecore.render.textures.*; import mightypork.gamecore.render.textures.*;
import org.newdawn.slick.opengl.Texture;
/** /**
* Static resource repository * Static resource repository
@ -65,6 +63,7 @@ public final class Res {
GLTexture texture; GLTexture texture;
texture = textures.loadTexture("test.kitten", "/res/img/kitten.png", FilterMode.LINEAR, WrapMode.CLAMP); 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); texture = textures.loadTexture("gui1", "/res/img/gui1.png", FilterMode.NEAREST, WrapMode.CLAMP);
final QuadGrid gui = texture.grid(4, 4); 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); return textures.getTexture(key);
} }

@ -42,7 +42,7 @@ public class LayerFlyingCat extends ScreenLayer implements MouseButtonEvent.List
cat_position.setTo(getDisplay().getCenter()); cat_position.setTo(getDisplay().getCenter());
cat_position.setDefaultDuration(3); 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.setRect(Rect.make(size).centerTo(cat_position));
cat.enableCaching(false); cat.enableCaching(false);

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

Loading…
Cancel
Save