Game Development Community

Trying do display sprite on main menu

by Arden · in Torque X 2D · 11/02/2010 (4:17 pm) · 12 replies

Hello all,

I'm trying to display a sprite on a main menu like this:

SimpleMaterial material = new SimpleMaterial();
material.TextureFilename = @"data\images\logo.png"; 
material.IsAdditive = false; 
material.IsTranslucent = false; 
material.TextureDivider = new GenericTextureDivider();

T2DStaticSprite ss1 = new T2DStaticSprite(); 
ss1.Material = material; 
ss1.Position = new Vector2(0, 0); 
ss1.Size = new Vector2(50, 50); 
TorqueObjectDatabase.Instance.Register(ss1);

When I run the program, nothing is displayed, but I'm not getting any errors. If I change the name of the image to something that doesn't exit for the material, I am not receiving any errors either. Is this the right method to draw a sprite on the Scenegraph prior to loading a Scene?

I define both the style and the sceneview in an XML file like this:

<GUIStyle name="gsvs" />
    <T2DSceneGraph name="gsg">
      <T2DSceneContainer>
        <BinSize>20</BinSize>
        <BinCount>256</BinCount>
      </T2DSceneContainer>
    </T2DSceneGraph>
    <Camera2D name="Camera">
      <CenterPosition>
        <X>540.000</X>
        <Y>260.000</Y>
      </CenterPosition>
      <Extent>
        <X>1080.000</X>
        <Y>520.000</Y>
      </Extent>
      <UseCameraWorldLimits>false</UseCameraWorldLimits>
    </Camera2D>

<GUISceneView name="gsv">
          <Style nameRef="gsvs"/>
          <SceneGraph nameRef="gsg"/>
          <Camera nameRef="Camera"/>
          <Position>
            <X>100</X>
            <Y>100</Y>
          </Position>
          <Size>
            <!-- <X>1080</X>
            <Y>520</Y> -->
            <X>1179</X>
            <Y>619</Y>
          </Size>
        </GUISceneView>

Any idea on what I would be doing wrong?

Thanks for any help.

#1
11/02/2010 (10:25 pm)
You need to set the SceneGraph property of the static sprite to the scene graph of the camera.

T2DSceneCamera camera = TorqueObjectDatabase.Instance.FindObject<T2DSceneCamera>("Camera");
            ss1.SceneGraph = camera.SceneGraph;

Give that a go, it should show up once that property is set.
#2
11/03/2010 (12:34 am)
Thanks for the tip Cosmic.

It's weird. Still not working. However, if I put the same code once my first level file is loaded in the main game screen, the sprite appears.

The reason I'm trying to generate a Sprite from code is to be able to display it in the main menu screen before the first scene is loaded, so I'm trying to isolate the condition that prevents it from appearing in that screen. I'm having no trouble with text.

Been hamering at this for about 5 hours now :)
Any additional pointers would really be appreciated.

Thanks

#3
11/03/2010 (1:09 am)
Thanks for the tip Cosmic.

It's weird. Still not working. However, if I put the same code once my first level file is loaded in the main game screen, the sprite appears.

The reason I'm trying to generate a Sprite from code is to be able to display it in the main menu screen before the first scene is loaded, so I'm trying to isolate the condition that prevents it from appearing in that screen. I'm having no trouble with text.

Been hamering at this for about 5 hours now :)
Any additional pointers would really be appreciated.

Thanks

#4
11/03/2010 (5:29 am)
Aha.

Is the sceneview and your other menu objects in the same .xml?

If it isn't, I assume you're loading both .xml files and then setting your parent bitmap from your menu file as the content control?

What you'll have to do from there is push the GUISceneview onto the canvas. Loading the .xml won't activate the sceneview. Only TX level files do that.

GUICanvas.Instance.PushDialogControl("gsv",0);

Give that a go. Don't you just love the long debugging process?
#5
11/03/2010 (5:10 pm)
Hehehe..... tried it, still no cigar :) I'm learning from your comments though. I ran some tests last night and I figured out that it's not the TX Level file that triggers it. If I create a clear GUICanvas instance not based on the XML, the sprites appear (but not the text).

Here is exactly what is happening:

