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.

[1.18.1] Trying to draw a small image on the screen

Featured Replies

Posted

I am trying to draw a simple icon on the screen. For starters, I thought I'd just try and draw part of an existing 256x256 texture so that I have the basic concepts working.

I have the following code:

public static void RenderGameOverlayEvent(RenderGameOverlayEvent.Post event) {
    Minecraft minecraft = Minecraft.getInstance();
    TextureManager tm = minecraft.getTextureManager();
    ResourceLocation rl = new ResourceLocation("minecraft", "textures/gui/recipe_book.png");
    RenderSystem.setShader(GameRenderer::getPositionTexShader);
    tm.bindForSetup(rl); // tried with and without this
    RenderSystem.enableTexture(); // tried with and without this
    RenderSystem.bindTexture(tm.getTexture(rl).getId()); // tried with and without this
    RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0f);
    GuiUtils.drawTexturedModalRect(event.getMatrixStack(), 0, 0, 0, 0, 16, 16, 0);
}

And this is what gets rendered in-game (garbage):

Image2.jpg

I have found that, even if I omit the calls for binding texture entirely, it makes no difference to the image. So I assume that I am not setting the texture correctly (my calls are effectively noops).
I have searched the code, forum and google for examples of how to do this drawing correctly but as far as I can tell I'm doing the same as others for whom it is working.

Can anyone see what I'm missing / doing wrong? Many thanks.

  • Author

I did see that as an alternative approach, and I tried to understand how OverlayRegistry works but couldn't see any usage examples. I found classes that call register but could not find any places where the registered items were subsequently used. Could you point me at an example or docs on how to use OverlayRegistry?

Also, is there a reason why my approach above doesn't work (apparently it works for other people) ?

  • Author

tysm I have it working now. Doing it via OverlayRegistry as follows works:

MyHudOverlay = OverlayRegistry.registerOverlayTop("MyHudOverlay", (gui, mStack, partialTicks, screenWidth, screenHeight) -> {
    Minecraft minecraft = Minecraft.getInstance();
    if (minecraft.gameMode == null) return;
    if (minecraft.level == null) return;
    TextureManager tm = minecraft.getTextureManager();
    ResourceLocation rl = new ResourceLocation("minecraft", "textures/gui/recipe_book.png");
    gui.setupOverlayRenderState(true, false, rl);
    RenderSystem.setShader(GameRenderer::getPositionTexShader);
    tm.bindForSetup(rl);
    RenderSystem.enableTexture();
    RenderSystem.bindTexture(tm.getTexture(rl).getId());
    RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0f);
    GuiUtils.drawTexturedModalRect(new PoseStack(), 0, 0, 0, 0, 16, 16, 0);
});

I also found that the key thing my original code was missing was

RenderSystem.setShaderTexture(0, res);

(which is called inside setupOverlayRenderState)

If I use that with my original code instead of using OverlayRegistry, I also get the desired result.
Is there a reason/benefit to using OverlayRegistry for this task rather than RenderGameOverlayEvent ?

Edited by Marshall7

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.