XNa getting started tutorial -- (1)

Source: Internet
Author: User
Document directory
  • Preface:
  • First xNa Program
  • Insert a sentence
  • Insert a sentence
  • Please note that in an xNa game, avoid using multiple spritebatch and draw all images between the beginning and end of spritebatch.
  • Series list
Preface:

A few days ago, I saw the cool on cnblogs using Silverlight/WPF to write a game. I tried to write a game as well.

As we all know, although MS has provided isolated storage support since silverlight2, it is obviously not enough space for a 1 M game. In other words, resource scheduling relies heavily on servers and networks for games in Silverlight.

Secondly, since the rendering of WPF is based on DirectX, winform is more suitable for transformation and refresh in form than winform In the Win32 age, but it is not a non-game framework after all, in essence, there is a lack of source support for interface message loops. Although this is simplified for Windows apps, it may not work for games.

By accident, we found Ms's xNa, a game development framework for DOTNET enthusiasts.

 

What is xNa:

XNa framework is built on. NET Framework 2.0. In addition, it has added some category libraries dedicated to game development to maximize code reuse on the specified platform. XNa framework runs on a Common Language Runtime version especially for managed games. This execution layer supports Windows XP, Windows Vista, and Xbox 360. Because the game is developed on the execution layer, the game can run on all platforms that support xNa framework with little or no changes required. Currently, all games on the xNa framework must be developed using C # And xNa game studio express IDE.

XNa framework includes all the low-level technologies used for game programming. As a result, game developers can focus on game content development without having to worry about porting games to different platforms, as long as the game is developed on the xNa platform, all hardware can run as long as it supports xNa. XNa framework also has some built-in tools, such as xact, to help develop game content. These tools can also help develop visual and auditory effects and model creation with high image fidelity.

XNa framework supports both 2D and 3D Game Development and the Controller and vibration effects of the Xbox 360. The Xbox Live store can upgrade the developer's xNa game studio express so that the game they developed can be used on the Xbox 360.

Because xNa is Based on. NET 2.0, its development efficiency is much faster than C ++'s direct call to the DirectX API. It is a good thing for me to wait for amateur game developers.

 

 

XNa problems:

Now the xNa version has been upgraded to 3.1, and video support has been added, and must be installed in vs2008 or its express version. (At least theoretically, my local vs2008 + 2010 + win7 has been automatically rolled back after repeated installation. It is still unsatisfactory to ask a foreigner, and I can only install express for xNa)

Second, there are very few xNa resources in China, which are heavily dependent on relevant English references. We suggest you go to learning xNa 3.0-xNa 3.0 game development for the PC, Xbox 360, and Zune; xNa 3.0 game programming recipes-a problem-solution approach and beginning xNa 3.0 game programming-from novice to Professional Books, I have read very well.

It seems that many people in China are translating or partially translating such text.

Download xNa

 

First xNa Program

After the xNa framework is installed, an xNa Project template is displayed in vs. We create an xNa project named windowsgame1.

After compilation, the blue sky and white clouds appear
 

Now, we are trying to add some elements to the blue sky and white clouds.

Insert a sentence

Friends who are used to Windows programming are often used to listening or triggering events. In fact, Windows itself is based on message queues, and events are constantly listening to messages and processing them accordingly. Unfamiliar users can reload wndproc in DOTNET winform.

In each game, there is a message loop, that is, the cycle of constantly refreshing the computing and control interfaces. XNa is no exception. Microsoft. xNa. Framework. Game and the gamecomponent we mentioned later all have their own message loops.

Where,

Initialize is used to initialize some objects in the game interface.

Loadcontent is used to load Resources in the game, such as files and sounds.

Then, update and draw start the message loop in the game. The former is mainly used for related computing, and the latter is used for drawing.

The default update rate of xNa is 60fps (frame/second)

 

First, add an existing image Old English sheepdog.png under content directory and change its content importer and processor to texture-xNa framework.

Note that the xNa source API does not support GIF. If you need to import the file, you must implement the importer and processor.

Insert a sentence

Mircosoft. xNa. Framework. content. pipeline is a specialized pipeline for processing game resources. processing and compilation of images, sounds, 3D resources, and XML are all processed and compiled under it.

After compilation, any resource will be compiled into. xNb (xNa binary file ). Please note that although xNa can share windows and Xbox, its resource file compilation process is different and is not universal and must be re-compiled.

 

Next, after the image is imported, we can continue with loadcontent, update, and draw.

 

// Create an IMG variable to store images
Texture2d IMG;

 

// Add in loadcontent
This. IMG = This. content. Load <texture2d> ("Old English sheepdog ");

 

"Old English sheepdog" is the name attribute of the image just loaded.

In this way, we can directly draw this image for each refresh of xNa in the draw method.

Code Protected override void draw (gametime)
{
Graphicsdevice. Clear (color. cornflowerblue );
Spritebatch. Begin ();
Spritebatch. Draw (IMG, new rectangle (0, 0, 50, 50), color. White );
Spritebatch. End ();
Base. Draw (gametime );
}

Briefly explain the above Code,

Graphicsdevice. Clear (color. cornflowerblue); used to erase the image on the current screen

Spritebatch is usually used to draw Sprite, that is, Sprite.

Please note that in an xNa game, avoid using multiple spritebatch and draw all images between the beginning and end of spritebatch.

Spritebatch. Draw has multiple overload values. For details, refer

 

In this way, our first image is born.

 

You can see it carefully. There is no mouse or cursor in the game interface. It's so easy to draw one by yourself.

// Declare the global variable P
Point P;
// Add the update Method
Mousestate MS = mouse. getstate ();
P = new point (Ms. X, ms. y );
// Modify the draw Method
Spritebatch. Draw (IMG, new rectangle (P. X, p. Y, 50, 50), color. White );

 

In this way, the cursor changes to the current image.

Of course, if you only need to use the Windows mouse cursor as the game cursor by default, you do not need to be so troublesome.

// In initialize
This. ismousevisible = true;

You can.

 

In this way, the first xNa program is completed.

Source code

To be continue ....


Series list

 


  1. XNa tutorial ---- (1)


  2. XNa tutorial ---- (2) mobile control


  3. XNa tutorial ---- (3) simple animation

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.