RonenNess / GeonBit.UI

UI system for MonoGame projects.
MIT License
477 stars 64 forks source link

UI prevents game from rendering #104

Closed seraph73 closed 5 years ago

seraph73 commented 5 years ago

Let me start by saying how awesome this project is, it's exactly what i was looking for.

I'm having an issue, because I want the UI to be displayed in front of the game itself, as it should be, however when I try to do that it replaces everything else that is behind it. I took some screenshots in order to better explain it. Screenshot_2 With the code like this, the UI draws itself before the actual game, and like this it works, not as intended, but it does. Screenshot_1

Now, when I replace the UserInterface draw instructions below the game itself, which should make it draw after the game, this happens: Screenshot_3 Interface shows properly, however the game itself vanishes. I was tinkering about and found that this line is actually making this happen. Screenshot_5 So what I did was change the Color, from white to transparent, and and also change on my main game class the order of the instructions, like so Screenshot_6 This makes the game draw itself again, but now the UI doesn't appear at all Screenshot_7

RonenNess commented 5 years ago

Hi @pirategbl, this is related to render targets behavior, your drawing order is most likely wrong. To explain in short:

  1. Every time you set a render target, the render target is cleared.
  2. This means the backbuffer is also cleared.
  3. GeonBit.UI uses an internal render target(s), which means drawing it trigger a target switch.

This means that the correct order you should to draw things is this:

       // draw ui (on internal render target)
        UserInterface.Active.Draw(spriteBatch);

        // clear buffer
        GraphicsDevice.Clear(Color.CornflowerBlue);

        // <-- PUT YOUR GAME RENDER CODE HERE
        spriteBatch.Begin();
        spriteBatch.Draw(GeonBit.UI.Resources.ButtonTextures[ButtonSkin.Alternative], new Rectangle(0, 0, 500, 500), Color.White);
        spriteBatch.End();

        // present UI on screen
        UserInterface.Active.DrawMainRenderTarget(spriteBatch);

        // call base draw function
        base.Draw(gameTime);

In the example above I added these lines:

        spriteBatch.Begin();
        spriteBatch.Draw(GeonBit.UI.Resources.ButtonTextures[ButtonSkin.Alternative], new Rectangle(0, 0, 500, 500), Color.White);
        spriteBatch.End();

That will draw one of GeonBit textures as a test. You can replace that part with your own rendering code.

If the above didn't work, please let me know and reopen this issue. Thanks :)

seraph73 commented 5 years ago

Worked like a charm, thanks for the quick reply and continue the awesome work 👍