Jump to content

Displaying text only for a couple of seconds


Bets

Recommended Posts

9 minutes ago, Lhykos said:

Have you already tried something?

 

Basically it is:

You simply set a timer variable which will decrements in each client tick. And if the timer is expired you will not render the text anymore.

 

I can try that, but what if I need to display more than one text at ones (the text must be dynamic too)

Or lets say I want the text to fade out

Link to comment
Share on other sites

You'll have to learn OpenGL.

What you seem to want, is called a HUD. (Heads Up Display).

I have a very old example here. It is from the 1.7 days, but it should be mostly correct still. ScaledResolution only needs the mc instance now, the width & height is done for you.

Do note, this involves Minecraft#getMinecraft() which is client-side only. As such, it must only be called client-side, as it will crash otherwise.

(The example I linked, is meant to print the given string in the middle of the screen, fully centered)

 

As Lhykos has states, you will need a timer. How you create that, is up to you.

Edited by Matryoshika

Also previously known as eAndPi.

"Pi, is there a station coming up where we can board your train of thought?" -Kronnn

Published Mods: Underworld

Handy links: Vic_'s Forge events Own WIP Tutorials.

Link to comment
Share on other sites

1 minute ago, Matryoshika said:

You'll have to learn OpenGL.

What you seem to want, is called a HUD. (Heads Up Display).

I have a very old example here. It is from the 1.7 days, but it should be mostly correct still. ScaledResolution only needs the mc instance now, the width & height is done for you.

Do note, this involves Minecraft#getMinecraft() which is client-side only. As such, it must only be called client-side, as it will crash otherwise.

 

As Lhykos has states, you will need a timer. How you create that, is up to you.

 

Yeah I already know how to display the text and everything, it works perfectly.

the only thing I needed was the display time and animation

Link to comment
Share on other sites

Just now, Bets said:

Yeah I already know how to display the text and everything, it works perfectly.

the only thing I needed was the display time and animation

I'm not sure if you can fade out a text in Minecraft on a normal way. I never done that before :D

 

  • Like 1
Link to comment
Share on other sites

I totally forgot about the subtitles and overlay messages from Minecraft!

In the GuiIngame class by line 241. Here is handled how to fade out and display a overlay message for a specific time. (When a record is played. In that fancy, colored way)


But like Matryoshika already said. Here you have to know how to use OpenGL. :)

Edited by Lhykos
  • Like 1
Link to comment
Share on other sites

3 minutes ago, Lhykos said:

I totally forgot about the subtitles and overlay messages from Minecraft!

In the GuiIngame class by line 241. Here is handled how to fade out and display a overlay message for a specific time. (When a record is played. In that fancy, colored way)


But like Matryoshika already said. Here you have to know how to use OpenGL. :)

 

Oh right! forgot about these! I'll check them out

Link to comment
Share on other sites

You have not to link them.

You call 'GlStateManager.enableBlend()' before you draw the string with 'fontrenderer.drawCenteredString(...)' to enable the transparency.

And don't forget to call 'GlStateManager.disableBlend()') when you are finished.

 

And to your color you have to add the alpha via "bit shifting":

int color = alpha << 24 + color;

Btw. here is a link to understand the bit shifting.

 

For a "really good" transparency you call this:

GlStateManager.tryBlendFuncSeparate(GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA, GlStateManager.SourceFactor.ONE, GlStateManager.DestFactor.ZERO);

to tell OpenGL how to handle the blending mode.

 

It's more copy & paste from the source code, but that should explain it. :)

Edited by Lhykos
edit wrong code
Link to comment
Share on other sites

1 hour ago, Lhykos said:

You have not to link them.

You call 'GlStateManager.enableBlend()' before you draw the string with 'fontrenderer.drawCenteredString(...)' to enable the transparency.

And don't forget to call 'GlStateManager.disableBlend()') when you are finished.

 

And to your color you have to add the alpha via "bit shifting":


int color = alpha << 24 & color;

Btw. here is a link to understand the bit shifting.

 

For a "really good" transparency you call this:


GlStateManager.tryBlendFuncSeparate(GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA, GlStateManager.SourceFactor.ONE, GlStateManager.DestFactor.ZERO);

to tell OpenGL how to handle the blending mode.

 

It's more copy & paste from the source code, but that should explain it. :)

1

 

So all I need to do is something like this inside my RenderGameOverlayEvent.Text event handler?

GlStateManager.enableBlend();
GlStateManager.tryBlendFuncSeparate(GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA, GlStateManager.SourceFactor.ONE, GlStateManager.DestFactor.ZERO);
mc.ingameGUI.drawCenteredString(mc.fontRendererObj, "fade string", sr.getScaledWidth() / 2, sr.getScaledHeight() - 97, color);
GlStateManager.disableBlend();

 

 

Link to comment
Share on other sites

You should use the fontRendererObj directly:

 mc.fontRendererObj.drawString("fade string", sr.getScaledWidth() / 2 - mc.fontRendererObj.getStringWidth("fade string") / 2 , sr.getScaledHeight() - 97,  color);

 

And don't forget to add the alpha:

int color += (alpha << 24)

 

