EditorKarte.cpp 17 KB

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