From the Game.cs file, I have this code:
SceneLoader.Load(@"data\ui\MainScreenControls.xml");
GUICanvas.Instance.SetContentControl("MainMenuRootControl");

GuiMainMenu _main = new GuiMainMenu();
// If I uncomment the following line, the sprites will appear on a black screen.
//GUICanvas.Instance.SetContentControl(_main);

The GuiMainMenu class contains the following:
class GuiMainMenu : GUISceneview, IGUIScreen
    {
        // Constructor
        // This code section is an alternative to using an XML file. It's an original member of Dragon's Tower. 
        public GuiMainMenu()
        {
            GUIStyle playStyle = new GUIStyle();
            Name = "GuiPlay";
            Style = playStyle;
	    // Based on your recommendation, I have tried both of these:
            GUICanvas.Instance.PushDialogControl("MainMenuRootControl", 0);
            GUICanvas.Instance.PushDialogControl("gsv", 0);

            //Find the Camera Object in the XML
            T2DSceneCamera camera = TorqueObjectDatabase.Instance.FindObject<T2DSceneCamera>("Camera");   
            GUISceneview view = TorqueObjectDatabase.Instance.FindObject<GUISceneview>("gsv");
            
                        
            SimpleMaterial material = new SimpleMaterial();
            material.TextureFilename = @"dataimagesdragondragonHead.png";
            material.IsAdditive = false;
            material.IsTranslucent = false;
            GenericTextureDivider TextureDivider = new GenericTextureDivider();  
            material.TextureDivider = TextureDivider;

            T2DStaticSprite ss1 = new T2DStaticSprite();
            ss1.Material = material;
            ss1.Name = "Tic";
            ss1.Size = new Vector2(50, 50);
            ss1.Rotation = 0; 
            ss1.Position = new Vector2(0, 0);
            ss1.Physics.Velocity = new Vector2(250, 0);
            TorqueObjectDatabase.Instance.Register(ss1);
            
            T2DStaticSprite ss2 = new T2DStaticSprite();
            ss2.Material = material;
            ss2.Position = new Vector2(540, 260);
            ss2.Size = new Vector2(50, 50);
            //ss2.SceneGraph = camera.SceneGraph; 
            TorqueObjectDatabase.Instance.Register(ss2);

            T2DStaticSprite ss3 = new T2DStaticSprite();
            ss3.Material = material;
            ss3.Position = new Vector2(1080, 520);
            ss3.Size = new Vector2(50, 50);
            //ss3.SceneGraph = camera.SceneGraph; 
            TorqueObjectDatabase.Instance.Register(ss3);
        }

        public void OnMainScreenWake(GUIControl mainGUI)
        {
            // perform screen-activation tasks
        }

    }

Which gives the result of the root control (background image) and child text controls but not the sprite. I have added a sprite object in the XML as well.

You'll notice that I've commented out the following line of code:
//GUICanvas.Instance.SetContentControl(_main);

If I remove that comment, a new _main screen is initialised and objects both created in the XML and GuiMainMenu() appear.

This is making me crazy :0
Anyways, again, appreciate the help.
#6
11/04/2010 (12:02 am)
Hmm.... y use T2DStaticSprite obj rather then GUIBitmap obj on your GUI extended menu screens to display bitmaps?
#7
11/04/2010 (1:17 pm)
Yeah, I think that's what I'm gonna do. I've been trying to spend too much time on this. Do you also know if there is the equivalent to this for animating a Bitmap?
#8
11/04/2010 (5:48 pm)
Hey,

I've got it working.

Here's an XML base I used:

