XNA Creators Club Online
Page 1 of 1 (9 items)
Sort Posts: Previous Next

I don't understand this in XNA

Last post 16/04/2009 20:32 by DeAndreon. 8 replies.
  • 16/04/2009 14:48

    I don't understand this in XNA

    Hello people!!!!!!! I'm a new member of this great community! But i'm a noob with XNA :( I've a question to do for you...
    Then, there is this source:


    using System;
    using System.Collections.Generic;
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Content;
    using Microsoft.Xna.Framework.GamerServices;
    using Microsoft.Xna.Framework.Graphics;
    using Microsoft.Xna.Framework.Input;

    namespace tutorial_XNA
    {

    public class Game1 : Microsoft.Xna.Framework.Game
    {
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    Texture2D imm_folletto;
    Vector2 posizione;

    public Game1()
    {
    graphics = new GraphicsDeviceManager(this);
    Content.RootDirectory = "Content";
    }


    protected override void Initialize()
    {
    spriteBatch = new SpriteBatch(GraphicsDevice);
    posizione = new Vector2(0f, 0f);

    base.Initialize();
    }


    protected override void LoadContent()
    {
    imm_folletto = Content.Load<Texture2D>("Immagini/folletto");
    }


    protected override void UnloadContent()
    {

    }


    protected override void Update(GameTime gameTime)
    {

    base.Update(gameTime);
    }


    protected override void Draw(GameTime gameTime)
    {
    graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

    spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Deferred, SaveStateMode.SaveState);
    spriteBatch.Draw(imm_folletto, posizione, Color.White);
    spriteBatch.End();

    base.Draw(gameTime);
    }
    }
    }


    Well, it put on the screen the image "folletto". And this is it main source where is the Main():

    http://www.wilez.it/tutorials/XNA/immagini/main_XNA1.gif

    My question is: in main source the object game is created on the class Game1 that in the other source. Well, there is the builder(or maker(i'm not english ;)) that has:

    graphics = new GraphicsDeviceManager(this);
    Content.RootDirectory = "Content";

    and then the object game has the object graphics and Content.RootDirectory = "Content";.
    I don't understand how the metods (or function of the class) will be used by the object " game" in the main source becouse i don't see anywhere where they are called but the programs go well...

    Please, help me! Are 2 days that i don't understand this...

  • 16/04/2009 15:05 In reply to

    Re: I don't understand this in XNA

    Welcome to the creator's club :-)  A few notes/points on your questions

    • You generally don't have to worry about the contents of the "Main" method.  All it really does is initialize and start the game, which is contained in "Game1"
    • The "Game1" class is just the default name, you can change it to something that suits your game, but it's not really nescessary as the player will never see it.
    • The Content.RootDirectory is set to "Content", because if you look in the bin folder, you'll find that all of your content (textures, audio, etc.) are all in a "Content" directory.  This setting allows you to simply refer to content in the ContentManager class without having to always specify the Content directory.  For example, 'Content.Load<Texture2d>("MySprite")'
    I'd suggest you take a look at the getting started videos on the site, particularly the 2D starter ... it should help you understand how the game's structure works and where to put your code.

    Good Luck!
    Joel Martinez - XNA MVP
    Blog: http://codecube.net
    Play Videos on an XNA Texture: Scurvy Media
    XNA Unit Testing: Scurvy Test
  • 16/04/2009 15:07 In reply to

    Re: I don't understand this in XNA

    Constructor is the correct word for maker or builder.

    The constructor is automatically called with the new keyword.

    using (Game1 game = new Game1()) // this calls the Game1 constructor
    {
    }
    Game hobbyist hell-bent on coding a diabolical Matrix
  • 16/04/2009 17:57 In reply to

    Re: I don't understand this in XNA

    Thanks for the answares! Ok, i've understood more thinks with your answares :) But i dont' understand how the object in the main source call the metods(or functions of the class) becouse there isn't (how i see) a calling in the constructor in the class and i see only the the 2 metods( one construct an object and one tell the directory Contenent). How the programs knows which metods we have to call? Becouse in the contructor of the object of the class Game1 there aren't the inizialization of the objetc to use the metods of the class... That i don't understand.

    Thanks for the future answares and if you have more tima, can you tell me the order of the operations that the program do to put on the screen the image( such as a degub ;))

    Thanks in advance!
  • 16/04/2009 18:01 In reply to

    Re: I don't understand this in XNA

    The framework takes care of calling the Initialize, LoadContent, Update, Draw etc. methods for you. It's all hiden inside game.Run().
  • 16/04/2009 18:24 In reply to

    Re: I don't understand this in XNA

    Ahhhhhhhhhhhhhhhhhhhhhhhhhhhhhh!!!!!!!!!!!!!!!!!!!! Thanks! Thanks! Thanks!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! One that tell me an right answare about that!!!!!!!!!!!!!!!!!!!! In other italian forums tall me that i haven't understood the O.O. programming and they do the "teacher" with me telling me that i've to study well O.O. programming but i've understood very well it! Thanks ! Thanks!


    I'm from today in a great community :) Thanks!
  • 16/04/2009 18:41 In reply to

    Re: I don't understand this in XNA

    Game1.cs is called through the program.cs file. If you've ever learned Java, you will learned that all programs are run through a "main method" like the following:

    public static void main(String[] args)
    {
    // The main processing of the program occurs here

    System.exit(0);
    }

    C# is nearly a rip off of Java (but runs faster for games), also having a main class where the Game1.cs class starts. Open the program.cs file and you will find the following code, very similar to Java:
        static class Program
        {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            static void Main(string[] args)
            {
                using (Game1 game = new Game1())
                {
                    game.Run();
                }
            }
        }

    This should answer your question about how Game1.cs starts.
  • 16/04/2009 18:44 In reply to

    Re: I don't understand this in XNA

    SedativeChunk:
    Game1.cs is called through the program.cs file. If you've ever learned Java, you will learned that all programs are run through a "main method" like the following:

    public static void main(String[] args)
    {
    // The main processing of the program occurs here

    System.exit(0);
    }

    C# is nearly a rip off of Java (but runs faster for games), also having a main class where the Game1.cs class starts. Open the program.cs file and you will find the following code, very similar to Java:
        static class Program
        {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            static void Main(string[] args)
            {
                using (Game1 game = new Game1())
                {
                    game.Run();
                }
            }
        }

    This should answer your question about how Game1.cs starts.

    I'm pretty sure I just pointed that out. ;-)
  • 16/04/2009 20:32 In reply to

    Re: I don't understand this in XNA

    Yes, you have reason! I've understood that in Gama1.cs there is the Main() but i haven't understood how the metods Inizialize, LoadContent etc... will charge but Run() metods of class Game. Now, i've understood that Run() metod inizialize in automatic the metods Inizialize, LoadContent etc... of the classe Game1 that is derivated from the class Game :)

    Now i've understood and lot of thanks for the helps!


Page 1 of 1 (9 items) Previous Next