Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



Game loop problem with XNA

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 30-11-2010
Member
 
Join Date: Jul 2010
Posts: 75
Game loop problem with XNA
  

The classic file Program.cs is, as in any application. NET, the entry point of the application, and in the case of applications XNA, instantiates the class Game1 defined in the file Game1.cs and call the method on the Run. The game, in the case of this simple example, where we do not make use of architectural virtuosity, will be fully defined in the file Game1.cs.
Code:
public class game1: Microsoft.Xna.Framework.Game
 {
   GraphicsDeviceManager graphics;
   SpriteBatch SpriteBatch;
  
   public game1 ()
   {
     graphics = new GraphicsDeviceManager (this);
     Content.RootDirectory = "Content";

     / / Frame rate is 30 fps by default for Windows Phone.
     TargetElapsedTime TimeSpan.FromTicks = (333,333);
   }
In the constructor of the class we find some important information: first GraphicsDeviceManager object is created that will manage the graphical portion of the device compared to our game. Then property is set RootDirectory content to the directory "content" of the project, where we added the picture just now. The third line of code opens our minds to the world XNA: XNA interface in a game is not based on form or pages in which to place controls that trigger events such as Silverlight, WPF, Windows Forms or additional Visual Basic, but on the concept loop runs at intervals, regular default, in which our code will draw on the screen. For comparison a bit 'forced, but that should better explain the concept to those coming from a more traditional programming, is how to create a blank form and a Timer control that triggers an event at regular intervals of time in which to manage our code.

Reply With Quote
  #2  
Old 30-11-2010
Member
 
Join Date: May 2008
Posts: 248
Re: Game loop problem with XNA

The XNA runtime is based on this philosophy: a regular (or irregular willing) time, the runtime calls the method Update is intended to contain the game logic and then invoke the method Draw , aimed at drawing on the screen. Both methods are already in place by the template of Visual Studio and, in a moment, we'll fill them with the code of our simple animation. TargetElapsedTime property indicates the time interval after which the runtime will invoke the two methods and therefore The framerate of our application (ie the number of times the game update our image to the screen). By default, Windows Phone 7, the frame rate is 30 fps (frames per second), while Windows is normally 60 fps. Before you can wire the code in the methods Update and Draw must inform the runtime that we want to use the image above in addition to our project "Content".
Reply With Quote
  #3  
Old 30-11-2010
Member
 
Join Date: Feb 2010
Posts: 644
Re: Game loop problem with XNA

You should also know that by default, Windows Phone 7, the frame rate is 30 fps (frames per second), while Windows is normally 60 fps. Before you can wire the code in the methods Update and Draw must inform the runtime that we want to use the image above in addition to our project "Content". Since RootDirectory has already been set, you simply define a variable to hold the Texture and assign it into the method LoadContent : This method is called only once at the start of the game and gives us a central place to load resources. You use the generic method Load<T> Item Content that receives the Asset Name in the game to load the resource. In our case, T must be set to Texture2D in when the image included in the project Content is a two-dimensional texture.
Reply With Quote
  #4  
Old 30-11-2010
Member
 
Join Date: Feb 2010
Posts: 658
Re: Game loop problem with XNA

To do this you need to define a variable in the class and value in the method game1 LoadContent as seen in the following code, the default method that has created the template for the project, to which we added a few lines:
Code:
Texture2D logoTexture;

         / / / 
         / / / LoadContent will be called ounces per game and is the position to load
         / / / All of your content. 
         / / / 
         protected override void LoadContent ()
         {
             / / Create a new SpriteBatch, Which can be used to draw textures.
             SpriteBatch = new SpriteBatch (GraphicsDevice);

             // TODO: use this.Content to load your game content
			
			 / / [Addition] 
             logoTexture this.Content.Load <Texture2D> = ("logo");
         }
The primary step, parting aside for now the logical algorithms, is to illustrate the image, the interface design is done from the Draw method.
Reply With Quote
  #5  
Old 30-11-2010
Member
 
Join Date: Feb 2010
Posts: 669
Re: Game loop problem with XNA

With the proposed code from Visual Studio, which automatically initializes the object SpriteBatch, class of XNA that is committed to draw the sprites on the screen, we can directly use this instance to draw the image. The method Draw class SpriteBatch must always be between a call to Begin and a call to the End : This technique allows you to group several operations into one transaction to design video. The code to draw our image on the screen, as Texture2D loaded in the Load method is as follows:
Code:
spriteBatch.Begin ();
             spriteBatch.Draw (logoTexture, new Vector2 (0, 0), Color.White);
             spriteBatch.End ();
Running our application with the classic F5 , you will get the emulator that will present our image in the upper left, as we indicated in the preceding code using the class Vector2 , a two-dimensional vector, which contains the values for 'X and Y axis (by default, the location of image is calculated by taking as a reference point in the upper left corner of the image).
Reply With Quote
  #6  
Old 30-11-2010
Member
 
Join Date: Feb 2010
Posts: 546
Re: Game loop problem with XNA

The coordinates XNA on the phone, by default, based on the position Left Landscape, or with the device oriented horizontally to the left starting from the vertical position. Then rotate the emulator using the appropriate button to check out. Enrich the simple example of trying to move the image diagonally down: for it will be necessary that the method Draw has two coordinate variables in each cycle. So we define a variable within the class Game1 , type Vector2 that every call in the loop of the method Update is increased by 1 position. You must define a variable outside the method, since, as we now understand, there are two methods that work in the loop: Update to the application logic and Draw for the design of the interface.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Game loop problem with XNA"
Thread Thread Starter Forum Replies Last Post
loop Pedals in Rocksmith Game Doshti Video Games 6 24-10-2011 03:06 PM
Stuck in Case failed loop in L.A. Noire Game Cherilyn Video Games 9 03-10-2011 06:25 PM
How to check Bingo game numbers in loop Afznotermi Software Development 3 29-10-2009 02:19 PM
Problem of infinite loop in C Firon Software Development 4 11-05-2009 06:12 PM
Problem with a foreach loop in PHP JiJi Software Development 2 20-11-2008 04:57 PM


All times are GMT +5.5. The time now is 08:52 PM.