That's how you get the transparency. For the fade-out you still need to setup a timer or use the partialTicks like in the GuiIngame.

  • Like 1
Link to comment
Share on other sites

3 hours ago, Lhykos said:

You should use the fontRendererObj directly:


 mc.fontRendererObj.drawString("fade string", sr.getScaledWidth() / 2 - mc.fontRendererObj.getStringWidth("fade string") / 2 , sr.getScaledHeight() - 97,  color);

 

And don't forget to add the alpha:


int color += (alpha << 24)

 

That's how you get the transparency. For the fade-out you still need to setup a timer or use the partialTicks like in the GuiIngame.

 

It works well when I'm running the game from eclipse, but when I compile it and load it with a regular client it sometimes does not fade.

Link to comment
Share on other sites

Does anyone know why is this happening? The name of the item I hold (when the name is displayed) is interrupting the title fade. perhaps because both are fading at the same time? All it will do (if I'm holding an item and the item's name is displayed) is just disable the fade animation.

 

The code for the fade I made is this:

if (aTimer > 0) {
	aTimer--;
	if (aTimer > 5 && aTimer <= 250) {
		theAlpha = (gainTimer << 24);
	}
    mc.fontRendererObj.drawString("FADING TEXT", sr.getScaledWidth() / 2 - mc.fontRendererObj.getStringWidth("FADING TEXT") / 		2,sr.getScaledHeight() - 95, theAlpha);
}

 

Link to comment
Share on other sites

11 hours ago, Bets said:

Does anyone know why is this happening? The name of the item I hold (when the name is displayed) is interrupting the title fade. perhaps because both are fading at the same time? All it will do (if I'm holding an item and the item's name is displayed) is just disable the fade animation.

 

The code for the fade I made is this:


if (aTimer > 0) {
	aTimer--;
	if (aTimer > 5 && aTimer <= 250) {
		theAlpha = (gainTimer << 24);
	}
    mc.fontRendererObj.drawString("FADING TEXT", sr.getScaledWidth() / 2 - mc.fontRendererObj.getStringWidth("FADING TEXT") / 		2,sr.getScaledHeight() - 95, theAlpha);
}

 

What is 'gainTimer', it is the color?
It where important to understand how bit shifting is working. For me you try to set the alpha by shifting 24 in a timer value. This doesn't work.

The better question is, why you want to display the item name twice? It's showing you the name already above the hotbar.

 

Link to comment
Share on other sites

9 hours ago, Lhykos said:

What is 'gainTimer', it is the color?
It where important to understand how bit shifting is working. For me you try to set the alpha by shifting 24 in a timer value. This doesn't work.

The better question is, why you want to display the item name twice? It's showing you the name already above the hotbar.

 

 

I'm not displaying the item name twice. this is another title for something else :)

gainTimer is the Alpha, yes. What do you mean by it doesn't work - am I doing something wrong? (it is fading out, yet the item name is interrupting this specific title fade)

Link to comment
Share on other sites

Maybe I thought too complicated O.o
I think the best solution is doing it like minecraft. Simple and works perfect for me.

 

Spoiler

    displayTime--;

    if (displayTime > 0)
    {
        float f2 = (float) displayTime - event.getPartialTicks();
        int l1 = (int) (f2 * 255.0F / 100.0F);
        int color = 0xFFFF00;

        if (l1 > 255)
        {
            l1 = 255;
        }

        if (l1 > 8)
        {
            GlStateManager.enableBlend();
            GlStateManager.tryBlendFuncSeparate(GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA, GlStateManager.SourceFactor.ONE,
                    GlStateManager.DestFactor.ZERO);
            Minecraft.getMinecraft().fontRendererObj.drawString(message, sr.getScaledWidth() / 2 - Minecraft.getMinecraft().fontRendererObj.getStringWidth(message) / 2,
                    sr.getScaledHeight() / 2, color + (l1 << 24 & -color));
            GlStateManager.disableBlend();
        }

    }

 

 

  • Like 1
Link to comment
Share on other sites

12 hours ago, Lhykos said:

Maybe I thought too complicated O.o
I think the best solution is doing it like minecraft. Simple and works perfect for me.

 

  Hide contents


    displayTime--;

    if (displayTime > 0)
    {
        float f2 = (float) displayTime - event.getPartialTicks();
        int l1 = (int) (f2 * 255.0F / 100.0F);
        int color = 0xFFFF00;

        if (l1 > 255)
        {
            l1 = 255;
        }

        if (l1 > 8)
        {
            GlStateManager.enableBlend();
            GlStateManager.tryBlendFuncSeparate(GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA, GlStateManager.SourceFactor.ONE,
                    GlStateManager.DestFactor.ZERO);
            Minecraft.getMinecraft().fontRendererObj.drawString(message, sr.getScaledWidth() / 2 - Minecraft.getMinecraft().fontRendererObj.getStringWidth(message) / 2,
                    sr.getScaledHeight() / 2, color + (l1 << 24 & -color));
            GlStateManager.disableBlend();
        }

    }

 

 

 

 

Haha, yeah well it works! I don't understand how - But it works xD

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.

Announcements



×
×
  • Create New...

Important Information

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