PolycodeTemplateApp.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. //
  2. // Polycode template. Write your code here.
  3. //
  4. #include "PolycodeTemplateApp.h"
  5. #include "polycode/view/android/PolycodeView.h"
  6. PolycodeTemplateApp::PolycodeTemplateApp(PolycodeView *view) {
  7. core = new POLYCODE_CORE(view, 800,480,false,false, 0,0,60, -1, true);
  8. core->addFileSource("archive", "default.pak");
  9. ResourcePool *globalPool = Services()->getResourceManager()->getGlobalPool();
  10. globalPool->loadResourcesFromFolder("default", true);
  11. // Write your code here!
  12. srand(time(NULL));
  13. scene = new Scene(Scene::SCENE_2D);
  14. scene->useClearColor = true;
  15. scene->clearColor.setColor(1.0f / (float)(rand() % 5),1.0f / (float)(rand()%5),1.0f / (float)(rand() % 5),1.0f);
  16. test = new ScenePrimitive(ScenePrimitive::TYPE_VPLANE, 0.5, 0.5);
  17. test->setPositionY(0.2);
  18. test->setMaterialByName("Unlit");
  19. test->getShaderPass(0).shaderBinding->loadTextureForParam("diffuse", "main_icon.png");
  20. SceneLabel *testLabel = new SceneLabel("Hello Polycode!", 32, "sans", Label::ANTIALIAS_FULL, 0.2);
  21. testLabel->setPositionY(-0.2);
  22. scene->addChild(testLabel);
  23. /*ScenePrimitive* box;
  24. for(int i = 0; i < 1000; i++){
  25. box = new ScenePrimitive(ScenePrimitive::TYPE_BOX, 0.2, 0.2,0.2);
  26. box->setMaterialByName("UnlitUntextured");
  27. //box->setColor(1.0, 0.0,0.0, 1.0);
  28. //box->getShaderPass(0).shaderBinding->loadTextureForParam("diffuse", "default/default.png");
  29. scene->addChild(box);
  30. }*/
  31. // bgSound = new Sound("FightBG.WAV");
  32. // bgSound->Play();
  33. // bgSound->setPitch(10.0);
  34. //sound1 = new Sound("hit.wav");
  35. //Logger::log("before Play");
  36. //sound1->Play(true);
  37. //Logger::log("after Play");
  38. // sound1->setPitch(2.3);
  39. // sound2 = new Sound("test.wav");
  40. // sound3 = new Sound("curve_02_c.wav");
  41. //sound2->Play(true);
  42. running = 0;
  43. rot = Vector3();
  44. fps = new SceneLabel("0", 32, "sans", Label::ANTIALIAS_FULL, 0.1);
  45. fps->setPosition(-0.6,0.3);
  46. scene->addChild(fps);
  47. scene->addChild(test);
  48. /*game = new Scene(Scene::SCENE_3D);
  49. game->useClearColor = true;
  50. game->getActiveCamera()->setPosition(0, 0, 10);
  51. box = new ScenePrimitive(ScenePrimitive::TYPE_BOX, 2, 2, 2);
  52. box->setMaterialByName("UnlitUntextured");
  53. box->setColor(0.1, 1.0, 1.0, 1.0);
  54. game->addChild(box);*/
  55. Services()->getInput()->addEventListener(this, InputEvent::EVENT_KEYDOWN);
  56. Services()->getInput()->addEventListener(this, InputEvent::EVENT_TOUCHES_BEGAN);
  57. Services()->getInput()->addEventListener(this, InputEvent::EVENT_TOUCHES_ENDED);
  58. Services()->getInput()->addEventListener(this, InputEvent::EVENT_TOUCHES_MOVED);
  59. Services()->getCore()->addEventListener(this, Core::EVENT_ACCELEROMETER_MOTION);
  60. Services()->getCore()->addEventListener(this, Core::EVENT_GYRO_ROTATION);
  61. }
  62. void PolycodeTemplateApp::handleEvent(Event *event) {
  63. if(event->getEventType()=="InputEvent"){
  64. InputEvent *inputEvent = (InputEvent*) event;
  65. if(inputEvent->getEventCode() == InputEvent::EVENT_KEYDOWN){
  66. switch(inputEvent->getKey()) {
  67. case KEY_z:
  68. sound1->Play(true);
  69. break;
  70. case KEY_x:
  71. sound2->Play();
  72. break;
  73. case KEY_c:
  74. sound3->Play();
  75. break;
  76. }
  77. } else if (inputEvent->getEventCode() == InputEvent::EVENT_TOUCHES_BEGAN){
  78. Ray r = scene->projectRayFromCameraAndViewportCoordinate(scene->getActiveCamera(),inputEvent->touch.position);
  79. test->setPosition(r.origin.x, r.origin.y);
  80. } else if(inputEvent->getEventCode() == InputEvent::EVENT_TOUCHES_ENDED){
  81. Ray r = scene->projectRayFromCameraAndViewportCoordinate(scene->getActiveCamera(),inputEvent->touch.position);
  82. test->setPosition(r.origin.x, r.origin.y);
  83. } else if(inputEvent->getEventCode() == InputEvent::EVENT_TOUCHES_MOVED){
  84. Ray r = scene->projectRayFromCameraAndViewportCoordinate(scene->getActiveCamera(),inputEvent->touch.position);
  85. test->setPosition(r.origin.x, r.origin.y);
  86. }
  87. } else if(event->getEventType() == "CoreMotionEvent"){
  88. CoreMotionEvent* motionEvent = (CoreMotionEvent*)event;
  89. if(motionEvent->getEventCode() == Core::EVENT_ACCELEROMETER_MOTION){
  90. } else if (motionEvent->getEventCode() == Core::EVENT_GYRO_ROTATION){
  91. test->Translate(motionEvent->amount.x*0.01, motionEvent->amount.y*0.01);
  92. }
  93. }
  94. }
  95. PolycodeTemplateApp::~PolycodeTemplateApp() {
  96. }
  97. bool PolycodeTemplateApp::Update() {
  98. if (!core->paused) {
  99. running += core->getElapsed();
  100. if(running >= 1){
  101. fps->setText(String::NumberToString(core->getFPS(),0));
  102. running = 0;
  103. }
  104. return core->updateAndRender();
  105. } else {
  106. usleep(200000);
  107. return true;
  108. }
  109. }