0
\$\begingroup\$

I'm working on a libGDX implementation for my library which draws ASCII tiles to create a text GUI. My current Swing + BufferedImage implementation looks like this:

enter image description here

This works by copying parts of a grayscale texture which has CP437 characters on it and transforming them by adding color to each pixel.

My problem is that my libGDX implementation does not work for some reason:

enter image description here

What I have tried is this:

    val backend: TextureRegion = region.getBackend()

    val texture = backend.texture
    if (!texture.textureData.isPrepared) {
        texture.textureData.prepare()
    }
    val pixmap = texture.textureData.consumePixmap()
    (0 until backend.regionWidth).forEach { x ->
        (0 until backend.regionHeight).forEach { y ->
            val color = Color(pixmap.getPixel(backend.regionX + x, backend.regionY + y))

            val ax = color.a
            var rx = color.r
            var gx = color.g
            var bx = color.b
            rx *= r
            gx *= g
            bx *= b
            if (ax < 50) {
                pixmap.drawPixel(backend.regionX + x, backend.regionY + y, textCharacter.getBackgroundColor().toAWTColor().rgb)
            } else {
                pixmap.drawPixel(backend.regionX + x, backend.regionY + y, Color(rx, gx, bx, ax).toIntBits())
            }
        }
    }

    return region

I know that I am missing something but I just can't figure out what the problem is here. I've tried with various options like copying the TextureRegion but I can't get this to work.

My other implementation uses BufferedImages and it just works:

    val r = textCharacter.getForegroundColor().getRed().toFloat() / 255
    val g = textCharacter.getForegroundColor().getGreen().toFloat() / 255
    val b = textCharacter.getForegroundColor().getBlue().toFloat() / 255

    val backend: BufferedImage = region.getBackend()
    (0 until backend.width).forEach { x ->
        (0 until backend.height).forEach { y ->
            val ax = backend.colorModel.getAlpha(backend.raster.getDataElements(x, y, null))
            var rx = backend.colorModel.getRed(backend.raster.getDataElements(x, y, null))
            var gx = backend.colorModel.getGreen(backend.raster.getDataElements(x, y, null))
            var bx = backend.colorModel.getBlue(backend.raster.getDataElements(x, y, null))
            rx = (rx * r).toInt()
            gx = (gx * g).toInt()
            bx = (bx * b).toInt()
            if (ax < 50) {
                backend.setRGB(x, y, textCharacter.getBackgroundColor().toAWTColor().rgb)
            } else {
                backend.setRGB(x, y, (ax shl 24) or (rx shl 16) or (gx shl 8) or (bx shl 0))
            }
        }
    }
    return region

What am I doing wrong?

\$\endgroup\$
1
\$\begingroup\$

Color.toIntBits returns ABGR format. Pixmap.drawPixel takes rgba8888

https://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/Pixmap.html#drawPixel-int-int-int- https://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/Color.html#toIntBits--

Use Color.rgba8888(color) instead.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.