EditorKarte.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  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 )
  99. {
  100. Model2DObject* model = new EdSpieler( s );
  101. Bild* bild;
  102. int fc = sts->spielerFarbe->get( s->id - 1 );
  103. for( auto r : resources )
  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. aktionen.add( [this, d]() {
  136. this->client->saveSpieler( d );
  137. } );
  138. } ) );
  139. }
  140. for( auto t : teams )
  141. {
  142. TeamDaten* d = t;
  143. d->addView( new UpdateObserver( [this, d]() {
  144. aktionen.add( [this, d]() {
  145. this->client->saveTeam( d );
  146. } );
  147. } ) );
  148. }
  149. for( auto o : objekte )
  150. {
  151. Model2DObject* model = new EdObjekt( o );
  152. Bild* bild;
  153. for( auto r : resources )
  154. {
  155. if( r->id == o->m2d )
  156. model->setModel( client->loadModel( r->path ) );
  157. if( r->id == o->bild )
  158. bild = client->loadBild( r->path );
  159. }
  160. if( bild )
  161. {
  162. Textur2D* textur = new Textur2D();
  163. textur->setTexturZ( bild );
  164. model->setTextur( textur );
  165. }
  166. model->setPosition( o->pos );
  167. model->setDrehung( o->rot );
  168. model->setSize( o->scale );
  169. model->setCollision( 0 );
  170. welt->addObject( model );
  171. ObjektDaten* d = o;
  172. d->addView( new UpdateObserver( [this, d, model]() {
  173. aktionen.add( [this, d, model]() {
  174. this->client->saveObjekt( d );
  175. Bild* bild = 0;
  176. Model2DData* da = 0;
  177. for( auto r : resources )
  178. {
  179. if( r->id == d->m2d )
  180. da = this->client->loadModel( r->path );
  181. if( r->id == d->bild )
  182. bild = this->client->loadBild( r->path );
  183. }
  184. if( bild && da )
  185. {
  186. Textur2D* textur = new Textur2D();
  187. textur->setTexturZ( bild );
  188. model->postAction( [model, da, textur]() {
  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 )
  210. delete i;
  211. for( auto i : objekte )
  212. delete i;
  213. for( auto i : spieler )
  214. delete i;
  215. for( auto i : teams )
  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 )
  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 )
  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. aktionen.add( [this, nd, model]() {
  265. this->client->saveObjekt( nd );
  266. Bild* bild = 0;
  267. Model2DData* d = 0;
  268. for( auto r : resources )
  269. {
  270. if( r->id == nd->m2d )
  271. d = this->client->loadModel( r->path );
  272. if( r->id == nd->bild )
  273. bild = this->client->loadBild( r->path );
  274. }
  275. if( bild && d )
  276. {
  277. Textur2D* textur = new Textur2D();
  278. textur->setTexturZ( bild );
  279. model->postAction( [model, d, textur]() {
  280. model->setModel( d );
  281. model->setTextur( textur );
  282. } );
  283. }
  284. else
  285. {
  286. if( bild )
  287. bild->release();
  288. if( d )
  289. d->release();
  290. }
  291. } );
  292. } ) );
  293. EditorKlient* c = client;
  294. aktionen.add( [nd, c, callBack]() {
  295. if( c->saveObjekt( nd ) )
  296. callBack( nd->id );
  297. else
  298. callBack( 0 );
  299. } );
  300. cs.unlock();
  301. }
  302. void KarteDaten::removeObjekt( int index )
  303. {
  304. cs.lock();
  305. int id = objekte.get( index )->id;
  306. EditorObject* obj = 0;
  307. for( auto o = welt->getMembers(); o; o++ )
  308. {
  309. obj = (EditorObject*)o._;
  310. if( obj->isObjekt( EditorObject::OBJEKT, id ) )
  311. break;
  312. }
  313. welt->removeObject( obj );
  314. delete objekte.get( index );
  315. objekte.remove( index );
  316. EditorKlient* c = client;
  317. aktionen.add( [id, c]() {
  318. c->deleteObjekt( id );
  319. } );
  320. cs.unlock();
  321. }
  322. void KarteDaten::thread()
  323. {
  324. while( !exit )
  325. {
  326. cs.lock();
  327. while( hasAktions() )
  328. {
  329. std::function< void() > ak = aktionen.get( 0 );
  330. cs.unlock();
  331. ak();
  332. cs.lock();
  333. aktionen.remove( 0 );
  334. }
  335. cs.unlock();
  336. Sleep( 100 );
  337. }
  338. }
  339. ResourceDaten* KarteDaten::getResource( int index )
  340. {
  341. ResourceDaten* ret = 0;
  342. cs.lock();
  343. ret = resources.get( index );
  344. cs.unlock();
  345. return ret;
  346. }
  347. ObjektDaten* KarteDaten::getObjekt( int index )
  348. {
  349. ObjektDaten* ret = 0;
  350. cs.lock();
  351. ret = objekte.get( index );
  352. cs.unlock();
  353. return ret;
  354. }
  355. SpielerDaten* KarteDaten::getSpieler( int index )
  356. {
  357. SpielerDaten* ret = 0;
  358. cs.lock();
  359. ret = spieler.get( index );
  360. cs.unlock();
  361. return ret;
  362. }
  363. TeamDaten* KarteDaten::getTeam( int index )
  364. {
  365. TeamDaten* ret = 0;
  366. cs.lock();
  367. ret = teams.get( index );
  368. cs.unlock();
  369. return ret;
  370. }
  371. const char* KarteDaten::getTeamName( int index )
  372. {
  373. return sts->teamName->z( teams.get( index )->id - 1 )->getText();
  374. }
  375. int KarteDaten::getSpielerIndexById( int id )
  376. {
  377. int index = 0;
  378. cs.lock();
  379. for( auto i : spieler )
  380. {
  381. if( i->id == id )
  382. break;
  383. index++;
  384. }
  385. cs.unlock();
  386. return index;
  387. }
  388. int KarteDaten::getSpielerAnzahl() const
  389. {
  390. return spieler.getEintragAnzahl();
  391. }
  392. int KarteDaten::getTeamIndexById( int id )
  393. {
  394. int index = 0;
  395. cs.lock();
  396. for( auto i : teams )
  397. {
  398. if( i->id == id )
  399. break;
  400. index++;
  401. }
  402. cs.unlock();
  403. return index;
  404. }
  405. int KarteDaten::getTeamAnzahl() const
  406. {
  407. return teams.getEintragAnzahl();
  408. }
  409. int KarteDaten::getObjektIndexById( int id )
  410. {
  411. int index = 0;
  412. cs.lock();
  413. for( auto i : objekte )
  414. {
  415. if( i->id == id )
  416. break;
  417. index++;
  418. }
  419. cs.unlock();
  420. return index;
  421. }
  422. int KarteDaten::getObjektAnzahl() const
  423. {
  424. return objekte.getEintragAnzahl();
  425. }
  426. int KarteDaten::getResourceIndexById( int id )
  427. {
  428. int index = 0;
  429. cs.lock();
  430. for( auto i : resources )
  431. {
  432. if( i->id == id )
  433. break;
  434. index++;
  435. }
  436. cs.unlock();
  437. return index;
  438. }
  439. int KarteDaten::getResourceAnzahl()
  440. {
  441. return resources.getEintragAnzahl();
  442. }
  443. bool KarteDaten::hasError() const
  444. {
  445. return !error.istGleich( "" );
  446. }
  447. char* KarteDaten::getError() const
  448. {
  449. return error;
  450. }
  451. bool KarteDaten::hasAktions() const
  452. {
  453. return aktionen.getEintragAnzahl() > 0;
  454. }
  455. Welt2D* KarteDaten::getWelt() const
  456. {
  457. return dynamic_cast<Welt2D*>(welt->getThis());
  458. }
  459. Welt2D* KarteDaten::zWelt() const
  460. {
  461. return welt;
  462. }
  463. void KarteDaten::getResourceIdFromPath( const char* path, std::function< void( int ) > callBack )
  464. {
  465. for( auto r : resources )
  466. {
  467. if( r->path.istGleich( path ) )
  468. {
  469. callBack( r->id );
  470. return;
  471. }
  472. }
  473. aktionen.add( [this, path, callBack]() {
  474. int id = client->addResource( path );
  475. if( !id )
  476. {
  477. callBack( 0 );
  478. return;
  479. }
  480. ResourceDaten* nr = new ResourceDaten();
  481. nr->id = id;
  482. nr->path = path;
  483. cs.lock();
  484. resources.add( nr );
  485. cs.unlock();
  486. callBack( id );
  487. } );
  488. }
  489. bool KarteDaten::doesResourceExist( const char* path )
  490. {
  491. for( auto r : resources )
  492. {
  493. if( r->path.istGleich( path ) )
  494. return 1;
  495. }
  496. return 0;
  497. }
  498. Model2DData* KarteDaten::loadModelFromRessource( int id )
  499. {
  500. for( auto r : resources )
  501. {
  502. if( r->id == id )
  503. return client->loadModel( r->path );
  504. }
  505. return 0;
  506. }
  507. Bild* KarteDaten::loadBildFromRessource( int id )
  508. {
  509. for( auto r : resources )
  510. {
  511. if( r->id == id )
  512. return client->loadBild( r->path );
  513. }
  514. return 0;
  515. }
  516. Model2DData* KarteDaten::loadModelFromPath( const char* path )
  517. {
  518. return client->loadModel( path );
  519. }
  520. Bild* KarteDaten::loadBildFromPath( const char* path )
  521. {
  522. return client->loadBild( path );
  523. }
  524. void KarteDaten::loadUnusedResourcePaths( std::function< void( RCArray< Text >* ) > callBack )
  525. {
  526. cs.lock();
  527. aktionen.add( [this, callBack]() {
  528. RCArray< Text >* result = new RCArray< Text >();
  529. loadSpielResourcePathsFromFolder( "data/spiele/Asteroids", result );
  530. RCArray< Text >* mapPaths = client->getAllMapResourcePaths();
  531. for( auto mp : *mapPaths )
  532. {
  533. if( !doesResourceExist( mp->getText() ) )
  534. result->add( dynamic_cast<Text*>(mp->getThis()) );
  535. }
  536. mapPaths->release();
  537. callBack( result );
  538. } );
  539. cs.unlock();
  540. }
  541. void KarteDaten::loadSpielResourcePathsFromFolder( const char* folderPath, RCArray< Text >* zPaths )
  542. {
  543. Datei f;
  544. f.setDatei( folderPath );
  545. if( f.istOrdner() )
  546. {
  547. RCArray< Text >* list = f.getDateiListe();
  548. for( auto n : *list )
  549. {
  550. Text path( folderPath );
  551. path += Text( "/" ) + (const char*)n->getText();
  552. loadSpielResourcePathsFromFolder( path, zPaths );
  553. }
  554. list->release();
  555. }
  556. else
  557. {
  558. if( f.zPfad()->hat( ".ltdb" ) )
  559. {
  560. LTDBDatei d;
  561. d.setDatei( new Text( folderPath ) );
  562. d.leseDaten( 0 );
  563. int anz = d.getBildAnzahl();
  564. for( int i = 0; i < anz; i++ )
  565. {
  566. Text* path = new Text( folderPath );
  567. path->ersetzen( 0, (int)strlen( "data/spiele/Asteroids" ), "spiel:" );
  568. path->append( (const char*)(Text( "/" ) + (const char*)d.zBildListe()->z( i )->getText()) );
  569. if( doesResourceExist( path->getText() ) )
  570. path->release();
  571. else
  572. zPaths->add( path );
  573. }
  574. }
  575. if( f.zPfad()->hat( ".m2" ) )
  576. {
  577. M2Datei d;
  578. d.setPfad( folderPath );
  579. d.leseDaten();
  580. int anz = d.getModelAnzahl();
  581. for( int i = 0; i < anz; i++ )
  582. {
  583. Text* path = new Text( folderPath );
  584. path->ersetzen( 0, (int)strlen( "data/spiele/Asteroids" ), "spiel:" );
  585. path->append( (const char*)(Text( "/" ) + (const char*)d.zModelName( i )->getText()) );
  586. if( doesResourceExist( path->getText() ) )
  587. path->release();
  588. else
  589. zPaths->add( path );
  590. }
  591. }
  592. if( f.zPfad()->hat( ".gsl" ) )
  593. {
  594. HMODULE dll = Framework::getDLLRegister()->ladeDLL( "GSL.dll", "data/bin/GSL.dll" );
  595. GSL::GSLDateiV* d = getGSLDatei();
  596. d->setDatei( (char*)folderPath );
  597. d->leseDaten();
  598. int anz = d->getSoundAnzahl();
  599. for( int i = 0; i < anz; i++ )
  600. {
  601. Text* path = new Text( folderPath );
  602. path->ersetzen( 0, (int)strlen( "data/spiele/Asteroids" ), "spiel:" );
  603. Text* name = d->getSoundName( i );
  604. path->append( (const char*)(Text( "/" ) + (const char*)name->getText()) );
  605. name->release();
  606. if( doesResourceExist( path->getText() ) )
  607. path->release();
  608. else
  609. zPaths->add( path );
  610. }
  611. }
  612. }
  613. }
  614. // löscht das objekt wenn es nicht mehr gebraucht wird und beendet den Thread
  615. void KarteDaten::stopWork()
  616. {
  617. if( run )
  618. {
  619. exit = 1;
  620. if( isRunning() )
  621. warteAufThread( INT_MAX );
  622. }
  623. }