Jump to content
  • Advertisement
Sign in to follow this  
flodywan

Why isn't xna game explicitly looping?

This topic is 2720 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have just a basic xna project. Why doesn't there seem to be an explicit game loop? Does xna take care of that for you? If so, where is the beginning of the loop, and where is the end? Where's a good resource that explains these type of things?


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;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace WindowsGame1
{


public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D backgroundTexture;
Texture2D foregroundTexture;
GraphicsDevice device;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}

/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected override void Initialize()
{
graphics.PreferredBackBufferWidth = 500;
graphics.PreferredBackBufferHeight = 500;
graphics.IsFullScreen = false;
Window.Title = "Alex's Game";
graphics.ApplyChanges();


base.Initialize();
}

/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
device = graphics.GraphicsDevice;
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);

backgroundTexture = Content.Load<Texture2D>("Clouds");
foregroundTexture = Content.Load<Texture2D>("Mountains");

// TODO: use this.Content to load your game content here
}

/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}

/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();

// TODO: Add your update logic here
base.Update(gameTime);
}

/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.LightBlue);

spriteBatch.Begin();
DrawScenery();
spriteBatch.End();

// TODO: Add your drawing code here

base.Draw(gameTime);
}

private void DrawScenery()
{
Rectangle screenRectangle = new Rectangle(0, 0, 500, 500);
spriteBatch.Draw(backgroundTexture, screenRectangle, Color.White);
spriteBatch.Draw(foregroundTexture, screenRectangle, Color.White);

}
}
}





Share this post


Link to post
Share on other sites
Advertisement
Your class Game1 derives from/extends the Microsoft.Xna.Framework.Game class. The Game class handles the loop and calls Update and Draw for you at the required times. It also handles other things that you shouldn't have to worry about, like back buffering, timing, and handling lost resources.

So just put the main loop of your game in the update function. All your drawing logic will go in the Draw function as well.

Share this post


Link to post
Share on other sites
XNA takes care of that for you; there's a lot of stuff hidden inside Game and its component classes. Your update/draw overrides are a part of the overall loop.

If you've ever used SlimDX (or look at its reference documentation - it's an alternative library for more general DirectX usage compared to XNA), you'll find your answer. (Good explanation under "Message Loop").

Here's a snippet (from my own code):

 
public event EventHandler Idle;

public void Run(Form gameForm) {
Application.Idle += new EventHandler(ApplicationIdle);
Application.Run(gameForm);
}

private void ApplicationIdle(object sender, EventArgs e) {
Tesla.Direct3D10.Util.Message msg;
while(!NativeMethods.PeekMessage(out msg, IntPtr.Zero, 0, 0, 0)) {
if(Idle != null) {
Idle(this, EventArgs.Empty);
}
}
}





The main loop is inside the delegate that gets called when the application is idle. Actual update/render calls can be in a separate function that hooks into the Idle event. I believe the XNA Game class does something similar, since Game is separate from the actual underlying Form.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!