Jump to content

[1.18.1] Dynamic texture generation and loading


Jacob Sapoznikow

Recommended Posts

Can someone please help me? I'm trying to create a dynamic texture (Via BufferedImage/Graphics2D) and it keeps crashing with "EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x00007ffc41cb2c70, pid=36820, tid=38756". Can someone help? I'll upload the file here.

Code:

Spoiler
package [REDACTED].spacecraft.overlay;

import com.mojang.blaze3d.platform.NativeImage;
import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Gui;
import net.minecraft.client.renderer.GameRenderer;
import net.minecraft.client.renderer.texture.DynamicTexture;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.client.event.RenderGameOverlayEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import [REDACTED].spacecraft.SpaceCraft;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.awt.image.WritableRaster;
import java.io.IOException;
import java.nio.ByteBuffer;

import static com.mojang.blaze3d.platform.GlStateManager.SourceFactor.SRC_ALPHA;
import static com.mojang.blaze3d.platform.GlStateManager.SourceFactor.ONE;
import static com.mojang.blaze3d.platform.GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA;
import static com.mojang.blaze3d.platform.GlStateManager.DestFactor.ZERO;

@Mod.EventBusSubscriber(modid = SpaceCraft.MODID, bus = Mod.EventBusSubscriber.Bus.FORGE, value = Dist.CLIENT)
public class OxygenOverlay {
    public static DynamicTexture generateImage(int level) throws IOException {
        int width = 16, height = 46, offset = 1, value = (int) (46 * (level / 100.0f));

        BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
        Graphics2D g2d = img.createGraphics();

        g2d.setColor(new Color(48, 48, 48, 255));
        g2d.fillRect(0, 0, width, height);

        g2d.setColor(new Color(58, 211, 255, 192));
        g2d.fillRect(offset, offset + value, width - (offset * 2), (height - (offset * 2)) - value);

        g2d.setColor(new Color(70, 211, 255, 30));
        g2d.fillRect(offset, offset, width - (offset * 2), value);

        g2d.dispose();

        WritableRaster raster = img.getRaster();
        DataBufferByte data = (DataBufferByte) raster.getDataBuffer();
        ByteBuffer bytes = ByteBuffer.wrap(data.getData());

        NativeImage nativeImage = NativeImage.read(NativeImage.Format.RGBA, bytes);
        DynamicTexture texture = new DynamicTexture(nativeImage);

        return texture;
    }

    @SubscribeEvent
    public static void eventHandler(RenderGameOverlayEvent.Pre event) {
        if (event.getType() == RenderGameOverlayEvent.ElementType.ALL) {
            Player entity = Minecraft.getInstance().player;
            Gui gui = Minecraft.getInstance().gui;

            if(entity == null) return;

            Level world = entity.level;
            int posX = 10, posY = 10;
            double x = entity.getX(), y = entity.getY(), z = entity.getZ();

            RenderSystem.disableDepthTest();
            RenderSystem.depthMask(false);
            RenderSystem.enableBlend();
            RenderSystem.setShader(GameRenderer::getPositionTexShader);
            RenderSystem.blendFuncSeparate(SRC_ALPHA, ONE_MINUS_SRC_ALPHA, ONE, ZERO);
            RenderSystem.setShaderColor(1, 1, 1, 1);

            try {
                Minecraft.getInstance().getTextureManager().register("spacecraft:oxygen_overlay", generateImage(0));
            } catch(IOException e) {
                e.printStackTrace();
            }

            ResourceLocation texture = new ResourceLocation(SpaceCraft.MODID, "oxygen_overlay.png");

            RenderSystem.setShaderTexture(0, texture);
            gui.blit(event.getMatrixStack(), posX, posY, 0, 0, 16, 46);

            RenderSystem.depthMask(true);
            RenderSystem.defaultBlendFunc();
            RenderSystem.enableDepthTest();
            RenderSystem.disableBlend();
            RenderSystem.setShaderColor(1, 1, 1, 1);
        }
    }

}

 

 

Edited by Jacob Sapoznikow
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.