EditorKarte.cpp 15 KB

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