TestManageRes.h 3.6 KB

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