Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

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

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.