locked
Game textures are being overwritten

    Question

  • I posted this in Game Studio Express forum, and didn't get any replies.  It occurred to me that I may be in the wrong forum.

    I made a simple game Menu class, as well as a MenuItem class, which allows me to build simple menus by

    providing 2 images for on and off state, x and y position, and an event handler.  I got this working perfectly

    Later, when  I began drawing game pieces, I find that the game peices showing up are images from my menu!  The widths and heights of the REAL images are being used, but the menu images are being resized, and drawn where the game pieces should be located.      I believe I have posted enough code below.

    Here's the code for the menu

      class Menu
        {
            ContentManager content;
            Hashtable menuItems;
          
            public Menu(ContentManager content)
            {
                this.content = content;
                menuItems = new Hashtable();
            }

            public void render(SpriteBatch spriteBatch)
            {
                IDictionaryEnumerator enumerator = menuItems.GetEnumerator();
          
                while (enumerator.MoveNext())
                {
                    ((MenuItem)enumerator.Value).render(spriteBatch);
                }
            }

     

            public void addItem(String asset_name, int x, int y)
            {
                addItem(asset_name,null, x, y, null);
            }


            public void addItem(String off_state_asset, String on_state_asset, int xPosition, int yPosition, ButtonEventHandler eventMethod)
            {
                //Trace.Assert(title.Equals(""), "Title is set to " + title);
                MenuItem item = new MenuItem();
                item.setName(off_state_asset);
                item.setOffTexture(content.Load<Texture2D>(off_state_asset));
              
                if (on_state_asset != null)
                {
                    item.setOnTexture(content.Load<Texture2D>(on_state_asset));
                }

                if (eventMethod != null)
                {
                    item.setEventHandler(eventMethod);
                }
              
                item.setPosition(xPosition, yPosition);

                //Hashtable of menu items (maybe convert to only a list)
                menuItems.Add(off_state_asset, item);
            }


            internal void checkEvents(MouseState mouseState)
            {
                IDictionaryEnumerator enumerator = menuItems.GetEnumerator();

                while (enumerator.MoveNext())
                {
                    ((MenuItem)enumerator.Value).checkState(mouseState);
                }
            }
        }

     

     

        class MenuItem
        {

            String name;
            Texture2D off_texture;
            Texture2D on_texture;
            bool on = false;

            int x, y; // position
            int width, height; // widths heights
          
            private event ButtonEventHandler onClick = null;

            public void setName(String name)
            {
                this.name = name;
            }

            public void setOffTexture(Texture2D texture)
            {
                off_texture = texture;
                this.width = off_texture.Width;
                this.height = off_texture.Height;
            }

            public void setOnTexture(Texture2D texture)
            {
                on_texture = texture;
            }

            public void render(SpriteBatch spriteBatch)
            {
              
                if (on)
                    spriteBatch.Draw(on_texture, new Rectangle(x, y, on_texture.Width, on_texture.Height), Color.White);      
                else
                    spriteBatch.Draw(off_texture, new Rectangle(x, y, off_texture.Width, off_texture.Height), Color.White);
              

            }


            public void setPosition(int x, int y)
            {
               this.x = x;
               this.y = y;
            }

            public void setEventHandler(ButtonEventHandler eventHandler)
            {
                this.onClick = eventHandler;
            }


            internal void checkState(MouseState mouseState)
            {
                on = false;
                if (mouseState.X > this.x && mouseState.X < (this.width + this.x) && mouseState.Y > this.y && mouseState.Y < (this.y + this.height))
                {
                    if (on_texture != null)
                    {
                        on = true;
                    }
                  
                    // ensure we have an event for this button
                    if (onClick != null)
                    {
                        // if the mouse is down, call the event
                        if (mouseState.LeftButton == ButtonState.Pressed)
                        {
                            onClick();
                        }
                    }
                }

            }
        }

     // Here's one of my game pieces

          
    class Hex
    {

           private static ContentManager contentManager;

           private static Texture2D hexLine;

           bool isRoot = false;

           int x,y;


            // Constructor
            public Hex(ContentManager contentManager, int x, int y, bool isRoot)
            {
                Hex.contentManager = contentManager;
                setRoot(isRoot);
                setPosition(x, y);
                init();
            }

     

            // initialize the Hex object (called from each constructor)
            private void init()
            {
                // load the texture from file, if this is the root
                if (isRoot)
                {
                    Hex.hexLine = Hex.contentManager.Load<Texture2D>("graphics/hexLines");
                }

           ...

        }

     
            public void renderHex(SpriteBatch spriteBatch)
            {
                spriteBatch.Draw(Hex.hexLine, new Rectangle(x - 37, y - 28, Hex.hexLine.Width, Hex.hexLine.Height), Color.White);
              
            }

     

     

    My Game Code, using the menu

    From my game class constructor

      gameMenu = new Menu(content);  (content is the ContentManager)

       rootHex = new Hex(content,640,480,true);

    Loading the menu contents in LoadGraphicsContent call

    if (loadAllContent)

    {

    spriteBatch = new SpriteBatch(graphics.GraphicsDevice);

    // TODO: Load any ResourceManagementMode.Automatic content

    //load menu content

    gameMenu.addItem("graphics/menu/logo", 350,60);

    gameMenu.addItem("graphics/menu/newgame_off", "graphics/menu/newgame_on", 432, 190, new ButtonEventHandler(newGameClicked));

    gameMenu.addItem("graphics/menu/options_off", 455, 250);

    gameMenu.addItem("graphics/menu/exit_off","graphics/menu/exit_on", 485, 310, new ButtonEventHandler(exitClicked));

      }

     

     - Draw method

            protected override void Draw(GameTime gameTime)
            {
                graphics.GraphicsDevice.Clear(Color.Black);
              
                spriteBatch.Begin();

                if (inMenu)
                {
                    gameMenu.render(spriteBatch);
                }

                if (inGame)
                {
                    rootHex.render(spriteBatch);
                }
              
                mousePointer.render(spriteBatch);
                spriteBatch.End();


                // TODO: Add your drawing code here

                base.Draw(gameTime);
            }

     

    Furthermore, when debugging my program, I take a look at the Texture2D objects holding the hexLine texture, and the Logo texture.

    Both are IDirect3DTexture9 pointers (mComPtr) , and both addresses are 001BF680.

    Tuesday, January 09, 2007 2:23 AM

Answers

  • Loads my textures wrong as in, I am loading them with the wrong method?

    I am using contentManager.Load<Texture2D>.

    I made it work, about an hour ago.

    I had to ensure that the textures were loaded in the LoadGraphicsContent method, though I'm not certain why.

    I added a loadTextures() method to my Hex class, which got a reference to the ContentManager upon instantiation.


    I called the loadTextures()  method in the LoadGraphicsContent method, and it worked great.

    I was previously calling init() from my Hex constructor, which was called immediately after the ContentManger was instantiated.

    I noticed that it takes one param, Services...

    content = new ContentManager(Services);


    It could be that "Services" wasn't in the correct state since I began using the ContentManager immediately after it was created in the main game constructor.  Beats me, but I got it working. ;)
    Wednesday, January 10, 2007 4:40 AM

All replies

  • Chances are that there's something in your program which loads the textures wrong. Or, perhaps, you have given the separate textures the same "resource name" in the texture processor, so they overwrite on build.

    One think you can do is run your program in PIX, capture the first frame to disk, and then examining the calls to lock/unlock, to see what texture objects actually get created.
    Tuesday, January 09, 2007 6:01 PM
  • Loads my textures wrong as in, I am loading them with the wrong method?

    I am using contentManager.Load<Texture2D>.

    I made it work, about an hour ago.

    I had to ensure that the textures were loaded in the LoadGraphicsContent method, though I'm not certain why.

    I added a loadTextures() method to my Hex class, which got a reference to the ContentManager upon instantiation.


    I called the loadTextures()  method in the LoadGraphicsContent method, and it worked great.

    I was previously calling init() from my Hex constructor, which was called immediately after the ContentManger was instantiated.

    I noticed that it takes one param, Services...

    content = new ContentManager(Services);


    It could be that "Services" wasn't in the correct state since I began using the ContentManager immediately after it was created in the main game constructor.  Beats me, but I got it working. ;)
    Wednesday, January 10, 2007 4:40 AM
  • b.t.w.  what is PIX ? how do I do that?
    Wednesday, January 10, 2007 4:42 AM