fork download
  1. #include "Rectangle.hpp"
  2. #include "Engine.hpp"
  3. #include "Line.hpp"
  4. #include "Image.hpp"
  5. #include <unistd.h>
  6. #include "Scene.hpp"
  7. #include "Text.hpp"
  8. #include "Timer.hpp"
  9. #include <time.h>
  10. #include "ResourcesManager.hpp"
  11. #include <string>
  12. #include <stdlib.h>
  13. #include "VertexBuffer.hpp"
  14. #include "GLState.hpp"
  15. #include "Scissors.hpp"
  16. #include <kst/Logger.hpp>
  17. #include "VertexBuffer.hpp"
  18. #include <set>
  19. #include <vector>
  20. #include <SDL/SDL.h>
  21. #include "kst/File.hpp"
  22.  
  23. using namespace glider;
  24.  
  25. const int CELL_SIZE=15;
  26.  
  27. struct AngleRange{
  28. int sa,ea;
  29. bool closed;
  30. static const int SCALED_PI_2 =90000000;
  31. static const int SCALED_PI =180000000;
  32. static const int SCALED_PIx2=360000000;
  33. AngleRange(int argSa=0,int argEa=0):sa(argSa),ea(argEa),closed(false){}
  34. void setSa(int argSa)
  35. {
  36. if(!closed && ((sa<ea && argSa>ea)||(sa>ea && argSa<sa && argSa>ea)))closed=true;
  37. sa=argSa;
  38. }
  39. void setEa(int argEa)
  40. {
  41. if(!closed && ((sa<ea && sa>argEa)||(sa>ea && argEa>ea && argEa<sa)))closed=true;
  42. ea=argEa;
  43. }
  44. bool operator<(const AngleRange& other)const
  45. {
  46. return ea<other.ea;
  47. }
  48. double saScaled()
  49. {
  50. return sa/1000000.0;
  51. }
  52. double eaScaled()
  53. {
  54. return ea/1000000.0;
  55. }
  56. int middle()
  57. {
  58. if(sa<ea)return (sa+ea)/2;
  59. return (sa+ea+SCALED_PIx2)%SCALED_PIx2;
  60. }
  61. void shrinkCorners(int saPrc,int eaPrc)
  62. {
  63. if(sa>ea)
  64. {
  65. ea+=AngleRange::SCALED_PIx2;
  66. }
  67. uint64_t mid=middle();
  68. uint64_t val=mid-sa;
  69. val*=saPrc;
  70. val/=100;
  71. sa=(mid-val)%AngleRange::SCALED_PIx2;
  72. val=ea-mid;
  73. val*=eaPrc;
  74. val/=100;
  75. ea=(mid+val)%AngleRange::SCALED_PIx2;
  76. }
  77. int diff()
  78. {
  79. if(sa<ea)return ea-sa;
  80. return (ea+SCALED_PIx2-sa)%SCALED_PIx2;
  81. }
  82. bool inRange(int a)
  83. {
  84. if(closed)return false;
  85. if(sa<ea)return a>=sa && a<=ea;
  86. return a>=sa || a<=ea;
  87. }
  88. void mirror45()
  89. {
  90. sa=(SCALED_PI*2+SCALED_PI/2-sa)%(SCALED_PI*2);
  91. ea=(SCALED_PI*2+SCALED_PI/2-ea)%(SCALED_PI*2);
  92. std::swap(sa,ea);
  93. }
  94. void mirror90()
  95. {
  96. sa=(SCALED_PI*3-sa)%(SCALED_PI*2);
  97. ea=(SCALED_PI*3-ea)%(SCALED_PI*2);
  98. std::swap(sa,ea);
  99. }
  100. void mirror180()
  101. {
  102. sa=(SCALED_PI*4-sa)%(SCALED_PI*2);
  103. ea=(SCALED_PI*4-ea)%(SCALED_PI*2);
  104. std::swap(sa,ea);
  105. }
  106. };
  107.  
  108. struct FovCell{
  109. AngleRange r;
  110. uint8_t sac,eac;
  111. uint8_t o1,o2;
  112. Posi<int> p;
  113. bool tempBlock;
  114. bool delayBlock;
  115. int range;
  116. bool operator<(const FovCell& other)const
  117. {
  118. return r<other.r;
  119. }
  120. void swapp()
  121. {
  122. std::swap(p.x,p.y);
  123. }
  124. void swapc()
  125. {
  126. std::swap(sac,eac);
  127. }
  128. };
  129.  
  130.  
  131. struct Cell{
  132. bool block;
  133. bool vis;
  134. bool tempBlock;
  135. bool delayBlock;
  136. int corners[4];
  137. FovCell fc;
  138. int r;
  139. std::string log;
  140. };
  141.  
  142. typedef std::vector<Cell> Row;
  143. typedef std::vector<Row> Matrix;
  144.  
  145. /*const char* testmap[]={
  146. "########################################",
  147. "#......................................#",
  148. "#......................................#",
  149. "#........................#########.....#",
  150. "#......########..........#.......#.....#",
  151. "#......#......############.......#.....#",
  152. "#......#.........................|.....#",
  153. "#......#......###########........|.....#",
  154. "#......###--###.........#........#.....#",
  155. "#.......................###.######.....#",
  156. "#.........................#.#..........#",
  157. "#......................####.####.......#",
  158. "#.......#.#.#.#.#......................#",
  159. "#........#.#.#.#.......#########.......#",
  160. "#.......#.#.#.#.#......................#",
  161. "#........#.#.#.#.......................#",
  162. "#...........................#..........#",
  163. "#.......#...................#..........#",
  164. "#...........................###........#",
  165. "#....#.................................#",
  166. "#.............#........................#",
  167. "#....................#.................#",
  168. "#..#......#.........#..................#",
  169. "#............###.####..................#",
  170. "#.......#...........#.........##.......#",
  171. "#............###.####.........##.......#",
  172. "#......#.......#.#.....................#",
  173. "#..............###.....................#",
  174. "#......................................#",
  175. "#......................................#",
  176. "#......................................#",
  177. "#.........*****........##--##..........#",
  178. "#....**.......*........#....#..........#",
  179. "#.............*........|....|..........#",
  180. "#.............*........|....|..........#",
  181. "#.............*........#....#..........#",
  182. "#......................##..##..........#",
  183. "#......................................#",
  184. "#......................................#",
  185. "########################################",
  186. };*/
  187.  
  188.  
  189.  
  190.  
  191. int px;
  192. int py;
  193. int sightRadius=15;
  194. VertexBuffer* vb;
  195. Line* lsa;
  196. Line* lea;
  197. Text* info;
  198.  
  199. void drawMap(Matrix& m)
  200. {
  201. int H=m.size();
  202. int W=m[0].size();
  203. Color clr;
  204. ClrVector& c=vb->getCBuf();
  205. for(int y=0;y<H;++y)
  206. {
  207. for(int x=0;x<W;++x)
  208. {
  209. if(m[y][x].block)clr=Color::white;else clr=Color::blue;
  210. if(x==px && y==py)clr=Color::red;
  211. if(m[y][x].tempBlock)
  212. {
  213. clr=Color::green;
  214. }
  215. if(m[y][x].delayBlock)
  216. {
  217. clr=Color::gray;
  218. }
  219. if(!m[y][x].vis)
  220. {
  221. clr=clr/2;
  222. }
  223. for(int i=0;i<4;++i)
  224. {
  225. c[(x+y*W)*4+i]=clr;
  226. }
  227. }
  228. }
  229. vb->update();
  230. }
  231.  
  232. namespace kst{
  233. void customformat(kst::FormatBuffer& buf,double v,int w,int p)
  234. {
  235. char buf2[64];
  236. size_t len=sprintf(buf2,"%*.*lf",w,p,v);
  237. buf.Append(buf2,len);
  238. }
  239. }
  240.  
  241. class CircularFov{
  242. public:
  243.  
  244. CircularFov():debug(true)
  245. {
  246. }
  247.  
  248. static int getAngle(double x,double y,double quadrant)
  249. {
  250. return (atan(y/x)+quadrant)*(AngleRange::SCALED_PI_2)/M_PI_2;
  251. }
  252.  
  253. void prepare(int maxRadius)
  254. {
  255. if(fov.empty())fov.push_back(Ring());
  256. for(int r=fov.size();r<=maxRadius;++r)
  257. {
  258. fov.push_back(Ring());
  259. Ring& ring=fov.back();
  260. //double rr=r*r;//(r+0.5)*(r+0.5);
  261. for(int y=0;y<=r;++y)
  262. {
  263. for(int x=y;x<=r;++x)
  264. {
  265. if(x==0 && y==0)continue;
  266. double dx=x;//-0.5;
  267. double dy=y;//-0.5;
  268. if(floor(sqrt(dx*dx+dy*dy))<=r)
  269. {
  270. Pos p(x,y);
  271. if(processed.find(p)!=processed.end())continue;
  272. processed.insert(p);
  273. int sa,ea,sac,eac,o1,o2;
  274. /*if(x==0)
  275.   {
  276.   sa=getAngle(0.5,y-0.5,0);
  277.   ea=getAngle(-0.5,y-0.5,M_PI_2);
  278.   }else
  279.   {*/
  280. if(y==0)
  281. {
  282. sa=getAngle(x-0.5,-0.5,M_PI*2);
  283. ea=getAngle(x-0.5,0.5,0)-1;
  284. sac=3;
  285. eac=0;
  286. o1=0;
  287. o2=7;
  288. }else if(x==y)
  289. {
  290. sa=getAngle(x+0.5,y-0.5,0);
  291. ea=getAngle(x-0.5,y+0.5,0);
  292. sac=2;
  293. eac=0;
  294. o1=0;
  295. o2=1;
  296. }else
  297. {
  298. sa=getAngle(x+0.5,y-0.5,0);
  299. ea=getAngle(x-0.5,y+0.5,0);
  300. sac=2;
  301. eac=0;
  302. o1=o2=0;
  303. }
  304. //}
  305. ring.push_back(FovCell());
  306. FovCell& fc=ring.back();
  307. fc.r.sa=sa+5;
  308. fc.r.ea=ea-5;
  309. fc.sac=sac;
  310. fc.eac=eac;
  311. fc.o1=o1;
  312. fc.o2=o2;
  313. fc.p=p;
  314. }
  315. }
  316. }
  317. size_t end=ring.size();
  318. for(size_t i=0;i<end;++i)
  319. {
  320. FovCell fc=ring[i];
  321. if(fc.p.x==fc.p.y)continue;
  322. fc.swapp();
  323. fc.r.mirror45();
  324. fc.sac=fc.sac==0?2:fc.sac==2?0:fc.sac;
  325. fc.eac=fc.eac==0?2:fc.eac==2?0:fc.eac;
  326. fc.swapc();
  327. fc.o1=1;
  328. fc.o2=fc.o2==7?2:1;
  329. ring.push_back(fc);
  330. }
  331. end=ring.size();
  332. int ct[4]={1,0,3,2};
  333. for(size_t i=0;i<end;++i)
  334. {
  335. FovCell fc=ring[i];
  336. if(fc.p.x==0)continue;
  337. fc.p.x=-fc.p.x;
  338. fc.r.mirror90();
  339. fc.sac=ct[fc.sac];
  340. fc.eac=ct[fc.eac];
  341. fc.swapc();
  342. fc.o1=fc.o1==0?3:2;
  343. fc.o2=fc.o2==7?4:fc.o2==0?3:2;
  344. ring.push_back(fc);
  345. }
  346. end=ring.size();
  347. int ct2[4]={3,2,1,0};
  348. for(size_t i=0;i<end;++i)
  349. {
  350. FovCell fc=ring[i];
  351. if(fc.p.y==0)continue;
  352. fc.p.y=-fc.p.y;
  353. fc.r.mirror180();
  354. fc.sac=ct2[fc.sac];
  355. fc.eac=ct2[fc.eac];
  356. fc.swapc();
  357. fc.o1=7-fc.o1;
  358. fc.o2=7-fc.o2;
  359. ring.push_back(fc);
  360. }
  361. //std::sort(ring.begin(),ring.end());
  362. }
  363. }
  364.  
  365. void generateFov(Matrix& m,int maxRadius)
  366. {
  367. if(fov.size()<maxRadius+1)
  368. {
  369. prepare(maxRadius);
  370. }
  371. int H=m.size();
  372. int W=m[0].size();
  373. for(int y=0;y<H;++y)
  374. {
  375. for(int x=0;x<W;++x)
  376. {
  377. m[y][x].vis=false;
  378. }
  379. }
  380. for(int i=0;i<8;++i)b[i].clear();
  381. for(int ri=1;ri<=maxRadius;++ri)
  382. {
  383. Ring& ring=fov[ri];
  384. for(Ring::iterator it=ring.begin(),end=ring.end();it!=end;++it)
  385. {
  386. FovCell fc=*it;
  387. int x=px+fc.p.x;
  388. int y=py+fc.p.y;
  389. if(x<0 || x>=W || y<0 || y>=H)continue;
  390. Cell& c=m[y][x];
  391. fc.delayBlock=c.delayBlock;
  392. fc.tempBlock=c.tempBlock;
  393. fc.range=ri+2;
  394. c.fc=fc;
  395. c.r=ri;
  396. c.log="";
  397. AngleRange r=fc.r;
  398. bool totalBlock=false;
  399. bool saBlock;
  400. int bso=-1,bsi=-1;
  401. int beo=-1,bei=-1;
  402. bool usedTempBlock=false;
  403. for(int k=0;k<2;++k)
  404. {
  405. int o=fc.o1;
  406. if(k==1)
  407. {
  408. if(fc.o1==fc.o2)break;
  409. o=fc.o2;
  410. }
  411.  
  412. for(size_t i=0;i<b[o].size();++i)
  413. {
  414. FovCell bc=b[o][i];
  415. if(bc.delayBlock && bc.range>=ri)continue;
  416. if(bc.tempBlock && bc.range<ri)continue;
  417. saBlock=false;
  418. if(!c.block && r.inRange(bc.r.sa) && r.inRange(bc.r.ea))
  419. {
  420. if(debug)
  421. {
  422. c.log+=FORMAT("sa=%{:,3},ea=%{:,3} totally blocked by %d,%d:%{:,3},%{:,3}\n",r.saScaled(),r.eaScaled(),px+bc.p.x,py+bc.p.y,bc.r.saScaled(),bc.r.eaScaled());;
  423. }
  424. totalBlock=true;
  425. usedTempBlock=usedTempBlock || bc.tempBlock;
  426. break;
  427. }
  428. if(bc.r.inRange(r.sa)/* && bc.ea>sa*/)
  429. {
  430. if(debug)
  431. {
  432. c.log+=FORMAT("sa=%{:,3} blocked by %d,%d:%{:,3},%{:,3}\n",r.saScaled(),px+bc.p.x,py+bc.p.y,bc.r.saScaled(),bc.r.eaScaled());
  433. }
  434. r.setSa(bc.r.ea);
  435. saBlock=true;
  436. bso=o;bsi=i;
  437. usedTempBlock=usedTempBlock || bc.tempBlock;
  438. }
  439. if(bc.r.inRange(r.ea)/* && bc.sa<ea*/)
  440. {
  441. if(debug)
  442. {
  443. c.log+=FORMAT("ea=%{:,3} blocked by %d,%d:%{:,3},%{:,3}\n",r.eaScaled(),px+bc.p.x,py+bc.p.y,bc.r.saScaled(),bc.r.eaScaled());
  444. }
  445. r.setEa(bc.r.sa);
  446. beo=o;bei=i;
  447. usedTempBlock=usedTempBlock || bc.tempBlock;
  448. if(saBlock)
  449. {
  450. totalBlock=true;
  451. break;
  452. }
  453. }
  454. if(r.closed)break;
  455. }
  456. if(r.closed || totalBlock)break;
  457. }
  458. if(c.block)
  459. {
  460. c.vis=usedTempBlock || (!totalBlock && !r.closed);
  461. if(debug)
  462. {
  463. c.log+=FORMAT("vis r.sa=%{:,3},r.ea=%{:,3} %{}\n",r.saScaled(),r.eaScaled(),r.closed?"closed":"");
  464. }
  465. }else
  466. {
  467. if(!totalBlock)
  468. {
  469. int r1=r.diff();
  470. int r2=fc.r.diff();
  471. if(debug)
  472. {
  473. c.log+=FORMAT("vis r.sa=%{:,3},r.ea=%{:,3}\ndiff=%d, fc.diff=%d\nmid=%d\n",r.saScaled(),r.eaScaled(),r1,r2,fc.r.middle());
  474. }
  475. c.vis=r1>=r2/2 || (r1>=r2/5 && r.inRange(fc.r.middle()));
  476. }
  477. }
  478.  
  479. if(c.vis && c.block)
  480. {
  481. if(c.corners[fc.sac]!=100 || c.corners[fc.eac]!=100)
  482. {
  483. fc.r.shrinkCorners(c.corners[fc.sac],c.corners[fc.eac]);
  484. c.fc=fc;
  485. }
  486. int used=0;
  487.  
  488. /*if(bso!=-1 && !fc.delayBlock && !fc.tempBlock)
  489.   {
  490.   FovCell& bfc=b[bso][bsi];
  491.   if(!bfc.tempBlock && !bfc.delayBlock)
  492.   {
  493.   used++;
  494.   if(debug)
  495.   {
  496.   c.log+=FORMAT("extended ea of %d,%d from %{:,3} to %{:,3}\n",bfc.p.x+px,bfc.p.y+py,bfc.r.eaScaled(),fc.r.eaScaled());
  497.   }
  498.   bfc.r.ea=fc.r.ea;
  499.   }
  500.   }
  501.   if(beo!=-1 && !fc.delayBlock && !fc.tempBlock)
  502.   {
  503.   FovCell& bfc=b[beo][bei];
  504.   if(!bfc.tempBlock && !bfc.delayBlock)
  505.   {
  506.   used++;
  507.   if(debug)
  508.   {
  509.   c.log+=FORMAT("extended sa of %d,%d from %{:,3} to %{:,3}\n",bfc.p.x+px,bfc.p.y+py,bfc.r.saScaled(),fc.r.saScaled());
  510.   }
  511.   bfc.r.sa=fc.r.sa;
  512.   }
  513.   }*/
  514. if(used==0)
  515. {
  516. b[fc.o1].push_back(fc);
  517. }
  518. if(fc.o1!=fc.o2)
  519. {
  520. b[fc.o2].push_back(fc);
  521. }
  522. }
  523.  
  524. }
  525. for(int i=0;i<8;++i)
  526. {
  527. b[i].insert(b[i].end(),t[i].begin(),t[i].end());
  528. t[i].clear();
  529. }
  530. }
  531. info->setText(FORMAT("bc[]={%{},%{},%{},%{},%{},%{},%{},%{}}",
  532. b[0].size(),b[1].size(),b[2].size(),b[3].size(),b[4].size(),b[5].size(),b[6].size(),b[7].size()));
  533. drawMap(m);
  534. }
  535.  
  536. //protected:
  537. typedef std::vector<FovCell> Ring;
  538. typedef std::vector<Ring> Circle;
  539.  
  540. Circle fov;
  541. std::set<Posi<int> > processed;
  542. std::vector<FovCell> b[8],t[8];
  543. bool debug;
  544. };
  545.  
  546. CircularFov cf;
  547.  
  548. class MyEventHandler:public EventHandler{
  549. public:
  550. MyEventHandler(Matrix& argM):m(argM)
  551. {
  552. viewAt.x=-1;
  553. viewAt.y=-1;
  554. }
  555. void onActiveChange(bool active)
  556. {
  557. }
  558. void onMouseEvent(MouseEvent& argEvent)
  559. {
  560. if(argEvent.eventType==metButtonPress && argEvent.eventButton==1)
  561. {
  562. int x=argEvent.x/CELL_SIZE;
  563. int y=argEvent.y/CELL_SIZE;
  564. if(x<0 || x>=(int)m[0].size() || y<0 || y>=(int)m.size())return;
  565. viewAt.x=x;
  566. viewAt.y=y;
  567. updateViewAt();
  568. }
  569. if(argEvent.eventType==metButtonPress && argEvent.eventButton==3)
  570. {
  571. viewAt.x=-1;viewAt.y=-1;
  572. lsa->setSource(Pos(0,0));
  573. lsa->setDestination(Pos(10,0));
  574. lea->setSource(Pos(0,0));
  575. lea->setDestination(Pos(0,10));
  576. }
  577. }
  578. void updateViewAt()
  579. {
  580. int x=viewAt.x;
  581. int y=viewAt.y;
  582. if(x==-1 || y==-1)return;
  583. //printf("select %d,%d\n",x,y);
  584. char buf[256];
  585. sprintf(buf,"x=%d,y=%d,r=%d,sa=%.3lf,ea=%.3lf\nsac=%d,eac=%d\n",x,y,m[y][x].r,m[y][x].fc.r.saScaled(),m[y][x].fc.r.eaScaled(),m[y][x].fc.sac,m[y][x].fc.eac);
  586. std::string txt=buf;
  587. txt+=m[y][x].log;
  588. //if(!m[y][x].vis)
  589. //{
  590. //Posi<int> bp=m[y][x].;
  591. //sprintf(buf+len,"\nbsa=%.3lf,bea=%.3lf",m[y][x].bfc.sa/1000000.0,m[y][x].bfc.ea/1000000.0);
  592. /*
  593.   m[bp.y][bp.x].mark=true;
  594.   m[lastMark.y][lastMark.x].mark=false;
  595.   lastMark=bp;*/
  596. //}
  597. info->setText(txt.c_str());
  598. Pos src=Pos(px*CELL_SIZE+CELL_SIZE/2.0,py*CELL_SIZE+CELL_SIZE/2.0);
  599. lsa->setSource(src);
  600. lea->setSource(src);
  601. Pos p;
  602. int mr=(sightRadius+1)*CELL_SIZE;
  603. double sa=m[y][x].fc.r.sa*M_PI/AngleRange::SCALED_PI;
  604. p.x=src.x+mr*cos(sa);
  605. p.y=src.y+mr*sin(sa);
  606. lsa->setDestination(p);
  607. double ea=m[y][x].fc.r.ea*M_PI/AngleRange::SCALED_PI;
  608. p.x=src.x+mr*cos(ea);
  609. p.y=src.y+mr*sin(ea);
  610. lea->setDestination(p);
  611. }
  612. void onKeyboardEvent(KeyboardEvent& argEvent)
  613. {
  614. if(argEvent.keySym==keyboard::GK_ESCAPE)
  615. {
  616. engine.exitApp();
  617. }
  618. if(argEvent.eventType==ketPress)
  619. {
  620. int ox=px;
  621. int oy=py;
  622. if(argEvent.keySym==keyboard::GK_UP || argEvent.keySym==keyboard::GK_w)py--;
  623. if(argEvent.keySym==keyboard::GK_DOWN || argEvent.keySym==keyboard::GK_s)py++;
  624. if(argEvent.keySym==keyboard::GK_LEFT || argEvent.keySym==keyboard::GK_a)px--;
  625. if(argEvent.keySym==keyboard::GK_RIGHT || argEvent.keySym==keyboard::GK_d)px++;
  626. if(argEvent.keySym==keyboard::GK_KP_PLUS){sightRadius++;ox=-1;}
  627. if(argEvent.keySym==keyboard::GK_KP_MINUS && sightRadius>2){sightRadius--;ox=-1;}
  628. if(px<0 || px>=(int)m[0].size() || py<0 || py>=(int)m.size() || m[py][px].block)
  629. {
  630. px=ox;
  631. py=oy;
  632. }
  633. if(ox!=px || oy!=py)
  634. {
  635. cf.generateFov(m,sightRadius);
  636. updateViewAt();
  637. }
  638. if(argEvent.keySym==keyboard::GK_t)
  639. {
  640. uint32_t start=SDL_GetTicks();
  641. cf.debug=false;
  642. for(int i=0;i<1000;++i)
  643. {
  644. cf.generateFov(m,sightRadius);
  645. }
  646. cf.debug=true;
  647. uint32_t duration=SDL_GetTicks()-start;
  648. info->setText(FORMAT("time=%d",duration));
  649. }
  650. }
  651. }
  652. void onResize(){};
  653. void onQuit()
  654. {
  655. engine.exitApp();
  656. }
  657.  
  658. void onFrameUpdate(int mcsec)
  659. {
  660. }
  661. Matrix& m;
  662. Posi<int> viewAt;
  663. };
  664.  
  665.  
  666.  
  667. int GliderAppMain(int argc,char* argv[])
  668. {
  669. kst::Logger::Init("simple.log");
  670. engine.setVSync(false);
  671. engine.selectResolution(1024,768,false);
  672. //engine.selectResolution(800,480,false);
  673. engine.setResolution();
  674. engine.setFpsLimit(60);
  675.  
  676.  
  677. //const int W=strlen(testmap[0]);
  678. //const int H=sizeof(testmap)/sizeof(testmap[0]);
  679. kst::File f;
  680. kst::Logger* lg=kst::Logger::getLogger("init");
  681. try{
  682. f.ROpen("testmap.txt");
  683. }catch(std::exception& e)
  684. {
  685. LOGERROR(lg,"exception:%s",e.what());
  686. return 0;
  687. }
  688. Matrix m;
  689. //m.resize(H,Row(W,Cell()));
  690.  
  691. //for(int y=0;y<H;++y)
  692. size_t y=0;
  693. std::string line;
  694. while(f.ReadLine(line))
  695. {
  696. m.push_back(Row(line.size(),Cell()));
  697. if(line.size()!=m[0].size())
  698. {
  699. LOGERROR(lg,"invalid line[%d] length=%d in testmap.txt, expected %d",y,line.size(),m[0].size());
  700. return 0;
  701. }
  702. for(size_t x=0;x<line.size();++x)
  703. {
  704. Cell& c=m[y][x];
  705. c.tempBlock=line[x]=='*';
  706. c.delayBlock=line[x]=='|' || line[x]=='-';
  707. c.block=line[x]=='#' || c.tempBlock || c.delayBlock;
  708. c.vis=true;
  709. c.corners[0]=100;
  710. c.corners[1]=100;
  711. c.corners[2]=100;
  712. c.corners[3]=100;
  713. }
  714. ++y;
  715. }
  716. int W=m[0].size();
  717. int H=m.size();
  718. int cornAngle=60;
  719. for(int y=0;y<H;++y)
  720. {
  721. for(int x=0;x<W;++x)
  722. {
  723. if(m[y][x].block)
  724. {
  725. if(y>0 && x>0 && !m[y-1][x].block && !m[y][x-1].block)
  726. {
  727. m[y][x].corners[3]=cornAngle;
  728. }
  729. if(y>0 && x<W-1 && !m[y-1][x].block && !m[y][x+1].block)
  730. {
  731. m[y][x].corners[2]=cornAngle;
  732. }
  733. if(y<H-1 && x<W-1 && !m[y+1][x].block && !m[y][x+1].block)
  734. {
  735. m[y][x].corners[1]=cornAngle;
  736. }
  737. if(y<H-1 && x>0 && !m[y+1][x].block && !m[y][x-1].block)
  738. {
  739. m[y][x].corners[0]=cornAngle;
  740. }
  741. }
  742. }
  743. }
  744. px=W/2;
  745. py=H/2;
  746.  
  747. MyEventHandler meh(m);
  748. engine.assignHandler(&meh);
  749.  
  750.  
  751. engine.enableKeyboardRepeat();
  752. Scene sc;
  753. FontRef fnt1=manager.getFont("FSEX300.ttf",16);
  754. Text* txt=new Text(fnt1,"hello");
  755. txt->setPosition(Pos(10,10));
  756. vb=new VertexBuffer;
  757. lsa=new Line(Pos(0,0),Pos(10,0));
  758. lea=new Line(Pos(0,0),Pos(0,10));
  759. info=new Text(fnt1,"Info");
  760. info->setPosition(Pos(W*CELL_SIZE,0));
  761. VxVector& v=vb->getVBuf();
  762. ClrVector& c=vb->getCBuf();
  763. for(int y=0;y<H;++y)
  764. {
  765. for(int x=0;x<W;++x)
  766. {
  767. //glider::Rectangle* r=new glider::Rectangle(Rect(x*15,y*15,10,10),Color(1,0.1+x/20+y/20,0.1));
  768. //sc.addObject(r);
  769. Rect(x*CELL_SIZE+1,y*CELL_SIZE+1,CELL_SIZE-2,CELL_SIZE-2).pushQuad(v);
  770. c.push4(Color::gray);
  771. }
  772. }
  773. vb->update();
  774. cf.prepare(sightRadius);
  775. cf.generateFov(m,sightRadius);
  776. drawMap(m);
  777. sc.addObject(vb);
  778. sc.addObject(lsa);
  779. sc.addObject(lea);
  780. sc.addObject(info);
  781. //sc.addObject(txt);
  782. engine.loop(&sc);
  783.  
  784. // for(CircularFov::Ring::iterator it=cf.fov[2].begin(),end=cf.fov[2].end();it!=end;++it)
  785. // {
  786. // FovCell& fc=*it;
  787. // printf("%d,%d:%.3lf-%.3lf\n",fc.p.x,fc.p.y,fc.sa/1000000.0,fc.ea/1000000.0);
  788. // }
  789. // for(CircularFov::Ring::iterator it=cf.fov[3].begin(),end=cf.fov[3].end();it!=end;++it)
  790. // {
  791. // FovCell& fc=*it;
  792. // printf("%d,%d:%.3lf-%.3lf\n",fc.p.x,fc.p.y,fc.sa/1000000.0,fc.ea/1000000.0);
  793. // }
  794.  
  795. return 0;
  796. }
  797.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty