EditorKarte.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  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;
  175. for( auto r = resources.getIterator(); r; r++ )
  176. {
  177. if( r->id == d->m2d )
  178. model->setModel( this->client->loadModel( r->path ) );
  179. if( r->id == d->bild )
  180. bild = this->client->loadBild( r->path );
  181. }
  182. if( bild )
  183. {
  184. Textur2D *textur = new Textur2D();
  185. textur->setTexturZ( bild );
  186. model->setTextur( textur );
  187. }
  188. } );
  189. } ) );
  190. }
  191. exit = 0;
  192. start();
  193. }
  194. KarteDaten::~KarteDaten()
  195. {
  196. cs.lock();
  197. for( auto i = resources.getIterator(); i; i++ )
  198. delete i._;
  199. for( auto i = objekte.getIterator(); i; i++ )
  200. delete i._;
  201. for( auto i = spieler.getIterator(); i; i++ )
  202. delete i._;
  203. for( auto i = teams.getIterator(); i; i++ )
  204. delete i._;
  205. sts->release();
  206. client->release();
  207. welt->release();
  208. Framework::getDLLRegister()->releaseDLL( "GSL.dll" );
  209. cs.unlock();
  210. }
  211. void KarteDaten::addObjekt( ObjektDaten & daten, std::function< void( int ) > callBack )
  212. {
  213. ObjektDaten *nd = new ObjektDaten( daten );
  214. cs.lock();
  215. nd->id = 0;
  216. bool found = 0;
  217. do
  218. {
  219. nd->id++;
  220. found = 0;
  221. for( auto o = objekte.getIterator(); o; o++ )
  222. {
  223. if( o->id == nd->id )
  224. {
  225. found = 1;
  226. break;
  227. }
  228. }
  229. } while( found );
  230. objekte.add( nd );
  231. Model2DObject *model = new EdObjekt( nd );
  232. Bild *bild;
  233. for( auto r = resources.getIterator(); r; r++ )
  234. {
  235. if( r->id == nd->m2d )
  236. model->setModel( client->loadModel( r->path ) );
  237. if( r->id == nd->bild )
  238. bild = client->loadBild( r->path );
  239. }
  240. if( bild )
  241. {
  242. Textur2D *textur = new Textur2D();
  243. textur->setTexturZ( bild );
  244. model->setTextur( textur );
  245. }
  246. model->setPosition( nd->pos );
  247. model->setDrehung( nd->rot );
  248. model->setSize( nd->scale );
  249. model->setCollision( 0 );
  250. welt->addObject( model );
  251. nd->addView( new UpdateObserver( [ this, nd, model ]()
  252. {
  253. aktionen.add( [ this, nd, model ]()
  254. {
  255. this->client->saveObjekt( nd );
  256. Bild *bild;
  257. for( auto r = resources.getIterator(); r; r++ )
  258. {
  259. if( r->id == nd->m2d )
  260. {
  261. Model2DData *d = this->client->loadModel( r->path );
  262. cs.lock();
  263. model->setModel( d );
  264. cs.unlock();
  265. }
  266. if( r->id == nd->bild )
  267. bild = this->client->loadBild( r->path );
  268. }
  269. if( bild )
  270. {
  271. Textur2D *textur = new Textur2D();
  272. cs.lock();
  273. textur->setTexturZ( bild );
  274. cs.unlock();
  275. model->setTextur( textur );
  276. }
  277. } );
  278. } ) );
  279. EditorKlient *c = client;
  280. aktionen.add( [ nd, c, callBack ]()
  281. {
  282. if( c->saveObjekt( nd ) )
  283. callBack( nd->id );
  284. else
  285. callBack( 0 );
  286. } );
  287. cs.unlock();
  288. }
  289. void KarteDaten::removeObjekt( int index )
  290. {
  291. cs.lock();
  292. int id = objekte.get( index )->id;
  293. delete objekte.get( index );
  294. objekte.remove( index );
  295. EditorKlient *c = client;
  296. aktionen.add( [ id, c ]()
  297. {
  298. c->deleteObjekt( id );
  299. } );
  300. cs.unlock();
  301. }
  302. void KarteDaten::thread()
  303. {
  304. while( !exit )
  305. {
  306. cs.lock();
  307. while( hasAktions() )
  308. {
  309. std::function< void() > ak = aktionen.get( 0 );
  310. cs.unlock();
  311. ak();
  312. cs.lock();
  313. aktionen.remove( 0 );
  314. }
  315. cs.unlock();
  316. Sleep( 100 );
  317. }
  318. }
  319. ResourceDaten *KarteDaten::getResource( int index )
  320. {
  321. ResourceDaten *ret = 0;
  322. cs.lock();
  323. ret = resources.get( index );
  324. cs.unlock();
  325. return ret;
  326. }
  327. ObjektDaten *KarteDaten::getObjekt( int index )
  328. {
  329. ObjektDaten *ret = 0;
  330. cs.lock();
  331. ret = objekte.get( index );
  332. cs.unlock();
  333. return ret;
  334. }
  335. SpielerDaten *KarteDaten::getSpieler( int index )
  336. {
  337. SpielerDaten *ret = 0;
  338. cs.lock();
  339. ret = spieler.get( index );
  340. cs.unlock();
  341. return ret;
  342. }
  343. TeamDaten *KarteDaten::getTeam( int index )
  344. {
  345. TeamDaten *ret = 0;
  346. cs.lock();
  347. ret = teams.get( index );
  348. cs.unlock();
  349. return ret;
  350. }
  351. const char *KarteDaten::getTeamName( int index )
  352. {
  353. return sts->teamName->z( teams.get( index )->id - 1 )->getText();
  354. }
  355. int KarteDaten::getSpielerIndexById( int id )
  356. {
  357. int index = 0;
  358. cs.lock();
  359. for( auto i = spieler.getIterator(); i; i++ )
  360. {
  361. if( i->id == id )
  362. break;
  363. index++;
  364. }
  365. cs.unlock();
  366. return index;
  367. }
  368. int KarteDaten::getSpielerAnzahl() const
  369. {
  370. return spieler.getEintragAnzahl();
  371. }
  372. int KarteDaten::getTeamIndexById( int id )
  373. {
  374. int index = 0;
  375. cs.lock();
  376. for( auto i = teams.getIterator(); i; i++ )
  377. {
  378. if( i->id == id )
  379. break;
  380. index++;
  381. }
  382. cs.unlock();
  383. return index;
  384. }
  385. int KarteDaten::getTeamAnzahl() const
  386. {
  387. return teams.getEintragAnzahl();
  388. }
  389. int KarteDaten::getObjektIndexById( int id )
  390. {
  391. int index = 0;
  392. cs.lock();
  393. for( auto i = objekte.getIterator(); i; i++ )
  394. {
  395. if( i->id == id )
  396. break;
  397. index++;
  398. }
  399. cs.unlock();
  400. return index;
  401. }
  402. int KarteDaten::getObjektAnzahl() const
  403. {
  404. return objekte.getEintragAnzahl();
  405. }
  406. int KarteDaten::getResourceIndexById( int id )
  407. {
  408. int index = 0;
  409. cs.lock();
  410. for( auto i = resources.getIterator(); i; i++ )
  411. {
  412. if( i->id == id )
  413. break;
  414. index++;
  415. }
  416. cs.unlock();
  417. return index;
  418. }
  419. int KarteDaten::getResourceAnzahl()
  420. {
  421. return resources.getEintragAnzahl();
  422. }
  423. bool KarteDaten::hasError() const
  424. {
  425. return !error.istGleich( "" );
  426. }
  427. char *KarteDaten::getError() const
  428. {
  429. return error;
  430. }
  431. bool KarteDaten::hasAktions() const
  432. {
  433. return aktionen.getEintragAnzahl() > 0;
  434. }
  435. Welt2D *KarteDaten::getWelt() const
  436. {
  437. return welt->getThis();
  438. }
  439. Welt2D *KarteDaten::zWelt() const
  440. {
  441. return welt;
  442. }
  443. void KarteDaten::getResourceIdFromPath( const char *path, std::function< void( int ) > callBack )
  444. {
  445. for( auto r = resources.getIterator(); r; r++ )
  446. {
  447. if( r->path.istGleich( path ) )
  448. {
  449. callBack( r->id );
  450. return;
  451. }
  452. }
  453. aktionen.add( [ this, path, callBack ]()
  454. {
  455. int id = client->addResource( path );
  456. if( !id )
  457. {
  458. callBack( 0 );
  459. return;
  460. }
  461. ResourceDaten *nr = new ResourceDaten();
  462. nr->id = id;
  463. nr->path = path;
  464. cs.lock();
  465. resources.add( nr );
  466. cs.unlock();
  467. callBack( id );
  468. } );
  469. }
  470. bool KarteDaten::doesResourceExist( const char *path )
  471. {
  472. for( auto r = resources.getIterator(); r; r++ )
  473. {
  474. if( r->path.istGleich( path ) )
  475. return 1;
  476. }
  477. return 0;
  478. }
  479. Model2DData *KarteDaten::loadModelFromRessource( int id )
  480. {
  481. for( auto r = resources.getIterator(); r; r++ )
  482. {
  483. if( r->id == id )
  484. return client->loadModel( r->path );
  485. }
  486. return 0;
  487. }
  488. Bild *KarteDaten::loadBildFromRessource( int id )
  489. {
  490. for( auto r = resources.getIterator(); r; r++ )
  491. {
  492. if( r->id == id )
  493. return client->loadBild( r->path );
  494. }
  495. return 0;
  496. }
  497. Model2DData *KarteDaten::loadModelFromPath( const char *path )
  498. {
  499. return client->loadModel( path );
  500. }
  501. Bild *KarteDaten::loadBildFromPath( const char *path )
  502. {
  503. return client->loadBild( path );
  504. }
  505. void KarteDaten::loadUnusedResourcePaths( std::function< void( RCArray< Text > * ) > callBack )
  506. {
  507. cs.lock();
  508. aktionen.add( [ this, callBack ]()
  509. {
  510. RCArray< Text > *result = new RCArray< Text >();
  511. loadSpielResourcePathsFromFolder( "data/spiele/Asteroids", result );
  512. RCArray< Text > *mapPaths = client->getAllMapResourcePaths();
  513. for( auto mp = mapPaths->getIterator(); mp; mp++ )
  514. {
  515. if( !doesResourceExist( mp->getText() ) )
  516. result->add( mp->getThis() );
  517. }
  518. mapPaths->release();
  519. callBack( result );
  520. } );
  521. cs.unlock();
  522. }
  523. void KarteDaten::loadSpielResourcePathsFromFolder( const char *folderPath, RCArray< Text > * zPaths )
  524. {
  525. Datei f;
  526. f.setDatei( folderPath );
  527. if( f.istOrdner() )
  528. {
  529. RCArray< Text > *list = f.getDateiListe();
  530. for( auto n = list->getIterator(); n; n++ )
  531. {
  532. Text path( folderPath );
  533. path += Text( "/" ) + (const char *)n->getText();
  534. loadSpielResourcePathsFromFolder( path, zPaths );
  535. }
  536. list->release();
  537. }
  538. else
  539. {
  540. if( f.zPfad()->hat( ".ltdb" ) )
  541. {
  542. LTDBDatei d;
  543. d.setDatei( new Text( folderPath ) );
  544. d.leseDaten( 0 );
  545. int anz = d.getBildAnzahl();
  546. for( int i = 0; i < anz; i++ )
  547. {
  548. Text *path = new Text( folderPath );
  549. path->ersetzen( 0, (int)strlen( "data/spiele/Asteroids" ), "spiel:" );
  550. path->append( (const char *)( Text( "/" ) + (const char *)d.zBildListe()->z( i )->getText() ) );
  551. if( doesResourceExist( path->getText() ) )
  552. path->release();
  553. else
  554. zPaths->add( path );
  555. }
  556. }
  557. if( f.zPfad()->hat( ".m2" ) )
  558. {
  559. M2Datei d;
  560. d.setPfad( folderPath );
  561. d.leseDaten();
  562. int anz = d.getModelAnzahl();
  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.zModelName( i )->getText() ) );
  568. if( doesResourceExist( path->getText() ) )
  569. path->release();
  570. else
  571. zPaths->add( path );
  572. }
  573. }
  574. if( f.zPfad()->hat( ".gsl" ) )
  575. {
  576. HMODULE dll = Framework::getDLLRegister()->ladeDLL( "GSL.dll", "data/bin/GSL.dll" );
  577. GSL::GSLDateiV *d = getGSLDatei();
  578. d->setDatei( (char *)folderPath );
  579. d->leseDaten();
  580. int anz = d->getSoundAnzahl();
  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. Text *name = d->getSoundName( i );
  586. path->append( (const char *)( Text( "/" ) + (const char *)name->getText() ) );
  587. name->release();
  588. if( doesResourceExist( path->getText() ) )
  589. path->release();
  590. else
  591. zPaths->add( path );
  592. }
  593. }
  594. }
  595. }
  596. // löscht das objekt wenn es nicht mehr gebraucht wird und beendet den Thread
  597. Thread *KarteDaten::release()
  598. {
  599. if( Thread::ref == 2 && run )
  600. {
  601. exit = 1;
  602. if( isRunning() )
  603. warteAufThread( INT_MAX );
  604. }
  605. return Thread::release();
  606. }