EditorKarte.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. #include "EditorKarte.h"
  2. #include <Model2D.h>
  3. #include <Bild.h>
  4. #include <Textur2D.h>
  5. #include <Datei.h>
  6. #include <DateiSystem.h>
  7. #include <M2Datei.h>
  8. #include <Globals.h>
  9. #include <DLLRegister.h>
  10. using namespace Editor;
  11. SpielerDaten::SpielerDaten()
  12. : Model()
  13. {}
  14. SpielerDaten::SpielerDaten( const SpielerDaten & daten )
  15. {
  16. *this = daten;
  17. views->getThis();
  18. }
  19. TeamDaten::TeamDaten()
  20. : Model()
  21. {}
  22. TeamDaten::TeamDaten( const TeamDaten & daten )
  23. {
  24. *this = daten;
  25. views->getThis();
  26. }
  27. ObjektDaten::ObjektDaten()
  28. : Model()
  29. {}
  30. ObjektDaten::ObjektDaten( const ObjektDaten &daten )
  31. {
  32. *this = daten;
  33. views->getThis();
  34. }
  35. EditorObject::EditorObject()
  36. : Model2DObject()
  37. {}
  38. EditorObject::~EditorObject()
  39. {}
  40. EdSpieler::EdSpieler( SpielerDaten *model )
  41. : EditorObject()
  42. {
  43. mdl = model;
  44. }
  45. EdSpieler::~EdSpieler()
  46. {}
  47. void EdSpieler::update()
  48. {
  49. mdl->pos = position;
  50. mdl->rot = (double)rotation;
  51. mdl->update();
  52. }
  53. EdObjekt::EdObjekt( ObjektDaten *model )
  54. : EditorObject()
  55. {
  56. mdl = model;
  57. }
  58. EdObjekt::~EdObjekt()
  59. {}
  60. void EdObjekt::update()
  61. {
  62. mdl->pos = position;
  63. mdl->rot = rotation;
  64. mdl->scale = size;
  65. mdl->update();
  66. }
  67. UpdateObserver::UpdateObserver( std::function< void() > f )
  68. {
  69. this->f = f;
  70. }
  71. void UpdateObserver::update( Model *m )
  72. {
  73. f();
  74. }
  75. KarteDaten::KarteDaten( EditorKlient *client, SpielerTeamStruktur *sts )
  76. : Thread()
  77. {
  78. HMODULE dll = Framework::getDLLRegister()->ladeDLL( "GSL.dll", "data/bin/GSL.dll" );
  79. getGSLDatei = (GetGSLDatei)GetProcAddress( dll, "getGSLDatei" );
  80. welt = new Welt2D();
  81. welt->setCircular( 1 );
  82. this->client = client;
  83. this->sts = (SpielerTeamStruktur *)sts->getThis();
  84. client->loadMapSize( size );
  85. client->loadResources( resources );
  86. client->loadSpieler( spieler );
  87. client->loadTeams( teams );
  88. client->loadObjekte( objekte );
  89. welt->setSize( size.x, size.y );
  90. welt->setSize( 1 );
  91. for( auto s = spieler.getIterator(); s; s++ )
  92. {
  93. Model2DObject *model = new EdSpieler( s._ );
  94. Bild *bild;
  95. int fc = sts->spielerFarbe->get( s->id - 1 );
  96. for( auto r = resources.getIterator(); r; r++ )
  97. {
  98. if( r->id == s->m2d )
  99. model->setModel( client->loadModel( r->path ) );
  100. if( r->id == s->bild )
  101. bild = client->loadBild( r->path );
  102. }
  103. if( bild )
  104. {
  105. Bild *shb = new Bild();
  106. shb->neuBild( bild->getBreite(), bild->getHeight(), 0 );
  107. int maxP = shb->getBreite() * shb->getHeight();
  108. int *buffer = bild->getBuffer();
  109. for( int i = 0; i < maxP; i++ )
  110. {
  111. if( buffer[ i ] )
  112. {
  113. shb->setPixelDP( i, fc );
  114. shb->alphaPixelDP2D( i, buffer[ i ] );
  115. }
  116. }
  117. Textur2D *textur = new Textur2D();
  118. textur->setTexturZ( shb );
  119. model->setTextur( textur, "ship" );
  120. bild->release();
  121. }
  122. model->setPosition( s->pos );
  123. model->setDrehung( (float)s->rot );
  124. model->setCollision( 0 );
  125. welt->addObject( model );
  126. SpielerDaten *d = s._;
  127. d->addView( new UpdateObserver( [ this, d ]()
  128. {
  129. aktionen.add( [ this, d ]()
  130. {
  131. this->client->saveSpieler( d );
  132. } );
  133. } ) );
  134. }
  135. for( auto t = teams.getIterator(); t; t++ )
  136. {
  137. TeamDaten *d = t._;
  138. d->addView( new UpdateObserver( [ this, d ]()
  139. {
  140. aktionen.add( [ this, d ]()
  141. {
  142. this->client->saveTeam( d );
  143. } );
  144. } ) );
  145. }
  146. for( auto o = objekte.getIterator(); o; o++ )
  147. {
  148. Model2DObject *model = new EdObjekt( o._ );
  149. Bild *bild;
  150. for( auto r = resources.getIterator(); r; r++ )
  151. {
  152. if( r->id == o->m2d )
  153. model->setModel( client->loadModel( r->path ) );
  154. if( r->id == o->bild )
  155. bild = client->loadBild( r->path );
  156. }
  157. if( bild )
  158. {
  159. Textur2D *textur = new Textur2D();
  160. textur->setTexturZ( bild );
  161. model->setTextur( textur );
  162. }
  163. model->setPosition( o->pos );
  164. model->setDrehung( o->rot );
  165. model->setSize( o->scale );
  166. model->setCollision( 0 );
  167. welt->addObject( model );
  168. ObjektDaten *d = o._;
  169. d->addView( new UpdateObserver( [ this, d, model ]()
  170. {
  171. aktionen.add( [ this, d, model ]()
  172. {
  173. this->client->saveObjekt( d );
  174. Bild *bild = 0;
  175. Model2DData *da = 0;
  176. for( auto r = resources.getIterator(); r; r++ )
  177. {
  178. if( r->id == d->m2d )
  179. da = this->client->loadModel( r->path );
  180. if( r->id == d->bild )
  181. bild = this->client->loadBild( r->path );
  182. }
  183. if( bild && da )
  184. {
  185. Textur2D *textur = new Textur2D();
  186. textur->setTexturZ( bild );
  187. model->postAction( [ model, da, textur ]()
  188. {
  189. model->setModel( da );
  190. model->setTextur( textur );
  191. } );
  192. }
  193. else
  194. {
  195. if( bild )
  196. bild->release();
  197. if( da )
  198. da->release();
  199. }
  200. } );
  201. } ) );
  202. }
  203. exit = 0;
  204. start();
  205. }
  206. KarteDaten::~KarteDaten()
  207. {
  208. cs.lock();
  209. for( auto i = resources.getIterator(); i; i++ )
  210. delete i._;
  211. for( auto i = objekte.getIterator(); i; i++ )
  212. delete i._;
  213. for( auto i = spieler.getIterator(); i; i++ )
  214. delete i._;
  215. for( auto i = teams.getIterator(); i; i++ )
  216. delete i._;
  217. sts->release();
  218. client->release();
  219. welt->release();
  220. Framework::getDLLRegister()->releaseDLL( "GSL.dll" );
  221. cs.unlock();
  222. }
  223. void KarteDaten::addObjekt( ObjektDaten & daten, std::function< void( int ) > callBack )
  224. {
  225. ObjektDaten *nd = new ObjektDaten( daten );
  226. cs.lock();
  227. nd->id = 0;
  228. bool found = 0;
  229. do
  230. {
  231. nd->id++;
  232. found = 0;
  233. for( auto o = objekte.getIterator(); o; o++ )
  234. {
  235. if( o->id == nd->id )
  236. {
  237. found = 1;
  238. break;
  239. }
  240. }
  241. } while( found );
  242. objekte.add( nd );
  243. Model2DObject *model = new EdObjekt( nd );
  244. Bild *bild;
  245. for( auto r = resources.getIterator(); r; r++ )
  246. {
  247. if( r->id == nd->m2d )
  248. model->setModel( client->loadModel( r->path ) );
  249. if( r->id == nd->bild )
  250. bild = client->loadBild( r->path );
  251. }
  252. if( bild )
  253. {
  254. Textur2D *textur = new Textur2D();
  255. textur->setTexturZ( bild );
  256. model->setTextur( textur );
  257. }
  258. model->setPosition( nd->pos );
  259. model->setDrehung( nd->rot );
  260. model->setSize( nd->scale );
  261. model->setCollision( 0 );
  262. welt->addObject( model );
  263. nd->addView( new UpdateObserver( [ this, nd, model ]()
  264. {
  265. aktionen.add( [ this, nd, model ]()
  266. {
  267. this->client->saveObjekt( nd );
  268. Bild *bild = 0;
  269. Model2DData *d = 0;
  270. for( auto r = resources.getIterator(); r; r++ )
  271. {
  272. if( r->id == nd->m2d )
  273. d = this->client->loadModel( r->path );
  274. if( r->id == nd->bild )
  275. bild = this->client->loadBild( r->path );
  276. }
  277. if( bild && d )
  278. {
  279. Textur2D *textur = new Textur2D();
  280. textur->setTexturZ( bild );
  281. model->postAction( [ model, d, textur ]()
  282. {
  283. model->setModel( d );
  284. model->setTextur( textur );
  285. } );
  286. }
  287. else
  288. {
  289. if( bild )
  290. bild->release();
  291. if( d )
  292. d->release();
  293. }
  294. } );
  295. } ) );
  296. EditorKlient *c = client;
  297. aktionen.add( [ nd, c, callBack ]()
  298. {
  299. if( c->saveObjekt( nd ) )
  300. callBack( nd->id );
  301. else
  302. callBack( 0 );
  303. } );
  304. cs.unlock();
  305. }
  306. void KarteDaten::removeObjekt( int index )
  307. {
  308. cs.lock();
  309. int id = objekte.get( index )->id;
  310. delete objekte.get( index );
  311. objekte.remove( index );
  312. EditorKlient *c = client;
  313. aktionen.add( [ id, c ]()
  314. {
  315. c->deleteObjekt( id );
  316. } );
  317. cs.unlock();
  318. }
  319. void KarteDaten::thread()
  320. {
  321. while( !exit )
  322. {
  323. cs.lock();
  324. while( hasAktions() )
  325. {
  326. std::function< void() > ak = aktionen.get( 0 );
  327. cs.unlock();
  328. ak();
  329. cs.lock();
  330. aktionen.remove( 0 );
  331. }
  332. cs.unlock();
  333. Sleep( 100 );
  334. }
  335. }
  336. ResourceDaten *KarteDaten::getResource( int index )
  337. {
  338. ResourceDaten *ret = 0;
  339. cs.lock();
  340. ret = resources.get( index );
  341. cs.unlock();
  342. return ret;
  343. }
  344. ObjektDaten *KarteDaten::getObjekt( int index )
  345. {
  346. ObjektDaten *ret = 0;
  347. cs.lock();
  348. ret = objekte.get( index );
  349. cs.unlock();
  350. return ret;
  351. }
  352. SpielerDaten *KarteDaten::getSpieler( int index )
  353. {
  354. SpielerDaten *ret = 0;
  355. cs.lock();
  356. ret = spieler.get( index );
  357. cs.unlock();
  358. return ret;
  359. }
  360. TeamDaten *KarteDaten::getTeam( int index )
  361. {
  362. TeamDaten *ret = 0;
  363. cs.lock();
  364. ret = teams.get( index );
  365. cs.unlock();
  366. return ret;
  367. }
  368. const char *KarteDaten::getTeamName( int index )
  369. {
  370. return sts->teamName->z( teams.get( index )->id - 1 )->getText();
  371. }
  372. int KarteDaten::getSpielerIndexById( int id )
  373. {
  374. int index = 0;
  375. cs.lock();
  376. for( auto i = spieler.getIterator(); i; i++ )
  377. {
  378. if( i->id == id )
  379. break;
  380. index++;
  381. }
  382. cs.unlock();
  383. return index;
  384. }
  385. int KarteDaten::getSpielerAnzahl() const
  386. {
  387. return spieler.getEintragAnzahl();
  388. }
  389. int KarteDaten::getTeamIndexById( int id )
  390. {
  391. int index = 0;
  392. cs.lock();
  393. for( auto i = teams.getIterator(); i; i++ )
  394. {
  395. if( i->id == id )
  396. break;
  397. index++;
  398. }
  399. cs.unlock();
  400. return index;
  401. }
  402. int KarteDaten::getTeamAnzahl() const
  403. {
  404. return teams.getEintragAnzahl();
  405. }
  406. int KarteDaten::getObjektIndexById( int id )
  407. {
  408. int index = 0;
  409. cs.lock();
  410. for( auto i = objekte.getIterator(); i; i++ )
  411. {
  412. if( i->id == id )
  413. break;
  414. index++;
  415. }
  416. cs.unlock();
  417. return index;
  418. }
  419. int KarteDaten::getObjektAnzahl() const
  420. {
  421. return objekte.getEintragAnzahl();
  422. }
  423. int KarteDaten::getResourceIndexById( int id )
  424. {
  425. int index = 0;
  426. cs.lock();
  427. for( auto i = resources.getIterator(); i; i++ )
  428. {
  429. if( i->id == id )
  430. break;
  431. index++;
  432. }
  433. cs.unlock();
  434. return index;
  435. }
  436. int KarteDaten::getResourceAnzahl()
  437. {
  438. return resources.getEintragAnzahl();
  439. }
  440. bool KarteDaten::hasError() const
  441. {
  442. return !error.istGleich( "" );
  443. }
  444. char *KarteDaten::getError() const
  445. {
  446. return error;
  447. }
  448. bool KarteDaten::hasAktions() const
  449. {
  450. return aktionen.getEintragAnzahl() > 0;
  451. }
  452. Welt2D *KarteDaten::getWelt() const
  453. {
  454. return welt->getThis();
  455. }
  456. Welt2D *KarteDaten::zWelt() const
  457. {
  458. return welt;
  459. }
  460. void KarteDaten::getResourceIdFromPath( const char *path, std::function< void( int ) > callBack )
  461. {
  462. for( auto r = resources.getIterator(); r; r++ )
  463. {
  464. if( r->path.istGleich( path ) )
  465. {
  466. callBack( r->id );
  467. return;
  468. }
  469. }
  470. aktionen.add( [ this, path, callBack ]()
  471. {
  472. int id = client->addResource( path );
  473. if( !id )
  474. {
  475. callBack( 0 );
  476. return;
  477. }
  478. ResourceDaten *nr = new ResourceDaten();
  479. nr->id = id;
  480. nr->path = path;
  481. cs.lock();
  482. resources.add( nr );
  483. cs.unlock();
  484. callBack( id );
  485. } );
  486. }
  487. bool KarteDaten::doesResourceExist( const char *path )
  488. {
  489. for( auto r = resources.getIterator(); r; r++ )
  490. {
  491. if( r->path.istGleich( path ) )
  492. return 1;
  493. }
  494. return 0;
  495. }
  496. Model2DData *KarteDaten::loadModelFromRessource( int id )
  497. {
  498. for( auto r = resources.getIterator(); r; r++ )
  499. {
  500. if( r->id == id )
  501. return client->loadModel( r->path );
  502. }
  503. return 0;
  504. }
  505. Bild *KarteDaten::loadBildFromRessource( int id )
  506. {
  507. for( auto r = resources.getIterator(); r; r++ )
  508. {
  509. if( r->id == id )
  510. return client->loadBild( r->path );
  511. }
  512. return 0;
  513. }
  514. Model2DData *KarteDaten::loadModelFromPath( const char *path )
  515. {
  516. return client->loadModel( path );
  517. }
  518. Bild *KarteDaten::loadBildFromPath( const char *path )
  519. {
  520. return client->loadBild( path );
  521. }
  522. void KarteDaten::loadUnusedResourcePaths( std::function< void( RCArray< Text > * ) > callBack )
  523. {
  524. cs.lock();
  525. aktionen.add( [ this, callBack ]()
  526. {
  527. RCArray< Text > *result = new RCArray< Text >();
  528. loadSpielResourcePathsFromFolder( "data/spiele/Asteroids", result );
  529. RCArray< Text > *mapPaths = client->getAllMapResourcePaths();
  530. for( auto mp = mapPaths->getIterator(); mp; mp++ )
  531. {
  532. if( !doesResourceExist( mp->getText() ) )
  533. result->add( mp->getThis() );
  534. }
  535. mapPaths->release();
  536. callBack( result );
  537. } );
  538. cs.unlock();
  539. }
  540. void KarteDaten::loadSpielResourcePathsFromFolder( const char *folderPath, RCArray< Text > * zPaths )
  541. {
  542. Datei f;
  543. f.setDatei( folderPath );
  544. if( f.istOrdner() )
  545. {
  546. RCArray< Text > *list = f.getDateiListe();
  547. for( auto n = list->getIterator(); n; n++ )
  548. {
  549. Text path( folderPath );
  550. path += Text( "/" ) + (const char *)n->getText();
  551. loadSpielResourcePathsFromFolder( path, zPaths );
  552. }
  553. list->release();
  554. }
  555. else
  556. {
  557. if( f.zPfad()->hat( ".ltdb" ) )
  558. {
  559. LTDBDatei d;
  560. d.setDatei( new Text( folderPath ) );
  561. d.leseDaten( 0 );
  562. int anz = d.getBildAnzahl();
  563. for( int i = 0; i < anz; i++ )
  564. {
  565. Text *path = new Text( folderPath );
  566. path->ersetzen( 0, (int)strlen( "data/spiele/Asteroids" ), "spiel:" );
  567. path->append( (const char *)( Text( "/" ) + (const char *)d.zBildListe()->z( i )->getText() ) );
  568. if( doesResourceExist( path->getText() ) )
  569. path->release();
  570. else
  571. zPaths->add( path );
  572. }
  573. }
  574. if( f.zPfad()->hat( ".m2" ) )
  575. {
  576. M2Datei d;
  577. d.setPfad( folderPath );
  578. d.leseDaten();
  579. int anz = d.getModelAnzahl();
  580. for( int i = 0; i < anz; i++ )
  581. {
  582. Text *path = new Text( folderPath );
  583. path->ersetzen( 0, (int)strlen( "data/spiele/Asteroids" ), "spiel:" );
  584. path->append( (const char *)( Text( "/" ) + (const char *)d.zModelName( i )->getText() ) );
  585. if( doesResourceExist( path->getText() ) )
  586. path->release();
  587. else
  588. zPaths->add( path );
  589. }
  590. }
  591. if( f.zPfad()->hat( ".gsl" ) )
  592. {
  593. HMODULE dll = Framework::getDLLRegister()->ladeDLL( "GSL.dll", "data/bin/GSL.dll" );
  594. GSL::GSLDateiV *d = getGSLDatei();
  595. d->setDatei( (char *)folderPath );
  596. d->leseDaten();
  597. int anz = d->getSoundAnzahl();
  598. for( int i = 0; i < anz; i++ )
  599. {
  600. Text *path = new Text( folderPath );
  601. path->ersetzen( 0, (int)strlen( "data/spiele/Asteroids" ), "spiel:" );
  602. Text *name = d->getSoundName( i );
  603. path->append( (const char *)( Text( "/" ) + (const char *)name->getText() ) );
  604. name->release();
  605. if( doesResourceExist( path->getText() ) )
  606. path->release();
  607. else
  608. zPaths->add( path );
  609. }
  610. }
  611. }
  612. }
  613. // löscht das objekt wenn es nicht mehr gebraucht wird und beendet den Thread
  614. Thread *KarteDaten::release()
  615. {
  616. if( Thread::ref == 2 && run )
  617. {
  618. exit = 1;
  619. if( isRunning() )
  620. warteAufThread( INT_MAX );
  621. }
  622. return Thread::release();
  623. }