My Game--File read data

Source: Internet
Author: User

My Game-segment Data In the context of the drawing from the Bezier curve generated line segments, with Drawnode to draw polygons, while a background has two mountains, a mountain has more than two Bezier curve saved, with nested data class:Bezier,linelayer,bglayerdata This is also done to facilitate reading data from a file

Put the background data in the file for easy modification, you can also do a tool to quickly draw the background, the following is the background of the data saved XML file:

1 <Rootlayer>2     <Layers>3         <Layer>4             <Beziers>5                 <BézierBX= "0.375" by= "0.000"ex= "1.000"ey= "0.500"c1x= "0.750"c1y= "1.000"c2x= "0.850"c2y= "0.875"/>6                 <BézierBX= "0.375" by= "0.500"ex= "1.000"ey= "0.650"c1x= "1.000"c1y= "0.400"c2x= "0.750"c2y= "0.750"/>7                 <BézierBX= "0.375" by= "0.125"ex= "1.000"ey= "0.375"c1x= "0.800"c1y= "0.130"c2x= "0.700"c2y= "0.375"/>8             </Beziers>9             <Colors>Ten                 <ColorR= "0.200"g= "0.439"b= "0.200"a= "1"/> One                 <ColorR= "0.196"g= "0.529"b= "0.251"a= "1"/> A                 <ColorR= "0.133"g= "0.675"b= "0.220"a= "1"/> -             </Colors> -         </Layer> the         <Layer> -             <Beziers> -                 <BézierBX= "0.000" by= "0.125"ex= "0.750"ey= "0.000"c1x= "0.250"c1y= "0.750"c2x= "0.375"c2y= "0.750"/> -                 <BézierBX= "0.000" by= "0.250"ex= "0.750"ey= "0.500"c1x= "0.375"c1y= "0.250"c2x= "0.375"c2y= "0.500"/> +             </Beziers> -             <Colors> +                 <ColorR= "0.255"g= "0.647"b= "0.310"a= "1"/> A                 <ColorR= "0.000"g= "0.600"b= "0.267"a= "1"/> at             </Colors> -         </Layer> -     </Layers> - </Rootlayer>
View Code

Two sets of elements under each Layer : Bezier curves and corresponding colors

Resources under the Resourcesmanage class to read the contents of the file, and to survive the corresponding data structure stored, easy to use

Code to read the data:

Bglayerdata * Resourcesmanage::getbglayerdatafromfile (std::string && fileName) {tinyxml2::xmldocument doc; Doc. LoadFile (Filename.c_str ()); auto Elemroot = doc. RootElement (); Auto layers = elemroot->firstchildelement ("layers"); auto Elemlayer = Layers->firstchildelement () ; while (Elemlayer! = 0) {//auto elem = elemlayer->firstchildelement (); auto Bézier = Elemlayer->firstchildelement (" Beziers "); auto BZ = Bezier->firstchildelement (); Auto color = elemlayer->firstchildelement (" Colors "); auto CO = Co Lor->firstchildelement (); auto layer = new Linelayer (); while (BZ! = 0 && Co! = 0) {Float bx = atof (Bz->att Ribute ("BX")); float by = Atof (Bz->attribute ("by")), float ex = Atof (Bz->attribute ("Ex")), float ey = atof ( Bz->attribute ("ey")), float c1x = atof (Bz->attribute ("c1x")), float c1y = atof (Bz->attribute ("c1y")); Flo At c2x = atof (Bz->attribute ("c2x")), float c2y = atof (Bz->attribute ("c2y")), float r = atOf (Co->attribute ("R")); float g = atof (Co->attribute ("G")); float B = atof (Co->attribute ("B")); float A = Atof (Co->attribute ("a")); Layer->addbezier (BX, by, ex, EY, C1x, c1y, C2X, c2y, R, G, B, a); BZ = Bz->nextsi Blingelement (); co = co->nextsiblingelement ();} if (!layer->isempty ()) {_bglayerdata->addlinelayer (layer);} Elemlayer = Elemlayer->nextsiblingelement ();} return _bglayerdata;}

Use tinyxml2 's XMLDocument to read the file and extract the data from the read content. First find the root node, and then find the first "Layers" node (that is, because there is only one background),"Layers" node corresponds to Bglayerdata class; Next find the first child node " Layer "," layer "corresponds to the Linelayer class, starts looping, loops through" layer " and gets " Bézier " and " color "created and added to Linelayer ; the loop increment condition is that the current node is assigned the next node, and the loop ends when the node is 0 . The data is extracted by a double loop and the next step can be used.

The Resourcesmanage class originally envisaged that there would be many tasks, but at present only use the above function, the other first put there, for the future to make a reference.

My Game--File read data

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.