1 #include <iostream>
2 #include <fstream>
3 #include "lvlPrevScene.h"
4 #include <QLineF>
5 #include <QVector>
6 
7 struct pos_s {
8   qreal x,y;
9   float color[3];
10   bool collision;
11 };
12 
13 typedef struct pos_s pos_t;
14 
15 using namespace std;
lvlPrevScene()16 lvlPrevScene::lvlPrevScene()
17 {
18   setBackgroundBrush(Qt::black);
19 }
20 
load(const char * file)21 void lvlPrevScene::load(const char* file)
22 {
23   string line;
24   ifstream load;
25 
26   load.open(file);
27 
28   if(!load.is_open())
29   {
30     cout << "could not load '" << file << "'" << endl;
31     return;
32   }
33 
34   int parseState=0, dataNum=0;
35 
36  pos_t l;
37  QVector<pos_t> tp; //temp poly
38  QVector< QVector<pos_t> > map;
39  clear();
40 
41   while(!load.eof())
42   {
43     getline(load, line);
44     if(line == "StartPoly")
45     {
46       parseState=1;
47     } else if(line=="EndPoly")
48     {
49       parseState=0;
50       map.push_back( tp );
51       tp.clear();
52     } else if(parseState==1)
53     {
54       if(line == "StartVert")
55       {
56         parseState=2;
57         dataNum=0;
58       }
59     } else if(parseState==2)
60     {
61       if(line == "EndVert")
62       {
63         tp.push_back( l );
64         parseState=1;
65       } else {
66         dataNum++;
67         switch(dataNum)
68         {
69           case 1:
70             l.x = atof(line.data());
71           break;
72           case 2:
73             l.y = atof(line.data());
74           break;
75           case 3:
76           l.color[0] = atof(line.data());
77           break;
78           case 4:
79           l.color[1] = atof(line.data());
80           break;
81           case 5:
82           l.color[2] = atof(line.data());
83           break;
84           case 6:
85           l.collision = atoi(line.data());
86           break;
87         }
88       }
89     }
90   } //while
91 
92   load.close();
93 
94   qreal x,y;
95   float c=0;
96   bool start;
97   QPen pen(0xff00ff00);
98 
99   for( QVector< QVector<pos_t> >::iterator it = map.begin() ;  it < map.end() ; ++ it)
100   {
101     //poly
102     start=1;
103 
104     c=0;
105     for( QVector<pos_t>::iterator pit = it->begin() ; pit < it->end() ; ++ pit)
106     {
107         //Add polys
108         if(start)
109         {
110           start=0;
111         } else {
112           if( c !=-1 && pit->color[0] != -1 )
113           {
114             addLine(x,y,pit->x,pit->y,pen);
115           }
116         }
117         x=pit->x;
118         y=pit->y;
119         c=pit->color[0];
120 
121     }
122   }
123 
124 }
125 
126