<?xml version="1.0" encoding="utf-8" ?>
<TorqueSceneData>
    <Version>1.0</Version>
    <SceneData>
        <GUIBitmapStyle name="basicStyle">
            <SizeToBitmap>false</SizeToBitmap>
        </GUIBitmapStyle>
      <GUITextStyle name="gaud20">
        <FontType>datafontsGaud20</FontType>
        <TextColors>
          <Color>
            <X>1</X>
            <Y>0.5</Y>
            <Z>0.5</Z>
            <W>1</W>
          </Color>
        </TextColors>
        <SizeToText>false</SizeToText>
        <Alignment>JustifyCenter</Alignment>
      </GUITextStyle>

      <GUIStyle name="gsvs" />
      <T2DSceneGraph name="gsg">
        <T2DSceneContainer>
          <BinSize>20</BinSize>
          <BinCount>256</BinCount>
        </T2DSceneContainer>
      </T2DSceneGraph>
      <Camera2D name="Camera">
        <CenterPosition>
          <X>540.000</X>
          <Y>260.000</Y>
        </CenterPosition>
        <Extent>
          <X>1080.000</X>
          <Y>520.000</Y>
        </Extent>
        <UseCameraWorldLimits>false</UseCameraWorldLimits>
      </Camera2D>
      
        <GUIBitmap name="Screen">
            <Style nameRef="basicStyle"/>
            <Bitmap>dataimagesGUIlocalProperties</Bitmap>
            <ChildControls>
              <GUIText name="typeLabel">
                <Style nameRef="gaud20"/>
                <Text> WHEEEEEEEEE </Text>
                <Position>
                  <X>590</X>
                  <Y>228</Y>
                </Position>
                <Size>
                  <X>500</X>
                  <Y>50</Y>
                </Size>
              </GUIText>
              <GUISceneview name="gsv">
                <Style nameRef="gsvs"/>
                <SceneGraph nameRef="gsg"/>
                <Camera nameRef="Camera"/>
                <Position>
                  <X>100</X>
                  <Y>100</Y>
                </Position>
                <Size>
                  <X>1080</X>
                  <Y>520</Y>
                </Size>
              </GUISceneview>
            </ChildControls>
        </GUIBitmap>
    </SceneData>
</TorqueSceneData>

First thing to note is that the camera and scenegraph are above the bitmap and the sceneview is inside the child controls of the bitmap.

I also found out that the source of the problem is a typo in the Introduction to Torque X GUI

in the xml GUISceneview needs to have a lowercase V

#9
11/04/2010 (5:49 pm)
From there, all you need is this in your GUIMainMenu.cs

class GuiMainMenu{

    // Constructor
    // This code section is an alternative to using an XML file. It's an original member of Dragon's Tower. 
    public GuiMainMenu() {

        SimpleMaterial material = new SimpleMaterial();
        material.TextureFilename = @"dataimagesgameImagesasteroid_ship";
        material.IsAdditive = false;
        material.IsTranslucent = false;
        GenericTextureDivider TextureDivider = new GenericTextureDivider();
        material.TextureDivider = TextureDivider;
        
        T2DStaticSprite ss1 = new T2DStaticSprite();
        ss1.Material = material;
        ss1.Name = "Tic";
        ss1.Size = new Vector2(500, 500);
        ss1.Rotation = 0;
        ss1.Position = new Vector2(0, 0);
        TorqueObjectDatabase.Instance.Register(ss1);

        T2DStaticSprite ss2 = new T2DStaticSprite();
        ss2.Material = material;
        ss2.Position = new Vector2(540, 260);
        ss2.Size = new Vector2(50, 50);
        TorqueObjectDatabase.Instance.Register(ss2);

        T2DStaticSprite ss3 = new T2DStaticSprite();
        ss3.Material = material;
        ss3.Position = new Vector2(1080, 520);
        ss3.Size = new Vector2(50, 50);
        TorqueObjectDatabase.Instance.Register(ss3);
    }
}

Changes to note: I got rid of the subclassing of GUISceneview. We already made one in the .xml

This class just creates the material, creates the sprites and is done.

Now, in your game you only need to do this:

SceneLoader.Load(@"GUIGUIMain.xml");
            GUICanvas.Instance.SetContentControl("Screen");

            x = new GuiMainMenu();


The biggest issue, the one that I had to step through the rendering of GUICanvas was the typo in GUISceneview... lowercase V.

When you use my code, make sure you change the file paths to your images.
#10
11/04/2010 (9:17 pm)
Cosmic...

!YOU ROCK! :)))))))

This is the conclusion of a 3 day ordeal, and a great feeling to be able to animate GUI screens with sprites.

A big THANK YOU for your help!
#11
11/04/2010 (10:36 pm)
No problem. If you give anyone that GUI tutorial pdf, make sure to let them know about the typo in it.
#12
11/05/2010 (12:35 am)
Sure will. It's amazing how a small detail like that can complicate things :)