Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


[XNA] Drawing List of Strings


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 nooblet   Members   -  Reputation: 167

Like
0Likes
Like

Posted 08 March 2012 - 02:10 AM

Hey!

I'm creating a ScreenManager, and essentially, I'd like to have the only menu items be strings instead of textures. When I run my program, it shows a clutter of text which doesn't look right at all. If you have any suggestions, please feel free to let me know:

MainMenu.cs:
[source lang="csharp"]using System;using System.Collections.Generic;using System.Linq;using System.Text;using Microsoft.Xna.Framework;using Microsoft.Xna.Framework.Audio;using Microsoft.Xna.Framework.Content;using Microsoft.Xna.Framework.GamerServices;using Microsoft.Xna.Framework.Graphics;using Microsoft.Xna.Framework.Input;using Microsoft.Xna.Framework.Media;namespace MyRTS{ class MainMenu : ScreenManager { public MainMenu() { Items = new List<string>(); Items.Add("New Game"); Items.Add("Exit Game"); Position = new List<Vector2>(); Position.Add(new Vector2(0, 25)); Position.Add(new Vector2(0, 50)); } public void LoadContent(ContentManager Content) { Font = Content.Load<SpriteFont>("MainMenuFont"); } }}[/source]

ScreenManager.cs
[source lang="csharp"]using System;using System.Collections.Generic;using System.Linq;using System.Text;using Microsoft.Xna.Framework;using Microsoft.Xna.Framework.Audio;using Microsoft.Xna.Framework.Content;using Microsoft.Xna.Framework.GamerServices;using Microsoft.Xna.Framework.Graphics;using Microsoft.Xna.Framework.Input;using Microsoft.Xna.Framework.Media;namespace MyRTS{ class ScreenManager { private Texture2D texture; private List<string> items; private SpriteFont font; private List<Vector2> position; private Color color; public Texture2D Texture { get { return texture; } set { texture = value; } } public List<string> Items { get { return items; } set { items = value; } } public SpriteFont Font { get { return font; } set { font = value; } } public List<Vector2> Position { get { return position; } set { position = value; } } public void Update() { } public void Draw(SpriteBatch spriteBatch) { foreach (string item in items) foreach (Vector2 pos in position) spriteBatch.DrawString(font, item, pos, Color.White); } }}[/source]

Game1.cs
[source lang="csharp"]using System;using System.Collections.Generic;using System.Linq;using Microsoft.Xna.Framework;using Microsoft.Xna.Framework.Audio;using Microsoft.Xna.Framework.Content;using Microsoft.Xna.Framework.GamerServices;using Microsoft.Xna.Framework.Graphics;using Microsoft.Xna.Framework.Input;using Microsoft.Xna.Framework.Media;namespace MyRTS{ public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; SpriteBatch spriteBatch; MainMenu mainMenu = new MainMenu(); public Game1() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; } protected override void Initialize() { base.Initialize(); } protected override void LoadContent() { spriteBatch = new SpriteBatch(GraphicsDevice); mainMenu.LoadContent(Content); } protected override void UnloadContent() { } protected override void Update(GameTime gameTime) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.Exit(); mainMenu.Update(); base.Update(gameTime); } protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); spriteBatch.Begin(); mainMenu.Draw(spriteBatch); spriteBatch.End(); base.Draw(gameTime); } }}[/source]

Sponsor:

#2 Manabreak   Members   -  Reputation: 141

Like
1Likes
Like

Posted 08 March 2012 - 07:52 AM

The problem is this:
public void Draw(SpriteBatch spriteBatch)
{
    foreach (string item in items)
        foreach (Vector2 pos in position)
            spriteBatch.DrawString(font, item, pos, Color.White);
}

This draws each string to each of the positions, which isn't the way you want it. Try this:

public void Draw(SpriteBatch spriteBatch)
{
	for(int i = 0; i < items.Count; ++i)
	{
		spriteBatch.DrawString(font, items[i], position[i], Color.White);
	}
}

This should do the trick. Although, I must warn you, there is no quarantee that the items and positions are in the same order. I would suggest creating a new class for each menu object, which would contain the string and the position for the object. Then, you could have a list of these new menu objects and draw them instead. Posted Image

#3 nooblet   Members   -  Reputation: 167

Like
0Likes
Like

Posted 08 March 2012 - 01:17 PM

Awesome, thanks a lot Manabreak! <3. Appreciate the assistance, man!




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS