C # development Wpf/silverlight animation and games series Tutorials (Game Course): (18)

Source: Internet
Author: User
Tags array example relative tostring silverlight


C # development Wpf/silverlight animation and games series (Game Course): (18) The slick of the Perfect Elf (WPF only) ②



In the last section, you first have to explain why you need to combine these 272 pictures into a larger picture. Because if the game also has equipment, mounts and other settings, then we need to the image source of the synthesis at all times, at the same time to 272 or even more pictures to synthesize the high efficiency or 2 large image synthesis efficiency this is obvious. In this example, the protagonist consists of two parts of the body (clothing) and the weapon; Therefore, we also need to define a jagged array to hold the loaded role equipment compositing into memory:



<summary>



Role Picture Caching



Jagged Array Example Partimage[a,b][c,d]



A For the clothing code (in this case can also be understood as body code, because the change of clothes is equivalent to the role of the body)



B is the weapon code.



C For Character Orientation code



D represents the number of columns in the consolidation diagram of the current action frame



In this case, 1-5 is standing, 6-13 is running, 14-20 is attacking, 21-26 is cast, and 27-34 is dead.



In this case partimage = new bitmapsource[10, 20][,]; there are 10 roles in the initial setting, 20 kinds of weapons



</summary>



public static bitmapsource[,][,] Partimage = new bitmapsource[10, 20][,];



For example, partimage[0,6] is the role of number No. 0 holding the 6th weapon in 8 directions. All Action Frame picture source



PARTIMAGE[4,0] Represents the role 4th empty start 8 direction all action frame picture source



...... So



If you have hats and mounts in your game, you will need to bitmapsource[,][,,,] Partimage This defines the second group as a 4-D array.



...... So



Of course, you can also use Hashtable (hash table), Dictionary (dictionary), etc. to replace partimage[,][,]. But in the way that numeric type keys are saved with object values, I prefer jagged arrays because it's clearer, more elegant, and more efficient.



With the role of the carrier, the following is how to the previous section of the synthesis of the role of large maps and weapons large map (extraction and synthesis of the same as the same section), the final frame storage into the partimage. Hey, and now the essence:


<summary>
Assemble roles + equipment to cut into series frame pictures and save into memory (equip role)
</summary>
<param name= "Equipment" > Equipment Code Array </param>
<param name= "RowNum" > Frame synthesis Chart Line </param>
<param name= "Colnum" > Frame number </param>
Total width </param> of <param name= "totalwidth" > Frame map synthesis
<param name= "Totalheight" > Frame map after synthesis of total high </param>
<param name= "Singlewidth" > Single frame width </param>
<param name= "Singleheight" > Single frame graph height </param>
<returns> returns the synthesized picture source </returns> If there is read cache in the cache.
public static bitmapsource[,] Equippart (int[] equipment, int rownum, int colnum, int totalwidth, int totalheight, int sing lewidth, int singleheight) {
Equipment[0] for the clothing code, EQUIPMENT[1] for the weapon code, this case is equipped with only clothing + weapons
If the device is not in memory the role of ready-made picture source is read
if (Partimage[equipment[0], equipment[1]] = = null) {
bitmapsource[,] bitmap = new bitmapsource[rownum, Colnum];
Load role clothes (body) big picture
BitmapSource BitmapSource = new BitmapImage (The new Uri (@ "images\body" + equipment[0]. ToString () + ". gif", urikind.relative);
If the weapon is not 0, that is, if the character has a weapon on it, not empty hands.
if (Equipment[1]!= 0) {
Loaded with large drawings of weapons, and assembled with large drawings of clothes
BitmapSource BitmapSource1 = new BitmapImage (The new Uri (@ "Images\weapon" + equipment[1]. ToString () + ". gif", urikind.relative);
DrawingVisual drawingvisual = new DrawingVisual ();
Rect Rect = new Rect (0, 0, totalwidth, totalheight);
DrawingContext DrawingContext = Drawingvisual.renderopen ();
Drawingcontext.drawimage (BitmapSource, rect);
Drawingcontext.drawimage (BitmapSource1, rect);
Drawingcontext.close ();
RenderTargetBitmap RenderTargetBitmap = new RenderTargetBitmap (totalwidth, totalheight, 0, 0, PixelFormats.Pbgra32);
Rendertargetbitmap.render (drawingvisual);
BitmapSource = RenderTargetBitmap;
Reduces picture quality to improve system performance (because it is already a low quality GIF type, so it doesn't work)
Renderoptions.setbitmapscalingmode (BitmapSource, bitmapscalingmode.lowquality);
}
for (int i = 0; i < rownum; i++) {
for (int j = 0; J < Colnum; J + +) {
Bitmap[i, j] = new Croppedbitmap (BitmapSource, New Int32Rect (J * singlewidth, I * singleheight, singlewidth, SingleHeight) );
}
}
Put the equipment synthesis diagram into memory
Partimage[equipment[0], equipment[1]] = bitmap;
return bitmap;
} else {
If the role picture source of the device already exists in memory returns the composite diagram from memory, greatly improving performance
Return partimage[equipment[0], equipment[1]];
}
}




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.