Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

How to render a BufferedImage in a GUI 1.16.5


Marelo
 Share

Recommended Posts

Hello, I'm new at modding and recently I was trying to put a image, which is outside .jar, into a gui. But the far I got is load this image in a byte[] the transforme this in a BufferedImage. I search some foruns and thigs about how I could solve my problem, a part of them seems to transform the ImageBuffer into a intByte them use GL11 to render the image, but i cant make the image appear in my GUI. The other part seems to use DynamicTexture, but the questions is outdate (like 2014 or 2015) and the code seems to cheged.

I think the most promising way to solve my problem is through GL11, but I am open to new ways and possibilties. I even tried to render this image with the JNA using GDI32, but ( because I'm a completely noob ) It doesn't work.

The reason why I want a BufferedImage is because the image will uptade according to another API thata generates random images each time. This is why I can't put the image at resources.

If anyone want any code for the gui.

Thanks if you readed this far.

 

Spoiler

import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.api.distmarker.Dist;

import net.minecraft.world.World;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.ResourceLocation;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.client.gui.screen.inventory.ContainerScreen;
import net.minecraft.client.renderer.texture.DynamicTexture;
import net.minecraft.client.Minecraft;


import com.mojang.blaze3d.systems.RenderSystem;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Map;

import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL12;

import static org.lwjgl.opengl.GL11.*;

import com.google.common.collect.Maps;
import com.mojang.blaze3d.matrix.MatrixStack;
import com.mojang.blaze3d.platform.GlStateManager;

@OnlyIn(Dist.CLIENT)
public class MyWindowGUI extends ContainerScreen<MyWindowGUIContainer.GuiContainerMod> {
    
    private World world;
    private int x, y, z;
    private PlayerEntity entity;
    public MyWindowGUI(MyWindowGUIContainer.GuiContainerMod container, PlayerInventory inventory, ITextComponent text) {
        super(container, inventory, text);
        this.world = container.world;
        this.x = container.x;
        this.y = container.y;
        this.z = container.z;
        this.entity = container.entity;
        this.xSize = 500;
        this.ySize = 400;
    }
    private static final ResourceLocation texture = new ResourceLocation("myMod:textures/textuteTest.png");

    
    @Override
    public void render(MatrixStack ms, int mouseX, int mouseY, float partialTicks) {
        this.renderBackground(ms);
        super.render(ms, mouseX, mouseY, partialTicks);
        this.renderHoveredTooltip(ms, mouseX, mouseY);
    

    }

    @Override
    protected void drawGuiContainerBackgroundLayer(MatrixStack ms, float partialTicks, int gx, int gy) {
        RenderSystem.color4f(1, 1, 1, 1);
        RenderSystem.enableBlend();
        RenderSystem.defaultBlendFunc();
        Minecraft.getInstance().getTextureManager().bindTexture(texture);
        int k = (this.width - this.xSize) / 2;
        int l = (this.height - this.ySize) / 2;
        this.blit(ms, k, l, 0, 0, this.xSize, this.ySize, this.xSize, this.ySize);
        try{
            //Minecraft.getInstance().getTextureManager().bindTexture(new ResourceLocation("modId:textures/screen1.jpeg"));
            }catch(Exception e){  }
        this.blit(ms, this.guiLeft + 42, this.guiTop + 84, 0, 0, 96, 128, 96, 128);
        RenderSystem.disableBlend();
    }

    @Override
    public boolean keyPressed(int key, int b, int c) {
        if (key == 256) {
            this.minecraft.player.closeScreen();
            return true;
        }
        return super.keyPressed(key, b, c);
    }

    @Override
    public void tick() {
        super.tick();
    }

    @Override
    protected void drawGuiContainerForegroundLayer(MatrixStack ms, int mouseX, int mouseY) {
        
        //my previos attempts
        
//        glPushMatrix();
//        
//        BufferedImage image = new BufferedImage(128, 128, BufferedImage.TYPE_INT_ARGB);
//        Graphics2D g2d = image.createGraphics();
//
//        g2d.setColor(new Color(1.0f, 1.0f, 1.0f, 0.5f));
//        g2d.fillRect(0, 0, 128, 128); //A transparent white background
//                
//        g2d.setColor(Color.red);
//        g2d.drawRect(0, 0, 127, 127); //A red frame around the image
//        g2d.fillRect(10, 10, 10, 10); //A red box 
//                
//        g2d.setColor(Color.blue);
//        g2d.drawString("Test image", 10, 64); //Some blue text
//        g2d.dispose();
//        
//        int[] pixels = new int[image.getWidth() * image.getHeight()];
//        image.getRGB(0, 0, image.getWidth(), image.getHeight(), pixels, 0, image.getWidth());
//
//        ByteBuffer buffer = BufferUtils.createByteBuffer(image.getWidth() * image.getHeight() * 4); //4 for RGBA, 3 for RGB
//        
//        for(int y = 0; y < image.getHeight() ; y++){
//            for(int x = 0; x < image.getWidth(); x++){
//                int pixel = pixels[y * image.getWidth() + x];
//                buffer.put((byte) ((pixel >> 16) & 0xFF));     // Red component
//                buffer.put((byte) ((pixel >> 8) & 0xFF));      // Green component
//                buffer.put((byte) (pixel & 0xFF));                // Blue component
//                buffer.put((byte) ((pixel >> 24) & 0xFF));    // Alpha component. Only for RGBA
//            }
//        }
//
//        buffer.flip(); //FOR THE LOVE OF GOD DO NOT FORGET THIS
//
//
//       int textureID = glGenTextures(); //Generate texture ID
//       
//       GlStateManager.bindBuffer(mouseX, textureID);
//        glBindTexture(GL_TEXTURE_2D, textureID); //Bind texture ID
//        
//        //Setup wrap mode
//        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
//        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);
//
//        //Setup texture scaling filtering
//        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
//        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
//        
//        //Send texel data to OpenGL
//        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, image.getWidth(), image.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
//
//        glBindTexture(GL_TEXTURE_2D, textureID);
//        glEnd();
        //images.put("minhaTextura", new DynamicTexture(GuiIsOpenProcedure.buffImage));
        
        //RenderSystem.enableBlend();
        //RenderSystem.defaultBlendFunc();
        //int k = (this.width - this.xSize) / 2;
        
        //int l = (this.height - this.ySize) / 2;
        //this.blit(ms, k, l, 0, 0, this.xSize, this.ySize, this.xSize, this.ySize);
        
        
        
        //Minecraft.getInstance().getTextureManager().get
        //System.out.println(
        //Minecraft.getInstance().getTextureManager().getDynamicTextureLocation("texture2", GuiIsOpenProcedure.buffImage)
        //);
    }

    @Override
    public void onClose() {
        super.onClose();
        GuiIsOpenProcedure.guiIsOpen = false;
        Minecraft.getInstance().keyboardListener.enableRepeatEvents(false);
    }

    @Override
    public void init(Minecraft minecraft, int width, int height) {
        super.init(minecraft, width, height);
        minecraft.keyboardListener.enableRepeatEvents(true);
    }
}
 

 

Edited by Marelo
Link to comment
Share on other sites

Posted (edited)

I read in stackOverflow that i can't save a image inside a .jar. But i don't know if is possible. Because i generate the image outside the .jar. What I tring to do looks like rendering the icon image of the world inside the game, it is just an example, isn't the case

I'm modding at eclipse, if I understand the question.

Edited by Marelo
Link to comment
Share on other sites

8 minutes ago, Marelo said:

I read in stackOverflow that i can't save a image inside a .jar. But i don't know if is possible. Because i generate the image outside the .jar. What I tring to do looks like rendering the icon image of the world inside the game, it is just an example, isn't the case

why do you want to do this?

8 minutes ago, Marelo said:

I'm modding at eclipse, if I understand the question.

using the mdk?

Link to comment
Share on other sites

Posted (edited)
2 minutes ago, Luis_ST said:

why do you want to do this?

using the mdk?

yes

2 minutes ago, Luis_ST said:

why do you want to do this?

Because my API only generates this outside the .jar

Edited by Marelo
Link to comment
Share on other sites

Posted (edited)
4 minutes ago, Luis_ST said:

why would you want to save a picture in a .jar?

ResourceLocation only load images inside the mod .jar, i thought it could be a way to solve my problem

Edited by Marelo
Link to comment
Share on other sites

  • 4 weeks later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

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

 Share



×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.