3 Replies - 2907 Views - Last Post: 16 September 2012 - 05:44 AM

#1 MheQ  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 14-September 12

XNA Beginner questions/confirmation of knowledge thusfar.

Posted 15 September 2012 - 11:10 AM

Hey!

I am a beginner programmer who knows basics of java and C# and now am working with a fun and educational project with few friends on XNA. My goal here is to gain more understanding and start implementing OOP with comfort and as a natural part of OOP programming. Therefore I'm in a spot where I want to create Sprite classes to use in my main code to call those sprites into my application. I have had several problems so I would love to gain a certain ground to be certain that if things that I at the moment know are correct or not. So I would be very glad if you could confirm or disprove my understanding thusfar. Also I would like to mention that I have read quite alot of the tutorials but getting bits and pieces from everything so heres my understanding of what I have gathered without knowing if what I have gathered is correct.

So to begin.
When I make my XNA game project I have

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


I guess the GraphicsDeviceManager implements my graphic settings from my computer. Does the "This" in method arguments can be changed to anything and if then for what reason?

Moving on we have the Initialize method

protected override void Initialize()
        {
            // TODO: Add your initialization logic here


            base.Initialize();
        }


I'm quite lost here. I know i can create my new objects there but im kind of clueless about the logic behind it. Short and logical explenation would be of great use here.


Next we have the LoadContent method.

Basically in LoadContent method I can load items from my content that I have added previously to GameContent in my case and then assign those items to certain objects?

Next thing that I dont understand fully is the line:
spriteBatch = new SpriteBatch(GraphicsDevice);


Next theres the Update method.
My questin here is that how many times per second update method runs aprox and where does this number come from?

If I would get answers to those questions then I think I understand that part of the section fully.
Moving on to Classes.

Lets say I make a new class called "Sprite".
Would love to have a confirmation or you know..the opposite of that word to my statements.

When I create a new Class then does classes in XNA behave the same way as for example in Java. So that at first i create all of the neccesary class variables that i need to use on that class( For example Vector2D as its location and Texture2D as its texture ). Next I would create the constructor but as far as i have seen from examples then Constructor in XNA is basically the LoadContent method? In where i give my previously declared variables their values. One thing I cant understand fully there is the cariable ContentManager which I apparantly need to use in the method.
Next having created the new class I would have to create a Draw method for the class right? In which I use theSpriteBatch.Draw(given texture, given position, given color ).

Having created the class I now turn towards my main Game1.cs in my case.

In the start of the program @ public class Game1 I declare that im going to use for example
Sprite man;

