HelloPolycodeApp.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #include "HelloPolycodeApp.h"
  2. HelloPolycodeApp::HelloPolycodeApp(PolycodeView *view) {
  3. #ifdef __APPLE__
  4. core = new CocoaCore(view, 640,480,false,false,0,0,90);
  5. #else
  6. core = new SDLCore(view, 640,480,false,false,0,0,90);
  7. #endif
  8. CoreServices::getInstance()->getResourceManager()->addArchive("Resources/default.pak");
  9. CoreServices::getInstance()->getResourceManager()->addDirResource("default", false);
  10. Screen *hud = new Screen();
  11. ScreenLabel *label = new ScreenLabel("Click on a shape to select it.", 16);
  12. hud->addChild(label);
  13. scene = new CollisionScene();
  14. ScenePrimitive *ground = new ScenePrimitive(ScenePrimitive::TYPE_PLANE, 10, 10);
  15. ground->loadTexture("Resources/green_texture.png");
  16. scene->addEntity(ground);
  17. box = new ScenePrimitive(ScenePrimitive::TYPE_BOX, 1,1,1);
  18. box->loadTexture("Resources/pink_texture.png");
  19. box->setPosition(0,1,0);
  20. scene->addCollisionChild(box, CollisionSceneEntity::SHAPE_MESH);
  21. box = new ScenePrimitive(ScenePrimitive::TYPE_CONE, 1,1,10);
  22. box->loadTexture("Resources/pink_texture.png");
  23. box->setPosition(1,1,2);
  24. scene->addCollisionChild(box, CollisionSceneEntity::SHAPE_CONE);
  25. box = new ScenePrimitive(ScenePrimitive::TYPE_CYLINDER, 2,0.5,10);
  26. box->loadTexture("Resources/pink_texture.png");
  27. box->setPosition(2,1,-1);
  28. scene->addCollisionChild(box, CollisionSceneEntity::SHAPE_CYLINDER);
  29. scene->getDefaultCamera()->setPosition(7,7,7);
  30. scene->getDefaultCamera()->lookAt(Vector3(0,0,0));
  31. core->getInput()->addEventListener(this, InputEvent::EVENT_MOUSEDOWN);
  32. lastEntity = NULL;
  33. }
  34. HelloPolycodeApp::~HelloPolycodeApp() {
  35. }
  36. void HelloPolycodeApp::handleEvent(Event *e) {
  37. if(e->getDispatcher() == core->getInput()) {
  38. InputEvent *inputEvent = (InputEvent*)e;
  39. switch(e->getEventCode()) {
  40. case InputEvent::EVENT_MOUSEDOWN:
  41. Vector3 dir = CoreServices::getInstance()->getRenderer()->projectRayFrom2DCoordinate(inputEvent->mousePosition.x, inputEvent->mousePosition.y);
  42. RayTestResult res = scene->getFirstEntityInRay(scene->getDefaultCamera()->getPosition(), dir * 1000);
  43. if(lastEntity) {
  44. lastEntity->setColor(1.0,1.0,1.0,1.0);
  45. }
  46. if(res.entity) {
  47. res.entity->setColor(1.0, 0.0,0.0,1.0);
  48. lastEntity = res.entity;
  49. }
  50. break;
  51. }
  52. }
  53. }
  54. bool HelloPolycodeApp::Update() {
  55. Number elapsed = core->getElapsed();
  56. return core->Update();
  57. }