Skip to content

Instantly share code, notes, and snippets.

@Geolykt
Created January 2, 2023 15:36
Show Gist options
  • Save Geolykt/844e007895b68108938a30d3e01da145 to your computer and use it in GitHub Desktop.
Save Geolykt/844e007895b68108938a30d3e01da145 to your computer and use it in GitHub Desktop.
Old half-broken Pixmap2PixmapBlitter
package de.geolykt.cityblitter.blitting;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import org.slf4j.LoggerFactory;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Pixmap.Format;
public class Pixmap2PixmapBlitter implements TextureBlitter {
// FIXME Pixmap2PixmapBlitter needs to be disposable in order to in turn free "reversed"!
// Ignoring that may produce dangerous memory leaks should the AsyncBlitter class be instantiated multiple times.
private final int srcX;
private final int srcY;
private final int srcW;
private final int srcH;
private final int dstW;
private final int dstH;
private final int dstX;
private final int dstY;
private final Pixmap source;
private final Pixmap reversed;
private boolean flag0 = false;
public Pixmap2PixmapBlitter(float srcX0, float srcY0, float srcX1, float srcY1, float w, float h, float x, float y, Pixmap source) {
if (srcX0 < 0 || srcY0 < 0) {
throw new AssertionError("srcX0 and srcY0 may not be negative.");
}
boolean switchX = false;
boolean switchY = true;;
if (w < 0) {
w = -w;
srcX0 -= w;
srcX1 -= w;
switchX = true;
flag0 = true;
}
if (h < 0) {
h = -h;
srcY0 -= h;
srcY1 -= h;
switchY = false;
flag0 = true;
}
this.srcX = (int) srcX0;
this.srcY = (int) srcY0;
this.srcW = (int) (srcX1 - srcX0);
this.srcH = (int) (srcY1 - srcY0);
this.dstW = (int) w;
this.dstH = (int) h;
this.dstX = (int) x;
this.dstY = (int) y;
this.source = source;
// The following is inefficient as hell, but without any OpenGL knowledge that is basically the only way I'd know how to solve that problem
this.reversed = new Pixmap(this.dstW, this.dstH, Format.RGBA8888);
this.reversed.drawPixmap(this.source, this.srcX, this.srcY, this.srcW, this.srcH, 0, 0, this.dstW, this.dstH);
ByteBuffer buffer = this.reversed.getPixels();
if (switchY) {
int byteWidth = this.dstW * 4;
byte[] swap0 = new byte[byteWidth];
byte[] swap1 = new byte[byteWidth];
for (int i = 0; i < this.dstH / 2; i++) {
int storeLoc0 = i * byteWidth;
int storeLoc1 = (this.dstH - i - 1) * byteWidth;
buffer.get(storeLoc0, swap0, 0, byteWidth);
buffer.get(storeLoc1, swap1, 0, byteWidth);
buffer.put(storeLoc1, swap0, 0, byteWidth);
buffer.put(storeLoc0, swap1, 0, byteWidth);
}
}
if (switchX) {
int[] swap0 = new int[this.dstW];
int[] swap1 = new int[this.dstW];
IntBuffer intBuffer = buffer.asIntBuffer();
for (int i = 0; i < this.dstH; i++) {
intBuffer.get(i * this.dstW, swap0, 0, this.dstW);
for (int j = 0; j < this.dstW; j++) {
swap1[this.dstW - j - 1] = swap0[j];
}
intBuffer.put(i * this.dstW, swap1, 0, this.dstW);
}
}
}
@Override
public void blit(Pixmap to, int x, int y) {
// FIXME Sometimes doesn't draw water. Yeah, modern problems - but those are severe as that happens rather often (12 - 25% of the time I'd guess).
// FIXME Grass is not being elevated by 4 px as it should've been.
if (flag0) {
LoggerFactory.getLogger(getClass()).warn("Drawing a Pixmap that was flipped. This process might not have been fully efficent.");
flag0 = false;
}
// to.drawPixmap(this.source, this.srcX, this.srcY, this.srcW, this.srcH, x + this.dstX, y + this.dstY, this.dstW, this.dstH);
to.drawPixmap(this.reversed, 0, 0, this.dstW, this.dstH, x + this.dstX, y + this.dstY, this.dstW, this.dstH);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment