Skip to content

Commit

Permalink
add necessary format conversion to GLESDrawContext and OriginalImageP…
Browse files Browse the repository at this point in the history
…rovider
  • Loading branch information
paulwedeck committed Jan 24, 2023
1 parent ee3d6c5 commit 89a41fc
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.io.InputStreamReader;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.util.ArrayList;

import go.graphics.AbstractColor;
Expand Down Expand Up @@ -117,17 +118,38 @@ public TextureHandle generateTexture(ImageData image, String name) {
return texture;
}

private ImageData fixFormat(ImageData orig) {
if(orig == null) return null;

ImageData conv = new ImageData(orig.getWidth(), orig.getHeight());
IntBuffer from = orig.getReadData32();
IntBuffer to = conv.getWriteData32();
while(from.hasRemaining()) {
int pixel = from.get();
int r = pixel&0xFF;
int g = (pixel>>8)&0xFF;
int b = (pixel>>16)&0xFF;
int a = (pixel>>24)&0xFF;

int reversed = r<<24 | g << 16 | b << 8 | a;
to.put(reversed);
}

to.rewind();
return conv;
}

public TextureHandle resizeTexture(TextureHandle textureIndex, ImageData image) {
bindTexture(textureIndex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image.getWidth(), image.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, image.getReadData32());
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image.getWidth(), image.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, fixFormat(image).getReadData32());
return textureIndex;
}

public void updateTexture(TextureHandle textureIndex, int left, int bottom,
ImageData image) {
bindTexture(textureIndex);
glTexSubImage2D(GL_TEXTURE_2D, 0, left, bottom, image.getWidth(), image.getHeight(),
GL_RGBA, GL_UNSIGNED_BYTE, image.getReadData32());
GL_RGBA, GL_UNSIGNED_BYTE, fixFormat(image).getReadData32());
}

protected void bindTexture(TextureHandle texture) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

package jsettlers.main.android.core.resources;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.IntBuffer;
import java.nio.ShortBuffer;
import java.util.ArrayList;
Expand Down Expand Up @@ -97,6 +99,18 @@ public ImageReference(ImageLink image) {
this.image = image;
}


private IntBuffer fixRGBA8Buffer(IntBuffer orig) {
IntBuffer bfr = ByteBuffer.allocateDirect(orig.remaining()*4).order(ByteOrder.nativeOrder()).asIntBuffer();
while(orig.hasRemaining()) {
int color = orig.get();
bfr.put(((color<<24)&0xFF000000) | ((color >> 8)&0xFFFFFF));
}
orig.rewind();
bfr.rewind();
return bfr;
}

public void load() {
Image loaded = ImageProvider.getInstance().getImage(image);
int[] colors = new int[1];
Expand All @@ -105,7 +119,7 @@ public void load() {

if (loaded instanceof SingleImage) {
ImageData data = ((SingleImage) loaded).getData();
IntBuffer bfr = data.getReadData32();
IntBuffer bfr = fixRGBA8Buffer(data.getReadData32());

width = data.getWidth();
height = data.getHeight();
Expand Down

0 comments on commit 89a41fc

Please sign in to comment.