TestManageRes.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #pragma once
  2. #include "test.h"
  3. #include "pthread.h"
  4. #ifdef EMSCRIPTEN
  5. #include <emscripten.h>
  6. #endif
  7. void* myThreadFunc(void* t)
  8. {
  9. Test* test = (Test*)t;
  10. resources.load();
  11. #ifndef __S3E__
  12. //sync with game thread and show notification
  13. core::getMainThreadDispatcher().postCallback([ = ]()
  14. {
  15. test->notify("loaded");
  16. });
  17. #endif
  18. return 0;
  19. }
  20. class ManageResTest: public Test
  21. {
  22. public:
  23. ManageResTest()
  24. {
  25. toggle sw[] = {toggle("unload resources", 1), toggle("load resources", 0)};
  26. addToggle("switch", sw, 2);
  27. #ifdef EMSCRIPTEN
  28. #else
  29. addButton("mt1", "load using ThreadLoader");
  30. addButton("mt2", "load from own thread");
  31. #endif
  32. Resources::resources items;
  33. resources.collect(items);
  34. for (size_t i = 0; i < items.size(); ++i)
  35. {
  36. ResAnim* ra = dynamic_cast<ResAnim*>(items[i].get());
  37. if (!ra)
  38. continue;
  39. if (ra->getName().find("_") != string::npos)
  40. continue;
  41. spSprite sprite = new Sprite;
  42. sprite->setResAnim(ra);
  43. if (ra->getTotalFrames() > 1)
  44. sprite->addTween(TweenAnim(ra), 500, -1);
  45. sprite->setPosition(scalar::randFloat(50.0f, getWidth() - 100.0f), scalar::randFloat(50.0f, getHeight() - 100.0f));
  46. sprite->attachTo(content);
  47. if (ra->isName("bg"))
  48. {
  49. sprite->setPosition(0, 0);
  50. sprite->setPriority(-1);
  51. }
  52. }
  53. spTextField text = new TextField;
  54. text->attachTo(content);
  55. text->setSize(300, 200);
  56. text->setPosition(140.0f, (float)getHeight() - text->getHeight());
  57. TextStyle st;
  58. st.font = resources.getResFont("font");
  59. st.vAlign = TextStyle::VALIGN_TOP;
  60. st.color = Color::CornflowerBlue;
  61. st.multiline = true;
  62. text->setStyle(st);
  63. text->setText("The quick brown fox jumps over the lazy dog. 1234567890");
  64. //text->addEventHandler(new DragHandler);
  65. }
  66. void _loaded(Event* event)
  67. {
  68. notify("Loaded!");
  69. ui->getChild("loading")->addTween(Sprite::TweenAlpha(0), 400)->detachWhenDone();
  70. releaseRef();//added ref earlier from void clicked(id)
  71. }
  72. void toggleClicked(string id, const toggle* data)
  73. {
  74. if (id == "switch")
  75. {
  76. if (data->value)
  77. resources.unload();
  78. else
  79. resources.load();
  80. }
  81. }
  82. spSprite createLoadingAnimation()
  83. {
  84. spSprite sp = new Sprite;
  85. sp->setName("loading");
  86. sp->setResAnim(resourcesUI.getResAnim("loading"));
  87. sp->attachTo(ui);
  88. sp->setAnchor(0.5f, 0.5f);
  89. sp->setPosition(getSize() - sp->getSize() / 4);
  90. sp->setScale(0.5f);
  91. sp->addTween(Actor::TweenRotation(-(float)MATH_PI * 2), 1500, -1);
  92. return sp;
  93. }
  94. void clicked(string id)
  95. {
  96. if (id == "mt1")
  97. {
  98. resources.unload();
  99. createLoadingAnimation();
  100. spThreadLoader loading = new ThreadLoader;
  101. loading->addEventListener(ThreadLoader::COMPLETE, CLOSURE(this, &ManageResTest::_loaded));
  102. addRef();//protect Test instance from automatic delete if you close it too fast
  103. loading->add(&resources);
  104. loading->start();
  105. }
  106. if (id == "mt2")
  107. {
  108. resources.unload();
  109. pthread_t thread;
  110. pthread_create(&thread, 0, myThreadFunc, this);
  111. }
  112. }
  113. };