Then towards the initialize method ( as said before I'm not totally sure why ) I create the object called man.
 man = new Sprite();
       // what else can I do in this method with my new sprite?



Next we have the LoadContent method in main hwere i just have to call my given sprite's LoadContent method like this :
man.LoadContent(this.Content, "Man" ) 
// given that my mans sprite name was Man.png



Now that I have loded the content, all for me to do is Draw it right?
For that in my main Game1.cs class, I turn to the Draw method and just type out :
spriteBatch.Begin();
man.Draw(this.spriteBatch);
spriteBatch.End();
// Again.. what is up with the spriteBatches?


I know this is a reaaaly beginner and noobish thread, but there arent really good tutorials online and I still have like a week at least til my book arrives.

If anyone would be awesome enough to help, I would greatly appriciate it!

Cheers
MheQ

Is This A Good Question/Topic? 0
  • +

Replies To: XNA Beginner questions/confirmation of knowledge thusfar.

#2 BBeck  Icon User is offline

  • Here to help.
  • member icon


Reputation: 536
  • View blog
  • Posts: 1,193
  • Joined: 24-April 12

Re: XNA Beginner questions/confirmation of knowledge thusfar.

Posted 15 September 2012 - 01:21 PM

So welcome to XNA. You're gonna love it.

Wow! That's a lot of questions for one post. Let me see how many I can tackle. First off, you're hitting me in one of my weakest areas: sprites and 2D drawing. I work almost entirely in 3D. But I'll tell you what I know or believe and someone can jump in and correct me or add to it if they want.

View PostMheQ, on 15 September 2012 - 12:10 PM, said:

Hey!

I am a beginner programmer who knows basics of java and C# and now am working with a fun and educational project with few friends on XNA. My goal here is to gain more understanding and start implementing OOP with comfort and as a natural part of OOP programming. Therefore I'm in a spot where I want to create Sprite classes to use in my main code to call those sprites into my application. I have had several problems so I would love to gain a certain ground to be certain that if things that I at the moment know are correct or not. So I would be very glad if you could confirm or disprove my understanding thusfar. Also I would like to mention that I have read quite alot of the tutorials but getting bits and pieces from everything so heres my understanding of what I have gathered without knowing if what I have gathered is correct.

So to begin.
When I make my XNA game project I have

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


I guess the GraphicsDeviceManager implements my graphic settings from my computer. Does the "This" in method arguments can be changed to anything and if then for what reason?


So, first of all, this is your constructor for the Game1 class right here that you were asking about later in the post. I believe "this" points to the current class (Game1). Other than pointing specifically to Game1, I don't think you would change it. Yes. GraphicsDeviceManager basically implements your graphics settings and the GraphicsDevice is basically the graphics card. So, I think you should probably leave that code alone until you know exactly what to do with it. I don't think I've ever changed it. You're probably more likely to change the location of Content then mess with that line initializing the GraphicsDeviceManager.

Quote

Moving on we have the Initialize method

protected override void Initialize()
        {
            // TODO: Add your initialization logic here


            base.Initialize();
        }


I'm quite lost here. I know i can create my new objects there but im kind of clueless about the logic behind it. Short and logical explenation would be of great use here.


Initialize and the constructor are very similar. Quite frankly, I'm not sure why they are two seperate things except that "base.Initialize()" line will probably call any game components that you have defined where the constructor won't. You can pretty much get away with using them the same way. Initialize is basically where your startup code goes that has nothing to do with Content. If it has to do with Content it goes in LoadContent().

Quote

Next we have the LoadContent method.

Basically in LoadContent method I can load items from my content that I have added previously to GameContent in my case and then assign those items to certain objects?


Yes. It's for loading content at startup.


Quote

Next thing that I dont understand fully is the line:
spriteBatch = new SpriteBatch(GraphicsDevice);



Again, kind of my weak area. But you're "instantiating", or creating an instance of a SpriteBatch object. And it has to point to your graphics card like it does there. Basically, a SpriteBatch is a batch of sprites that get drawn together. You can have only one sprite batch per game, and that's actually probably more prefered. But you can also declare more if you have reason to.

The graphics card likes to bunch up sprites and draw them together. And for example, 3D meshes are not sprites but could be displayed on the same screen.

The graphics card likes drawing all sprites that use the same texture together. If it switches textures, I believe it actually becomes another batch internally. It will even try to group things with the same texture together in a drawing pass. I was just reading about this this morning in some book. I'm reading about 6 books simultaneously right now, so I don't remember which one off the top of my head.

Quote

Next theres the Update method.
My questin here is that how many times per second update method runs aprox and where does this number come from?


It will call the update and draw 60 times per second by default unless you do something that slows it down slower than that. There's a setting where you can change it to go full speed, but I wouldn't worry about that for now. 60 frames per second is plenty fast enough.

Quote

If I would get answers to those questions then I think I understand that part of the section fully.
Moving on to Classes.

Lets say I make a new class called "Sprite".
Would love to have a confirmation or you know..the opposite of that word to my statements.

When I create a new Class then does classes in XNA behave the same way as for example in Java. So that at first i create all of the neccesary class variables that i need to use on that class( For example Vector2D as its location and Texture2D as its texture ). Next I would create the constructor but as far as i have seen from examples then Constructor in XNA is basically the LoadContent method? In where i give my previously declared variables their values. One thing I cant understand fully there is the cariable ContentManager which I apparantly need to use in the method.


Classes in Java and C#/XNA are pretty much the same. The constructor is the constructor (as mentioned above) not LoadContent. Your own classes only have to have a constructor and nothing else (you will pretty much always use Game1 and then add your own classes to that as needed). Game components are special classes that usually have their own LoadContent, Update, Draw, etc. and much more closely resemble Game1, but that's a more advanced topic.

Quote

Next having created the new class I would have to create a Draw method for the class right? In which I use theSpriteBatch.Draw(given texture, given position, given color ).

Having created the class I now turn towards my main Game1.cs in my case.

In the start of the program @ public class Game1 I declare that im going to use for example
Sprite man;

Then towards the initialize method ( as said before I'm not totally sure why ) I create the object called man.
 man = new Sprite();
       // what else can I do in this method with my new sprite?



Next we have the LoadContent method in main hwere i just have to call my given sprite's LoadContent method like this :
man.LoadContent(this.Content, "Man" ) 
// given that my mans sprite name was Man.png




That man.LoadContent looks wrong to me, but if it works it works.


Quote

Now that I have loded the content, all for me to do is Draw it right?
For that in my main Game1.cs class, I turn to the Draw method and just type out :
spriteBatch.Begin();
man.Draw(this.spriteBatch);
spriteBatch.End();
// Again.. what is up with the spriteBatches?


This looks basically correct. You have to tell spriteBatch to begin and end drawing. It's doing a lot of work in the background you're not aware of. Basically, you want to draw all your sprites in your Game1 Draw() method between the Begin and End there. You want to modify them in your Update(). You can also create methods to draw each sprite and call them from your Draw() method in Game1. Or you could create an entirely new class and call that object's Draw method from inside your Game1 Draw() method.

Quote

I know this is a reaaaly beginner and noobish thread, but there arent really good tutorials online and I still have like a week at least til my book arrives.

If anyone would be awesome enough to help, I would greatly appriciate it!

Cheers
MheQ



Check out RB Whitiker's tutorials.


Here is my link's page from my website:
http://xna-3d-101.com/Links/Links.html

It has a link to RB's site and Riemer's. Both good tutorials. I think there are actually a lot of good 2D XNA tutorials out there. There's a permenant thread here with a lot of them.
Was This Post Helpful? 0
  • +
  • -

#3 MheQ  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 14-September 12

Re: XNA Beginner questions/confirmation of knowledge thusfar.

Posted 16 September 2012 - 05:08 AM

Cant thank you enough for answering. Definitely got more understanding. I have NO idea how i managed to skip http://rbwhitaker.wikidot.com .. I guess the page just looked so bad that I didn't think anything of it... now im thinking its a really good place to find answers to all of my other questions.
Was This Post Helpful? 0
  • +
  • -

#4 BBeck  Icon User is offline

  • Here to help.
  • member icon


Reputation: 536
  • View blog
  • Posts: 1,193
  • Joined: 24-April 12

Re: XNA Beginner questions/confirmation of knowledge thusfar.

Posted 16 September 2012 - 05:44 AM

Yes. His home page doesn't exactly reach out and grab you. But, he's got some really good stuff on there. I've mainly just gone through his 3D stuff and HLSL stuff, but if the C# and 2D stuff is like the 3D stuff it will be well worth your time.

Riemer's stuff's actually pretty good too. Again, I've only gone through his 3D stuff. But if that's any indication of his 2D stuff it will again be worth your time. Although, Riemer has been known to talk "over my head" a lot. For example, I went through his HLSL tutorial when I first started XNA and almost all of it went over my head. I got almost nothing out of it. Then I figured out I didn't need HLSL anyway (you can get really deep into XNA before you really need to learn it). So, now I've been studying HLSL for about a month and I've gone back to his site and I think I'm getting more out of his HLSL than any other tutorial when it comes to shadows.

With Riemer's stuff you may have to do it now and get what you can out of it, and then go away and come back in a year or two and see how much more you can get out of it.

There are other sites out there too, but those two are my favorites.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1